Files
plugins/packages/page-model-quota/test/page.test.mjs
T
1sh1ro 7153e59d78 page-model-quota 0.1.0: the tokens each model has left, across accounts (#932)
A page that shows how many tokens each model has left across your accounts. It is estimated from magpie's quota windows (/v1/magpie/quotas) and past usage rounds: the median tokens per percent over the rounds that can be used, or one round, or "sparse". The report carries no prose. The page words everything in English or Chinese. The service binds only to loopback, checks the Host, refuses a foreign Origin, wants a per-run key for Origin null, serves index.html to navigations only, and writes nothing under HOME. page-model-quota 0.1.0.

Run at the reviewed head dd8d44164d505088efeb07d80784128134aad213, on the Windows box (mote's sshd was refusing connections):
- node --test test/*.test.mjs: 14 pass. bun test: 14 pass. bun scripts/check.mjs: exit 0. It merges cleanly with main 86d3febf.
- Broken by hand: taking the space out of " (from past rounds)" again turns i18n.test red (13 of 14).

At a5d9a8c5, on mote and in WSL (from the review): the Host, Origin, key and navigate-only checks each went red when broken. Wrong Hosts got 403, Origin null without a key got 401, traversal got 404, and a restart rotated the key. The page rendered in Chromium and WebKit at 1200px and 420px, with no Chinese in English. Since then, only the English note's space, an unused usableRounds, the English gateway error and template-literal coverage in the i18n test have changed.

Not tried: in a real magpie window with real quota windows.
2026-11-11 10:49:01 +08:00

48 lines
2.0 KiB
JavaScript
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.
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 read = (base, file) => readFileSync(join(base, file), "utf8");
const pkg = JSON.parse(readFileSync(join(dir, "package.json"), "utf8"));
test("the launcher 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]);
// 页面的 CSP 是 connect-src 'none',magpie 也会拒绝这些调用。
assert.doesNotMatch(source, /\bfetch\(|XMLHttpRequest|WebSocket|sendBeacon|EventSource|importScripts/);
}
assert.deepEqual([...reads], []);
});
test("the launcher navigates instead of probing", () => {
const boot = read(pageDir, "boot.js");
assert.match(boot, /window\.magpie\?\.service/);
assert.match(boot, /location\.replace/);
assert.doesNotMatch(boot, /\bfetch\(/);
});
test("plugin metadata speaks Chinese and English", () => {
assert.equal(pkg.magpie.name, "Model Quota");
assert.equal(pkg.magpie.page.titles.zh, "模型剩余额度");
assert.equal(pkg.magpie.page.titles.en, "Model Quota");
assert.match(pkg.magpie.summary.zh, /额度窗口/);
assert.match(pkg.magpie.summary.en, /quota window/);
assert.match(pkg.description, /A page in Magpie's window/);
const readme = read(dir, "README.md");
assert.match(readme, /magpie plugin add @magpie-community\/page-model-quota/);
assert.match(readme, /## 中文说明/);
});
test("the declared port is the one the page falls back to", () => {
const boot = read(pageDir, "boot.js");
const declared = pkg.magpie.page.service.port;
assert.equal(pkg.magpie.options.port, declared);
assert.match(boot, new RegExp(`const DEFAULT_PORT = ${declared}`));
});