Cotton code
56 lines · 3.4 KB · TypeScriptRaw
/** pnpm film:reshape (scripts/film-reshape.ts): the owner token read from its file and never said, the act sent once, its answer or its refusal printed. */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 { ReshapeRefusal, reshapeFilms, parseArgs } from "../film-reshape.ts";const dir = mkdtempSync(join(tmpdir(), "film-reshape-"));const TOKEN = "ctn_11111111-1111-4111-8111-111111111111_not-a-real-secret";writeFileSync(join(dir, "token"), `${TOKEN}\n`);writeFileSync(join(dir, "empty"), "\n");writeFileSync(join(dir, "device"), "ctd_invented-device-credential\n");afterAll(() => rmSync(dir, { recursive: true, force: true }));describe("film:reshape", () => {  it("reads the token from its file, relative to where pnpm was run, and the base without its slash", () => {    expect(parseArgs(["--base", "http://127.0.0.1:8787/b/dev/", "--token-file", "token"], dir)).toEqual({ base: "http://127.0.0.1:8787/b/dev", token: TOKEN });  });  it("refuses a missing flag, an unknown one, and an empty file, never saying the token", () => {    expect(() => parseArgs(["--base", "https://cottondb.com"], dir)).toThrow(ReshapeRefusal);    expect(() => parseArgs(["--base", "https://cottondb.com", "--token-file", "token", "--force", "yes"], dir)).toThrow(/no option --force/);    expect(() => parseArgs(["--base", "cottondb.com", "--token-file", "token"], dir)).toThrow(/http/);    expect(() => parseArgs(["--base", "https://cottondb.com", "--token-file", "empty"], dir)).toThrow(/is empty/);    // Token scopes (24 Nov 2026): this Mac's device credential is taken as the act's bearer.    expect(parseArgs(["--base", "https://cottondb.com", "--token-file", "device"], dir).token).toBe("ctd_invented-device-credential");  });  it("sends reshape_films once with the owner token and answers what the object moved and laid", async () => {    const sent: { url: string; init: RequestInit | undefined }[] = [];    const answer = { version: 9, changed: [], mapped: { status: 2, review: 1, added_on: 3 }, laid: ["film", "cinemas"], removed: ["films"] };    const fetcher = (async (url: string, init?: RequestInit) => {      sent.push({ url, init });      return Response.json(answer);    }) as typeof fetch;    const done = await reshapeFilms({ base: "https://cottondb.com", token: TOKEN }, fetcher);    expect(done).toEqual({ mapped: answer.mapped, laid: answer.laid, removed: answer.removed });    expect(sent).toHaveLength(1);    expect(sent[0]!.url).toBe("https://cottondb.com/v1/act");    expect(new Headers(sent[0]!.init?.headers).get("authorization")).toBe(`Bearer ${TOKEN}`);    expect(JSON.parse(String(sent[0]!.init?.body))).toEqual({ act: "reshape_films", args: {} });  });  it("says the object's refusal in its own words, without the token", async () => {    const fetcher = (async () => Response.json({ code: "no_films", message: "this database has no film table" }, { status: 404 })) as unknown as typeof fetch;    const refused = await reshapeFilms({ base: "https://cottondb.com", token: TOKEN }, fetcher).then(      () => null,      (error: unknown) => error as Error,    );    expect(refused).toBeInstanceOf(ReshapeRefusal);    if (!refused) return;    expect(refused.message).toContain("no_films");    expect(refused.message).not.toContain(TOKEN);  });});