Files
plugins/packages/zcode/team-gift.test.mjs
T
Jinpy 017a7ee3b2 zcode 0.1.45: the daily claim reads the feed as the current client does (#678)
The marketing feed (touch) and the claim now go out under ZCode 3.17.2's
version header. jinpy666 found that under the pinned 3.16.3 the feed
answers empty, while under 3.17.2 the day's banner shows, and checked it
against the live API. The check-in no longer reads /billing/preview,
which the v3.17 client doesn't poll (docs/marketing-touch-client.md in
zai-org/ZCode@a9fe401).

Reviewed at f805d97 and run there on mote:
- bun test packages/zcode: 187 pass, 0 fail.
- bun scripts/check.mjs: clean.
- Broken by hand: with MARKETING_VERSION set back to 3.16.3, 4 tests go
  red (the touch and claim version pins, and the card's feed read).

Not tried here: a live feed or claim. No ZCode account was in a
sandbox, so the live run is jinpy666's (10 accounts: 7 captcha, 3
inactive).

Reported by jinpy666 (#676), who wrote the fix.
2026-11-03 21:29:46 +08:00

111 lines
6.4 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/marketing/touch": return ok({ deliveries: [{ banner: { background: { args: { zcode_plan: { name: "Trust Build" } } }, buttons: [{ action: { type: "claim_zcode_plan", args: { plan_id: "trust" } } }] } }] })
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()
})