joycode 0.2.4: plan expiry, and one retry when gray access is denied (#701)
joycode 0.2.4 (kasisama): - usage: the plan's expiry from the IDE's expireText (end of that day, China time) as `until`, with each pool's runsOutAt/runsOut; the package row decides, otherwise the earliest pool. - a gray-access denial is retried once with the identity read again. - gateway failures are read from a clone of the reply (64KB at most); a JSON reply over 8MB answers 502 response_too_large. - an empty catalogue keeps the built-in models, which are text-only. The first head (f16034b2) turned the repo suite red: usage.test.mjs called t.mock.method(globalThis, 'fetch') several times in one test, Bun 1.4 restored the wrong function, and the next file (joycode/login.test) and ~450 tests after it kept hitting the mock. b1f06b75 installs the mock once per test; nothing outside usage.test.mjs changed. Run at the reviewed head b1f06b7501bddb4f035eee069bf44d89f02e5349 (mote): - whole repo `bun test`: 5374 pass, 19 skip, 0 fail (401 files). - at f16034b2: the same run had its first red in joycode/login.test.mjs right after usage.test.mjs, an AssertionError from usage.test.mjs's mock (the leak above). - CI run 41772835368 on b1f06b75: success. Not tried: a real JoyCode account (none here); the gray-denial retry and expiry phrases are checked against the tests' fixtures only.
This commit is contained in:
@@ -28,7 +28,8 @@ function chunks(parts) {
|
||||
return new ReadableStream({ start(c) { for (const p of parts) c.enqueue(new TextEncoder().encode(p)); c.close() } })
|
||||
}
|
||||
test('HTTP 200 raw gateway denial becomes actionable error, not empty SSE', async t => {
|
||||
const r = await request(t, new Response(chunks([' {"error":', '{"code":"AI_GRAY_ACCESS_DENIED","message":"访问受限,请联系管理员开通"}}']), { headers: { 'content-type': 'text/event-stream' } }))
|
||||
const denial = () => new Response(chunks([' {"error":', '{"code":"AI_GRAY_ACCESS_DENIED","message":"访问受限,请联系管理员开通"}}']), { headers: { 'content-type': 'text/event-stream' } })
|
||||
const r = await request(t, denial)
|
||||
assert.equal(r.status, 403)
|
||||
assert.equal(r.headers.get('x-magpie-sign-in'), 'kept')
|
||||
assert.equal((await r.json()).error.code, 'AI_GRAY_ACCESS_DENIED')
|
||||
@@ -495,7 +496,98 @@ test('flatten applies only to Chat Completions, never Responses', async t => {
|
||||
})
|
||||
|
||||
test('HTTP 403 permission denial is not marked as expired credentials', async t => {
|
||||
const r = await request(t, Response.json({ error: { code: 'AI_GRAY_ACCESS_DENIED', message: 'denied' } }, { status: 403 }))
|
||||
const r = await request(t, () => Response.json({ error: { code: 'AI_GRAY_ACCESS_DENIED', message: 'denied' } }, { status: 403 }))
|
||||
assert.equal(r.status, 403)
|
||||
assert.equal(r.headers.get('x-magpie-sign-in'), 'kept')
|
||||
})
|
||||
|
||||
test('AI_GRAY_ACCESS_DENIED refreshes identity once and retries with the new account', async t => {
|
||||
let infoCalls = 0
|
||||
let chats = 0
|
||||
t.mock.method(globalThis, 'fetch', async (url, init) => {
|
||||
if (new URL(url).searchParams.get('functionId') === 'joycode_userInfo') {
|
||||
infoCalls++
|
||||
return Response.json({
|
||||
code: 0,
|
||||
data: infoCalls === 1
|
||||
? { userId: 'test-user', tenant: '', orgFullName: '' }
|
||||
: { userId: 'test-user', tenant: 'refreshed-tenant', orgFullName: 'refreshed-org' },
|
||||
})
|
||||
}
|
||||
chats++
|
||||
const sent = JSON.parse(init.body)
|
||||
if (chats === 1) {
|
||||
assert.equal(sent.orgFullName, '')
|
||||
assert.equal(sent.tenant, '')
|
||||
return Response.json({ code: 'AI_GRAY_ACCESS_DENIED', msg: '访问受限,请联系管理员开通' })
|
||||
}
|
||||
assert.equal(sent.orgFullName, 'refreshed-org')
|
||||
assert.equal(sent.tenant, 'refreshed-tenant')
|
||||
assert.equal(init.headers.get('tenant'), 'refreshed-tenant')
|
||||
return new Response('data: [DONE]\n\n', { headers: { 'content-type': 'text/event-stream' } })
|
||||
})
|
||||
const hooks = await plugin.server({})
|
||||
const loader = await hooks.auth.loader(async () => ({ type: 'api', key: 'fake-key' }))
|
||||
const r = await loader.fetch('https://api-ai.jd.com/v1/chat/completions', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ model: 'JoyAI-Code-1.6', messages: [{ role: 'user', content: 'OK' }], stream: true }),
|
||||
})
|
||||
assert.equal(r.status, 200)
|
||||
assert.equal(await r.text(), 'data: [DONE]\n\n')
|
||||
assert.equal(infoCalls, 2)
|
||||
assert.equal(chats, 2)
|
||||
})
|
||||
|
||||
test('a second AI_GRAY_ACCESS_DENIED stays 403 and does not fetch identity again', async t => {
|
||||
let infoCalls = 0
|
||||
let chats = 0
|
||||
t.mock.method(globalThis, 'fetch', async (url) => {
|
||||
if (new URL(url).searchParams.get('functionId') === 'joycode_userInfo') {
|
||||
infoCalls++
|
||||
return Response.json({ code: 0, data: { userId: 'test-user', tenant: 't', orgFullName: 'o' } })
|
||||
}
|
||||
chats++
|
||||
return Response.json({ code: 'AI_GRAY_ACCESS_DENIED', msg: '访问受限,请联系管理员开通' })
|
||||
})
|
||||
const hooks = await plugin.server({})
|
||||
const loader = await hooks.auth.loader(async () => ({ type: 'api', key: 'fake-key' }))
|
||||
const r = await loader.fetch('https://api-ai.jd.com/v1/chat/completions', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ model: 'JoyAI-Code-1.6', messages: [{ role: 'user', content: 'OK' }] }),
|
||||
})
|
||||
assert.equal(r.status, 403)
|
||||
assert.equal(r.headers.get('x-magpie-sign-in'), 'kept')
|
||||
assert.equal((await r.json()).error.message, '访问受限,请联系管理员开通')
|
||||
assert.equal(infoCalls, 2)
|
||||
assert.equal(chats, 2)
|
||||
})
|
||||
|
||||
test('plain HTTP 403 is not retried', async t => {
|
||||
let infoCalls = 0
|
||||
let chats = 0
|
||||
t.mock.method(globalThis, 'fetch', async (url) => {
|
||||
if (new URL(url).searchParams.get('functionId') === 'joycode_userInfo') {
|
||||
infoCalls++
|
||||
return Response.json({ code: 0, data: { userId: 'test-user', tenant: 't', orgFullName: 'o' } })
|
||||
}
|
||||
chats++
|
||||
return new Response('nope', { status: 403 })
|
||||
})
|
||||
const hooks = await plugin.server({})
|
||||
const loader = await hooks.auth.loader(async () => ({ type: 'api', key: 'fake-key' }))
|
||||
const r = await loader.fetch('https://api-ai.jd.com/v1/chat/completions', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ model: 'JoyAI-Code-1.6', messages: [{ role: 'user', content: 'OK' }] }),
|
||||
})
|
||||
assert.equal(r.status, 403)
|
||||
assert.equal(r.headers.get('x-magpie-sign-in'), 'kept')
|
||||
assert.equal(infoCalls, 1)
|
||||
assert.equal(chats, 1)
|
||||
})
|
||||
|
||||
test('non-streaming chat JSON larger than 1MB is returned intact', async t => {
|
||||
const content = 'x'.repeat(1_200_000)
|
||||
const r = await request(t, Response.json({ choices: [{ message: { content } }] }))
|
||||
assert.equal(r.status, 200)
|
||||
assert.equal((await r.json()).choices[0].message.content, content)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user