Adds packages/window-warmup (@magpie-community/page-window-warmup 0.1.0), by YangHonghang: a page plugin whose host part runs a service on 127.0.0.1:3492 and, on a schedule (after a quota window resets, daily at a set time, or every N hours), sends "hi" with max_tokens 16 through magpie's gateway to a provider/account/model the user picks. The registry entry moves from the author's own npm package magpie-window-warmup to this one. The package README opens with the zh/en warning that sends spend real quota and that some vendors (Claude subscriptions especially) may treat scheduled requests as unusual use. Reviewed at 07a0b217adb2a111f839c984cbc955c5bb45b68d: - Outside packages/window-warmup only README.md and registry.json change; no workflow and no other package. - The service binds 127.0.0.1, checks Host (DNS rebinding) and Origin (null or itself), needs a per-process 32-byte token on /api/*, and serves the index with a nonce CSP. - It reaches only MAGPIE_ADDR (default 127.0.0.1:3425). providers.json is read for sha256 key fingerprints only; no spawn, eval or outside hosts. - A window task sends at most once per resetsAt (or once per hour when idle); daily/every tasks catch up only the latest missed point, once. Run on mote (bun 1.4.2): - HOME=$(mktemp -d) bun test at the repo root: 5142 pass, 15 skip, 0 fail. - scripts/check.mjs ok. - packages/window-warmup bun test: 242 pass (dist.test rebuilds dist byte for byte). - browser.test.ts with Playwright 1.62.0: chromium and webkit pass. Bun ignores NODE_PATH, so it needs node_modules/playwright linked and PLAYWRIGHT_BROWSERS_PATH set under a temp HOME; without them it skips. Not tried: a real send through a live magpie gateway, and tsc --noEmit (no typescript on mote).
129 lines
5.0 KiB
TypeScript
129 lines
5.0 KiB
TypeScript
// 带路页 boot.js 的失败测试(抄 fingerprint tests/boot.test.ts 的做法):
|
||
// 在新上下文里执行脚本,断言跳转目标;并确保脚本没有任何联网调用
|
||
// (magpie 页面 CSP connect-src 'none',只能整页跳转)。
|
||
import { expect, test } from "bun:test";
|
||
import { readFile } from "node:fs/promises";
|
||
import { runInNewContext } from "node:vm";
|
||
|
||
const source = await readFile(new URL("../page/boot.js", import.meta.url), "utf8");
|
||
|
||
test("脚本中不出现 fetch / XMLHttpRequest / WebSocket", () => {
|
||
expect(source).not.toMatch(/fetch|XMLHttpRequest|WebSocket/);
|
||
});
|
||
|
||
interface BootResult {
|
||
destination?: URL;
|
||
said: { texts: string[] };
|
||
}
|
||
|
||
async function boot(opts: {
|
||
dark?: boolean;
|
||
tokens?: Record<string, string>;
|
||
service?: string;
|
||
port?: number;
|
||
remote?: boolean;
|
||
} = {}): Promise<BootResult> {
|
||
const said = { texts: [] as string[] };
|
||
const el = () => {
|
||
const kids: { textContent: string; style: Record<string, string>; appendChild: (x: unknown) => void }[] = [];
|
||
return {
|
||
textContent: "",
|
||
style: {} as Record<string, string>,
|
||
addEventListener: (_t: string, _fn: () => void) => {},
|
||
appendChild(child: { textContent?: string }) {
|
||
kids.push(child as never);
|
||
said.texts.push(String(child?.textContent ?? ""));
|
||
},
|
||
};
|
||
};
|
||
let destination: URL | undefined;
|
||
const document = {
|
||
getElementById: (_id: string) => el(),
|
||
createElement: (_tag: string) => el(),
|
||
};
|
||
await runInNewContext(source, {
|
||
URL,
|
||
URLSearchParams,
|
||
btoa,
|
||
atob,
|
||
document,
|
||
window: {
|
||
magpie: {
|
||
ready: Promise.resolve({ dark: opts.dark ?? true, tokens: opts.tokens }),
|
||
service: opts.service ?? "",
|
||
options: { get: async () => ({ port: opts.port }) },
|
||
},
|
||
},
|
||
location: {
|
||
hostname: opts.remote ? "magpie.example" : "127.0.0.1",
|
||
href: opts.remote ? "http://magpie.example/boot/index.html" : "http://127.0.0.1/boot/index.html",
|
||
replace: (url: string) => {
|
||
destination = new URL(url);
|
||
},
|
||
},
|
||
});
|
||
// 带路页内部有多个 await(ready、options.get),等一个宏任务让它们全部走完
|
||
await new Promise((r) => setTimeout(r, 0));
|
||
return { destination, said };
|
||
}
|
||
|
||
test("有 magpie.service → 跳 service,并带上主题与令牌", async () => {
|
||
const r = await boot({
|
||
service: "http://magpie.example:8080/plugins/scheduled-messages/",
|
||
tokens: { "--magpie-bg": "#101217" },
|
||
});
|
||
expect(r.destination?.origin).toBe("http://magpie.example:8080");
|
||
expect(r.destination?.pathname).toBe("/plugins/scheduled-messages/");
|
||
expect(r.destination?.searchParams.get("theme")).toBe("dark");
|
||
const tokens = r.destination?.searchParams.get("tokens") ?? "";
|
||
const decoded = JSON.parse(
|
||
new TextDecoder().decode(Uint8Array.from(atob(tokens), (c) => c.charCodeAt(0))),
|
||
) as Record<string, string>;
|
||
expect(decoded["--magpie-bg"]).toBe("#101217");
|
||
});
|
||
|
||
test("无 service → 跳 127.0.0.1 的插件端口", async () => {
|
||
const r = await boot();
|
||
expect(r.destination?.hostname).toBe("127.0.0.1");
|
||
expect(r.destination?.port).toBe("3492"); // 默认端口与 package.json 的 magpie.options 一致
|
||
expect(r.destination?.searchParams.get("theme")).toBe("dark");
|
||
});
|
||
|
||
test("插件选项 port 覆盖默认端口", async () => {
|
||
const r = await boot({ port: 4567 });
|
||
expect(r.destination?.port).toBe("4567");
|
||
});
|
||
|
||
test("无效端口退回默认 3492", async () => {
|
||
const r = await boot({ port: -1 });
|
||
expect(r.destination?.port).toBe("3492");
|
||
});
|
||
|
||
test("浅色主题跟随宿主", async () => {
|
||
const r = await boot({ dark: false });
|
||
expect(r.destination?.searchParams.get("theme")).toBe("light");
|
||
});
|
||
|
||
test("远程打开且无 service → 原地不动,给出 SSH 端口转发提示", async () => {
|
||
const r = await boot({ remote: true, port: 3492 });
|
||
expect(r.destination).toBeUndefined();
|
||
expect(r.said.texts.join("\n")).toContain("SSH");
|
||
expect(r.said.texts.join("\n")).toContain("3492");
|
||
});
|
||
|
||
test("页面资源与 API 全走相对路径:任意服务前缀下都落回转发内,本地直达不受影响", async () => {
|
||
// magpie web 把服务转发在自己的前缀路径下,根绝对引用会解析到宿主根而 404
|
||
const html = await readFile(new URL("../web/index.html", import.meta.url), "utf8");
|
||
expect(html).not.toContain('href="/');
|
||
expect(html).not.toContain('src="/');
|
||
const app = await readFile(new URL("../web/app.ts", import.meta.url), "utf8");
|
||
expect(app).not.toMatch(/["'`]\/api\//);
|
||
expect(app).not.toContain('src="/');
|
||
// 相对引用的解析语义:转发前缀下落回前缀内,本地根路径下等价于根绝对
|
||
const base = "http://magpie.example:8080/plugins/scheduled-messages/";
|
||
expect(new URL("app.js", base).pathname).toBe("/plugins/scheduled-messages/app.js");
|
||
expect(new URL("style.css", base).pathname).toBe("/plugins/scheduled-messages/style.css");
|
||
expect(new URL("api/state", base).pathname).toBe("/plugins/scheduled-messages/api/state");
|
||
expect(new URL("api/state", "http://127.0.0.1:3492/").pathname).toBe("/api/state");
|
||
});
|