diff --git a/packages/zcode/README.md b/packages/zcode/README.md index a4d21df..391760f 100644 --- a/packages/zcode/README.md +++ b/packages/zcode/README.md @@ -268,8 +268,19 @@ plans once a day (`auth.checkin`; `magpie accounts checkin` asks now). It reads both lists above (the banners' feed and the preview, each plan once) and posts each plan to `POST /api/v1/zcode-plan/billing/claim` (`{"plan_id": …}`) with the -account's ZCode session, as the app does. The answer is judged by the -body's code, not the HTTP status: +account's ZCode session, as the app does. An answer of nothing to claim +carries an `until` half an hour out, so magpie's loop asks again then, +and again, all day while neither list names a plan — two GETs, nothing +posted: ZCode drops plans into the feed on its own schedule (a reset +card, a weekend banner starting at 19:00 Beijing), and one that arrives +after the day's ask used to sit unclaimed past its short life. The +other answers stand for the day: a claimed plan leaves the feed, so +asking again half an hour later would find nothing and flip the card +from "Checked in today" to "No check-in event now"; a captcha plan +re-asked makes ZCode demand its captcha again; a done plan re-asked +posts a claim ZCode already answered. So a second plan the same day, +after one was claimed, is seen the next day. The answer is judged by +the body's code, not the HTTP status: - nothing listed: *inactive* (ZCode answers an account with nothing to claim `{"code":0,"msg":""}`, with no list at all). A list that doesn't diff --git a/packages/zcode/checkin.test.mjs b/packages/zcode/checkin.test.mjs index 5ef0198..c2ee866 100644 --- a/packages/zcode/checkin.test.mjs +++ b/packages/zcode/checkin.test.mjs @@ -86,10 +86,36 @@ function claimWith(preview, claim, touch = TOUCH_NONE) { } const claims = (calls) => calls.filter((c) => c.url.pathname === CLAIM) +// only the answer of nothing to claim is asked again: until wakes +// magpie's loop half an hour out, so a plan dropped into the feed later +// in the day is still claimed. claimed, done and captcha settle for the +// day (a claimed plan leaves the feed; a captcha plan re-asked makes +// ZCode demand its captcha again; a done plan re-asked posts a claim +// answered 1003), and a failed needs none — magpie asks a failed again +// of its own. +test("an inactive says until half an hour out; claimed, done, captcha and failed say none", async () => { + const soon = (r) => { + const t = Date.parse(r.until) + expect(t).toBeGreaterThan(Date.now() + 25 * 60_000) + expect(t).toBeLessThan(Date.now() + 35 * 60_000) + } + const none = (r) => expect(r.until).toBeUndefined() + claimWith(PREVIEW_NONE, () => json(CLAIMED_NOW)) + soon(await checkin()) + claimWith(PREVIEW_ONE, () => json(CLAIMED_NOW)) + none(await checkin()) + claimWith(PREVIEW_ONE, () => json(CLAIMED_BEFORE)) + none(await checkin()) + claimWith(PREVIEW_ONE, () => json(CAPTCHA, 400)) + none(await checkin()) + claimWith("", () => json(CLAIMED_NOW)) + none(await checkin()) +}) + test("nothing to claim: inactive, and no claim is asked", async () => { const calls = claimWith(PREVIEW_NONE, () => json(CLAIMED_NOW)) const r = await checkin() - expect(r).toEqual({ outcome: "inactive", message: "no ZCode gift plan to claim" }) + expect(r).toEqual({ outcome: "inactive", message: "no ZCode gift plan to claim", until: expect.any(String) }) expect(claims(calls)).toEqual([]) // the preview went out as the app's billing calls do const p = calls.find((c) => c.url.pathname === PREVIEW) @@ -154,7 +180,7 @@ 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(await checkin()).toEqual({ outcome: "inactive", message: "no ZCode gift plan to claim", until: expect.any(String) }) expect(claims(calls)).toEqual([]) } }) @@ -248,7 +274,7 @@ test("a feed with no claim button, or nothing in it, is nothing to claim", async const closeOnly = { code: 0, msg: "", data: { deliveries: [{ campaign_id: "1", banner: { buttons: [{ action: { type: "close", args: {} } }] } }] } } for (const feed of [TOUCH_NONE, '{"code":0,"msg":""}', closeOnly]) { const calls = claimWith(PREVIEW_NO_DATA, () => json(CLAIMED_NOW), feed) - expect(await checkin()).toEqual({ outcome: "inactive", message: "no ZCode gift plan to claim" }) + expect(await checkin()).toEqual({ outcome: "inactive", message: "no ZCode gift plan to claim", until: expect.any(String) }) expect(claims(calls)).toEqual([]) } }) @@ -308,7 +334,7 @@ test("the real delivery without its claim button is nothing to claim", async () // 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(await checkin()).toEqual({ outcome: "inactive", message: "no ZCode gift plan to claim", until: expect.any(String) }) expect(claims(calls)).toEqual([]) }) @@ -321,6 +347,6 @@ test("after the claim the feed has no delivery: nothing to claim, not a failure" 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(await checkin()).toEqual({ outcome: "inactive", message: "no ZCode gift plan to claim", until: expect.any(String) }) expect(claims(calls)).toEqual([]) }) diff --git a/packages/zcode/index.mjs b/packages/zcode/index.mjs index 79b54b0..e339681 100644 --- a/packages/zcode/index.mjs +++ b/packages/zcode/index.mjs @@ -802,20 +802,42 @@ async function toClaim(s) { return { plans, unread } } +// RECHECK is how long an "nothing to claim" stands before magpie's loop +// asks again: ZCode drops gift plans into the feed during the day (a +// reset card, a weekend banner) on its own schedule, and the day's ask +// may have run hours before, so an inactive settled for the day lets a +// plan sit unclaimed past its short life (the weekend plan of +// testdata/touch-1030-wk.json starts 19:00 Beijing). `until` wakes the +// loop again in half an hour, and it asks again all day while neither +// list names a plan — two GETs and no claim posted. The other settled +// answers stand for the day: a claimed plan leaves the feed, so asking +// a claimed account again half an hour later finds nothing and flips +// its card from "Checked in today" to "No check-in event now"; a +// captcha plan re-asked makes ZCode demand its captcha again each time, +// and a done plan re-asked posts a claim it already answered 1003. The +// same shape as Qoder's campaign that opens mid-day, asked again on +// magpie's side there (yetone/magpie#2696). A failed needs no until: +// magpie asks a failed again after 30 minutes of its own. +const RECHECK_MS = 30 * 60_000 +const recheck = () => new Date(Date.now() + RECHECK_MS).toISOString() + // claimDaily claims every gift plan ZCode offers the account (#503: // toClaim, the app's banners and its preview), as auth.checkin's answer: // inactive when neither lists one, claimed when one was claimed now, // done when each was claimed before, captcha when ZCode wants the app's // captcha for one (it is claimed in the ZCode app), failed when a list // or a claim can't be read. A failed one is asked again later in the -// day, when a plan claimed now answers 1003. +// day, when a plan claimed now answers 1003. An inactive stands only +// until RECHECK_MS from now, so a plan dropped into the feed later in +// the day is still claimed; the other settled answers stand for the day +// (see RECHECK). // Nothing here marks the account: a failed claim keeps its sign-in. async function claimDaily(s) { if (!s?.jwt) return { outcome: "ineligible", message: "ZCode's gift plans are claimed with a ZCode sign-in; this account has none" } if (jwtExpired(s.jwt)) return { outcome: "failed", message: EXPIRED } const { plans, unread } = await toClaim(s) if (!plans.length && unread.length) return { outcome: "failed", message: unread.join("; ") } - if (!plans.length) return { outcome: "inactive", message: "no ZCode gift plan to claim" } + if (!plans.length) return { outcome: "inactive", message: "no ZCode gift plan to claim", until: recheck() } const got = [] for (const p of plans) got.push({ name: p.name, ...(await claimOne(s, p.plan_id)) }) const named = (o) => got.filter((g) => g.outcome === o).map((g) => g.name) diff --git a/packages/zcode/package.json b/packages/zcode/package.json index 0d19037..7652248 100644 --- a/packages/zcode/package.json +++ b/packages/zcode/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-zcode-auth", - "version": "0.1.40", + "version": "0.1.41", "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",