New page plugin @magpie-community/page-usage-tree 0.1.0 by CHENZUDE: magpie's usage as a provider -> account -> model tree, served by a small server that reads the gateway's /v1/magpie/usage and /usage/requests.
The last round (f9e0907) gives the model row's calls cell the calls-cell class, so CALLS values sit under their right-aligned header, adds a test for it, and comments why the server's retry budget is 16 x 100ms.
What was run at f9e09079 (mote, macOS, sandbox HOME):
- In the package: node --test 50 pass; bun test 47 tests, 0 fail.
- f9e09079 merged onto main 8765cf8f: bun scripts/check.mjs ok; whole-repo bun test 6149 pass, 20 skip, 0 fail.
- Playwright against a fake gateway: CALLS header and value right edges 616/616 (1280px), 423/423 (900), 341/341 (760, 621) in Chromium and WebKit. Chromium 1280-620px, WebKit 900/760, both engines at 420px in en and zh: no clipped cell, no sideways scroll, no <select>, no page errors.
- Broken by hand: the old `model.errors ? "bad" : ""` puts values at 544 and 273 and turns "model calls sit under the right-aligned calls header" red (27 pass, 1 fail).
Not tried: a real magpie with real accounts, the desktop app, Windows and Linux.
78 lines
2.5 KiB
JavaScript
78 lines
2.5 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { createServer } from "node:http";
|
|
import { mkdirSync, mkdtempSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { loadUsageData } from "../lib/data.mjs";
|
|
|
|
test("a stale disk cache says so while it refreshes behind the page", async (t) => {
|
|
const root = mkdtempSync(join(tmpdir(), "usage-tree-cache-"));
|
|
const previousCache = process.env.MAGPIE_CACHE_DIR;
|
|
process.env.MAGPIE_CACHE_DIR = root;
|
|
mkdirSync(join(root, "usage-tree"), { recursive: true });
|
|
writeFileSync(
|
|
join(root, "usage-tree", "today.json"),
|
|
JSON.stringify({
|
|
v: 6,
|
|
at: Date.now() - 60_000,
|
|
data: {
|
|
period: "today",
|
|
generatedAt: "old",
|
|
summary: { calls: 1 },
|
|
providers: [],
|
|
accounts: [],
|
|
warnings: [],
|
|
},
|
|
}),
|
|
"utf8",
|
|
);
|
|
|
|
const gateway = createServer((req, res) => {
|
|
if (req.url.startsWith("/v1/magpie/usage")) {
|
|
res.setHeader("content-type", "application/json");
|
|
res.end(JSON.stringify({
|
|
period: "today",
|
|
calls: 2,
|
|
errors: 0,
|
|
input: 2,
|
|
output: 0,
|
|
cache_read: 0,
|
|
cache_write: 0,
|
|
cost: 0,
|
|
unpriced: 0,
|
|
accounts: [],
|
|
models: [],
|
|
}));
|
|
return;
|
|
}
|
|
res.writeHead(404).end();
|
|
});
|
|
await new Promise((resolve) => gateway.listen(0, "127.0.0.1", resolve));
|
|
const base = `http://127.0.0.1:${gateway.address().port}`;
|
|
|
|
t.after(async () => {
|
|
await new Promise((resolve) => gateway.close(resolve));
|
|
if (previousCache === undefined) delete process.env.MAGPIE_CACHE_DIR;
|
|
else process.env.MAGPIE_CACHE_DIR = previousCache;
|
|
rmSync(root, { recursive: true, force: true });
|
|
});
|
|
|
|
const first = await loadUsageData("today", { gateway: base });
|
|
assert.equal(first.cached, true);
|
|
assert.equal(first.stale, true);
|
|
assert.equal(first.summary.calls, 1);
|
|
|
|
let latest = first;
|
|
for (let attempt = 0; attempt < 40 && latest.summary.calls !== 2; attempt += 1) {
|
|
await new Promise((resolve) => setTimeout(resolve, 25));
|
|
latest = await loadUsageData("today", { gateway: base });
|
|
}
|
|
assert.equal(latest.summary.calls, 2);
|
|
assert.equal(latest.stale, false);
|
|
if (process.platform !== "win32") {
|
|
assert.equal(statSync(join(root, "usage-tree")).mode & 0o777, 0o700);
|
|
assert.equal(statSync(join(root, "usage-tree", "today.json")).mode & 0o777, 0o600);
|
|
}
|
|
});
|