|
| 1 | +/** |
| 2 | + * Tests for `src/claim/client.ts` — mock fetch asserts request shape (URL, |
| 3 | + * query params, headers incl. captcha/version/platform) and canned responses |
| 4 | + * verify preview parsing and the claim biz-code failure mapping. |
| 5 | + */ |
| 6 | +import { describe, it, expect, mock } from "bun:test"; |
| 7 | +import { createClaimClient } from "./client.js"; |
| 8 | +import type { ClaimFailureKind } from "./types.js"; |
| 9 | + |
| 10 | +function makeMockFetch(impl: (req: Request, init?: RequestInit) => Promise<Response>): typeof fetch { |
| 11 | + return mock((url: string | URL | Request, init?: RequestInit) => { |
| 12 | + const req = new Request(typeof url === "string" ? url : url.toString(), init); |
| 13 | + return impl(req, init); |
| 14 | + }) as unknown as typeof fetch; |
| 15 | +} |
| 16 | + |
| 17 | +function jsonResp(body: unknown, status = 200): Response { |
| 18 | + return new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } }); |
| 19 | +} |
| 20 | + |
| 21 | +const PREVIEW_BODY = { |
| 22 | + code: 0, |
| 23 | + data: { |
| 24 | + plans: [ |
| 25 | + { |
| 26 | + plan_id: "weekend-free-1024", |
| 27 | + name: "Weekend Free", |
| 28 | + description: "weekend trial", |
| 29 | + priority: 5, |
| 30 | + starts_at: 1783000000, |
| 31 | + ends_at: 1783200000, |
| 32 | + entitlements: [ |
| 33 | + { |
| 34 | + entitlement_id: "ent-1", |
| 35 | + show_name: "GLM-5.2", |
| 36 | + meter: "token", |
| 37 | + unit_type: "tokens", |
| 38 | + capabilities: ["chat"], |
| 39 | + grant_units: 100000, |
| 40 | + period: "daily", |
| 41 | + priority: 1, |
| 42 | + effective_at: 1783000000, |
| 43 | + }, |
| 44 | + { no_id: "dropped" }, |
| 45 | + ], |
| 46 | + }, |
| 47 | + { name: "no plan_id — dropped" }, |
| 48 | + ], |
| 49 | + }, |
| 50 | +}; |
| 51 | + |
| 52 | +describe("createClaimClient — getPreviews", () => { |
| 53 | + it("fetches preview with app_version + platform query and parses plans/entitlements", async () => { |
| 54 | + let capturedUrl = ""; |
| 55 | + const fetchImpl = makeMockFetch((req) => { |
| 56 | + capturedUrl = req.url; |
| 57 | + return Promise.resolve(jsonResp(PREVIEW_BODY)); |
| 58 | + }); |
| 59 | + const client = createClaimClient({ origin: "https://zcode.z.ai/", jwt: "jwt-1", appVersion: "3.10.0", platform: "win32-x64", fetchImpl }); |
| 60 | + |
| 61 | + const plans = await client.getPreviews(); |
| 62 | + |
| 63 | + expect(capturedUrl).toBe("https://zcode.z.ai/api/v1/zcode-plan/billing/preview?app_version=3.10.0&platform=win32-x64"); |
| 64 | + expect(plans).toHaveLength(1); |
| 65 | + const p = plans[0]; |
| 66 | + expect(p.planId).toBe("weekend-free-1024"); |
| 67 | + expect(p.name).toBe("Weekend Free"); |
| 68 | + expect(p.priority).toBe(5); |
| 69 | + expect(p.startsAt).toBe(1783000000); |
| 70 | + expect(p.endsAt).toBe(1783200000); |
| 71 | + expect(p.entitlements).toHaveLength(1); |
| 72 | + expect(p.entitlements[0]).toEqual({ |
| 73 | + entitlementId: "ent-1", |
| 74 | + showName: "GLM-5.2", |
| 75 | + meter: "token", |
| 76 | + unitType: "tokens", |
| 77 | + capabilities: ["chat"], |
| 78 | + grantUnits: 100000, |
| 79 | + period: "daily", |
| 80 | + priority: 1, |
| 81 | + effectiveAt: 1783000000, |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + it("sends Authorization only when jwt provided", async () => { |
| 86 | + const auths: (string | null)[] = []; |
| 87 | + const fetchImpl = makeMockFetch((req) => { |
| 88 | + auths.push(req.headers.get("authorization")); |
| 89 | + return Promise.resolve(jsonResp(PREVIEW_BODY)); |
| 90 | + }); |
| 91 | + const withJwt = createClaimClient({ origin: "https://zcode.z.ai", jwt: "jwt-1", appVersion: "3.10.0", platform: "win32-x64", fetchImpl }); |
| 92 | + await withJwt.getPreviews(); |
| 93 | + const anon = createClaimClient({ origin: "https://zcode.z.ai", appVersion: "3.10.0", platform: "win32-x64", fetchImpl }); |
| 94 | + await anon.getPreviews(); |
| 95 | + expect(auths[0]).toBe("Bearer jwt-1"); |
| 96 | + expect(auths[1]).toBeNull(); |
| 97 | + }); |
| 98 | + |
| 99 | + it("throws on non-zero biz code and missing data", async () => { |
| 100 | + const fetchImpl = makeMockFetch(() => Promise.resolve(jsonResp({ code: 1002, msg: "campaign ended" }))); |
| 101 | + const client = createClaimClient({ origin: "https://zcode.z.ai", appVersion: "3.10.0", platform: "win32-x64", fetchImpl }); |
| 102 | + await expect(client.getPreviews()).rejects.toThrow("1002"); |
| 103 | + const fetchNoData = makeMockFetch(() => Promise.resolve(jsonResp({ code: 0 }))); |
| 104 | + const client2 = createClaimClient({ origin: "https://zcode.z.ai", appVersion: "3.10.0", platform: "win32-x64", fetchImpl: fetchNoData }); |
| 105 | + await expect(client2.getPreviews()).rejects.toThrow("preview failed"); |
| 106 | + }); |
| 107 | +}); |
| 108 | + |
| 109 | +describe("createClaimClient — claim", () => { |
| 110 | + function capture(req: Request) { |
| 111 | + return { |
| 112 | + url: req.url, |
| 113 | + auth: req.headers.get("authorization"), |
| 114 | + captchaParam: req.headers.get("x-aliyun-captcha-verify-param"), |
| 115 | + captchaRegion: req.headers.get("x-aliyun-captcha-verify-region"), |
| 116 | + appVersion: req.headers.get("x-zcode-app-version"), |
| 117 | + platform: req.headers.get("x-platform"), |
| 118 | + contentType: req.headers.get("content-type"), |
| 119 | + }; |
| 120 | + } |
| 121 | + |
| 122 | + it("POSTs plan_id with jwt + captcha + version + platform headers", async () => { |
| 123 | + let cap: ReturnType<typeof capture> | undefined; |
| 124 | + let body = ""; |
| 125 | + const fetchImpl = makeMockFetch(async (req) => { |
| 126 | + cap = capture(req); |
| 127 | + body = await req.text(); |
| 128 | + return jsonResp({ code: 0, data: { plan: { starts_at: 1783000000, ends_at: 1783200000 } } }); |
| 129 | + }); |
| 130 | + const client = createClaimClient({ origin: "https://zcode.z.ai", jwt: "jwt-1", appVersion: "3.10.0", platform: "linux-x64", fetchImpl }); |
| 131 | + |
| 132 | + const out = await client.claim("weekend-free-1024", { verifyParam: "cap-token", region: "cn-hangzhou" }); |
| 133 | + |
| 134 | + expect(cap!.url).toBe("https://zcode.z.ai/api/v1/zcode-plan/billing/claim"); |
| 135 | + expect(cap!.auth).toBe("Bearer jwt-1"); |
| 136 | + expect(cap!.captchaParam).toBe("cap-token"); |
| 137 | + expect(cap!.captchaRegion).toBe("cn-hangzhou"); |
| 138 | + expect(cap!.appVersion).toBe("3.10.0"); |
| 139 | + expect(cap!.platform).toBe("linux-x64"); |
| 140 | + expect(cap!.contentType).toBe("application/json"); |
| 141 | + expect(JSON.parse(body)).toEqual({ plan_id: "weekend-free-1024" }); |
| 142 | + expect(out).toEqual({ ok: true, planId: "weekend-free-1024", startsAt: 1783000000, endsAt: 1783200000 }); |
| 143 | + }); |
| 144 | + |
| 145 | + it("omits region header when region missing/empty", async () => { |
| 146 | + let region: string | null = "sentinel"; |
| 147 | + const fetchImpl = makeMockFetch((req) => { |
| 148 | + region = req.headers.get("x-aliyun-captcha-verify-region"); |
| 149 | + return Promise.resolve(jsonResp({ code: 0, data: { plan: {} } })); |
| 150 | + }); |
| 151 | + const client = createClaimClient({ origin: "https://zcode.z.ai", jwt: "jwt-1", appVersion: "3.10.0", platform: "win32-x64", fetchImpl }); |
| 152 | + await client.claim("p1", { verifyParam: "t" }); |
| 153 | + expect(region).toBeNull(); |
| 154 | + }); |
| 155 | + |
| 156 | + it("maps biz codes to failure kinds and surfaces failureEndsAt", async () => { |
| 157 | + const cases: Array<[number, ClaimFailureKind]> = [ |
| 158 | + [1001, "not_found"], |
| 159 | + [1002, "unavailable"], |
| 160 | + [1003, "already_claimed"], |
| 161 | + [1004, "ineligible"], |
| 162 | + [1005, "quota_exhausted"], |
| 163 | + [3001, "invalid_request"], |
| 164 | + [3007, "captcha"], |
| 165 | + [9999, "unknown"], |
| 166 | + ]; |
| 167 | + for (const [code, kind] of cases) { |
| 168 | + const fetchImpl = makeMockFetch(() => |
| 169 | + Promise.resolve(jsonResp({ code, msg: `biz ${code}`, data: { plan: { ends_at: 1783100000 } } })), |
| 170 | + ); |
| 171 | + const client = createClaimClient({ origin: "https://zcode.z.ai", jwt: "jwt-1", appVersion: "3.10.0", platform: "win32-x64", fetchImpl }); |
| 172 | + const out = await client.claim("p1", { verifyParam: "t" }); |
| 173 | + expect(out.ok).toBe(false); |
| 174 | + if (!out.ok) { |
| 175 | + expect(out.failureKind).toBe(kind); |
| 176 | + expect(out.code).toBe(code); |
| 177 | + expect(out.failureEndsAt).toBe(1783100000); |
| 178 | + } |
| 179 | + } |
| 180 | + }); |
| 181 | + |
| 182 | + it("derives http_error / login_required from HTTP status without biz code", async () => { |
| 183 | + const fetch500 = makeMockFetch(() => Promise.resolve(jsonResp({ msg: "boom" }, 500))); |
| 184 | + const c1 = createClaimClient({ origin: "https://zcode.z.ai", jwt: "j", appVersion: "3.10.0", platform: "p", fetchImpl: fetch500 }); |
| 185 | + const out1 = await c1.claim("p1", { verifyParam: "t" }); |
| 186 | + expect(out1.ok).toBe(false); |
| 187 | + if (!out1.ok) expect(out1.failureKind).toBe("http_error"); |
| 188 | + |
| 189 | + const fetch401 = makeMockFetch(() => Promise.resolve(new Response("", { status: 401 }))); |
| 190 | + const c2 = createClaimClient({ origin: "https://zcode.z.ai", jwt: "j", appVersion: "3.10.0", platform: "p", fetchImpl: fetch401 }); |
| 191 | + const out2 = await c2.claim("p1", { verifyParam: "t" }); |
| 192 | + expect(out2.ok).toBe(false); |
| 193 | + if (!out2.ok) expect(out2.failureKind).toBe("login_required"); |
| 194 | + }); |
| 195 | + |
| 196 | + it("returns login_required without HTTP call when jwt missing", async () => { |
| 197 | + const fetchImpl = makeMockFetch(() => { |
| 198 | + throw new Error("should not be called"); |
| 199 | + }); |
| 200 | + const client = createClaimClient({ origin: "https://zcode.z.ai", appVersion: "3.10.0", platform: "p", fetchImpl }); |
| 201 | + const out = await client.claim("p1", { verifyParam: "t" }); |
| 202 | + expect(out.ok).toBe(false); |
| 203 | + if (!out.ok) expect(out.failureKind).toBe("login_required"); |
| 204 | + }); |
| 205 | +}); |
0 commit comments