Files
plugins/packages/qoder/checkin-window.test.mjs
T
yetone fd40b20c3b qoder 0.2.43: the check-in says when to ask again, as the 10:00 (UTC+8) campaign opens
sdytman on Discord: Qoder's daily credits start at 10:00, can the plugin's
check-in follow it? Qoder's /me/campaigns lists each day's CLAIM_BENEFIT
campaign with startAt 10:00 and endAt 09:59 the next day, both UTC+8
("Daily reset: 10:00 (UTC+8)"), read with a real account at 09:27 CST on
10-28. Qoder.app 0.4.3 polls that list every 5 minutes and treats
startAt <= now < endAt as active; there is no next-time field. A check-in
before 10:00 met yesterday's campaign, and magpie settled for the Beijing
day, so today's campaign waited until tomorrow, or was lost.

checkin now returns until: the next campaign's opening (the later of
endAt and startAt + 1 day, or the next startAt in the gap) plus a minute,
on done, claimed and "not offered yet". All in epoch time, so the
machine's zone doesn't matter; qoder and qoder-cn share the code. magpie
asks again at until once it reads it (yetone/magpie, next release).

Run (mote): checkin-window, native-checkin and checkin tests under
TZ=UTC, Etc/GMT+8 and Asia/Shanghai, 66 pass each; HOME=$(mktemp -d)
bun test, 2938 pass 0 fail 8 skip; bun scripts/check.mjs. A real qoder
account at 09:35 CST: claimed 100, until 2026-10-28T02:01:00Z.
Broken by hand: nextOpens returning endAt only, 11 of 13 checkin-window
tests red; askAgain returning {}, 12 of 13 red.
Not tried: a real Qoder CN account (none here); the server's list in the
09:59-10:00 gap.
2026-10-28 09:37:56 +08:00

129 lines
6.5 KiB
JavaScript

// The daily credits' window (sdytman on Discord: qoder每天10点开始签到).
// Qoder lists each day's 100 credits from 10:00 (UTC+8) to 09:59 the next
// day; testdata/campaigns-1028.json is a live list read at 09:27 (UTC+8)
// on 2026-10-28, the day before's campaign still open. A check-in says
// when the next one opens (until), so magpie asks again then instead of at
// the next calendar day, whatever zone the machine is in: run this file
// with TZ=UTC and TZ=Etc/GMT+8 (UTC-8) too.
import { afterEach, beforeEach, expect, setSystemTime, spyOn, test } from "bun:test"
import * as childProcess from "node:child_process"
import { mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs"
import { tmpdir } from "node:os"
import { join, sep } from "node:path"
import { QoderAuthPlugin, QoderCNAuthPlugin } from "./index.mjs"
const real = globalThis.fetch
const originalRuntime = process.env.QODER_RUNTIME_INFO
const originalCNRuntime = process.env.QODERCN_RUNTIME_INFO
let sandbox, native
beforeEach(() => {
sandbox = mkdtempSync(join(tmpdir(), "qoder-checkin-window-"))
process.env.QODER_RUNTIME_INFO = join(sandbox, process.platform === "win32" ? "runtime-info.exe" : "runtime-info")
writeFileSync(process.env.QODER_RUNTIME_INFO, "fixture")
process.env.QODERCN_RUNTIME_INFO = process.env.QODER_RUNTIME_INFO
native = spyOn(childProcess, "execFile").mockImplementation((file, args, options, done) => ({
stdin: { on() {}, end() { queueMicrotask(() => done(null, JSON.stringify({ machineToken: "mt", machineType: "cfe1" }))) } },
}))
})
afterEach(() => {
globalThis.fetch = real
native.mockRestore()
setSystemTime()
if (originalRuntime === undefined) delete process.env.QODER_RUNTIME_INFO
else process.env.QODER_RUNTIME_INFO = originalRuntime
if (originalCNRuntime === undefined) delete process.env.QODERCN_RUNTIME_INFO
else process.env.QODERCN_RUNTIME_INFO = originalCNRuntime
const target = realpathSync(sandbox)
if (!target.startsWith(realpathSync(tmpdir()) + sep)) throw new Error("test directory escaped temp root")
rmSync(target, { recursive: true, force: true })
})
// the live list: the campaign of 10-27 10:00 to 10-28 09:59 (UTC+8)
const LIVE = JSON.parse(readFileSync(join(import.meta.dir, "testdata", "campaigns-1028.json"), "utf8"))
const OCT27 = LIVE.campaigns[0]
// the next day's, as Qoder lists one: a day later, the same length
const OCT28 = { ...OCT27, campaignId: "oct28", campaignKey: "act-20261028-1", startAt: OCT27.startAt + 86400, endAt: OCT27.endAt + 86400 }
const listOf = (...cs) => ({ ...LIVE, campaigns: cs })
const account = () => ({
type: "oauth", access: "jt", refresh: "rt", expires: Date.now() + 3_600_000,
accountId: "one@x", uid: "u1", deviceToken: "dt", deviceRefresh: "drt",
})
const json = (v, status = 200) => new Response(JSON.stringify(v), { status })
async function checkin(make, list) {
let auth = account()
const claims = []
globalThis.fetch = async (url) => {
const u = new URL(String(url))
if (u.pathname.endsWith("/claim")) {
claims.push(u.pathname.split("/").at(-2))
return json({ status: "CLAIMED", benefit: { amount: 100 } })
}
return json(list)
}
const hooks = await make({ client: { auth: { set: async ({ body }) => (auth = body) } } })
return { out: await hooks.auth.checkin(async () => auth), claims }
}
// 10:01 (UTC+8) on the day: a minute after the campaign opens
const askAt = (day) => `2026-10-${day}T02:01:00.000Z`
test("the live list's fixture is the day before's campaign, 10:00 to 09:59 (UTC+8)", () => {
expect(new Date(OCT27.startAt * 1000).toISOString()).toBe("2026-10-27T02:00:00.000Z")
expect(new Date(OCT27.endAt * 1000).toISOString()).toBe("2026-10-28T01:59:00.000Z")
expect(OCT27.placements[0].content.en.description).toContain("Daily reset: 10:00 (UTC+8)")
})
for (const make of [QoderAuthPlugin, QoderCNAuthPlugin]) {
const site = make === QoderAuthPlugin ? "qoder" : "qoder-cn"
test(`${site}: before 10:00 the day before's credits are claimed, and the check-in asks again at 10:00`, async () => {
setSystemTime(new Date("2026-10-28T01:27:00Z")) // 09:27 (UTC+8), as the live list was read
const p = await checkin(make, LIVE)
expect(p.claims).toEqual([OCT27.campaignId])
expect(p.out).toEqual({ outcome: "claimed", credit: 100, until: askAt(28) })
})
test(`${site}: before 10:00 with the day before's already claimed is done until 10:00, not the day`, async () => {
setSystemTime(new Date("2026-10-28T01:27:00Z"))
const p = await checkin(make, listOf({ ...OCT27, claimStatus: "CLAIMED" }))
expect(p.claims).toEqual([])
expect(p.out).toEqual({ outcome: "done", credit: 100, until: askAt(28) })
})
test(`${site}: after 10:00 today's credits are claimed, and the next ask is tomorrow 10:00`, async () => {
setSystemTime(new Date("2026-10-28T02:01:30Z")) // 10:01 (UTC+8)
const p = await checkin(make, listOf({ ...OCT27, claimStatus: "CLAIMED", endAt: OCT27.endAt }, OCT28))
expect(p.claims).toEqual(["oct28"])
expect(p.out).toEqual({ outcome: "claimed", credit: 100, until: askAt(29) })
})
test(`${site}: across midnight (UTC+8) a claimed campaign holds until 10:00, not until the date changes`, async () => {
for (const t of ["2026-10-28T15:59:00Z", "2026-10-28T16:01:00Z"]) { // 23:59 and 00:01 (UTC+8)
setSystemTime(new Date(t))
const p = await checkin(make, listOf({ ...OCT28, claimStatus: "CLAIMED" }))
expect(p.out).toEqual({ outcome: "done", credit: 100, until: askAt(29) })
}
})
test(`${site}: between the old campaign's end and 10:00 the check-in fails and asks again at 10:00`, async () => {
setSystemTime(new Date("2026-10-28T01:59:30Z")) // 09:59:30 (UTC+8)
const p = await checkin(make, listOf({ ...OCT27, claimStatus: "CLAIMED" }))
expect(p.out).toEqual({ outcome: "failed", message: "campaigns: today's credits aren't offered yet", until: askAt(28) })
})
}
test("a campaign longer than a day is asked again at its end", async () => {
setSystemTime(new Date("2026-10-28T03:00:00Z"))
const long = { ...OCT28, claimStatus: "CLAIMED", endAt: OCT28.startAt + 3 * 86400 }
const p = await checkin(QoderAuthPlugin, listOf(long))
expect(p.out.until).toBe(new Date((long.endAt + 60) * 1000).toISOString())
})
test("times in milliseconds read the same as in seconds", async () => {
setSystemTime(new Date("2026-10-28T01:27:00Z"))
const p = await checkin(QoderAuthPlugin, listOf({ ...OCT27, claimStatus: "CLAIMED", startAt: OCT27.startAt * 1000, endAt: OCT27.endAt * 1000 }))
expect(p.out).toEqual({ outcome: "done", credit: 100, until: askAt(28) })
})