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).
119 lines
4.2 KiB
TypeScript
119 lines
4.2 KiB
TypeScript
// 页面端到端冒烟:真实浏览器(Chromium 与 WebKit)打开本地服务页面,走一遍新建任务并保存。
|
||
// Playwright 是可选依赖:仓库不安装它,评审机通过 NODE_PATH 提供(1.62.0,Chromium + WebKit)。
|
||
// 导入失败或浏览器启动失败时整组跳过,仓库根目录的 bun test 不受影响。
|
||
import { afterAll, beforeAll, expect, test } from "bun:test";
|
||
import { mkdtemp, rm } from "node:fs/promises";
|
||
import { tmpdir } from "node:os";
|
||
import { join } from "node:path";
|
||
import { startService, type ServiceHandle } from "../src/server";
|
||
import { fakeGateway, type FakeGateway } from "./fake-gateway";
|
||
|
||
// Playwright 的最小类型面:不依赖它的类型声明,没装也能通过 tsc
|
||
interface Locator {
|
||
click(): Promise<void>;
|
||
fill(v: string): Promise<void>;
|
||
first(): Locator;
|
||
waitFor(o?: { timeout?: number }): Promise<void>;
|
||
}
|
||
interface Page {
|
||
goto(url: string): Promise<unknown>;
|
||
on(ev: "pageerror", cb: (e: unknown) => void): void;
|
||
getByRole(role: string, o?: { name?: string; exact?: boolean }): Locator;
|
||
getByPlaceholder(t: string): Locator;
|
||
getByText(t: string): Locator;
|
||
}
|
||
interface Browser {
|
||
newPage(): Promise<Page>;
|
||
close(): Promise<void>;
|
||
}
|
||
interface Engine {
|
||
launch(): Promise<Browser>;
|
||
}
|
||
interface Playwright {
|
||
chromium: Engine;
|
||
webkit: Engine;
|
||
}
|
||
|
||
const pw = await (async (): Promise<Playwright | null> => {
|
||
try {
|
||
const name = "playwright"; // 非字面量:未安装时也不要求模块声明
|
||
return (await import(name)) as unknown as Playwright;
|
||
} catch {
|
||
return null;
|
||
}
|
||
})();
|
||
|
||
const ENGINES: Array<[string, Engine]> = pw ? [["chromium", pw.chromium], ["webkit", pw.webkit]] : [];
|
||
|
||
// 浏览器缺失时跳过整组(test.skipIf 在定义时求值):探针各启动一次并立即关闭
|
||
const browsersReady = await (async () => {
|
||
for (const [, engine] of ENGINES) {
|
||
try {
|
||
const b = await engine.launch();
|
||
await b.close();
|
||
} catch {
|
||
return false;
|
||
}
|
||
}
|
||
return ENGINES.length > 0;
|
||
})();
|
||
const testBrowser = test.skipIf(!browsersReady);
|
||
|
||
if (!browsersReady) test.skip("浏览器冒烟:本机没有可用的 Playwright/Chromium/WebKit,已跳过", () => {});
|
||
|
||
let svc: ServiceHandle;
|
||
let gw: FakeGateway;
|
||
let dir: string;
|
||
|
||
beforeAll(async () => {
|
||
if (!browsersReady) return; // 没浏览器就不起服务
|
||
dir = await mkdtemp(join(tmpdir(), "ww-browser-"));
|
||
gw = fakeGateway({
|
||
models: [
|
||
{ id: "acme/acme-pro-2", owned_by: "acme", reasoning: true, reasoning_levels: ["low", "high"] },
|
||
{ id: "acme/acme-mini-3", owned_by: "acme" },
|
||
],
|
||
quotas: [
|
||
{
|
||
provider: "acme",
|
||
user: "me@x.com",
|
||
windows: [{ name: "5h", used: 40, resetsAt: new Date(Date.now() + 2 * 3600_000).toISOString() }],
|
||
},
|
||
],
|
||
chat: { status: 200, body: { id: "x", choices: [] } },
|
||
});
|
||
svc = await startService({ port: 0, dir, base: gw.base, uiDir: join(import.meta.dir, "..", "dist", "ui") });
|
||
});
|
||
|
||
afterAll(async () => {
|
||
await svc?.stop();
|
||
gw?.stop();
|
||
if (dir) await rm(dir, { recursive: true, force: true });
|
||
});
|
||
|
||
for (const [name, engine] of ENGINES) {
|
||
testBrowser(
|
||
`${name}:新建任务、切换触发方式并保存`,
|
||
async () => {
|
||
const browser = await engine.launch();
|
||
try {
|
||
const page = await browser.newPage();
|
||
const errors: string[] = [];
|
||
page.on("pageerror", (e) => errors.push(String(e)));
|
||
await page.goto(`http://127.0.0.1:${svc.port}/?lang=zh`);
|
||
// 目录加载完成前按钮就存在;等账号下拉渲染出目录里的账号再操作
|
||
await page.getByRole("button", { name: "新建任务" }).click();
|
||
await page.getByRole("button", { name: "账号 me@x.com", exact: true }).waitFor({ timeout: 15_000 });
|
||
await page.getByPlaceholder("给任务起个名字").fill("浏览器冒烟");
|
||
await page.getByRole("button", { name: "每天定时", exact: true }).click();
|
||
await page.getByRole("button", { name: "保存", exact: true }).click();
|
||
await page.getByText("浏览器冒烟").first().waitFor({ timeout: 15_000 });
|
||
expect(errors).toEqual([]);
|
||
} finally {
|
||
await browser.close();
|
||
}
|
||
},
|
||
90_000,
|
||
);
|
||
}
|