Files
plugins/packages/zcode/own.test.mjs
T
yetone 826d487839 workbuddy: the refusal of Codex's and Claude Code's prompt carries magpie's hint, and a model of no credits is marked free; zcode: sign in as the ZCode app, following its own sign-in and team switches
- workbuddy: 'Illegal API invocation from an unapproved channel' ends in the hint magpie's built-in added (provider.WBRefusedHint), where magpie's routing page finds it; models with credits x0.00 carry free: true
- zcode: a new sign-in, "ZCode app's sign-in", reads ~/.zcode/v2 (credentials decrypted as ZCode keeps them, setting.json's team plan) for every request, as magpie's built-in did; magpie moves ZCode's own account marked source: zcode
2026-10-01 01:21:06 +08:00

71 lines
3.4 KiB
JavaScript

// "ZCode app's sign-in" reads what ZCode keeps, as magpie's built-in did
// (internal/provider/zcode.go zcodeOwn, zcode_team.go zcodeOwnTeam), and
// follows it: a request made after ZCode switched to a team's plan, or
// signed in anew, uses what ZCode has now.
import { test, expect, beforeAll } from "bun:test"
import { homedir, tmpdir } from "node:os"
import { mkdirSync, realpathSync, writeFileSync } from "node:fs"
import { createCipheriv, createHash, randomBytes } from "node:crypto"
import { join } from "node:path"
let ZCodeAuthPlugin, _internal
beforeAll(async () => {
if (![tmpdir(), realpathSync(tmpdir())].some((t) => homedir().startsWith(t))) throw new Error("run with HOME=$(mktemp -d) bun test")
process.env.ZCODE_CREDENTIAL_SECRET = "test-seed"
;({ ZCodeAuthPlugin, _internal } = await import("./index.mjs"))
})
const dir = () => join(homedir(), ".zcode", "v2")
function enc(text) {
const key = createHash("sha256").update("test-seed").digest()
const iv = randomBytes(12)
const c = createCipheriv("aes-256-gcm", key, iv)
const data = Buffer.concat([c.update(text, "utf8"), c.final()])
return "enc:v1:" + [iv, c.getAuthTag(), data].map((b) => b.toString("base64url")).join(".")
}
function zcodeHas(store, setting = {}) {
mkdirSync(dir(), { recursive: true })
writeFileSync(join(dir(), "credentials.json"), JSON.stringify(Object.fromEntries(Object.entries(store).map(([k, v]) => [k, enc(v)]))))
writeFileSync(join(dir(), "setting.json"), JSON.stringify(setting))
}
const KEY = "account-provider:coding-plan:account:zai-individual-coding-plan:account:u1:api-key"
test("the sign-in takes ZCode's account and key, and follows ZCode after", async () => {
zcodeHas({ [KEY]: "id1.secret1", zcodejwttoken: "Bearer j1", "oauth:zai:user_info": JSON.stringify({ email: "ada@phone.local" }) })
const hooks = await ZCodeAuthPlugin({ client: {} })
const m = hooks.auth.methods.find((x) => x.label === "ZCode app's sign-in")
const got = await (await m.authorize()).callback()
expect(got.type).toBe("success")
expect(got.accountId).toBe("ada")
expect(got.access).toBe("id1.secret1")
expect(JSON.parse(got.refresh).source).toBe("zcode")
const auth = { ...got, type: "oauth" }
expect(_internal.stateOf(auth)).toMatchObject({ key: "id1.secret1", jwt: "j1", base: "https://api.z.ai/api/anthropic" })
// ZCode signs in again with a new key
zcodeHas({ [KEY]: "id2.secret2", zcodejwttoken: "j2" })
expect(_internal.stateOf(auth)).toMatchObject({ key: "id2.secret2", jwt: "j2" })
// and switches to a team's plan
zcodeHas(
{ [KEY]: "id2.secret2", zcodejwttoken: "j2", "oauth:zai:access_token": "tok-team" },
{ providerFamilyConnectionSelections: { zai: { kind: "team-coding-plan", organizationId: "org1", projectId: "p1" } } },
)
expect(_internal.stateOf(auth)).toMatchObject({ key: "", token: "tok-team", org: "org1", project: "p1", jwt: "j2" })
})
test("with ZCode signed out, the sign-in says so", async () => {
zcodeHas({})
const hooks = await ZCodeAuthPlugin({ client: {} })
const m = hooks.auth.methods.find((x) => x.label === "ZCode app's sign-in")
const got = await (await m.authorize()).callback()
expect(got.type).toBe("failed")
expect(got.error).toContain("ZCode isn't signed in")
})
test("a sign-in not ZCode's keeps its own key", () => {
zcodeHas({ [KEY]: "id9.secret9" })
const s = _internal.stateOf({ type: "oauth", access: "mine.k", refresh: JSON.stringify({ site: "zai", key: "mine.k" }) })
expect(s.key).toBe("mine.k")
})