Files
plugins/packages/zcode/signin.test.mjs
T
yetone 71021b8f72 zcode 0.1.25: a key with no GLM Coding Plan says a ZCode gift plan (Start Plan, 1024 Dev Day) needs ZCode's sign-in
rtbeilin (X, 10-24) has ZCode 1024 Dev Day and no GLM Coding Plan.
"ZCode app's sign-in" works in magpie; the browser sign-in and the API
key don't.

The key: ZCode's gift plans are served by zcode.z.ai to ZCode's session
token (zcodejwttoken, the poll's `token`). A key carries none, so a key
reaches api.z.ai's / open.bigmodel.cn's coding plan alone, and a
gift-only account has nothing there. The card was empty, no error
(usage.test's own "API key, BigModel's" case: an empty list and no
limits gave {windows: []}), and each request got Z.ai's 1113 "no
resource package" with nothing saying why. Now:
- codingUsage: a subscription list read as having no VALID plan, with no
  level, no counted window and no saved plan, is the card's error
  NO_PLAN_KEY, which names the gift plans and says to add the account
  with "ZCode app's sign-in" or a ZCode browser sign-in. A list that
  can't be read keeps the card as it was.
- the fetch: a coding-plan refusal >= 400 to a sign-in with no ZCode
  session, whose code is 1113 (error.code, error.type or the envelope's
  code), keeps its status and Z.ai's words and adds NO_PLAN_KEY. The
  body is read only after the request failed. A sign-in with a session
  and any other code go as they came.
README says it under the key method.

The browser sign-in: no cause found. 3.16.3's bundle (the current
stable, downloaded from zcode.z.ai's release manifest) starts, polls
and reads the flow as the plugin does: same init body, the same
redirect / redirect_uri override, the same ready fields, Z.ai's token
exchanged at /api/auth/z/login. yg6888's screenshot in the same thread
shows two Dev Day accounts working in magpie. Not changed here.

Run:
- bun test packages/zcode, scratch HOME: 99 pass. The two new tests
  (usage "a key with no GLM Coding Plan...", signin "a key's 1113...")
  fail on 0.1.24's index.mjs. bun scripts/check.mjs zcode: ok.
- Real: the owner's real Z.ai key through the new usage hook: card
  "GLM Coding Lite", 5 hours + Weekly, no error. A bogus key: card
  "Authentication Failed", a request 401 {"error":{"message":
  "Authentication Failed","type":"1000"}} unchanged.

Not tried: a real key with no plan (none at hand), so the 1113 body is
magpie's zhipu_coding_test fixture, not a captured one. BigModel's real
answer neither. The browser sign-in with a Dev Day account.
2026-10-24 16:37:39 +08:00

84 lines
4.9 KiB
JavaScript

// A request's failures, as magpie's built-in ZCode account answered them
// (internal/provider/zcode.go zcodeProvider): the plan's 401 goes on as it
// came, saying X-Magpie-Sign-In: kept, as the built-in never marked a
// ZCode account lapsed; a Start Plan sign-in past its end fails in
// errZCodeExpired's words.
import { afterEach, beforeAll, expect, test } from "bun:test"
import { homedir, tmpdir } from "node:os"
import { realpathSync } from "node:fs"
let ZCodeAuthPlugin
beforeAll(async () => {
if (![tmpdir(), realpathSync(tmpdir())].some((t) => homedir().startsWith(t))) throw new Error("run with HOME=$(mktemp -d) bun test")
;({ ZCodeAuthPlugin } = await import("./index.mjs"))
})
const real = globalThis.fetch
afterEach(() => (globalThis.fetch = real))
const jwt = (exp) => ["{}", JSON.stringify({ exp })].map((s) => Buffer.from(s).toString("base64url")).join(".") + ".sig"
async function send(auth, reply) {
globalThis.fetch = async () => reply()
const hooks = await ZCodeAuthPlugin({ client: { auth: { set: async () => {} } } })
const opts = await hooks.auth.loader(async () => auth, { id: "zcode" })
return opts.fetch("https://api.z.ai/api/anthropic/v1/messages", { method: "POST", body: "{}" })
}
const key = { type: "api", key: "one.secret", metadata: { site: "zai" } }
test("the plan's 401 goes on as it came, the account kept", async () => {
const res = await send(key, () => Response.json({ error: { message: "token expired or incorrect" } }, { status: 401 }))
expect(res.status).toBe(401)
expect(res.headers.get("X-Magpie-Sign-In")).toBe("kept")
expect(await res.json()).toEqual({ error: { message: "token expired or incorrect" } })
})
// nor cleared one: an answer that went through leaves the mark as it is
test("other answers go as they came, the account kept", async () => {
for (const status of [200, 429]) {
const res = await send(key, () => new Response("x", { status }))
expect(res.status).toBe(status)
expect(res.headers.get("X-Magpie-Sign-In")).toBe("kept")
expect(await res.text()).toBe("x")
}
})
test("a Start Plan sign-in past its end fails as errZCodeExpired", async () => {
const s = { site: "zai", jwt: jwt(Math.floor(Date.now() / 1000) - 60) }
const auth = { type: "oauth", access: s.jwt, refresh: JSON.stringify(s), expires: 0 }
await expect(send(auth, () => new Response(""))).rejects.toThrow("ZCode's sign-in has expired; sign in to ZCode again (or add the account again in magpie)")
})
// rtbeilin (X, 10-24): a key whose account has only ZCode's gift plan is
// refused 1113 by the coding plan; the answer says why, status kept
test("a key's 1113 says a ZCode gift plan needs ZCode's sign-in", async () => {
const body = { error: { code: "1113", message: "Insufficient balance or no resource package. Please recharge." } }
const res = await send(key, () => Response.json(body, { status: 429 }))
expect(res.status).toBe(429)
expect(res.headers.get("X-Magpie-Sign-In")).toBe("kept")
const got = await res.json()
expect(got.error.code).toBe("1113")
expect(got.error.message).toStartWith("Insufficient balance or no resource package. Please recharge. — this key's account has no GLM Coding Plan;")
expect(got.error.message).toContain("ZCode app's sign-in")
// the code as Z.ai's 401 names its own (error.type), and in BigModel's envelope
const typed = await send(key, () => Response.json({ error: { type: "1113", message: "no package" } }, { status: 429 }))
expect((await typed.json()).error.message).toStartWith("no package — this key's account has no GLM Coding Plan;")
const env = await send(key, () => Response.json({ code: 1113, msg: "余额不足", success: false }, { status: 429 }))
expect((await env.json()).msg).toStartWith("余额不足 — this key's account has no GLM Coding Plan;")
// another refusal, and a ZCode sign-in's 1113, go as they came
const other = await send(key, () => Response.json({ error: { code: "1302", message: "rate" } }, { status: 429 }))
expect(await other.json()).toEqual({ error: { code: "1302", message: "rate" } })
const s = { site: "zai", key: "one.secret", jwt: jwt(Math.floor(Date.now() / 1000) + 3600) }
const withJwt = { type: "oauth", access: s.key, refresh: JSON.stringify(s), expires: 0 }
globalThis.fetch = async (u) => {
// the plan list says a plan, so the request stays on the coding plan
if (String(u).includes("/subscription/list")) return Response.json({ code: 200, data: [{ productName: "GLM Coding Lite", status: "VALID" }] })
if (String(u).includes("/billing/balance")) return Response.json({ code: 0, data: { plans: [], balances: [] } })
return Response.json(body, { status: 429 })
}
const hooks = await ZCodeAuthPlugin({ client: { auth: { set: async () => {} } } })
const opts = await hooks.auth.loader(async () => withJwt, { id: "zcode" })
const own = await opts.fetch("https://api.z.ai/api/anthropic/v1/messages", { method: "POST", body: "{}" })
expect(await own.json()).toEqual(body)
})