Files
plugins/packages/joycode/models.test.mjs
T
kassama f6fdf8fee1 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.
2026-11-04 12:42:45 +08:00

87 lines
4.2 KiB
JavaScript

import test from 'node:test'
import assert from 'node:assert/strict'
import plugin from './index.mjs'
const catalogue = [
{ chatApiModel: 'JoyAI-Code-1.6', label: 'JoyAI-Code-1.6', maxTotalTokens: 200000, respMaxTokens: 64000, features: ['function_call'] },
{ chatApiModel: 'GLM-5.3-agent', label: 'GLM-5.3', maxTotalTokens: 200000, respMaxTokens: 64000, features: ['function_call'] },
{ chatApiModel: 'GPT-5.6 Sol', label: 'GPT-5.6 Sol', extJson: { adapterType: 'openai-response' } },
{ chatApiModel: 'Claude-Opus-5-hq', label: 'Claude-Opus-5', extJson: { adapterType: 'anthropic' } },
]
const billingRate = { excludeTenants: ['JD'], billingRates: [
{ chatApiModel: 'JoyAI-Code-1.6', billingRate: '0x' },
{ chatApiModel: 'GLM-5.3-agent', billingRate: '0.6x' },
{ chatApiModel: 'unavailable-model', billingRate: '0.2x' },
] }
async function load(t, { tenant = 'personal', failRates = false, failList = false, list = catalogue } = {}) {
t.mock.method(globalThis, 'fetch', async (url, init) => {
const fn = new URL(url).searchParams.get('functionId')
assert.equal(init.headers.tenant, tenant)
if (fn === 'joycode_modelList') {
if (failList) throw new Error('offline')
assert.equal(init.body, '{}')
return Response.json({ code: 0, data: list })
}
assert.equal(fn, 'plugin_config')
assert.deepEqual(JSON.parse(init.body), { sceneType: 'billingRate' })
if (failRates) throw new Error('offline')
return Response.json({ code: 0, data: { billingRate } })
})
const hooks = await plugin.server({})
const cfg = {}
await hooks.config(cfg)
const provider = cfg.provider.joycode
assert.equal(typeof hooks.provider?.models, 'function')
return { provider, models: await hooks.provider.models(provider, { auth: { type: 'api', key: 'fake', metadata: { tenant } } }) }
}
test('sync current model multipliers including zero without adding pricing-only models', async t => {
const { models } = await load(t)
assert.deepEqual(Object.keys(models), ['JoyAI-Code-1.6', 'GLM-5.3-agent', 'GPT-5.6 Sol', 'Claude-Opus-5-hq'])
assert.equal(models['JoyAI-Code-1.6'].rate, 0)
assert.equal(models['JoyAI-Code-1.6'].free, true)
assert.equal(models['GLM-5.3-agent'].rate, 0.6)
assert.equal(models['GLM-5.3-agent'].capabilities.input.image, false)
})
test('model adapter chooses the SDK that speaks its upstream protocol', async t => {
const { models } = await load(t)
assert.equal(models['JoyAI-Code-1.6'].api.npm, '@ai-sdk/openai-compatible')
assert.equal(models['GPT-5.6 Sol'].api.npm, '@ai-sdk/openai')
assert.equal(models['Claude-Opus-5-hq'].api.npm, '@ai-sdk/anthropic')
for (const model of Object.values(models)) assert.equal(model.api.url, 'https://api-ai.jd.com/v1')
})
test('excluded tenants do not inherit public pricing', async t => {
const { models } = await load(t, { tenant: 'JD' })
assert.equal(models['GLM-5.3-agent'].rate, undefined)
assert.notEqual(models['JoyAI-Code-1.6'].free, true)
})
test('model-specific billing rate takes priority', async t => {
const { models } = await load(t, { list: [{ ...catalogue[1], billingRate: '0.3x' }] })
assert.equal(models['GLM-5.3-agent'].rate, 0.3)
})
test('pricing fetch failure retains models without invented rates', async t => {
const { models } = await load(t, { failRates: true })
assert.equal(Object.keys(models).length, 4)
assert.equal(models['GLM-5.3-agent'].rate, undefined)
})
test('catalogue fetch failure returns original fallback model list', async t => {
const { models, provider } = await load(t, { failList: true })
assert.equal(models, provider.models)
})
test('empty catalogue keeps the fallback model list', async t => {
const { models, provider } = await load(t, { list: [] })
assert.equal(models, provider.models)
})
test('a catalogue of only hidden models does not fall back', async t => {
const { models } = await load(t, { list: [{ chatApiModel: 'JoyAI-Code-1.6', hidden: true }] })
assert.deepEqual(models, {})
})
test('static fallback models do not advertise image input', async t => {
const hooks = await plugin.server({})
const cfg = {}
await hooks.config(cfg)
for (const id of ['Kimi-K2.6-agent', 'MiniMax-M3-agent', 'Doubao-Seed-2.0-pro-agent']) {
assert.deepEqual(cfg.provider.joycode.models[id].modalities.input, ['text'])
}
})