import test from "node:test"; import assert from "node:assert/strict"; import { readFileSync, readdirSync } from "node:fs"; import { join } from "node:path"; const dir = join(import.meta.dirname, ".."); const pageDir = join(dir, "page"); const uiDir = join(dir, "ui"); const read = (base, file) => readFileSync(join(base, file), "utf8"); const pkg = JSON.parse(readFileSync(join(dir, "package.json"), "utf8")); // view.js is a classic script, as the page loads it. const scope = {}; new Function("globalThis", read(uiDir, "i18n.js"))(scope); new Function("globalThis", read(uiDir, "view.js"))(scope); const V = scope.UsageView; const summary = { period: "7d", accounts: 2, models: 3, calls: 120, errors: 3, input: 1200000, output: 300000, cache: 900000, cacheRead: 900000, cacheWrite: 0, cacheUnknown: 0, tokens: 1500000, cost: 42.5, unpriced: 2, }; const accounts = [ { id: "codex\u001falice@example.com", account: "alice@example.com", alias: "10月学生Plus", provider: "codex", providerName: "Codex", calls: 100, errors: 3, tokens: 1400000, cost: 40, unpriced: 2, share: 0.8, bar: 1, models: [ { id: "m1", model: "gpt-6-luna", providerName: "Codex", calls: 100, errors: 3, tokens: 1400000, cost: 40, unpriced: 2, share: 1, bar: 1, hostNotes: [{ account: "chatgpt.com", calls: 14 }], }, ], }, { id: "raccoon\u001fbob", account: "bob", alias: "", provider: "raccoon", providerName: "Raccoon", calls: 20, errors: 0, tokens: 100000, cost: 2.5, unpriced: 0, share: 0.2, bar: 0.15, models: [], }, ]; test("the page reads nothing of magpie and reaches no network", () => { assert.deepEqual(pkg.magpie.permissions ?? [], []); const reads = new Set(); for (const file of readdirSync(pageDir).filter((f) => f.endsWith(".js"))) { const source = read(pageDir, file); for (const match of source.matchAll(/magpie\.read\(\s*"([^"]+)"/g)) reads.add(match[1]); // magpie's policy refuses these anyway; the page must not try. assert.doesNotMatch( source, /\bfetch\(|XMLHttpRequest|WebSocket|sendBeacon|EventSource|importScripts/, ); } assert.deepEqual([...reads], []); for (const file of readdirSync(uiDir).filter((f) => f.endsWith(".js"))) { const source = read(uiDir, file); assert.doesNotMatch(source, /\.read\(\s*"usage:day"|\bread\(\s*"usage:day"/); } }); test("plugin metadata speaks Chinese and English", () => { assert.equal(pkg.magpie.name, "账号用量 / Account usage"); assert.equal(pkg.magpie.page.titles.zh, "账号用量"); assert.match(pkg.magpie.summary.zh, /供应商 -> 账号 -> 模型/); assert.match(pkg.magpie.summary.en, /provider -> account -> model/); assert.match(pkg.description, /在 Magpie 窗口中/); assert.match(pkg.description, /A page in Magpie's window/); const readme = read(join(dir), "README.md"); assert.match(readme, /## 中文说明/); assert.ok(readme.indexOf("## 中文说明") < readme.indexOf("## English")); for (const text of [pkg.description, pkg.magpie.page.titles.zh, pkg.magpie.summary.zh, readme]) { assert.doesNotMatch(text, /\uFFFD/); } }); test("is a page magpie serves: its files in page/, scripts as files", () => { assert.equal(pkg.magpie.page.file, "page/index.html"); assert.equal(pkg.magpie.page.icon, "page/icon.svg"); const html = read(pageDir, "index.html"); assert.match(html, /