Adds packages/amp-usage (@magpie-community/page-amp-usage 0.1.0). It is a magpie page that reads Amp's local thread files and the gateway's usage.jsonl and shows per-day usage. The service listens on loopback only and requires a per-start service token on every API call. The page that carries the token is served only to a navigation (Sec-Fetch-Mode). The gateway log is read incrementally from a saved offset, whole lines only, and is re-indexed on rotation. The UI words are in ui/words.js (en, zh).
Also bumps packages/day-card to 0.3.1.
Round 3 (13c1f33) adds three things:
- a test that bytes before the gateway-log offset are never re-read;
- ui.test.mjs, which checks that every error code the server can return has words in each language, and that an unknown code falls back;
- words.js, moved out of app.js, served at /words.js and included in the pack.
What ran, at 13c1f33 merged with main 14187408, on mica:
- amp-usage `node --test`: 47/47.
- `bun test packages/amp-usage packages/day-card`: 65 pass, 0 fail.
- `bun scripts/check.mjs`, all packages: ok.
- `npm pack --dry-run`: amp-usage 0.1.0 has 19 files including ui/words.js; day-card 0.3.1 has 17 files.
- Root `bun test` with a temp HOME: 8362 pass, 0 fail (8382 tests, 590 files).
- Render smoke in Chromium and WebKit at 1200 and 420px, en and zh: words.js loads, both languages show their own words, no page errors, no horizontal overflow.
Broken by hand, each run with node and bun:
- collector.mjs:102 made to re-read the whole file every scan: the new offset test goes red.
- `const cut = buf.length`, so a partial line is taken: red.
- Raw error codes shown instead of their words (`w.err[code] || code`): red.
- A zh string deleted: red.
Not tried:
- A real Amp account or the real `amp` CLI.
- Changing the auto-refresh default from 300 to 60 fails no test. The author left it untested and round 2 called it optional.
Rounds 1 and 2 also ran the 1M-line log probe (rss steady at about 363MB, scans of 70 to 114ms after the first), the bad-record and paging probes, and the full render matrix including dark mode.
145 lines
5.3 KiB
JavaScript
145 lines
5.3 KiB
JavaScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { mkdtemp, rm, readFile, writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { tmpdir } from "node:os";
|
|
import { readCloud, readLocal } from "../lib/sources.mjs";
|
|
const usage = {
|
|
model: "gpt-test",
|
|
timestamp: "2026-01-01T00:00:00Z",
|
|
inputTokens: 10,
|
|
outputTokens: 1,
|
|
};
|
|
async function dir(t) {
|
|
const d = await mkdtemp(join(tmpdir(), "amp-source-"));
|
|
t.after(() => rm(d, { recursive: true, force: true }));
|
|
return d;
|
|
}
|
|
test("cloud exports include archived threads, project private fields away, and reuse unchanged cache", async (t) => {
|
|
const dataDir = await dir(t),
|
|
calls = [];
|
|
const run = async (args) => {
|
|
calls.push(args);
|
|
if (args[1] === "list")
|
|
return [{ id: "T-cloud", updated: usage.timestamp }];
|
|
return {
|
|
id: "T-cloud",
|
|
title: "private",
|
|
messages: [
|
|
{
|
|
role: "assistant",
|
|
messageId: 1,
|
|
content: [{ text: "private" }],
|
|
usage,
|
|
},
|
|
],
|
|
};
|
|
};
|
|
const first = await readCloud({ dataDir, run });
|
|
assert.equal(first.exported, 1);
|
|
assert.ok(calls[0].includes("--include-archived"));
|
|
const text = await readFile(join(dataDir, "cloud/T-cloud.json"), "utf8");
|
|
assert.ok(!text.includes("private"));
|
|
assert.equal((await readCloud({ dataDir, run })).exported, 0);
|
|
});
|
|
test("failed exports do not advance checkpoints", async (t) => {
|
|
const dataDir = await dir(t);
|
|
let fail = true;
|
|
const run = async (args) => {
|
|
if (args[1] === "list")
|
|
return [{ id: "T-cloud", updated: usage.timestamp }];
|
|
if (fail) throw new Error("Offline");
|
|
return { id: "T-cloud", messages: [] };
|
|
};
|
|
assert.equal((await readCloud({ dataDir, run })).errors.length, 1);
|
|
fail = false;
|
|
assert.equal((await readCloud({ dataDir, run })).exported, 1);
|
|
});
|
|
test("a changed thread keeps its last good copy while its export fails or waits", async (t) => {
|
|
const dataDir = await dir(t);
|
|
let updated = usage.timestamp,
|
|
fail = false;
|
|
const run = async (args) => {
|
|
if (args[1] === "list") return [{ id: "T-cloud", updated }];
|
|
if (fail) throw new Error("Offline");
|
|
return { id: "T-cloud", messages: [] };
|
|
};
|
|
await readCloud({ dataDir, run });
|
|
updated = "2026-01-02T00:00:00Z";
|
|
fail = true;
|
|
const failed = await readCloud({ dataDir, run });
|
|
assert.equal(failed.errors.length, 1);
|
|
assert.equal(failed.threads.length, 1);
|
|
const waiting = await readCloud({ dataDir, run, maxExports: 0 });
|
|
assert.equal(waiting.remaining, 1);
|
|
assert.equal(waiting.threads.length, 1);
|
|
});
|
|
test("bounded exports resume on the next run", async (t) => {
|
|
const dataDir = await dir(t);
|
|
const run = async (args) =>
|
|
args[1] === "list"
|
|
? [
|
|
{ id: "T-one", updated: usage.timestamp },
|
|
{ id: "T-two", updated: usage.timestamp },
|
|
]
|
|
: { id: args[2], messages: [] };
|
|
assert.equal((await readCloud({ dataDir, run, maxExports: 1 })).remaining, 1);
|
|
assert.equal((await readCloud({ dataDir, run, maxExports: 1 })).remaining, 0);
|
|
});
|
|
test("a missing configured directory is visible; Amp's own may be absent", async (t) => {
|
|
const missing = [join(await dir(t), "missing")];
|
|
assert.equal((await readLocal(missing)).errors.length, 1);
|
|
assert.equal(
|
|
(await readLocal(missing, { required: false })).errors.length,
|
|
0,
|
|
);
|
|
});
|
|
test("projections from before sent times are exported again", async (t) => {
|
|
const dataDir = await dir(t);
|
|
const run = async (args) =>
|
|
args[1] === "list"
|
|
? [{ id: "T-cloud", updated: usage.timestamp }]
|
|
: { id: "T-cloud", messages: [] };
|
|
await readCloud({ dataDir, run });
|
|
await writeFile(
|
|
join(dataDir, "cloud-index.json"),
|
|
JSON.stringify({ version: 1, threads: { "T-cloud": usage.timestamp } }),
|
|
);
|
|
assert.equal((await readCloud({ dataDir, run })).exported, 1);
|
|
});
|
|
test("listing stops at the first page with nothing new, yet keeps every thread", async (t) => {
|
|
const dataDir = await dir(t);
|
|
let clock = Date.parse(usage.timestamp);
|
|
const ids = Array.from({ length: 250 }, (_, i) => `T-${1000 + i}`);
|
|
const updated = Object.fromEntries(ids.map((id) => [id, usage.timestamp]));
|
|
const lists = [];
|
|
const run = async (args) => {
|
|
if (args[1] !== "list") return { id: args[2], messages: [] };
|
|
const offset = Number(args[args.indexOf("--offset") + 1]);
|
|
lists.push(offset);
|
|
// Newest first, as Amp lists them.
|
|
return ids
|
|
.map((id) => ({ id, updated: updated[id] }))
|
|
.sort((a, b) => b.updated.localeCompare(a.updated))
|
|
.slice(offset, offset + 200);
|
|
};
|
|
const read = () =>
|
|
readCloud({ dataDir, run, maxExports: 300, now: () => clock });
|
|
assert.equal((await read()).threads.length, 250);
|
|
assert.deepEqual(lists.splice(0), [0, 200]);
|
|
const again = await read();
|
|
assert.deepEqual(lists.splice(0), [0]);
|
|
assert.equal(again.threads.length, 250);
|
|
assert.equal(again.exported, 0);
|
|
// A thread from the second page changes: it moves to the first.
|
|
updated["T-1249"] = "2026-01-02T00:00:00Z";
|
|
const changed = await read();
|
|
assert.deepEqual(lists.splice(0), [0, 200]);
|
|
assert.equal(changed.exported, 1);
|
|
assert.equal(changed.threads.length, 250);
|
|
// A day on, the whole list is read again.
|
|
clock += 86_400_000;
|
|
await read();
|
|
assert.deepEqual(lists.splice(0), [0, 200]);
|
|
});
|