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:
@@ -5,7 +5,11 @@ import plugin from './index.mjs'
|
||||
const auth = { type: 'api', key: 'fake-ticket', metadata: { tenant: '测试租户', loginType: 'PIN_JD_CLOUD' } }
|
||||
const item = { type: 'seatPackage', title: '套餐积分', badge: '团队版(限免)', used: 0, total: 10000, remain: 10000, usedPercent: 0, expireText: '有效期至 2026年11月11日' }
|
||||
|
||||
async function query(t, response, account = auth) {
|
||||
// Install fetch once per test. Calling mock.method again in the same test
|
||||
// makes Bun 1.4 restore the wrong function, and packages that captured
|
||||
// global fetch at import (the nonet guards) keep calling this mock.
|
||||
function mockQuota(t, account = auth) {
|
||||
let response = () => { throw new Error('no quota response') }
|
||||
t.mock.method(globalThis, 'fetch', async (url, init) => {
|
||||
assert.equal(new URL(url).origin, 'https://api-ai.jd.com')
|
||||
assert.equal(new URL(url).searchParams.get('functionId'), 'get_newpoint_ide')
|
||||
@@ -17,9 +21,16 @@ async function query(t, response, account = auth) {
|
||||
assert.ok(init.signal)
|
||||
return response()
|
||||
})
|
||||
const hooks = await plugin.server({})
|
||||
assert.equal(typeof hooks.auth.usage, 'function')
|
||||
return hooks.auth.usage(async () => account)
|
||||
return async next => {
|
||||
response = next
|
||||
const hooks = await plugin.server({})
|
||||
assert.equal(typeof hooks.auth.usage, 'function')
|
||||
return hooks.auth.usage(async () => account)
|
||||
}
|
||||
}
|
||||
|
||||
async function query(t, response, account = auth) {
|
||||
return mockQuota(t, account)(response)
|
||||
}
|
||||
|
||||
test('maps actual IDE quota schema including zero usage and expiry text', async (t) => {
|
||||
@@ -32,6 +43,50 @@ test('maps actual IDE quota schema including zero usage and expiry text', async
|
||||
assert.match(result.windows[0].display, /剩余 10,000/)
|
||||
assert.match(result.windows[0].display, /2026年11月11日/)
|
||||
assert.equal(result.windows[0].resetsAt, undefined)
|
||||
// 23:59:59 on 2026-11-11 in China, shown as "将于 … 过期", not a reset.
|
||||
assert.equal(result.until, '2026-11-11T15:59:59.000Z')
|
||||
assert.equal(result.windows[0].runsOutAt, '2026-11-11T15:59:59.000Z')
|
||||
assert.equal(result.windows[0].runsOut, 10000)
|
||||
})
|
||||
|
||||
test('plan expiry accepts the IDE date phrases and ignores text that is not a date', async (t) => {
|
||||
const run = mockQuota(t)
|
||||
for (const [expireText, until] of [
|
||||
['有效期至2026年11月11日', '2026-11-11T15:59:59.000Z'],
|
||||
['2026-11-11', '2026-11-11T15:59:59.000Z'],
|
||||
['2026/11/11', '2026-11-11T15:59:59.000Z'],
|
||||
['2024年2月29日', '2024-02-29T15:59:59.000Z'],
|
||||
['长期有效', undefined],
|
||||
['有效期至 2026年2月31日', undefined],
|
||||
['有效期至 1999年11月11日', undefined],
|
||||
]) {
|
||||
const result = await run(() => Response.json({ code: 0, data: { usageItems: [{ ...item, expireText }] } }))
|
||||
assert.equal(result.until, until, expireText)
|
||||
assert.equal(result.windows[0].runsOutAt, until, expireText)
|
||||
assert.equal(result.windows[0].runsOut, until ? 10000 : undefined, expireText)
|
||||
assert.equal(result.windows[0].resetsAt, undefined)
|
||||
assert.match(result.windows[0].display, new RegExp(expireText.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')))
|
||||
}
|
||||
})
|
||||
|
||||
test('plan expiry follows the package row, and otherwise the earliest pool', async (t) => {
|
||||
const run = mockQuota(t)
|
||||
const packaged = await run(() => Response.json({ code: 0, data: { usageItems: [
|
||||
{ ...item, expireText: '有效期至 2026年12月1日' },
|
||||
{ title: '加油包', used: 0, total: 100, remain: 100, usedPercent: 0, expireText: '2026-11-01' },
|
||||
] } }))
|
||||
assert.equal(packaged.until, '2026-12-01T15:59:59.000Z')
|
||||
assert.equal(packaged.windows[0].runsOutAt, '2026-12-01T15:59:59.000Z')
|
||||
assert.equal(packaged.windows[0].runsOut, 10000)
|
||||
assert.equal(packaged.windows[1].runsOutAt, '2026-11-01T15:59:59.000Z')
|
||||
assert.equal(packaged.windows[1].runsOut, 100)
|
||||
assert.ok(packaged.windows.every(window => window.resetsAt === undefined))
|
||||
|
||||
const pools = await run(() => Response.json({ code: 0, data: { usageItems: [
|
||||
{ title: '套餐积分', used: 0, total: 100, remain: 100, usedPercent: 0, expireText: '2026-12-01' },
|
||||
{ title: '加油包', used: 0, total: 100, remain: 100, usedPercent: 0, expireText: '2026-11-01' },
|
||||
] } }))
|
||||
assert.equal(pools.until, '2026-11-01T15:59:59.000Z')
|
||||
})
|
||||
|
||||
test('multiple pools preserve balances without one exhausted pool blocking the account', async (t) => {
|
||||
@@ -40,6 +95,7 @@ test('multiple pools preserve balances without one exhausted pool blocking the a
|
||||
{ title: '加油包', used: 250, total: 1000, remain: 750, usedPercent: 25 },
|
||||
] } }))
|
||||
assert.equal(result.balance, undefined)
|
||||
assert.equal(result.until, '2026-11-11T15:59:59.000Z')
|
||||
assert.deepEqual(result.windows.map(w => w.used), [100, 25])
|
||||
assert.ok(result.windows.every(w => w.aside === true))
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user