diff --git a/packages/zcode/checkin.test.mjs b/packages/zcode/checkin.test.mjs index 27c733d..5ef0198 100644 --- a/packages/zcode/checkin.test.mjs +++ b/packages/zcode/checkin.test.mjs @@ -5,7 +5,7 @@ // fixture carries only the code. import { test, expect, afterAll, beforeAll, beforeEach } from "bun:test" import { homedir, tmpdir } from "node:os" -import { realpathSync } from "node:fs" +import { readFileSync, realpathSync } from "node:fs" let ZCodeAuthPlugin, _internal beforeAll(async () => { @@ -272,3 +272,55 @@ test("a feed that doesn't read is failed (unknown), never nothing to claim", asy expect(r.message).toStartWith("claimed ZCode Trust Build; gift plans to claim: ") expect(claims(calls)).toHaveLength(1) }) + +// #503, 10-30 22:11 +08:00 (kongbai01203, comment 6526694290): the real +// marketing/touch reply for the account that claimed ZCODE WEEKEND BUILD, +// in testdata/touch-1030-wk.json. The bytes are the reporter's own, but +// their paste was cut short inside success_popup.description.content's +// HTML (2510 of 2628 characters); only the closing `"}}}}]}}` that string +// and the objects it was in need is added, and the reporter's "…" mark +// taken off. What the real reply has that touchOf above doesn't: a close +// button with "args": null, button text as {format, content}, a bundle +// background, resource_position and priority, and a success_popup whose +// own buttons (navigate, copy_text) and hero repeat the zcode_plan. +const TOUCH_REAL = readFileSync(new URL("./testdata/touch-1030-wk.json", import.meta.url), "utf8") + +test("the real banner feed: its one claim button is claimed, by the plan's name", async () => { + const calls = claimWith(PREVIEW_NO_DATA, (id) => (id === WEEKEND ? json(CLAIMED_WEEKEND) : json("", 404)), TOUCH_REAL) + expect(await checkin()).toEqual({ outcome: "claimed", message: "claimed ZCODE WEEKEND BUILD" }) + // one claim, of the claim button's plan_id, with the body the app sends; + // the close button and the success popup's buttons claim nothing + expect(claims(calls).map((c) => c.body)).toEqual(['{"plan_id":"zcode-v3-start-plan-1030-wk"}']) + const [c] = claims(calls) + expect(c.headers.Authorization).toBe("Bearer " + TOKEN) + expect(c.headers["X-Device-Mid"]).toBe(DEVICE) + // nothing is sent back to the feed (marketing/touch/action is the + // banner's confirm/close, not a claim) + expect(calls.filter((x) => x.method !== "GET").map((x) => x.url.pathname)).toEqual([CLAIM]) +}) + +test("the real delivery without its claim button is nothing to claim", async () => { + const feed = JSON.parse(TOUCH_REAL) + const banner = feed.data.deliveries[0].banner + banner.buttons = banner.buttons.filter((b) => b.action.type !== "claim_zcode_plan") + expect(banner.buttons.map((b) => b.action.type)).toEqual(["close"]) + // the zcode_plan it still describes (background, success popup) is no + // offer to claim + expect(JSON.stringify(feed)).toContain('"zcode_plan":{"plan_id":"zcode-v3-start-plan-1030-wk"') + const calls = claimWith(PREVIEW_NO_DATA, () => json(CLAIMED_WEEKEND), feed) + expect(await checkin()).toEqual({ outcome: "inactive", message: "no ZCode gift plan to claim" }) + expect(claims(calls)).toEqual([]) +}) + +test("after the claim the feed has no delivery: nothing to claim, not a failure", async () => { + // kongbai01203's second finding (10-30): once claimed, the campaign leaves + // the account's deliveries while its window is still open (to 11-02 + // 09:00). The count (0) is theirs; these bytes are the real reply's + // top level with its deliveries emptied. magpie keeps the day's claim + // (checkin.go, claimedOn) over this later inactive. + const after = JSON.parse(TOUCH_REAL) + after.data.deliveries = [] + const calls = claimWith(PREVIEW_NO_DATA, () => json(CLAIMED_WEEKEND), after) + expect(await checkin()).toEqual({ outcome: "inactive", message: "no ZCode gift plan to claim" }) + expect(claims(calls)).toEqual([]) +}) diff --git a/packages/zcode/testdata/touch-1030-wk.json b/packages/zcode/testdata/touch-1030-wk.json new file mode 100644 index 0000000..f2509e2 --- /dev/null +++ b/packages/zcode/testdata/touch-1030-wk.json @@ -0,0 +1 @@ +{"code":0,"msg":"","data":{"server_time":1793369478,"language":"zh-CN","deliveries":[{"campaign_id":"2101141259813175296","resource_position":"banner","priority":50,"banner":{"buttons":[{"text":{"format":"","content":""},"action":{"type":"close","args":null}},{"text":{"format":"","content":""},"action":{"type":"claim_zcode_plan","args":{"plan_id":"zcode-v3-start-plan-1030-wk"}}}],"background":{"type":"bundle","bundle":{"bundle":{"src":"https://cdn-zcode.z.ai/marketing/touch/assets/2104840819634155520/weekend-banner-3.1.2.zip","sha256":"570aac77453f535288183e70a9c0478ad0cafec8db1c75433b5e741271fb5f35"},"entry":"index.html"},"args":{"zcode_plan":{"plan_id":"zcode-v3-start-plan-1030-wk","name":"ZCODE WEEKEND BUILD","priority":101,"starts_at":1793358000,"ends_at":1793581200,"entitlements":[{"entitlement_id":"ent-zcode-v3-start-plan-1030-wk-1","show_name":"GLM-5.3-Flash","meter":"model_usage","unit_type":"token","capabilities":["model:glm-5.3-flash"],"grant_units":500000000,"period":"one_time","effective_at":1793372400,"priority":101}]}}},"success_popup":{"buttons":[{"text":{"format":"plaintext","content":"模型设置"},"action":{"type":"navigate","args":{"page":"settings","section":"models"}}},{"text":{"format":"plaintext","content":"复制分享"},"theme":{"variant":"secondary"},"action":{"type":"copy_text","args":{"text":"我在 ZCode 领取了 GLM-5.3-Flash 500000000 Tokens。邀请你也来下载安装即可领取。\nhttps://zcode.z.ai"}}}],"hero":{"type":"bundle","bundle":{"bundle":{"src":"https://cdn-zcode.z.ai/marketing/touch/assets/2101141874404540416/weekend-plan-hero-v5.zip","sha256":"d5f34e2cf6d7289294f1138bd0dfe6eb791518588885ecb6dfa0d34e9dd6c8e1"},"entry":"index.html"},"args":{"zcode_plan":{"plan_id":"zcode-v3-start-plan-1030-wk","name":"ZCODE WEEKEND BUILD","priority":101,"starts_at":1793358000,"ends_at":1793581200,"entitlements":[{"entitlement_id":"ent-zcode-v3-start-plan-1030-wk-1","show_name":"GLM-5.3-Flash","meter":"model_usage","unit_type":"token","capabilities":["model:glm-5.3-flash"],"grant_units":500000000,"period":"one_time","effective_at":1793372400,"priority":101}]}}},"title":{"format":"plaintext","content":"ZCODE WEEKEND BUILD 领取成功"},"description":{"format":"html","content":" \n \n \u003cp class=\"text-center text-ui-base/relaxed text-foreground-subtle\"\u003e\u003cb class=\"font-semibold text-foreground\"\u003eGLM-5.3-Flash\u003c/b\u003e 将于 \u003cspan class=\"whitespace-nowrap font-semibold text-foreground underline decoration-foreground-subtle decoration-dashed underline-offset-4\"\u003e"}}}}]}} \ No newline at end of file diff --git a/packages/zcode/usage.test.mjs b/packages/zcode/usage.test.mjs index ce4722e..045c9be 100644 --- a/packages/zcode/usage.test.mjs +++ b/packages/zcode/usage.test.mjs @@ -3,7 +3,7 @@ // zcode_start_test.go TestZCodeStartPlanOwnAccount, zcode_team_test.go). import { test, expect, afterAll, beforeAll, beforeEach } from "bun:test" import { homedir, tmpdir } from "node:os" -import { realpathSync } from "node:fs" +import { readFileSync, realpathSync } from "node:fs" let ZCodeAuthPlugin, _internal beforeAll(async () => { @@ -487,3 +487,19 @@ test("a coding account's line goes after its coding and gift windows", async () expect(u.windows.at(-1)).toEqual(TRUST_BUILD) expect(calls.filter((c) => c.url.pathname.endsWith("/preview"))).toHaveLength(1) }) + +// #503: the same card line from the real marketing/touch reply +// (testdata/touch-1030-wk.json; see checkin.test.mjs for how it was cut) +test("the real banner feed names its plan once on the card", async () => { + const now = Math.trunc(Date.now() / 1000) + const token = jwt(now + 86400) + const real = readFileSync(new URL("./testdata/touch-1030-wk.json", import.meta.url), "utf8") + const calls = serve(({ url }) => { + if (url.pathname === "/api/v1/zcode-plan/billing/balance") return ok(startBalance(now, "active")) + if (url.pathname === "/api/v1/zcode-plan/billing/preview") return ok(PREVIEW_CLAIMED) + if (url.pathname === "/api/v1/marketing/touch") return new Response(real) + }) + const u = await usage(oauth({ site: "zai", jwt: token, device: "11111111-2222-4333-8444-555555555555" })) + expect(u.windows.at(-1)).toEqual({ name: "Gift plans to claim in the ZCode app", used: 0, aside: true, display: "1 · ZCODE WEEKEND BUILD" }) + expect(calls.some((c) => c.url.pathname.endsWith("/claim"))).toBe(false) +})