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.
105 lines
2.9 KiB
JavaScript
105 lines
2.9 KiB
JavaScript
import { request } from "node:http";
|
|
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { mkdtemp, rm } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { tmpdir } from "node:os";
|
|
import { startServer } from "../lib/server.mjs";
|
|
|
|
test("loopback service protects every API and accepts authenticated sandbox requests", async (t) => {
|
|
const dir = await mkdtemp(join(tmpdir(), "amp-service-"));
|
|
let scanned = 0;
|
|
const fake = {
|
|
view: () => ({ phase: "idle" }),
|
|
run: async () => {
|
|
scanned++;
|
|
},
|
|
close: async () => {},
|
|
};
|
|
const s = await startServer({
|
|
port: 0,
|
|
dataDir: dir,
|
|
configDir: dir,
|
|
collector: fake,
|
|
});
|
|
t.after(async () => {
|
|
await s.close();
|
|
await rm(dir, { recursive: true, force: true });
|
|
});
|
|
const base = `http://127.0.0.1:${s.port}`;
|
|
// Only a navigation reads the page that carries the token; Node's fetch
|
|
// always says cors.
|
|
const page = (mode) =>
|
|
new Promise((resolve, reject) => {
|
|
const req = request(
|
|
base,
|
|
{ headers: { "sec-fetch-mode": mode } },
|
|
(res) => {
|
|
let body = "";
|
|
res.on("data", (c) => (body += c));
|
|
res.on("end", () =>
|
|
resolve({ status: res.statusCode, body, headers: res.headers }),
|
|
);
|
|
},
|
|
);
|
|
req.on("error", reject);
|
|
req.end();
|
|
});
|
|
assert.equal((await page("cors")).status, 403);
|
|
const root = await page("navigate");
|
|
assert.equal(root.headers["access-control-allow-origin"], undefined);
|
|
const token = root.body.match(
|
|
/name="service-token" content="([a-f0-9]+)"/,
|
|
)[1];
|
|
const denied = await fetch(base + "/api/status");
|
|
assert.equal(denied.status, 403);
|
|
assert.equal((await denied.json()).code, "reconnect");
|
|
assert.equal(
|
|
(
|
|
await fetch(base + "/api/status", {
|
|
headers: { "x-amp-usage-token": token, origin: "https://evil.test" },
|
|
})
|
|
).status,
|
|
403,
|
|
);
|
|
const badHost = await new Promise((resolve, reject) => {
|
|
const req = request(
|
|
base + "/api/status",
|
|
{ headers: { "x-amp-usage-token": token, Host: "evil.test" } },
|
|
(res) => {
|
|
res.resume();
|
|
resolve(res.statusCode);
|
|
},
|
|
);
|
|
req.on("error", reject);
|
|
req.end();
|
|
});
|
|
assert.equal(badHost, 403);
|
|
const response = await fetch(base + "/api/refresh", {
|
|
method: "POST",
|
|
headers: {
|
|
"x-amp-usage-token": token,
|
|
origin: "null",
|
|
"content-type": "application/json",
|
|
},
|
|
body: "{}",
|
|
});
|
|
assert.equal(response.status, 202);
|
|
assert.equal(response.headers.get("access-control-allow-origin"), "null");
|
|
assert.equal(scanned, 1);
|
|
assert.equal(
|
|
(
|
|
await fetch(base + "/api/refresh", {
|
|
method: "POST",
|
|
headers: {
|
|
"x-amp-usage-token": token,
|
|
"content-type": "application/json",
|
|
},
|
|
body: "{",
|
|
})
|
|
).status,
|
|
400,
|
|
);
|
|
assert.equal((await fetch(base + "/unknown")).status, 404);
|
|
});
|