diff --git a/packages/zcode/README.md b/packages/zcode/README.md index 0f2b9d5..511aa8b 100644 --- a/packages/zcode/README.md +++ b/packages/zcode/README.md @@ -216,7 +216,8 @@ reads the app's preview list and posts each plan it lists to account's ZCode session, as the app does. The answer is judged by the body's code, not the HTTP status: -- nothing listed: *inactive*; +- nothing listed: *inactive* (ZCode answers an account with nothing to + claim `{"code":0,"msg":""}`, with no list at all); - code 0: *claimed* (the next request uses the plan); - code 1003 "target already exists" (HTTP 200): *done*, claimed before; - code 3007 "captcha verify failed": *captcha*, the plan is claimed in diff --git a/packages/zcode/checkin.test.mjs b/packages/zcode/checkin.test.mjs index 591dc0c..db1100d 100644 --- a/packages/zcode/checkin.test.mjs +++ b/packages/zcode/checkin.test.mjs @@ -46,6 +46,9 @@ async function checkin(auth = SIGNED_IN) { // #503: the preview of an account with nothing to claim const PREVIEW_NONE = '{"code":0,"msg":"","data":{"plans":[],"server_time":1793318263}}' +// 10-30 (the owner's account, read for hykql's report on Discord): the +// preview of an account with nothing to claim carries no data at all +const PREVIEW_NO_DATA = '{"code":0,"msg":"","logid":"202610300535585c4e3de75769b684da87"}' // usage.test.mjs's real preview, the day's ZCode Trust Build unclaimed const PREVIEW_ONE = { code: 0, msg: "", @@ -143,11 +146,20 @@ test("a claim that doesn't read is failed (unknown), never claimed", async () => } }) +test("a preview with nothing in it is nothing to claim, not a failed check-in", async () => { + for (const body of [PREVIEW_NO_DATA, '{"code":0,"msg":"","data":null}', '{"code":0,"msg":"","data":{"server_time":1793318263}}', '{"code":0,"msg":"","data":{"plans":null}}']) { + const calls = serve(({ url }) => (url.pathname === PREVIEW ? json(body) : json(CLAIMED_NOW))) + expect(await checkin()).toEqual({ outcome: "inactive", message: "no ZCode gift plan to claim" }) + expect(claims(calls)).toEqual([]) + } +}) + test("a preview that doesn't read is failed (unknown), and nothing is claimed", async () => { for (const answer of [ () => json("", 500), () => json("garbled"), - () => json('{"code":0,"data":{}}'), // no plan list + () => json('{"code":0,"data":{"plans":"zcode-v3-start-plan-1029"}}'), // a plan list that isn't a list + () => json('{"code":0,"data":"plans"}'), // data that isn't an object () => json('{"code":401,"msg":"token invalid"}', 401), () => { throw new Error("no network in tests") }, ]) { diff --git a/packages/zcode/index.mjs b/packages/zcode/index.mjs index f51faf5..2c50f3f 100644 --- a/packages/zcode/index.mjs +++ b/packages/zcode/index.mjs @@ -663,7 +663,7 @@ async function claimHint(s) { } catch { return null } - const plans = (Array.isArray(d?.plans) ? d.plans : []).filter((p) => typeof p?.plan_id === "string" && p.plan_id.trim()) + const plans = (previewPlans(d) ?? []).filter((p) => typeof p?.plan_id === "string" && p.plan_id.trim()) if (!plans.length) return null const n = plans.length const plan = first(...plans.map((p) => (typeof p?.name === "string" ? p.name : "")), "ZCode gift plan") @@ -718,6 +718,20 @@ async function claimOne(s, planID) { return { outcome: "failed", why: `${msg || "error"} (HTTP ${res.status}, code ${code})` } } +// previewPlans is /billing/preview's plan list. An account with nothing to +// claim is answered {"code":0,"msg":"","logid":…}: no data at all, not +// "plans": [] (the owner's account read 10-30 for hykql's report on +// Discord; the list is left out when it is empty). call has already judged the code, so a reply +// without data, or data without plans, is a list of none. Only data that +// isn't an object, or plans that are there and aren't a list, don't read +// (null, unknown). +function previewPlans(d) { + if (d == null) return [] + if (typeof d !== "object" || Array.isArray(d)) return null + if (d.plans == null) return [] + return Array.isArray(d.plans) ? d.plans : null +} + // claimDaily claims every gift plan ZCode's preview lists for the account // (#503), as auth.checkin's answer: inactive when it lists none, claimed // when one was claimed now, done when each was claimed before, captcha @@ -734,8 +748,9 @@ async function claimDaily(s) { } catch (e) { return { outcome: "failed", message: `gift plans to claim: ${e?.message ?? String(e)}` } } - if (!Array.isArray(d?.plans)) return { outcome: "failed", message: "gift plans to claim: no plan list in the reply" } - const plans = d.plans.filter((p) => typeof p?.plan_id === "string" && p.plan_id.trim()) + const listed = previewPlans(d) + if (!listed) return { outcome: "failed", message: "gift plans to claim: the plan list doesn't read" } + const plans = listed.filter((p) => typeof p?.plan_id === "string" && p.plan_id.trim()) if (!plans.length) return { outcome: "inactive", message: "no ZCode gift plan to claim" } const got = [] for (const p of plans) { diff --git a/packages/zcode/package.json b/packages/zcode/package.json index 6d18b68..a29245c 100644 --- a/packages/zcode/package.json +++ b/packages/zcode/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-zcode-auth", - "version": "0.1.36", + "version": "0.1.37", "description": "OpenCode provider plugin: signs in to Z.ai's or BigModel's (智谱) GLM Coding Plan as ZCode does, including team seats and ZCode's Start Plan, and claims its gift plans daily", "type": "module", "main": "./index.mjs",