Files
plugins/packages/window-warmup/tests/icon.test.ts
T
YangHonghang 1ab8238ade window-warmup 0.1.0: a page that sends a short message after a quota window resets, daily or every few hours (#661)
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).
2026-11-03 15:43:10 +08:00

36 lines
1.8 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.
// 图标字段与 magpie 的读取规则对齐(internal/plugin/market.go ownIcon、internal/plugin/page.go):
// 市场与插件行读 package.json 的 magpie.icon(只收 https:// 或 data:image/,上限 3<<19 字节);
// 侧边栏页面图标读 magpie.page.icon(必须落在页面文件所在目录 page/ 内)。
import { stat } from "node:fs/promises";
import { resolve } from "node:path";
import { expect, test } from "bun:test";
const pkg = (await Bun.file(new URL("../package.json", import.meta.url)).json()) as {
magpie?: { icon?: string; page?: { icon?: string; file?: string } };
};
test("magpie.icon:data:image URI,且不超过 magpie ownIcon 的 1.5MB 上限", () => {
const icon = pkg.magpie?.icon ?? "";
expect(icon.startsWith("data:image/")).toBe(true);
expect(icon.length).toBeLessThanOrEqual(3 << 19);
});
test("magpie.icon 与 page/icon.svg 是同一张图:插件页卡片与侧边栏图标不能只改一份", async () => {
const icon = pkg.magpie?.icon ?? "";
const b64 = icon.slice(icon.indexOf(",") + 1);
const svg = await Bun.file(new URL("../page/icon.svg", import.meta.url)).text();
expect(Buffer.from(b64, "base64").toString("utf8")).toBe(svg);
});
test("magpie.page.icon:文件存在,且在页面文件(page/)所在目录内", async () => {
const pageIcon = pkg.magpie?.page?.icon ?? "";
const pageFile = pkg.magpie?.page?.file ?? "";
expect(pageFile.startsWith("page/")).toBe(true);
expect(pageIcon.startsWith("page/")).toBe(true);
// 与 magpie page.go 一致:图标相对页面目录解析后不得越出
const rel = pageIcon.slice("page/".length);
expect(rel).not.toContain("..");
expect(rel).not.toMatch(/[/\\]/); // 就放在 page/ 一层,最简单也最稳
await expect(stat(resolve(import.meta.dir, "..", pageIcon))).resolves.toBeTruthy();
});