82 lines · 4.6 KB · TypeScriptRaw
/** pnpm media:cap and retired:erase (scripts/account-verbs.ts): the token read from its file and never said, each verb's one request, its refusal in the account's words. */import { mkdtempSync, rmSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";import { afterAll, describe, expect, it } from "vitest";import { callUntilDone, callVerb, parseVerb, VerbRefusal } from "../account-verbs.ts";const dir = mkdtempSync(join(tmpdir(), "account-verbs-"));const TOKEN = "ctn_11111111-1111-4111-8111-111111111111_not-a-real-secret";writeFileSync(join(dir, "token"), `${TOKEN}\n`);writeFileSync(join(dir, "empty"), "\n");afterAll(() => rmSync(dir, { recursive: true, force: true }));const BASE = ["--base", "https://example.test/", "--token-file", "token"];describe("media:cap", () => { it("sends the bytes, or null for default", () => { expect(parseVerb("media:cap", [...BASE, "--bytes", "2199023255552"], dir)).toEqual({ base: "https://example.test", token: TOKEN, path: "/v1/admin/media/cap", body: { bytes: 2199023255552 } }); expect(parseVerb("media:cap", [...BASE, "--bytes", "default"], dir).body).toEqual({ bytes: null }); }); it("refuses a missing or unreadable value", () => { expect(() => parseVerb("media:cap", BASE, dir)).toThrow(/--bytes is needed/); expect(() => parseVerb("media:cap", [...BASE, "--bytes", "2TB"], dir)).toThrow(/whole number/); expect(() => parseVerb("media:cap", [...BASE, "--bytes", "1", "--yes"], dir)).toThrow(/no option --yes/); expect(() => parseVerb("media:cap", ["--base", "https://example.test", "--token-file", "empty", "--bytes", "1"], dir)).toThrow(/is empty/); });});describe("retired:erase", () => { it("only counts without --yes, and confirms with it", () => { expect(parseVerb("retired:erase", BASE, dir)).toMatchObject({ path: "/v1/admin/retired/erase", body: {} }); expect(parseVerb("retired:erase", [...BASE, "--yes"], dir).body).toEqual({ confirm: "erase" }); });});describe("erase", () => { it("sends each set's body, counting without --yes and confirming with it", () => { expect(parseVerb("erase", [...BASE, "--set", "tables", "--tables", "news_story, mood"], dir)).toMatchObject({ path: "/v1/admin/erase", body: { set: "tables", tables: ["news_story", "mood"] } }); expect(parseVerb("erase", [...BASE, "--set", "tables", "--tables", "mood", "--yes"], dir).body).toEqual({ set: "tables", tables: ["mood"], confirm: "erase" }); expect(parseVerb("erase", [...BASE, "--set", "unlinked_photos", "--ignore", "media.key"], dir).body).toEqual({ set: "unlinked_photos", ignore: ["media.key"] }); expect(parseVerb("erase", [...BASE, "--set", "old_prompts", "--days", "30", "--yes"], dir).body).toEqual({ set: "old_prompts", days: 30, confirm: "erase" }); expect(() => parseVerb("erase", [...BASE, "--set", "tables"], dir)).toThrow(/--tables is needed/); expect(() => parseVerb("erase", [...BASE, "--set", "everything"], dir)).toThrow(/--set is/); }); it("asks again until the account says done", async () => { let n = 0; const fetcher = (async () => Response.json({ phase: ++n < 3 ? "rows" : "done", done: n >= 3, calls: n })) as unknown as typeof fetch; const said: string[] = []; expect(await callUntilDone(parseVerb("erase", [...BASE, "--set", "old_prompts", "--yes"], dir), fetcher, (l) => said.push(l))).toMatchObject({ done: true, calls: 3 }); expect(said).toHaveLength(3); });});describe("the request", () => { const call = parseVerb("retired:erase", BASE, dir); it("is one POST bearing the token, answering the account's answer", async () => { const sent: { url: string; init: RequestInit | undefined }[] = []; const fetcher = (async (url: string, init?: RequestInit) => { sent.push({ url, init }); return Response.json({ erased: false, total: 3 }); }) as typeof fetch; expect(await callVerb(call, fetcher)).toEqual({ erased: false, total: 3 }); expect(sent).toHaveLength(1); expect(sent[0]!.url).toBe("https://example.test/v1/admin/retired/erase"); expect(new Headers(sent[0]!.init?.headers).get("authorization")).toBe(`Bearer ${TOKEN}`); }); it("says a refusal in the account's words, without the token", async () => { const fetcher = (async () => Response.json({ code: "admin_only", message: "this needs the admin token" }, { status: 403 })) as unknown as typeof fetch; const refused = await callVerb(call, fetcher).then( () => null, (error: unknown) => error as Error, ); expect(refused).toBeInstanceOf(VerbRefusal); expect(refused?.message).toContain("admin_only"); expect(refused?.message).not.toContain(TOKEN); });});