The request (X, 2117210897471169009): when the account has gift quota for a GLM model, which ZCode hands out every weekend, spend that first. freeFirst (#505) already does it, but only when it was turned on, so a gift lapsed unused behind an agent that asks for the plain model. It is now on unless the plugin's options or the provider's say `"freeFirst": false`, which are still honoured as they are. Every way the gift can fail (spent, a model it doesn't serve, no ZCode session, a refusal's minute of rest) still falls back to the coding plan, as before. A team seat's plain model spends the personal gift first too. The tests that are about the plan and the -Trial picks now build the plugin with freeFirst off, as they assumed it was. Run on mote: HOME=$(mktemp -d) bun test in packages/zcode, 187 pass; bun scripts/check.mjs zcode ok. Broken by hand: with the default back to off, "free first is on when nothing sets it" and "free first is on by default for a team seat" fail; the rest pass. Not tried: a real account with a claimed gift. The owner's account has only an unclaimed one, which wasn't claimed for this.
111 lines
6.3 KiB
JavaScript
111 lines
6.3 KiB
JavaScript
import { afterEach, beforeEach, expect, test } from "bun:test"
|
|
import { homedir, tmpdir } from "node:os"
|
|
import { realpathSync } from "node:fs"
|
|
|
|
if (![tmpdir(), realpathSync(tmpdir())].some((t) => homedir().startsWith(t))) throw new Error("run with an isolated HOME")
|
|
const { ZCodeAuthPlugin, _internal } = await import("./index.mjs")
|
|
const jwt = ["{}", JSON.stringify({ exp: Math.floor(Date.now() / 1000) + 3600 })].map((s) => Buffer.from(s).toString("base64url")).join(".") + ".sig"
|
|
const state = { site: "bigmodel", base: "https://open.bigmodel.cn/api/anthropic", key: "team.secret", token: "team-token", org: "org", project: "project", jwt, device: "test-device", plan: "Team Max" }
|
|
const auth = (s = state) => ({ type: "oauth", access: s.key, refresh: JSON.stringify(s), expires: 0 })
|
|
const now = Math.floor(Date.now() / 1000)
|
|
const balance = {
|
|
plans: [{ plan_id: "dev-day", name: "ZCode 1024 Dev Day", status: "active", ends_at: now + 86400 }],
|
|
balances: [{ plan_id: "dev-day", show_name: "GLM-5.3", capabilities: ["model:glm-5.3"], total_units: 100, used_units: 25, remaining_units: 75 }],
|
|
}
|
|
const real = globalThis.fetch
|
|
let sent, failGift
|
|
const ok = (data) => Response.json({ code: 0, data })
|
|
beforeEach(() => {
|
|
_internal.routes.clear()
|
|
_internal.blocked.clear()
|
|
sent = []
|
|
failGift = false
|
|
globalThis.fetch = async (input, init = {}) => {
|
|
const url = new URL(String(input))
|
|
const headers = new Headers(init.headers)
|
|
sent.push({ url, headers, body: init.body })
|
|
switch (url.pathname) {
|
|
case "/api/v1/zcode-plan/billing/balance": return failGift ? new Response("", { status: 502 }) : ok(balance)
|
|
// A team's personal subscription list is empty; it must stay on its team.
|
|
case "/api/biz/subscription/list": return ok([])
|
|
case "/api/monitor/usage/quota/limit": return ok({ limits: [{ type: "CREDIT_LIMIT", unit: 6, number: 1, percentage: 90 }] })
|
|
case "/api/biz/team/subscribe/product/querySubscribeDetail": return ok({ hasSubscription: true, status: "EFFECTIVE", memberGrantStatus: "VALID", productName: "Team Max" })
|
|
case "/api/biz/customer-package-reset/list": return ok({})
|
|
case "/api/v1/zcode-plan/billing/preview": return ok({ plans: [{ plan_id: "trust", name: "Trust Build" }] })
|
|
case "/api/anthropic/v1/messages":
|
|
case "/api/v1/zcode-plan/anthropic/v1/messages": return ok({ type: "message", content: [] })
|
|
default: return new Response("", { status: 404 })
|
|
}
|
|
}
|
|
})
|
|
afterEach(() => { globalThis.fetch = real })
|
|
// the team plan and the -Trial picks, with freeFirst (on by default) off;
|
|
// "free first is on by default for a team seat" builds its own
|
|
const hooks = (options = { freeFirst: false }) => ZCodeAuthPlugin({ client: {} }, options)
|
|
|
|
test("a team seat lists personal gift trials even without a resolved team key", async () => {
|
|
const h = await hooks()
|
|
for (const key of [state.key, ""]) {
|
|
const models = await h.provider.models({ models: {} }, { auth: auth({ ...state, key }) })
|
|
expect(models["GLM-5.3-Trial"]).toBeDefined()
|
|
expect(models["GLM-5.3"]).toBeDefined()
|
|
expect((await _internal.plansOf({ ...state, key })).start).toBe(false)
|
|
}
|
|
expect(sent.filter((x) => x.url.pathname === "/api/biz/subscription/list")).toHaveLength(0)
|
|
})
|
|
|
|
test("the team card includes gift buckets and hints, with separate model limits", async () => {
|
|
const h = await hooks()
|
|
const card = await h.auth.usage(async () => auth())
|
|
expect(card.plan).toBe("Team Max")
|
|
expect(card.windows[0]).toMatchObject({ used: 90, notModels: ["glm-5.3-trial"] })
|
|
expect(card.windows.find((w) => w.models?.includes("GLM-5.3-Trial"))).toMatchObject({ used: 25 })
|
|
expect(card.windows.find((w) => w.name === "Gift plans to claim in the ZCode app")).toBeDefined()
|
|
})
|
|
|
|
test("a gift failure preserves the team card and retries within a minute", async () => {
|
|
failGift = true
|
|
const h = await hooks()
|
|
await h.provider.models({ models: {} }, { auth: auth() })
|
|
const card = await h.auth.usage(async () => auth())
|
|
expect(card.error).toBeUndefined()
|
|
expect(card.windows[0].used).toBe(90)
|
|
const route = await _internal.plansOf(state)
|
|
expect(route).toMatchObject({ start: false, gift: null })
|
|
expect(route.ttl).toBeLessThanOrEqual(60000)
|
|
})
|
|
|
|
test("a team without a ZCode session explains the missing trials", async () => {
|
|
const h = await hooks()
|
|
const card = await h.auth.usage(async () => auth({ ...state, jwt: "" }))
|
|
expect(card.windows.find((w) => w.name === "ZCode gift plans (-Trial models) need ZCode's sign-in")).toBeDefined()
|
|
expect(sent.filter((x) => x.url.pathname.includes("/billing/"))).toHaveLength(0)
|
|
})
|
|
|
|
test("ordinary models spend the team; trials spend the personal gift without team credentials", async () => {
|
|
const h = await hooks()
|
|
const loader = await h.auth.loader(async () => auth(), { id: "zcode" })
|
|
for (const model of ["GLM-5.3", "GLM-5.3-Trial"]) {
|
|
expect((await loader.fetch(state.base + "/v1/messages", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ model, max_tokens: 1, messages: [{ role: "user", content: "hi" }] }) })).status).toBe(200)
|
|
}
|
|
const messages = sent.filter((x) => x.url.pathname.endsWith("/messages"))
|
|
expect(messages).toHaveLength(2)
|
|
expect(messages[0].url.origin).toBe("https://open.bigmodel.cn")
|
|
expect(messages[0].headers.get("x-api-key")).toBe(state.key)
|
|
expect(messages[1].url.origin).toBe("https://zcode.z.ai")
|
|
expect(messages[1].headers.get("authorization")).toBe("Bearer " + jwt)
|
|
expect(messages[1].headers.get("x-api-key")).toBeNull()
|
|
expect(messages[1].headers.get("Bigmodel-Organization")).toBeNull()
|
|
expect(JSON.parse(messages[1].body).model).toBe("glm-5.3")
|
|
})
|
|
|
|
test("free first is on by default for a team seat: the plain model spends the personal gift", async () => {
|
|
const h = await hooks({})
|
|
const loader = await h.auth.loader(async () => auth(), { id: "zcode" })
|
|
expect((await loader.fetch(state.base + "/v1/messages", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ model: "GLM-5.3", max_tokens: 1, messages: [{ role: "user", content: "hi" }] }) })).status).toBe(200)
|
|
const messages = sent.filter((x) => x.url.pathname.endsWith("/messages"))
|
|
expect(messages).toHaveLength(1)
|
|
expect(messages[0].url.origin).toBe("https://zcode.z.ai")
|
|
expect(messages[0].headers.get("x-api-key")).toBeNull()
|
|
})
|