Files
plugins/packages/window-warmup/tests/scheduler.test.ts
T
zhou.han b4916d4143 window-warmup tests: put XDG_CONFIG_HOME and XDG_CACHE_HOME back after each file (#996, #1001)
window-warmup's tests no longer leave XDG_CONFIG_HOME and XDG_CACHE_HOME set for every later file of the root `bun test` (#996).

What changed (test files only; no runtime code, no version bump, no dist rebuild):
- tests/fake-gateway.ts no longer sets the two XDG variables on import. Its `--serve` demo calls the new pinMagpieDirs() first, so the demo still runs with the temporary dirs.
- tests/test-env.ts: pinMagpieDirsForTest() records the inherited values, pins both to $TMPDIR/window-warmup-test-magpie-{config,cache}, and returns a restore that puts back each inherited value, or deletes one that wasn't set.
- browser, diagnostics, gateway, scheduler and server tests pin at load and restore in afterAll (in a finally after their own cleanup). diagnostics' own override is put back before the file's pin is.
- Every test file that imports fake-gateway.ts, or reaches magpieDirs()' defaults through startService/Scheduler, pins its own; magpie-files and store tests pass env explicitly.

Run at the reviewed head c13d2844 (its parent is origin/main 740e1af2), on magpie-desktop (WSL Ubuntu 24.04, Bun 1.4.2), HOME=$(mktemp -d):
- bun test packages/window-warmup plus a trailing probe file asserting both variables are back: 280 pass, 0 fail with both unset (probe sees null/null), and with both inherited as /tmp/inh-cfg and /tmp/inh-cache (probe sees them exactly).
- bun scripts/check.mjs window-warmup: ok.
- Root bun test (both XDG unset): 8298 pass, 2 skip, 0 fail, 8317 tests in 584 files.

Broken by hand, same probe:
- test-env.ts restore made a no-op: probe red (sees the window-warmup temp dirs).
- only server.test.ts skipping its restore: probe red.
- origin/main's window-warmup tests (740e1af2) with the probe: red, as #996 describes.

Not tried: `bun run typecheck` (CI doesn't run it either), and Windows.
2026-11-11 20:39:49 +08:00

1545 lines
64 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 调度器的失败测试:注入假时钟、假定时器与假网关函数,不发任何真实请求。
// 单个定时器有 10 分钟封顶,跨长时段的断言一律看 snapshot().next(语义到期点)而非定时器 at。
import { afterAll, afterEach, expect, test } from "bun:test";
import { createHash } from "node:crypto";
import { mkdtemp, readFile, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import type { CachedModel, KeyDirectoryEntry } from "../src/magpie-files";
import { openStore, type Store, type TaskMarks } from "../src/store";
import { Scheduler, type TaskInput } from "../src/scheduler";
import { pinMagpieDirsForTest } from "./test-env";
import type { QuotaEntry, RawModel, Run, SendResult, Task } from "../src/types";
// 测试进程绝不读真实用户的 Magpie 配置:把默认目录推导用的 XDG 钉到临时位置
const restoreMagpieDirs = pinMagpieDirsForTest();
const HOUR = 3_600_000;
const T0 = "2026-10-31T05:00:00+08:00"; // 周六 05:00,上海
const ms = (iso: string) => new Date(iso).getTime();
afterAll(restoreMagpieDirs);
/** 不思考的模型:effort 应为 null,请求不带 reasoning_effort */
const M1: RawModel = { id: "m1", owned_by: "p1", reasoning: false };
function dailyTask(over: Partial<TaskInput> = {}): TaskInput {
return {
name: "早报", enabled: true, provider: "openai", account: "",
model: "m1", message: "hi",
trigger: { kind: "daily", time: "06:00", timeZone: "Asia/Shanghai" },
...over,
};
}
interface SendCall {
task: Pick<Task, "id" | "model">;
/** 调度器按统一账号规则算好的账号头值;null = 不带头 */
pin: string | null;
effort: string | null;
signal: AbortSignal;
}
interface TimerRec { fn: () => void; at: number; cancelled: boolean }
interface HarnessOpts {
models?: RawModel[];
quotas?: () => { entries: QuotaEntry[] };
/** 单卡刷新的替身(改写 quotas 的数据源模拟 Magpie 重读了卡);不传 = 空操作 */
refresh?: (provider: string, user: string) => void | Promise<void>;
sendImpl?: (call: SendCall) => Promise<SendResult>;
/** 复用已有数据目录(模拟重启续跑) */
dir?: string;
/** 假时钟起点(默认 T0;续跑时传上个 harness 的当前时刻) */
startISO?: string;
/** 包装 store,模拟落盘失败 */
storeWrap?: (s: Store) => Store;
/** providers.json 目录的替身(指纹与 accountModels);不传 = 读不出 */
keyDir?: Map<string, KeyDirectoryEntry>;
/** 直接注入 keyDirFn(延迟 resolve 等竞态测试用);优先于 keyDir */
keyDirFn?: () => Promise<Map<string, KeyDirectoryEntry> | null>;
/** 供应商 → 模型缓存替身;不传 = 没有缓存 */
modelCache?: (providerId: string) => CachedModel[] | null;
}
interface Harness {
scheduler: Scheduler;
store: Store;
sendCalls: SendCall[];
models: RawModel[];
quotasCalls: number;
/** 单卡刷新调用:[供应商, 账号] */
refreshCalls: [string, string][];
advance: (delta: number) => void;
now: () => Date;
fireDue: () => void;
pendingTimers: () => TimerRec[];
flush: () => Promise<void>;
dir: string;
}
const cleanupDirs: string[] = [];
afterEach(async () => {
while (cleanupDirs.length) await rm(cleanupDirs.pop()!, { recursive: true, force: true });
});
async function harness(opts: HarnessOpts = {}): Promise<Harness> {
const dir = opts.dir ?? (await mkdtemp(join(tmpdir(), "sched-test-")));
cleanupDirs.push(dir);
let nowMs = ms(opts.startISO ?? T0);
const advance = (delta: number) => { nowMs += delta; };
const now = () => new Date(nowMs);
const timers: TimerRec[] = [];
const setTimer = (fn: () => void, delayMs: number) => {
const rec: TimerRec = { fn, at: nowMs + Math.max(0, delayMs), cancelled: false };
timers.push(rec);
return () => { rec.cancelled = true; };
};
const fireDue = () => {
for (let round = 0; round < 3; round++) {
const due = timers.filter((t) => !t.cancelled && t.at <= nowMs);
if (due.length === 0) break;
for (const t of due) { t.cancelled = true; t.fn(); }
}
};
const pendingTimers = () => timers.filter((t) => !t.cancelled);
const sendCalls: SendCall[] = [];
const sendFn = async (
_base: string,
task: Pick<Task, "id" | "model">,
pin: string | null,
effort: string | null,
signal: AbortSignal,
): Promise<SendResult> => {
const call: SendCall = { task, pin, effort, signal };
sendCalls.push(call);
return opts.sendImpl ? opts.sendImpl(call) : { outcome: "ok", reason: "", ms: 2 };
};
const models = opts.models ?? [M1];
let quotasCalls = 0;
const quotasFn = opts.quotas
? async () => {
quotasCalls += 1;
return { at: now(), entries: opts.quotas!().entries };
}
: undefined;
const refreshCalls: [string, string][] = [];
const refreshCardFn = async (_base: string, provider: string, user: string) => {
refreshCalls.push([provider, user]);
await opts.refresh?.(provider, user);
};
const rawStore = await openStore(dir);
const store = opts.storeWrap ? opts.storeWrap(rawStore) : rawStore;
const scheduler = new Scheduler({
store,
base: "http://gateway.test",
now,
setTimer,
sendFn,
modelsFn: async () => models,
...(quotasFn ? { quotasFn } : {}),
refreshCardFn,
// key 目录与模型缓存一律注入替身,测试进程不碰真实文件
keyDirFn: opts.keyDirFn ?? (async () => opts.keyDir ?? null),
modelCacheFn: async (pid) => opts.modelCache?.(pid) ?? null,
});
return {
scheduler, store, sendCalls, models,
get quotasCalls() { return quotasCalls; },
refreshCalls,
advance, now, fireDue, pendingTimers, flush: () => scheduler.flush(), dir,
};
}
/** 启动并把假时钟推到每天 06:00 的到期点触发一轮 */
async function firedDaily(h: Harness): Promise<void> {
h.scheduler.start();
h.advance(HOUR); // 05:00 → 06:00
h.fireDue();
await h.flush();
}
test("daily 到点发送一次并记录 ok,下次到期点设在次日", async () => {
const h = await harness();
const id = (await h.scheduler.upsert(dailyTask())).id;
h.scheduler.start();
expect(h.sendCalls).toHaveLength(0);
expect(h.pendingTimers()).toHaveLength(1);
expect(h.scheduler.snapshot().next[id]).toBe(new Date(ms("2026-10-31T06:00:00+08:00")).toISOString());
await firedDaily(h);
expect(h.sendCalls).toHaveLength(1);
expect(h.sendCalls[0].task.id).toBe(id);
expect(h.sendCalls[0].effort).toBeNull(); // 模型不思考
const run = h.store.data.runs.find((r) => r.taskId === id)!;
expect(run.outcome).toBe("ok");
expect(run.source).toBe("schedule");
expect(run.due).toBe(new Date(ms("2026-10-31T06:00:00+08:00")).toISOString());
expect(h.store.data.marks[id].lastDue).toBe(run.due);
expect(h.scheduler.snapshot().next[id]).toBe(new Date(ms("2026-11-01T06:00:00+08:00")).toISOString());
// 落盘内容一致
const onDisk = JSON.parse(await readFile(join(h.dir, "state.json"), "utf8")) as { runs: Run[] };
expect(onDisk.runs[0].outcome).toBe("ok");
});
test("调用 send 前已落盘 running 记录与 lastDue", async () => {
let diskDuringSend = "";
let dirRef = "";
const h = await harness({
sendImpl: async () => {
diskDuringSend = await readFile(join(dirRef, "state.json"), "utf8");
return { outcome: "ok", reason: "", ms: 1 };
},
});
dirRef = h.dir;
await h.scheduler.upsert(dailyTask());
await firedDaily(h);
const disk = JSON.parse(diskDuringSend) as { runs: Run[]; marks: Record<string, TaskMarks> };
expect(disk.runs[0].outcome).toBe("running");
expect(disk.runs[0].endedAt).toBeNull();
expect(disk.marks[h.store.data.tasks[0].id].lastDue).toBe(disk.runs[0].due);
expect(h.store.data.runs[0].outcome).toBe("ok");
});
test("启动时把遗留的 running 记录改判 unknown(Magpie 中途退出)", async () => {
const h = await harness();
const task = await h.scheduler.upsert(dailyTask());
h.store.data.runs.push({
id: "stale", taskId: task.id, source: "schedule", due: "2026-10-31T06:00:00.000Z",
startedAt: "2026-10-31T06:00:00.000Z", endedAt: null, outcome: "running",
reason: "", effort: null, model: null, ms: null, resetKey: null,
});
await h.store.save();
h.scheduler.start();
await h.flush();
expect(h.store.data.runs[0].outcome).toBe("unknown");
expect(h.store.data.runs[0].reason).toBe("Magpie 在发送中途退出,结果未知");
const reopened = await openStore(h.dir);
expect(reopened.data.runs[0].outcome).toBe("unknown");
});
test("发送失败不改 enabled、原因可读,下一时刻照常再发", async () => {
const h = await harness({
sendImpl: async () => ({ outcome: "failed", reason: "HTTP 403:账号不对", ms: 3 }),
});
const id = (await h.scheduler.upsert(dailyTask())).id;
await firedDaily(h);
expect(h.sendCalls).toHaveLength(1);
const run = h.store.data.runs.find((r) => r.taskId === id)!;
expect(run.outcome).toBe("failed");
expect(run.reason).toBe("HTTP 403:账号不对");
expect(h.store.data.tasks[0].enabled).toBe(true);
h.advance(HOUR * 24);
h.fireDue();
await h.flush();
expect(h.sendCalls).toHaveLength(2);
});
test("睡眠跨 3 个 every 点只补发一次(source=catchup)", async () => {
const h = await harness();
const id = (await h.scheduler.upsert(dailyTask({
name: "循环", trigger: { kind: "every", hours: 1, anchor: T0 },
}))).id;
h.scheduler.start(); // 定时点 T0+1h
h.advance(3 * HOUR + 60_000); // 睡到 T0+3h+1m
h.fireDue();
await h.flush();
expect(h.sendCalls).toHaveLength(1);
const run = h.store.data.runs[0];
expect(run.source).toBe("catchup");
expect(run.due).toBe(new Date(ms(T0) + 3 * HOUR).toISOString());
// 补发后下一个点顺延
expect(h.scheduler.snapshot().next[id]).toBe(new Date(ms(T0) + 4 * HOUR).toISOString());
});
test("编辑任务后不补发编辑前错过的点", async () => {
const h = await harness();
const task = await h.scheduler.upsert(dailyTask({
name: "循环", trigger: { kind: "every", hours: 1, anchor: T0 },
}));
h.scheduler.start();
h.advance(2.5 * HOUR); // T0+1h、T0+2h 已错过但尚未唤醒
await h.scheduler.upsert({ ...task, name: "改名" }); // 编辑 → updatedAt=现在,旧到期点作废
h.advance(0.5 * HOUR); // 到 T0+3h:编辑后的第一个常规点
h.fireDue();
await h.flush();
expect(h.sendCalls).toHaveLength(1);
const run = h.store.data.runs[0];
expect(run.source).toBe("schedule");
expect(run.due).toBe(new Date(ms(T0) + 3 * HOUR).toISOString());
});
test("window:同一 resetKey 只发一次;空闲窗口同一小时不重试,下个小时可再试", async () => {
const h = await harness({
quotas: () => ({
entries: [{
provider: "openai", account: "acct-yang", label: "Y", viaKey: false,
windows: [{ name: "5h", used: 0, resetsAt: null }], // 空闲窗口:立即到期
}],
}),
});
await h.scheduler.upsert(dailyTask({
name: "开窗", trigger: { kind: "window", window: "5h" }, account: "acct-yang",
}));
h.scheduler.start();
h.fireDue(); // 启动即取快照:空闲 → 立即发送
await h.flush();
expect(h.sendCalls).toHaveLength(1);
expect(h.store.data.runs[0].resetKey).toMatch(/^idle:/);
h.advance(11 * 60_000);
h.fireDue(); // 同一小时刷新 → 同一 resetKey → 不再发
await h.flush();
expect(h.sendCalls).toHaveLength(1);
h.advance(HOUR); // 跨入下一个小时:评估前先刷新 → 新小时的 idle key → 空闲窗口按小时重试
h.fireDue();
await h.flush();
expect(h.sendCalls).toHaveLength(2);
expect(h.store.data.runs[1].resetKey).toBe("idle:2026-10-30T22:00:00.000Z");
h.advance(11 * 60_000); // 同一小时已试过 → 不再发
h.fireDue();
await h.flush();
expect(h.sendCalls).toHaveLength(2);
});
test("window:账号在但窗口暂时不在 → 瞬态,不记失败,下一轮刷新继续等", async () => {
// 插件供应商的用量读取会瞬断(卡时有时无),按失败上报只会刷一屏红字
let windows = [{ name: "week", used: 1, resetsAt: null }]; // 没有 5h 窗口
const h = await harness({
quotas: () => ({
entries: [{
provider: "openai", account: "acct-yang", label: "Y", viaKey: false, windows,
}],
}),
});
await h.scheduler.upsert(dailyTask({
name: "开窗", trigger: { kind: "window", window: "5h" }, account: "acct-yang",
}));
h.scheduler.start();
h.fireDue();
await h.flush();
expect(h.sendCalls).toHaveLength(0);
expect(h.store.data.runs).toHaveLength(0); // 不记失败
h.advance(10 * 60_000);
h.fireDue(); // 依旧没有 5h 窗口 → 仍不记失败、不发送
await h.flush();
expect(h.sendCalls).toHaveLength(0);
expect(h.store.data.runs).toHaveLength(0);
// 额度里出现 5h 空闲窗口 → 正常开窗发送(快照新鲜 + 空闲 + 未发过 → 立即)
windows = [{ name: "5h", used: 0, resetsAt: null }];
h.advance(10 * 60_000);
h.fireDue();
await h.flush();
expect(h.sendCalls).toHaveLength(1);
});
test("window:resetsAt 在未来 → 到期点为 resetsAt+30s,按 resetsAt 去重", async () => {
const resetAt = new Date(ms(T0) + 2 * HOUR);
const h = await harness({
quotas: () => ({
entries: [{
provider: "openai", account: "acct-yang", label: "Y", viaKey: false,
windows: [{ name: "5h", used: 3, resetsAt: resetAt.toISOString() }],
}],
}),
});
const id = (await h.scheduler.upsert(dailyTask({
name: "开窗", trigger: { kind: "window", window: "5h" }, account: "acct-yang",
}))).id;
h.scheduler.start();
h.fireDue(); // 启动即取快照并排程
await h.flush();
expect(h.sendCalls).toHaveLength(0);
expect(h.scheduler.snapshot().next[id]).toBe(new Date(resetAt.getTime() + 30_000).toISOString());
h.advance(2 * HOUR + 30_000);
h.fireDue();
await h.flush();
expect(h.sendCalls).toHaveLength(1);
expect(h.store.data.runs[0].resetKey).toBe(resetAt.toISOString());
expect(h.store.data.runs[0].due).toBe(new Date(resetAt.getTime() + 30_000).toISOString());
expect(h.sendCalls[0].pin).toBe("acct-yang"); // 订阅账号名原样作头值
});
test("发送进行中再次到期 → 记一次「上一次发送还没结束,本次跳过」;runNow 冲突", async () => {
const h = await harness({ sendImpl: () => new Promise(() => {}) }); // 永不返回
await h.scheduler.upsert(dailyTask());
h.scheduler.start();
h.advance(HOUR);
h.fireDue();
await h.flush();
expect(h.sendCalls).toHaveLength(1);
expect(h.store.data.runs[0].outcome).toBe("running");
h.advance(HOUR * 24);
h.fireDue(); // 上一次还没结束 → 跳过
await h.flush();
expect(h.sendCalls).toHaveLength(1);
const skip = h.store.data.runs[1];
expect(skip.outcome).toBe("failed");
expect(skip.reason).toBe("上一次发送还没结束,本次跳过");
expect(skip.source).toBe("schedule");
await expect(h.scheduler.runNow(h.store.data.tasks[0].id)).rejects.toThrow(/还没结束/);
});
test("runNow 立即发送:source=manual、due 为空、不影响计划", async () => {
const h = await harness();
const id = (await h.scheduler.upsert(dailyTask())).id;
h.scheduler.start();
const run = await h.scheduler.runNow(id);
await h.flush();
expect(run.source).toBe("manual");
expect(run.due).toBeNull();
expect(run.outcome).toBe("ok");
expect(h.sendCalls).toHaveLength(1);
expect(h.store.data.marks[id].lastDue).toBeNull(); // 不影响计划
expect(h.scheduler.snapshot().next[id]).toBe(new Date(ms("2026-10-31T06:00:00+08:00")).toISOString());
});
test("进行中禁用 → 请求被 abort,记录 unknown「任务已停用,发送被取消」", async () => {
const h = await harness({
sendImpl: (call) =>
new Promise((resolve) => {
call.signal.addEventListener("abort", () =>
resolve({ outcome: "unknown", reason: "发送被取消", ms: 1 }),
);
}),
});
const task = await h.scheduler.upsert(dailyTask());
h.scheduler.start();
h.advance(HOUR);
h.fireDue();
await h.flush();
expect(h.sendCalls[0].signal.aborted).toBe(false);
await h.scheduler.upsert({ ...task, enabled: false });
expect(h.sendCalls[0].signal.aborted).toBe(true);
const run = h.store.data.runs[0];
expect(run.outcome).toBe("unknown");
expect(run.reason).toBe("任务已停用,发送被取消");
expect(h.store.data.tasks[0].enabled).toBe(false);
});
test("删除进行中的任务 → 任务、记录、marks 全清,异步完成不会加回来", async () => {
const h = await harness({
sendImpl: (call) =>
new Promise((resolve) => {
call.signal.addEventListener("abort", () =>
resolve({ outcome: "unknown", reason: "发送被取消", ms: 1 }),
);
}),
});
const task = await h.scheduler.upsert(dailyTask());
h.scheduler.start();
h.advance(HOUR);
h.fireDue();
await h.flush();
expect(h.store.data.runs[0].outcome).toBe("running");
await h.scheduler.remove(task.id);
expect(h.store.data.tasks).toHaveLength(0);
expect(h.store.data.runs).toHaveLength(0);
expect(h.store.data.marks[task.id]).toBeUndefined();
const reopened = await openStore(h.dir);
expect(reopened.data.tasks).toHaveLength(0);
expect(reopened.data.runs).toHaveLength(0);
h.advance(HOUR * 24);
h.fireDue(); // 迟到的发送收尾/补发评估都不应把记录加回来
await h.flush();
const again = await openStore(h.dir);
expect(again.data.runs).toHaveLength(0);
expect(again.data.tasks).toHaveLength(0);
});
test("思考档位动态:两次发送之间模型档位变化,分别记录当次档位,任务不存档位字段", async () => {
const h = await harness({
models: [{ id: "m1", owned_by: "p1", reasoning: true, reasoning_levels: ["none", "high"] }],
});
const id = (await h.scheduler.upsert(dailyTask())).id;
await firedDaily(h);
expect(h.sendCalls[0].effort).toBe("none");
expect(h.store.data.runs[0].effort).toBe("none");
h.models[0].reasoning_levels = ["low", "high"];
h.advance(HOUR * 24);
h.fireDue();
await h.flush();
expect(h.sendCalls[1].effort).toBe("low");
expect(h.store.data.runs[1].effort).toBe("low");
const onDisk = JSON.parse(await readFile(join(h.dir, "state.json"), "utf8")) as {
tasks: Array<Record<string, unknown>>;
};
expect("effort" in onDisk.tasks[0]).toBe(false);
expect("reasoning" in onDisk.tasks[0]).toBe(false);
});
test("模型从目录消失 → failed「模型 X 已不在 Magpie 的模型列表里」,不调用 send", async () => {
const h = await harness();
const id = (await h.scheduler.upsert(dailyTask())).id;
h.models.length = 0; // 目录里没有该模型了
await firedDaily(h);
expect(h.sendCalls).toHaveLength(0);
const run = h.store.data.runs.find((r) => r.taskId === id)!;
expect(run.outcome).toBe("failed");
expect(run.reason).toBe("模型 m1 已不在 Magpie 的模型列表里");
});
test("固定模型被路由组藏出 /v1/models 但仍在 chosen → 照发,档位按「不适用」", async () => {
const h = await harness({
keyDir: new Map([["openai", { keys: [], accountModels: {}, models: ["m1"] }]]),
});
h.models.length = 0; // /v1/models 里没有 m1(被 unlists 藏掉)
await h.scheduler.upsert(dailyTask()); // model: "m1",provider openai
await firedDaily(h);
expect(h.sendCalls).toHaveLength(1);
expect(h.sendCalls[0].task.model).toBe("m1");
expect(h.sendCalls[0].effort).toBeNull(); // 拿不到档位元数据 → 不带 reasoning_effort
const run = h.store.data.runs[0];
expect(run.outcome).toBe("ok");
expect(run.model).toBe("m1");
});
test("自动模型:/v1/models 全空但 chosen 有清单 → 从 chosen 现选照发", async () => {
const h = await harness({
keyDir: new Map([["openai", { keys: [], accountModels: {}, models: ["m1", "m1-flash"] }]]),
});
h.models.length = 0;
await h.scheduler.upsert(dailyTask({ model: "" }));
await firedDaily(h);
expect(h.sendCalls).toHaveLength(1);
expect(h.sendCalls[0].task.model).toBe("openai/m1-flash"); // flash 规格词优先
expect(h.sendCalls[0].effort).toBeNull();
expect(h.store.data.runs[0].model).toBe("openai/m1-flash");
});
test("自动模型:缓存只有分档变体、v1 是合并名 → 选 v1 的合并名照发,不报「没有可用模型」", async () => {
const h = await harness({
models: [{ id: "openai/m1-flash", owned_by: "openai", reasoning: false }],
keyDir: new Map([["openai", { keys: [], accountModels: {}, models: ["m1-flash"] }]]),
modelCache: () => [
{ id: "m1-flash-high", keys: [] },
{ id: "m1-flash-low", keys: [] },
],
});
await h.scheduler.upsert(dailyTask({ model: "" }));
await firedDaily(h);
expect(h.sendCalls).toHaveLength(1);
expect(h.sendCalls[0].task.model).toBe("openai/m1-flash");
expect(h.store.data.runs[0].outcome).toBe("ok");
});
test("模型真不在 chosen 里(重新获取后已剔除)→ 仍报「已不在模型列表」", async () => {
const h = await harness({
keyDir: new Map([["openai", { keys: [], accountModels: {}, models: ["other"] }]]),
});
h.models.length = 0;
const id = (await h.scheduler.upsert(dailyTask())).id;
await firedDaily(h);
expect(h.sendCalls).toHaveLength(0);
const run = h.store.data.runs.find((r) => r.taskId === id)!;
expect(run.outcome).toBe("failed");
expect(run.reason).toBe("模型 m1 已不在 Magpie 的模型列表里");
});
test("model 为空 → 每次发送前现选供应商模型,发送与 run.model 都用当次选中的", async () => {
const h = await harness({
models: [
{ id: "p1/gpt-5", owned_by: "p1", reasoning: false },
{ id: "p1/gpt-5-mini", owned_by: "p1", reasoning: false },
{ id: "p2/m", owned_by: "p2", reasoning: false }, // 别家供应商不参与
],
});
const id = (await h.scheduler.upsert(dailyTask({ provider: "p1", model: "" }))).id;
await firedDaily(h);
expect(h.sendCalls[0].task.model).toBe("p1/gpt-5-mini");
expect(h.sendCalls[0].effort).toBeNull();
const first = h.store.data.runs.find((r) => r.taskId === id)!;
expect(first.outcome).toBe("ok");
expect(first.model).toBe("p1/gpt-5-mini");
// 目录变了下一次发送跟着变(与思考档位一样现算,任务里只存 "")
h.models.length = 0;
h.models.push({ id: "p1/gpt-5-flash", owned_by: "p1", reasoning: false });
h.advance(HOUR * 24);
h.fireDue();
await h.flush();
expect(h.sendCalls[1].task.model).toBe("p1/gpt-5-flash");
expect(h.store.data.runs.filter((r) => r.taskId === id)[1].model).toBe("p1/gpt-5-flash");
// 落盘的任务保持 model: "",不写死选中值
const onDisk = JSON.parse(await readFile(join(h.dir, "state.json"), "utf8")) as {
tasks: Array<Record<string, unknown>>;
};
expect(onDisk.tasks[0].model).toBe("");
});
test("model 为空且既无缓存也无模型 → failed「供应商 X 还没有模型列表」,不调用 send", async () => {
const h = await harness({ models: [{ id: "p2/m", owned_by: "p2", reasoning: false }] });
const id = (await h.scheduler.upsert(dailyTask({ provider: "p1", model: "" }))).id;
await firedDaily(h);
expect(h.sendCalls).toHaveLength(0);
const run = h.store.data.runs.find((r) => r.taskId === id)!;
expect(run.outcome).toBe("failed");
expect(run.reason).toBe("供应商 p1 还没有模型列表,无法自动选模型");
expect(run.model).toBeNull();
});
test("固定 model 的任务照旧直发,run.model 记当次模型", async () => {
const h = await harness();
const id = (await h.scheduler.upsert(dailyTask())).id;
await firedDaily(h);
expect(h.sendCalls[0].task.model).toBe("m1");
expect(h.store.data.runs.find((r) => r.taskId === id)!.model).toBe("m1");
});
test("snapshot:next 与 timeline 反映各任务的下一个执行点,停用任务为 null", async () => {
const h = await harness();
const a = await h.scheduler.upsert(dailyTask());
const b = await h.scheduler.upsert(dailyTask({ name: "停用", enabled: false }));
h.scheduler.start();
const view = h.scheduler.snapshot();
expect(view.next[a.id]).toBe(new Date(ms("2026-10-31T06:00:00+08:00")).toISOString());
expect(view.next[b.id]).toBeNull();
expect(view.timeline.map((p) => p.taskId)).toEqual([a.id]);
expect(view.runs).toEqual([]);
expect(view.tasks).toHaveLength(2);
});
test("落盘失败 → 不发送、错误可见;恢复持久化后同一到期点不双发", async () => {
let failSave = false;
const h = await harness({
storeWrap: (s) => ({
get data() {
return s.data;
},
save: () => (failSave ? Promise.reject(new Error("模拟磁盘写失败")) : s.save()),
}),
});
const id = (await h.scheduler.upsert(dailyTask())).id;
await h.flush();
failSave = true;
h.scheduler.start();
h.advance(HOUR); // 06:00 到期
h.fireDue();
await h.flush();
expect(h.sendCalls).toHaveLength(0); // 持久化没成立就不发请求
const run = h.store.data.runs.find((r) => r.taskId === id)!;
expect(run.outcome).toBe("failed");
expect(run.reason).toBe("状态文件写不进去,本次不发送:模拟磁盘写失败");
expect(h.store.data.tasks[0].enabled).toBe(true); // 失败不暂停
expect(h.scheduler.snapshot().saveError).toContain("模拟磁盘写失败"); // 对外可见
// 恢复持久化:下一个到期点照常发送(不自动补发已作废的点),错误随之清除
failSave = false;
h.advance(HOUR * 24);
h.fireDue();
await h.flush();
expect(h.sendCalls).toHaveLength(1);
expect(h.scheduler.snapshot().saveError).toBeNull();
// 重启读到的是恢复后的完整状态:旧点已消费、记录齐全,不会再发第二次
const h2 = await harness({ dir: h.dir, startISO: h.now().toISOString() });
h2.scheduler.start();
h2.fireDue();
await h2.flush();
expect(h2.sendCalls).toHaveLength(0);
expect(h2.store.data.marks[id].lastDue).toBe(new Date(ms(T0) + HOUR * 25).toISOString());
expect(h2.store.data.runs.map((r) => r.outcome)).toEqual(["failed", "ok"]);
});
test("window:同一份旧 resetsAt 刷新/跨小时/重启均不重发,新 resetsAt 才再触发", async () => {
let resetsAt = new Date(ms(T0) + 2 * HOUR);
const quotas = () => ({
entries: [{
provider: "openai", account: "acct-yang", label: "Y", viaKey: false,
windows: [{ name: "5h", used: 3, resetsAt: resetsAt.toISOString() }],
}],
});
const h = await harness({ quotas });
const id = (await h.scheduler.upsert(dailyTask({
name: "开窗", trigger: { kind: "window", window: "5h" }, account: "acct-yang",
}))).id;
h.scheduler.start();
h.fireDue();
await h.flush(); // 取快照、排程到 R+30s
expect(h.sendCalls).toHaveLength(0);
h.advance(2 * HOUR + 30_000);
h.fireDue();
await h.flush(); // R+30s:发送第 1 次
expect(h.sendCalls).toHaveLength(1);
expect(h.store.data.runs[0].resetKey).toBe(resetsAt.toISOString());
h.advance(10 * 60_000); // 强刷后 R 已过:同一旧 R 不当新空闲事件
h.fireDue();
await h.flush();
expect(h.sendCalls).toHaveLength(1);
h.advance(HOUR); // 跨小时,快照仍是同一份旧 R
h.fireDue();
await h.flush();
expect(h.sendCalls).toHaveLength(1);
// R 之外还消费了重置所在小时的 idle key(缓存丢掉 resetsAt 时同一小时不再发)
expect(h.store.data.marks[id].resetKeys).toEqual([
resetsAt.toISOString(),
"idle:2026-10-30T23:00:00.000Z",
]);
// 重启:盘上已消费 R,同一旧 R 依旧不重发
const h2 = await harness({ dir: h.dir, startISO: h.now().toISOString(), quotas });
h2.scheduler.start();
h2.fireDue();
await h2.flush();
expect(h2.sendCalls).toHaveLength(0);
// Magpie 公布新的重置时间,且到刷新时它已过 → 评估前先刷新,按新 R 立即开窗
//(不再拿旧快照空转一轮:评估永远基于刚刷新的数据)
resetsAt = new Date(h2.now().getTime() + 8 * 60_000);
h2.advance(10 * 60_000);
h2.fireDue();
await h2.flush();
expect(h2.sendCalls).toHaveLength(1);
expect(h2.store.data.runs.at(-1)!.resetKey).toBe(resetsAt.toISOString());
expect(h2.store.data.marks[id].resetKeys).toContain(resetsAt.toISOString());
});
test("window:评估前先刷新——手动发送刚把窗口开起来,旧快照的空闲不再触发第二次", async () => {
// idle=true 时额度里是没开始的窗口(无 resetsAt);手动发送后窗口开始,出现未来 resetsAt
let idle = true;
const h = await harness({
quotas: () => ({
entries: [{
provider: "openai", account: "acct-yang", label: "Y", viaKey: false,
windows: [{
name: "5h", used: idle ? 0 : 1,
resetsAt: idle ? null : new Date(h.now().getTime() + 5 * HOUR).toISOString(),
}],
}],
}),
});
const id = (await h.scheduler.upsert(dailyTask({
name: "开窗", trigger: { kind: "window", window: "5h" }, account: "acct-yang",
}))).id;
h.scheduler.start();
h.fireDue();
await h.flush();
// 快照新鲜 + 空闲窗口 + 本轮没发过 → 保存即发送,不再等 10 分钟
expect(h.sendCalls).toHaveLength(1);
expect(h.store.data.runs[0].resetKey).toMatch(/^idle:/);
// 等待期间用户手动又发了一次,窗口开始(resetsAt 出现);下一个唤醒先刷新再评估
idle = false;
h.advance(10 * 60_000);
h.fireDue();
await h.flush();
expect(h.sendCalls).toHaveLength(1); // 不按旧快照的空闲再发
// 快照里的 resetsAt 是刷新时刻 + 5 小时,到期点再 + 30 秒宽限
expect(h.scheduler.snapshot().next[id]).toBe(
new Date(h.now().getTime() + 5 * HOUR + 30_000).toISOString(),
);
});
test("window:实际重置发送后,同小时内缓存丢掉 resetsAt 不再按 idle key 二次发送", async () => {
const resetAt = new Date(ms(T0) + 2 * HOUR); // 07:00+08:00
let entries: QuotaEntry[] = [{
provider: "openai", account: "acct-yang", label: "Y", viaKey: false,
windows: [{ name: "5h", used: 3, resetsAt: resetAt.toISOString() }],
}];
const h = await harness({ quotas: () => ({ entries }) });
const id = (await h.scheduler.upsert(dailyTask({
name: "开窗", trigger: { kind: "window", window: "5h" }, account: "acct-yang",
}))).id;
h.scheduler.start();
h.fireDue();
await h.flush(); // 排程到 R+30s
h.advance(2 * HOUR + 30_000); // 07:00:30 发送第 1 次(key=R)
h.fireDue();
await h.flush();
expect(h.sendCalls).toHaveLength(1);
// 额度缓存把 resetsAt 丢成 null(同一实际重置周期内)
entries = [{
provider: "openai", account: "acct-yang", label: "Y", viaKey: false,
windows: [{ name: "5h", used: 0, resetsAt: null }],
}];
h.advance(10 * 60_000); // 07:10:30 刷新拿到新快照
h.fireDue();
await h.flush();
h.advance(10 * 60_000); // 07:20:30 用新快照评估
h.fireDue();
await h.flush();
// 修复前:idle key 与 R 互不认账 → 这里多一次付费发送
expect(h.sendCalls).toHaveLength(1);
// 发送那次已把重置所在小时的 idle key 一并消费
const sendHour = new Date(
Math.floor(new Date(ms(T0) + 2 * HOUR + 30_000).getTime() / HOUR) * HOUR,
).toISOString();
expect(h.store.data.marks[id].resetKeys).toContain(`idle:${sendHour}`);
});
test("window:不可解析的 resetsAt 按空闲每小时限频,key 不用原始字符串", async () => {
const h = await harness({
quotas: () => ({
entries: [{
provider: "openai", account: "acct-yang", label: "Y", viaKey: false,
windows: [{ name: "5h", used: 0, resetsAt: "not-a-date" }],
}],
}),
});
const id = (await h.scheduler.upsert(dailyTask({
name: "开窗", trigger: { kind: "window", window: "5h" }, account: "acct-yang",
}))).id;
h.scheduler.start();
h.fireDue();
await h.flush();
expect(h.sendCalls).toHaveLength(1);
expect(h.store.data.marks[id].resetKeys[0]).toMatch(/^idle:/);
h.advance(10 * 60_000); // 同一小时 → 限频不再发
h.fireDue();
await h.flush();
expect(h.sendCalls).toHaveLength(1);
h.advance(HOUR); // 跨小时 → 先刷出新小时的快照
h.fireDue();
await h.flush();
h.advance(10 * 60_000); // 用新小时快照评估 → 可再试一次,key 仍是 idle:
h.fireDue();
await h.flush();
expect(h.sendCalls).toHaveLength(2);
expect(h.store.data.runs.at(-1)!.resetKey).toMatch(/^idle:/);
});
test("单个任务调度出错:每小时只记一次失败,且不拖垮其他任务", async () => {
const h = await harness();
await h.scheduler.upsert(dailyTask());
// 绕过校验直接塞坏时区任务(模拟系统时区库变更后 state.json 里的旧时区失效)
h.store.data.tasks.push({
id: "t-bad", name: "坏时区", enabled: true, provider: "openai", account: "",
model: "m1", message: "hi",
trigger: { kind: "daily", time: "06:00", timeZone: "Mars/Olympus" },
createdAt: "2026-10-30T00:00:00.000Z", updatedAt: "2026-10-30T00:00:00.000Z",
});
h.scheduler.start(); // 修复前:rearm 对坏任务抛错 → 定时器排不出来,整个调度停摆
h.advance(HOUR); // 06:00
h.fireDue();
await h.flush();
expect(h.sendCalls).toHaveLength(1); // 好任务照常发送
const badRuns = () => h.store.data.runs.filter((r) => r.taskId === "t-bad");
expect(badRuns()).toHaveLength(1);
expect(badRuns()[0].reason).toContain("调度出错");
h.advance(10 * 60_000); // 同一小时再醒 → 不重复记
h.fireDue();
await h.flush();
expect(badRuns()).toHaveLength(1);
});
test("坏时区任务不拖垮 snapshot:该任务 next 置空、不进时间轴,好任务照常", async () => {
const h = await harness();
const good = (await h.scheduler.upsert(dailyTask())).id;
// 绕过校验直接塞坏时区任务(模拟系统时区库变更后 state.json 里的旧时区失效)
h.store.data.tasks.push({
id: "t-bad", name: "坏时区", enabled: true, provider: "openai", account: "",
model: "m1", message: "hi",
trigger: { kind: "daily", time: "06:00", timeZone: "Mars/Olympus" },
createdAt: "2026-10-30T00:00:00.000Z", updatedAt: "2026-10-30T00:00:00.000Z",
});
h.scheduler.start(); // rearm 已有守卫,start 不受影响
const view = h.scheduler.snapshot(); // 修复前:抛 invalid time zone → GET /api/state 500
expect(view.tasks.map((t) => t.id)).toContain("t-bad"); // 任务仍在列表,页面才删得掉
expect(view.next["t-bad"]).toBeNull();
expect(view.timeline.some((p) => p.taskId === "t-bad")).toBe(false);
expect(view.next[good]).toBe(new Date(ms("2026-10-31T06:00:00+08:00")).toISOString());
expect(view.timeline.some((p) => p.taskId === good)).toBe(true);
});
// ---------- 统一账号规则接入调度器(key 目录与模型缓存注入替身) ----------
const fingerprint = (key: string): string =>
createHash("sha256").update(key, "utf8").digest("hex").slice(0, 10);
test("自动模型按账号列表选:key 账号按缓存 Keys 与 accountModels 过滤,并带上 P#指纹 头", async () => {
const fp1 = fingerprint("sk-test-AAAA11");
const fp2 = fingerprint("sk-test-BBBB22");
const h = await harness({
models: [
{ id: "p1/m-one", owned_by: "p1", reasoning: false },
{ id: "p1/m-two", owned_by: "p1", reasoning: false },
],
keyDir: new Map([
[
"p1",
{
keys: [{ fingerprint: fp1, name: "GLM" }, { fingerprint: fp2, name: "免费flash" }],
accountModels: { [fp1]: ["m-one"] },
models: [],
},
],
]),
modelCache: () => [{ id: "m-one", keys: [fp1] }, { id: "m-two", keys: [fp2] }],
});
await h.scheduler.upsert(dailyTask({ provider: "p1", model: "", account: fp1 }));
await firedDaily(h);
// m-two 的 Keys 不含 fp1 → 第 1 层剔除;accountModels 限定 m-one
expect(h.sendCalls[0].task.model).toBe("p1/m-one");
expect(h.sendCalls[0].pin).toBe(`p1#${fp1}`); // 启用 key ≥2 把 → <P>#指纹
expect(h.sendCalls[0].effort).toBeNull();
expect(h.store.data.runs[0].model).toBe("p1/m-one");
});
test("只有一把 key 时指纹账号不发账号头;旧任务里存的 key 名原样发", async () => {
const fp1 = fingerprint("sk-test-AAAA11");
const h = await harness({
models: [{ id: "p1/m-one", owned_by: "p1", reasoning: false }],
keyDir: new Map([["p1", { keys: [{ fingerprint: fp1, name: "GLM" }], accountModels: {}, models: [] }]]),
modelCache: () => [{ id: "m-one", keys: [] }],
});
await h.scheduler.upsert(dailyTask({ provider: "p1", model: "", account: fp1 }));
await firedDaily(h);
expect(h.sendCalls[0].pin).toBeNull(); // 单把 key:候选 ID 就是供应商 id,不带头
const h2 = await harness({
models: [{ id: "p1/m-one", owned_by: "p1", reasoning: false }],
keyDir: new Map([["p1", { keys: [{ fingerprint: fp1, name: "免费flash" }], accountModels: {}, models: [] }]]),
modelCache: () => [{ id: "m-one", keys: [] }],
});
await h2.scheduler.upsert(dailyTask({ provider: "p1", model: "", account: "免费flash" }));
await firedDaily(h2);
expect(h2.sendCalls[0].pin).toBe("免费flash"); // 旧数据兼容:key 名原样作头值
});
test("空账号自动选模型:不带头,用「自动选账号」的模型列表(不受单把 key 的 Keys 限制)", async () => {
const h = await harness({
models: [
{ id: "p1/m-one", owned_by: "p1", reasoning: false },
{ id: "p1/m-one-flash", owned_by: "p1", reasoning: false },
],
keyDir: new Map([
["p1", { keys: [{ fingerprint: fingerprint("sk-test-AAAA11"), name: "GLM" }], accountModels: {}, models: [] }],
]),
modelCache: () => [{ id: "m-one", keys: [fingerprint("sk-test-AAAA11")] }, { id: "m-one-flash", keys: [] }],
});
await h.scheduler.upsert(dailyTask({ provider: "p1", model: "" }));
await firedDaily(h);
expect(h.sendCalls[0].pin).toBeNull();
expect(h.sendCalls[0].task.model).toBe("p1/m-one-flash");
});
test("窗口任务用统一账号列表找窗口:key 账号按指纹反查 key 名再匹配 ~keys 卡", async () => {
const fp1 = fingerprint("sk-test-AAAA11");
const entries: QuotaEntry[] = [
{
provider: "p1", account: "GLM", label: "GLM", viaKey: true,
windows: [{ name: "5h", used: 0, resetsAt: null }], // 空闲窗口:立即到期
},
];
const h = await harness({
quotas: () => ({ entries }),
keyDir: new Map([["p1", { keys: [{ fingerprint: fp1, name: "GLM" }], accountModels: {}, models: [] }]]),
modelCache: () => [{ id: "m-one", keys: [] }],
models: [{ id: "p1/m-one", owned_by: "p1", reasoning: false }],
});
await h.scheduler.upsert(
dailyTask({ provider: "p1", model: "", account: fp1, trigger: { kind: "window", window: "5h" } }),
);
h.scheduler.start();
h.fireDue();
await h.flush();
expect(h.sendCalls).toHaveLength(1);
expect(h.sendCalls[0].pin).toBeNull(); // 目录里只有 1 把 key:指纹账号不发头
});
test("key 目录比额度快照后到 → 首轮不误报「额度里没有账号」,到齐后照常发送(竞态回归)", async () => {
const fp1 = fingerprint("sk-test-AAAA11");
const entries: QuotaEntry[] = [
{
provider: "p1", account: "GLM", label: "GLM", viaKey: true,
windows: [{ name: "5h", used: 0, resetsAt: null }], // 空闲窗口:立即到期
},
];
const keyDir = new Map([
["p1", { keys: [{ fingerprint: fp1, name: "GLM" }], accountModels: {}, models: [] }],
]);
const h = await harness({
quotas: () => ({ entries }),
models: [{ id: "p1/m-one", owned_by: "p1", reasoning: false }],
modelCache: () => [{ id: "m-one", keys: [] }],
// key 目录读取(真实文件 I/O 的形态)慢于额度快照返回
keyDirFn: () => new Promise((resolve) => setTimeout(() => resolve(keyDir), 20)),
});
await h.scheduler.upsert(
dailyTask({ provider: "p1", model: "", account: fp1, trigger: { kind: "window", window: "5h" } }),
);
h.scheduler.start();
h.fireDue(); // 首轮 tick:额度先落,key 目录 20ms 后到
await h.flush();
// 修复前:keyDirMap 还是 null → 指纹解析不出 key 名 → 记「额度里没有账号 <指纹>」且不发送
const failed = h.store.data.runs.filter((r) => r.outcome === "failed");
expect(failed.some((r) => r.reason.includes("额度里没有账号"))).toBe(false);
expect(h.sendCalls).toHaveLength(1);
expect(h.sendCalls[0].pin).toBeNull(); // 目录里只有 1 把 key:不发头
});
test("window:窗口在计时但额度卡瞬断丢了 resetsAt → 不当空闲重发", async () => {
// 插件供应商的用量读取时好时坏:同一扇正在计时的窗口,这一轮读出来没有 resetsAt,
// 看起来像空闲。0.2.2 之前会按「空闲立即发」重发一次;记住最近见过的重置时间后能分清
let resetsAt: string | null = new Date(ms(T0) + 11 * HOUR).toISOString(); // 未来
const h = await harness({
quotas: () => ({
entries: [{
provider: "openai", account: "acct-yang", label: "Y", viaKey: false,
windows: [{ name: "5h", used: 1, resetsAt }],
}],
}),
});
const id = (await h.scheduler.upsert(dailyTask({
name: "开窗", trigger: { kind: "window", window: "5h" }, account: "acct-yang",
}))).id;
h.scheduler.start();
h.fireDue(); // 读到未来 resetsAt → 记下并排程到 R+30s
await h.flush();
expect(h.sendCalls).toHaveLength(0);
resetsAt = null; // 卡瞬断
h.advance(10 * 60_000);
h.fireDue();
await h.flush();
expect(h.sendCalls).toHaveLength(0); // 不当空闲发送
resetsAt = new Date(ms(T0) + 11 * HOUR).toISOString(); // 卡恢复
h.advance(10 * 60_000);
h.fireDue();
await h.flush();
expect(h.sendCalls).toHaveLength(0);
expect(h.scheduler.snapshot().next[id]).toBe(new Date(ms(T0) + 11 * HOUR + 30_000).toISOString());
});
// ---------- 额度缓存滞后(Docker 实机 2026-11-02):magpie web 无托盘时只在路由请求那一刻读卡,
// 读到的是发送前的空闲,之后无人再读;插件跨整点把旧空闲当新事件,每次开窗后都多发一次 ----------
interface Card { readAt: string; resetsAt: string | null }
function zcodeCard(card: () => Card): () => { entries: QuotaEntry[] } {
return () => ({
entries: [{
provider: "zcode", account: "a@b.c", label: "A", viaKey: false, readAt: card().readAt,
windows: [{ name: "5 hours", used: 0, resetsAt: card().resetsAt }],
}],
});
}
const zcodeWindowTask = () => dailyTask({
name: "开窗", provider: "zcode", account: "a@b.c", trigger: { kind: "window", window: "5 hours" },
});
test("window:发送后缓存一直停在发送前的空闲读数 → 跨整点也不重发", async () => {
// 单卡刷新也没拿到新读数(替身空操作):readAt 早于上次发送,空闲不可信,宁可不发
const card: Card = { readAt: new Date(ms(T0)).toISOString(), resetsAt: null };
const h = await harness({ quotas: zcodeCard(() => card) });
await h.scheduler.upsert(zcodeWindowTask());
h.scheduler.start();
h.fireDue();
await h.flush();
expect(h.sendCalls).toHaveLength(1);
for (let i = 0; i < 18; i++) { // 3 小时,跨 3 个整点
h.advance(10 * 60_000);
h.fireDue();
await h.flush();
}
expect(h.sendCalls).toHaveLength(1);
expect(h.refreshCalls.length).toBeGreaterThan(0); // 期间一直在设法拿新读数
expect(h.refreshCalls.every(([p, u]) => p === "zcode" && u === "a@b.c")).toBe(true);
});
test("window:到期先单卡刷新,刷新后看到窗口已在计时 → 不发,排到新的 resetsAt+30s", async () => {
const reset = new Date(ms(T0) + 3 * HOUR).toISOString();
let card: Card = { readAt: new Date(ms(T0) - 2 * HOUR).toISOString(), resetsAt: null };
const h: Harness = await harness({
quotas: zcodeCard(() => card),
refresh: () => { card = { readAt: h.now().toISOString(), resetsAt: reset }; },
});
const id = (await h.scheduler.upsert(zcodeWindowTask())).id;
h.scheduler.start();
h.fireDue();
await h.flush();
expect(h.refreshCalls).toEqual([["zcode", "a@b.c"]]);
expect(h.sendCalls).toHaveLength(0);
expect(h.scheduler.snapshot().next[id]).toBe(new Date(ms(reset) + 30_000).toISOString());
});
test("window:发送 60 秒后单卡刷新拿到新 resetsAt 精确排程;到期后新读数为空闲才再发", async () => {
let card: Card = { readAt: new Date(ms(T0)).toISOString(), resetsAt: null };
let opened: string | null = null; // 厂商侧:发送开窗后的重置时间
const h: Harness = await harness({
quotas: zcodeCard(() => card),
refresh: () => {
const live = opened !== null && ms(opened) > h.now().getTime() ? opened : null;
card = { readAt: h.now().toISOString(), resetsAt: live };
},
sendImpl: async () => {
opened = new Date(h.now().getTime() + 5 * HOUR).toISOString();
return { outcome: "ok", reason: "", ms: 2 };
},
});
const id = (await h.scheduler.upsert(zcodeWindowTask())).id;
h.scheduler.start();
h.fireDue();
await h.flush();
expect(h.sendCalls).toHaveLength(1);
expect(h.refreshCalls).toHaveLength(0); // 读数刚读的,不必再刷
// 发送后唤醒排在 60 秒后,专门去拿开窗后的读数
expect(h.pendingTimers().some((t) => t.at === ms(T0) + 60_000)).toBe(true);
h.advance(60_000);
h.fireDue();
await h.flush();
expect(h.refreshCalls).toHaveLength(1);
expect(h.scheduler.snapshot().next[id]).toBe(new Date(ms(opened!) + 30_000).toISOString());
h.advance(5 * HOUR - 60_000 + 30_000); // 到 resetsAt+30s
h.fireDue();
await h.flush();
expect(h.sendCalls).toHaveLength(2);
});
test("previewSend:按发送前同一套规则现算模型/账号头/档位,不发请求、不写执行记录", async () => {
const h = await harness();
const task = await h.scheduler.upsert(dailyTask());
const before = h.store.data.runs.length;
expect(await h.scheduler.previewSend(task)).toEqual({ model: "m1", pin: null, effort: null });
h.models.length = 0;
expect(await h.scheduler.previewSend(task)).toEqual({ error: "模型 m1 已不在 Magpie 的模型列表里" });
expect(h.sendCalls).toHaveLength(0);
expect(h.store.data.runs.length).toBe(before);
});
test("debugView:暴露定时器为固定任务排的到期点与进行中的发送,只读", async () => {
const h = await harness();
const id = (await h.scheduler.upsert(dailyTask())).id;
expect(h.scheduler.debugView()).toMatchObject({ quotaAt: null, inflight: [], armedFor: {} });
h.scheduler.start();
const view = h.scheduler.debugView();
expect(view.inflight).toEqual([]);
expect(Object.keys(view.armedFor)).toEqual([id]);
expect(new Date(view.armedFor[id]).toISOString()).toBe(new Date("2026-10-31T06:00:00+08:00").toISOString());
expect(h.scheduler.quotaEntries()).toBeNull();
});
// ---------- 空闲窗口的另一种报法(Docker 实机 2026-11-06,Antigravity):空闲时 resetsAt 不清空,
// 恒为「读卡时刻 + 5 小时」,每次重读都往后推;插件当成在计时,重置后一直不发 ----------
interface LiveCard { readAt: string; used: number; resetsAt: string | null }
/** 模拟厂商侧:opened = 当前窗口的重置时刻;空闲时按 idle 规则报(滑动或缺失) */
function vendorCard(
now: number,
live: { opened: number | null; used: number },
idle: "roll" | "null",
): LiveCard {
if (live.opened !== null && live.opened > now) {
return { readAt: new Date(now).toISOString(), used: live.used, resetsAt: new Date(live.opened).toISOString() };
}
// Antigravity 的 resetsAt 精确到秒
const rolled = new Date(Math.floor((now + 5 * HOUR) / 1000) * 1000).toISOString();
return { readAt: new Date(now).toISOString(), used: 0, resetsAt: idle === "roll" ? rolled : null };
}
function cardQuotas(provider: string, card: () => LiveCard): () => { entries: QuotaEntry[] } {
return () => ({
entries: [{
// readAt 为空串表示旧版 magpie 不给读卡时刻
provider, account: "a@b.c", label: "A", viaKey: false, readAt: card().readAt || undefined,
windows: [{ name: "5 hours", used: card().used, resetsAt: card().resetsAt }],
}],
});
}
/** 每分钟推进一次假时钟直到 untilMs,模拟真实唤醒 */
async function runUntil(h: Harness, untilMs: number): Promise<void> {
while (h.now().getTime() < untilMs) {
h.advance(60_000);
h.fireDue();
await h.flush();
}
}
test("window:空闲时 resetsAt 随读卡滑动 → 重置后几分钟内识别为空闲并发送,开窗后不重发", async () => {
const start = ms(T0);
const live = { opened: start + 2 * HOUR as number | null, used: 3 };
let h!: Harness;
let card = vendorCard(ms(T0), live, "roll");
const sentAt: number[] = [];
h = await harness({
quotas: cardQuotas("antigravity", () => card),
refresh: () => { card = vendorCard(h.now().getTime(), live, "roll"); },
sendImpl: async () => {
sentAt.push(h.now().getTime());
live.opened = h.now().getTime() + 5 * HOUR;
live.used = 0; // 最坏情况:开窗后用量仍读作 0,只能靠「重读不变即固定」撤销滑动判定
return { outcome: "ok", reason: "", ms: 2 };
},
});
const id = (await h.scheduler.upsert({ ...zcodeWindowTask(), provider: "antigravity" })).id;
h.scheduler.start();
await runUntil(h, start + 2 * HOUR + 15 * 60_000);
expect(sentAt).toHaveLength(1);
expect(sentAt[0]).toBeGreaterThan(start + 2 * HOUR);
expect(sentAt[0]).toBeLessThanOrEqual(start + 2 * HOUR + 5 * 60_000);
// 新窗口里 resetsAt 固定,多次重读也不再发;下一次重置后同样能识别
await runUntil(h, sentAt[0] + 5 * HOUR - 60_000);
expect(sentAt).toHaveLength(1);
await runUntil(h, sentAt[0] + 5 * HOUR + 10 * 60_000);
expect(sentAt).toHaveLength(2);
expect(h.store.data.marks[id].resetRolls).toBe(false); // 开窗后的读数不再判为滑动
});
test("window:重置点固定、用量读数为 0 的计时窗口 → 复读确认后照常等到重置点", async () => {
const reset = ms(T0) + 3 * HOUR;
let h!: Harness;
let card: LiveCard = { readAt: new Date(ms(T0)).toISOString(), used: 0, resetsAt: new Date(reset).toISOString() };
h = await harness({
quotas: cardQuotas("zcode", () => card),
refresh: () => { card = { ...card, readAt: h.now().toISOString() }; },
});
const id = (await h.scheduler.upsert(zcodeWindowTask())).id;
h.scheduler.start();
await runUntil(h, reset - 10 * 60_000);
expect(h.sendCalls).toHaveLength(0);
expect(h.refreshCalls.length).toBeLessThanOrEqual(1); // 只为确认补读一次,不反复重读
expect(h.scheduler.snapshot().next[id]).toBe(new Date(reset + 30_000).toISOString());
});
test("window:resetsAt 与读卡同步后挪但用量非 0(按最后使用滑动的计时窗口)→ 不当空闲", async () => {
let h!: Harness;
const rolling = (): LiveCard => ({
readAt: h.now().toISOString(), used: 12,
resetsAt: new Date(Math.floor((h.now().getTime() + 5 * HOUR) / 1000) * 1000).toISOString(),
});
let card: LiveCard = { readAt: new Date(ms(T0)).toISOString(), used: 12, resetsAt: new Date(ms(T0) + 5 * HOUR).toISOString() };
h = await harness({
quotas: cardQuotas("zcode", () => card),
refresh: () => { card = rolling(); },
});
await h.scheduler.upsert(zcodeWindowTask());
h.scheduler.start();
for (let i = 0; i < 12; i++) { // 每 10 分钟有别的请求让 magpie 重读一次
await runUntil(h, h.now().getTime() + 10 * 60_000);
card = rolling();
}
expect(h.sendCalls).toHaveLength(0);
expect(h.refreshCalls).toHaveLength(0); // 用量非 0 不可能是滑动空闲,不为判定多读卡
});
test("window:开窗后用量仍读作 0、每窗都在发送 1 分钟后读卡 → 不误判滑动,每窗只发一次", async () => {
// 上一窗的重置点与发送后读数的间隔,恰好等于下一窗的;靠「旧重置点已过」排除
const live = { opened: null as number | null, used: 0 };
let h!: Harness;
let card = vendorCard(ms(T0), live, "null");
const sentAt: number[] = [];
h = await harness({
quotas: cardQuotas("zcode", () => card),
refresh: () => { card = vendorCard(h.now().getTime(), live, "null"); },
sendImpl: async () => {
sentAt.push(h.now().getTime());
live.opened = h.now().getTime() + 5 * HOUR;
return { outcome: "ok", reason: "", ms: 2 };
},
});
await h.scheduler.upsert(zcodeWindowTask());
h.scheduler.start();
await runUntil(h, ms(T0) + 16 * HOUR);
expect(sentAt.length).toBe(4);
for (let i = 1; i < sentAt.length; i++) expect(sentAt[i] - sentAt[i - 1]).toBeGreaterThanOrEqual(5 * HOUR);
});
/** 空闲时按「读卡时刻 + 5 小时 + drift」报 resetsAt 的卡;readAt 为 false 时模拟旧版 magpie */
function rollingIdle(now: number, opts: { readAt?: boolean; drift?: number } = {}): LiveCard {
return {
readAt: opts.readAt === false ? "" : new Date(now).toISOString(),
used: 0,
resetsAt: new Date(Math.floor((now + 5 * HOUR + (opts.drift ?? 0)) / 1000) * 1000).toISOString(),
};
}
test("window:旧版 magpie 不给 readAt → 无从判定滑动,保持原行为(不发、不为判定多读卡)", async () => {
let h!: Harness;
let card = rollingIdle(ms(T0), { readAt: false });
h = await harness({
quotas: cardQuotas("antigravity", () => card),
refresh: () => { card = rollingIdle(h.now().getTime(), { readAt: false }); },
});
const id = (await h.scheduler.upsert({ ...zcodeWindowTask(), provider: "antigravity" })).id;
h.scheduler.start();
await runUntil(h, ms(T0) + 2 * HOUR);
expect(h.sendCalls).toHaveLength(0);
expect(h.refreshCalls).toHaveLength(0);
expect(h.store.data.marks[id].resetRolls).toBeFalsy();
});
test("window:重置点后挪量与读卡间隔相差超过 5 秒 → 不判滑动", async () => {
let h!: Harness;
let reads = 0;
// 每次重读额外多挪 10 秒:不是「读卡时刻 + 固定窗口长度」
let card = rollingIdle(ms(T0));
h = await harness({
quotas: cardQuotas("antigravity", () => card),
refresh: () => { reads += 1; card = rollingIdle(h.now().getTime(), { drift: reads * 10_000 }); },
});
const id = (await h.scheduler.upsert({ ...zcodeWindowTask(), provider: "antigravity" })).id;
h.scheduler.start();
await runUntil(h, ms(T0) + HOUR);
expect(reads).toBeGreaterThan(0);
expect(h.sendCalls).toHaveLength(0);
expect(h.store.data.marks[id].resetRolls).toBe(false);
});
test("window:复读拿不到新读数(readAt 不变的缓存卡)→ 不发,同一个重置点只复读一次", async () => {
const card = rollingIdle(ms(T0));
const h = await harness({ quotas: cardQuotas("antigravity", () => card) }); // refresh 空操作
await h.scheduler.upsert({ ...zcodeWindowTask(), provider: "antigravity" });
h.scheduler.start();
await runUntil(h, ms(T0) + HOUR);
expect(h.sendCalls).toHaveLength(0);
expect(h.refreshCalls).toHaveLength(1);
});
test("window:0.1.3 升级上来的 marks 只有 lastReset、没有读卡时刻 → 补记后照样识别滑动", async () => {
let h!: Harness;
let card = rollingIdle(ms(T0));
h = await harness({
quotas: cardQuotas("antigravity", () => card),
refresh: () => { card = rollingIdle(h.now().getTime()); },
});
const id = (await h.scheduler.upsert({ ...zcodeWindowTask(), provider: "antigravity" })).id;
// 旧版写下的 marks:同一个滑动值,没有 lastResetReadAt / resetRolls;之后没人再读卡
h.store.data.marks[id] = { lastDue: null, resetKeys: [], lastReset: card.resetsAt };
h.scheduler.start();
await runUntil(h, ms(T0) + 5 * 60_000);
expect(h.sendCalls).toHaveLength(1);
});
test("window:已判为滑动空闲,但读数早于「上次发送 + 1 分钟」→ 不发,等发送后的读数", async () => {
// 发送刚结束(如进程在此时重启):缓存里还是发送前的滑动空闲读数,跨整点也不能当新空闲再发
const before = ms(T0) - 50_000; // 读数仍在 1 分钟新鲜期内,「到期先重读」不会替门槛兜底
const sent = ms(T0) - 30_000;
let h!: Harness;
let card = rollingIdle(before);
h = await harness({
quotas: cardQuotas("antigravity", () => card),
// 厂商侧:那次发送已开窗,重读拿到固定重置点
refresh: () => { card = { readAt: h.now().toISOString(), used: 0, resetsAt: new Date(sent + 5 * HOUR).toISOString() }; },
});
const id = (await h.scheduler.upsert({ ...zcodeWindowTask(), provider: "antigravity" })).id;
h.store.data.marks[id] = {
lastDue: null, resetKeys: [], lastReset: card.resetsAt,
lastResetReadAt: new Date(before).toISOString(), resetRolls: true,
lastSent: new Date(sent).toISOString(),
};
h.scheduler.start();
h.fireDue(); // 启动当刻读数还新鲜,就在这一轮判断
await h.flush();
expect(h.sendCalls).toHaveLength(0);
await runUntil(h, ms(T0) + HOUR);
expect(h.sendCalls).toHaveLength(0);
expect(h.store.data.marks[id].resetRolls).toBe(false);
});
// ---------- 空闲窗口的第三种报法(Docker 实机 2026-11-06 12:11,Antigravity):旧窗口到期后不报空闲,
// 而是直接报「旧重置点 + 5 小时」并保持不动,看着像在计时;之前要等约 1 小时它改成滑动形态才发 ----------
/** 厂商侧:opened = 当前窗口重置时刻;到期后报 opened + 5h 并固定(carried),直到有人发送开窗 */
function carriedVendor(
h: () => Harness,
init: { opened: number; used?: number },
onSend: "open" | "keep" = "open",
) {
const live = { opened: init.opened, carried: false, used: init.used ?? 3 };
const read = (): LiveCard => {
const now = h().now().getTime();
if (live.opened <= now) { live.opened += 5 * HOUR; live.carried = true; live.used = 0; }
return { readAt: new Date(now).toISOString(), used: live.used, resetsAt: new Date(live.opened).toISOString() };
};
const send = () => {
if (onSend === "open" || !live.carried) {
live.opened = Math.floor((h().now().getTime() + 5 * HOUR) / 1000) * 1000;
live.used = 0.0067;
}
live.carried = false;
};
return { read, send };
}
async function carriedHarness(init: { opened: number; used?: number }, onSend: "open" | "keep" = "open") {
let h!: Harness;
const vendor = carriedVendor(() => h, init, onSend);
let card: LiveCard = { readAt: new Date(ms(T0)).toISOString(), used: init.used ?? 3, resetsAt: new Date(init.opened).toISOString() };
const sentAt: number[] = [];
h = await harness({
quotas: cardQuotas("antigravity", () => card),
refresh: () => { card = vendor.read(); },
sendImpl: async () => {
sentAt.push(h.now().getTime());
vendor.send();
return { outcome: "ok", reason: "", ms: 2 };
},
});
const id = (await h.scheduler.upsert({ ...zcodeWindowTask(), provider: "antigravity" })).id;
h.scheduler.start();
return { h, id, sentAt, setCard: (c: LiveCard) => { card = c; } };
}
test("window:旧重置点过后卡报「旧重置点 + 5h」并固定 → 重置后几分钟内当空闲发送,开窗后不重发", async () => {
const reset = ms(T0) + 2 * HOUR;
const { h, sentAt } = await carriedHarness({ opened: reset });
await runUntil(h, reset + 15 * 60_000);
expect(sentAt).toHaveLength(1);
expect(sentAt[0]).toBeGreaterThan(reset);
expect(sentAt[0]).toBeLessThanOrEqual(reset + 3 * 60_000);
// 发送开窗(send + 5h):这一窗不再发;到期后厂商又报预报值,下一轮照样及时发
await runUntil(h, sentAt[0] + 5 * HOUR - 60_000);
expect(sentAt).toHaveLength(1);
await runUntil(h, sentAt[0] + 5 * HOUR + 10 * 60_000);
expect(sentAt).toHaveLength(2);
expect(sentAt[1] - sentAt[0]).toBeLessThanOrEqual(5 * HOUR + 3 * 60_000);
});
test("window:预报值在发送后也不变(厂商其实自动开了下一窗)→ 每轮只多发这一次", async () => {
const reset = ms(T0) + 2 * HOUR;
const { h, sentAt } = await carriedHarness({ opened: reset }, "keep");
await runUntil(h, reset + 5 * HOUR - 60_000);
expect(sentAt).toHaveLength(1);
});
test("window:旧重置点过后新重置点用量非 0(有人用过、真开了窗)→ 不当空闲", async () => {
const reset = ms(T0) + 2 * HOUR;
const { h, sentAt, setCard } = await carriedHarness({ opened: reset });
await runUntil(h, reset - 60_000);
// 用户在别处刚用过:新窗口 reset + 4h50m,用量 2
const real = { readAt: "", used: 2, resetsAt: new Date(reset + 5 * HOUR - 10 * 60_000).toISOString() };
h.advance(2 * 60_000);
setCard({ ...real, readAt: h.now().toISOString() });
await runUntil(h, reset + HOUR);
expect(sentAt).toHaveLength(0);
});
test("window:插件在旧重置点之后发过 → 新重置点不按预报值处理", async () => {
const reset = ms(T0) - 10 * 60_000;
const { h, id, sentAt, setCard } = await carriedHarness({ opened: reset + 6 * HOUR });
// 旧窗口 reset 已过、插件在其后发过(lastSent > reset),卡上随即出现新重置点、用量读作 0
h.store.data.marks[id] = {
lastDue: null, resetKeys: [], lastReset: new Date(reset).toISOString(),
lastResetReadAt: new Date(reset - HOUR).toISOString(), lastSent: new Date(reset + 2 * 60_000).toISOString(),
};
setCard({ readAt: h.now().toISOString(), used: 0, resetsAt: new Date(reset + 5 * HOUR).toISOString() });
await runUntil(h, ms(T0) + HOUR);
expect(sentAt).toHaveLength(0);
expect(h.store.data.marks[id].resetCarried).toBeNull();
});
test("window:旧版 magpie 不给 readAt → 预报值无从判定,保持原行为", async () => {
const reset = ms(T0) + 2 * HOUR;
let h!: Harness;
let card: LiveCard = { readAt: "", used: 3, resetsAt: new Date(reset).toISOString() };
h = await harness({
quotas: cardQuotas("antigravity", () => card),
refresh: () => { card = { readAt: "", used: 0, resetsAt: new Date(reset + 5 * HOUR).toISOString() }; },
});
await h.scheduler.upsert({ ...zcodeWindowTask(), provider: "antigravity" });
h.scheduler.start();
await runUntil(h, reset + HOUR);
expect(h.sendCalls).toHaveLength(0);
});
// ---------- PR #878 评审补测:两次读数间隔门槛、撤销滑动判定、复读唤醒 ----------
test("window:两次读数只隔 5 秒、重置点也后挪 5 秒 → 间隔不够不判滑动,不发", async () => {
// 真在计时的窗口,卡片读数抖动几秒也会让「挪动量 ≈ 读卡间隔」凑巧成立,60 秒门槛挡的就是它
const reset = ms(T0) + 3 * HOUR;
const card: LiveCard = { readAt: new Date(ms(T0)).toISOString(), used: 0, resetsAt: new Date(reset + 5_000).toISOString() };
const h = await harness({ quotas: cardQuotas("antigravity", () => card) }); // refresh 空操作
const id = (await h.scheduler.upsert({ ...zcodeWindowTask(), provider: "antigravity" })).id;
h.store.data.marks[id] = {
lastDue: null, resetKeys: [], lastReset: new Date(reset).toISOString(),
lastResetReadAt: new Date(ms(T0) - 5_000).toISOString(),
};
h.scheduler.start();
h.fireDue();
await h.flush();
expect(h.store.data.marks[id].resetRolls).toBe(false);
await runUntil(h, ms(T0) + 30 * 60_000);
expect(h.sendCalls).toHaveLength(0);
});
test("window:已判滑动,发送与最后一次滑动读数同秒、开窗后重置点恰好不变 → 重读确认后撤销,整点后不再发", async () => {
const sent = ms(T0) - 90_000;
const reset = new Date(sent + 5 * HOUR).toISOString(); // 最后一次滑动读数 = 开窗后的固定重置点
let h!: Harness;
let card: LiveCard = { readAt: new Date(ms(T0)).toISOString(), used: 0, resetsAt: reset };
h = await harness({
quotas: cardQuotas("antigravity", () => card),
refresh: () => { card = { ...card, readAt: h.now().toISOString() }; },
});
const id = (await h.scheduler.upsert({ ...zcodeWindowTask(), provider: "antigravity" })).id;
h.store.data.marks[id] = {
lastDue: null, resetKeys: [], lastReset: reset, lastResetReadAt: new Date(sent).toISOString(),
resetRolls: true, lastSent: new Date(sent).toISOString(),
};
h.scheduler.start();
await runUntil(h, ms(T0) + 2 * HOUR); // 跨两个整点
expect(h.store.data.marks[id].resetRolls).toBe(false);
expect(h.sendCalls).toHaveLength(0);
});
test("window:新出现的未来重置点 → 首读满 60 秒就醒来复读,不等 10 分钟的常规刷新", async () => {
let h!: Harness;
let card = rollingIdle(ms(T0));
h = await harness({
quotas: cardQuotas("antigravity", () => card),
refresh: () => { card = { ...card, readAt: h.now().toISOString() }; }, // 固定重置点,只看复读时机
});
await h.scheduler.upsert({ ...zcodeWindowTask(), provider: "antigravity" });
h.scheduler.start();
h.fireDue();
await h.flush();
expect(h.refreshCalls).toHaveLength(0);
h.advance(60_000);
h.fireDue();
await h.flush();
expect(h.refreshCalls).toHaveLength(1);
});