/** * EVERY KIT NAMES A FACE THE APP DRAWS (Loom step 5). * * A card names the face that draws it by an open id (`"card": {"face": * "todo"}`). The faces the app draws are Loom's registry * (`apps/loom/faces.json`, generated from the face files) and, until each * moves into Loom, the ones CottonUI still draws (`apps/loom/faces-pending.json`). * The core and the Worker refuse any other id when a definition is authored; * this holds every kit in the repository to the same ids before either runs, * and keeps the pending list honest: a face that has moved is not pending. */ import { readdirSync, readFileSync, statSync } from "node:fs"; import { dirname, join, relative, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import { describe, expect, it } from "vitest"; import { cardsOf, faceProblem } from "../../apps/business/src/faces"; const REPO_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "../.."); const KITS = join(REPO_ROOT, "crates/cotton-core/kits"); const registry: { id: string }[] = JSON.parse(readFileSync(join(REPO_ROOT, "apps/loom/faces.json"), "utf8")); const pending: string[] = JSON.parse(readFileSync(join(REPO_ROOT, "apps/loom/faces-pending.json"), "utf8")); const moved = new Set(registry.map((row) => row.id)); const known = new Set([...moved, ...pending]); function jsonFiles(dir: string): string[] { return readdirSync(dir).flatMap((name) => { const path = join(dir, name); if (statSync(path).isDirectory()) return jsonFiles(path); return name.endsWith(".json") ? [path] : []; }); } describe("every kit names a face the app draws", () => { const named = jsonFiles(KITS).flatMap((path) => cardsOf(JSON.parse(readFileSync(path, "utf8")), relative(KITS, path))); /// RULE: Every card in every kit (crates/cotton-core/kits/**) names a face in apps/loom/faces.json or faces-pending.json: a kit never ships a card the phone cannot draw. it("holds every kit card to faces.json and faces-pending.json", () => { expect(named.length, "the kits carry cards").toBeGreaterThan(0); const unknown = named.filter((card) => typeof card.face !== "string" || !known.has(card.face)).map((card) => `${card.at}: ${JSON.stringify(card.face)}`); expect(unknown, "name a face the app draws, or add the face to Loom first").toEqual([]); }); it("checks ordered offers and refuses unknown, duplicate or missing base faces", () => { const doc = (offers: unknown) => ({ card: { face: "person.rolodex", offers } }); expect(faceProblem(doc(["person.rolodex", "person.passport"]))).toBeNull(); for (const offers of [["invented"], ["person.passport"], ["person.rolodex", "person.rolodex"], "person.passport"]) { expect(faceProblem(doc(offers))).not.toBeNull(); } expect(cardsOf(doc(["person.rolodex", "person.passport"])).map(c => c.face)).toEqual(["person.rolodex", "person.rolodex", "person.passport"]); }); it("validates the offers on every shipped card", () => { for (const path of jsonFiles(KITS)) expect(faceProblem(JSON.parse(readFileSync(path, "utf8"))), path).toBeNull(); }); /// RULE: faces-pending.json never names a face faces.json already has: a face that moved into Loom leaves the pending list (`pnpm gen:faces`). it("names no moved face as pending", () => { expect(pending.filter((id) => moved.has(id)), "run `pnpm gen:faces`").toEqual([]); expect(new Set(pending).size, "each pending face once").toBe(pending.length); }); });