Files
plugins/packages/trae/usage.test.mjs
T
yetone a6ebbc55d8 trae 0.1.1: SOLO's models are listed too, and asked through SOLO's function (yetone/magpie#681)
DeepSeek-V4.1-Flash, which Trae CN's SOLO picker offers, was missing from
magpie's list: the plugin read the account's models from
/api/ide/v1/get_detail_param for chat_v3 (the classic IDE's chat) only, and
SOLO's models (the user's list showed DeepSeek-V4-Flash-Official and the
custom_model_* entries of chat_v3, no V4.1) come under solo_work_lite. Now
the list is asked for every chat function (chat_v3, solo_work_lite) and put
together, chat_v3's first; one list failing leaves the other's, a lapsed
sign-in still throws. A model only SOLO lists is asked through
solo_work_lite first, the 4001/4023/1005 fallback kept.

usage.test.mjs "SOLO's models are listed too" fails on 0.1.0 (V4.1 absent)
and passes; bun test: 21 pass. Not tried with a real Trae CN account (none
signed in here).
2026-10-03 21:17:55 +08:00

102 lines
5.2 KiB
JavaScript

// The usage card: the account's credits from its entitlement packs; and
// the model list, the account's own when Trae answers.
import "./nonet.mjs"
import { afterEach, expect, test } from "bun:test"
import { TraeCNAuthPlugin, _internal } from "./index.mjs"
import { fakeTrae, json, signedIn, sse } from "./fake.mjs"
let f
afterEach(() => f?.close())
test("credits are the packs' limits and what they used", async () => {
f = fakeTrae()
f.route("POST /trae/api/v2/pay/ide_user_ent_usage", () => json({ is_credits_billing: false, user_entitlement_pack_list: [
{ entitlement_base_info: { quota: { credits_limit: 300 }, end_time: 1790000000 }, usage: { credits_amount: 75 } },
{ entitlement_base_info: { quota: {} }, usage: {} }, // a feature pack, no credits
{ entitlement_base_info: { quota: { credits_limit: 100 } }, usage: { credits_amount: 25 } },
] }))
const hooks = await TraeCNAuthPlugin({ client: {} })
const u = await hooks.auth.usage(async () => signedIn())
expect(f.seen[0].json).toEqual({ require_usage: true, req_source: 0 })
expect(f.seen[0].headers.get("authorization")).toBe("Cloud-IDE-JWT jwt-1")
expect(u.plan).toBe("Free")
expect(u.user).toBe("Ann")
expect(u.signIn).toBe("kept")
expect(u.balance).toBe("300 of 400 credits left")
expect(u.windows).toEqual([{ name: "Credits", used: 25, resetsAt: new Date(1790000000 * 1000).toISOString() }])
})
test("unlimited packs say so", () => {
expect(_internal.credits({ user_entitlement_pack_list: [{ entitlement_base_info: { quota: { credits_limit: -1 } }, usage: { credits_amount: 3 } }] }).unlimited).toBe(true)
})
test("a 401 reading credits marks the account", async () => {
f = fakeTrae()
f.route("POST /trae/api/v2/pay/ide_user_ent_usage", () => json({ code: 1001, message: "not login" }, 401))
const hooks = await TraeCNAuthPlugin({ client: {} })
const u = await hooks.auth.usage(async () => signedIn())
expect(u.signIn).toBe("expired")
expect(u.error).toContain("sign in again")
})
async function given(hooks) {
const cfg = { provider: {} }
await hooks.config(cfg)
const p = cfg.provider["trae-cn"]
return { id: "trae-cn", models: Object.fromEntries(Object.entries(p.models).map(([id, m]) => [id, { id, ...m }])) }
}
test("the config declares the known models; the live list replaces them", async () => {
f = fakeTrae()
f.route("POST /api/ide/v1/get_detail_param", () => json({ config_info_list: [
{ config_name: "glm-5.2", display_name: "GLM-5.2", context_window_size: { max: [200000] } },
{ config_name: "DeepSeek-V4-Pro", display_name: "DeepSeek-V4-Pro" },
] }))
const hooks = await TraeCNAuthPlugin({ client: {} })
const p = await given(hooks)
expect(Object.keys(p.models)).toContain("kimi-k2.6")
const live = await hooks.provider.models(p, { auth: signedIn() })
expect(Object.keys(live)).toEqual(["glm-5.2", "DeepSeek-V4-Pro"])
expect(live["glm-5.2"].limit.context).toBe(200000)
expect(f.seen.map((r) => r.json.function).sort()).toEqual(["chat_v3", "solo_work_lite"])
})
test("SOLO's models are listed too, and asked through SOLO's function (yetone/magpie#681)", async () => {
f = fakeTrae()
f.route("POST /api/ide/v1/get_detail_param", (r) => json({ config_info_list: r.json.function === "chat_v3"
? [{ config_name: "glm-5.2", display_name: "GLM-5.2" }, { config_name: "DeepSeek-V4-Flash-Official", display_name: "DeepSeek-V4-Flash-Official" }]
: [{ config_name: "glm-5.2", display_name: "GLM-5.2" }, { config_name: "DeepSeek-V4.1-Flash", display_name: "DeepSeek-V4.1-Flash", context_window_size: { max: [1000000] } }] }))
f.route("POST /api/agent/v3/llm_utils_chat", (r) => sse([["output", { response: r.json.function }], ["done", {}]]))
const hooks = await TraeCNAuthPlugin({ client: {} })
const p = await given(hooks)
const live = await hooks.provider.models(p, { auth: signedIn() })
expect(Object.keys(live)).toEqual(["glm-5.2", "DeepSeek-V4-Flash-Official", "DeepSeek-V4.1-Flash"])
expect(live["DeepSeek-V4.1-Flash"].name).toBe("DeepSeek-V4.1-Flash")
expect(live["DeepSeek-V4.1-Flash"].limit.context).toBe(1000000)
const opts = await hooks.auth.loader(async () => signedIn())
const ask = async (model) => {
const res = await opts.fetch(opts.baseURL + "/chat/completions", { method: "POST", body: JSON.stringify({ model, messages: [{ role: "user", content: "hi" }] }) })
return (await res.json()).choices[0].message.content
}
expect(await ask("DeepSeek-V4.1-Flash")).toBe("solo_work_lite")
expect(await ask("glm-5.2")).toBe("chat_v3")
})
test("one function's list failing leaves the other's", async () => {
f = fakeTrae()
f.route("POST /api/ide/v1/get_detail_param", (r) => r.json.function === "chat_v3"
? json({ config_info_list: [{ config_name: "glm-5.2" }] })
: new Response("down", { status: 503 }))
const hooks = await TraeCNAuthPlugin({ client: {} })
const p = await given(hooks)
expect(Object.keys(await hooks.provider.models(p, { auth: signedIn() }))).toEqual(["glm-5.2"])
})
test("when the list can't be read, the known models stand", async () => {
f = fakeTrae()
f.route("POST /api/ide/v1/get_detail_param", () => new Response("down", { status: 503 }))
const hooks = await TraeCNAuthPlugin({ client: {} })
const p = await given(hooks)
expect(await hooks.provider.models(p, { auth: signedIn() })).toBe(p.models)
})