Cotton code
112 lines · 4.1 KB · TypeScriptRaw
/** * Every colour the phone declares must survive as a COLORSET WITH EXACTLY ONE * APPEARANCE. * * An asset catalogue is JSON on disk, and a `.colorset/Contents.json` that * carries no colours fails silently: `Color("accent", bundle: .module)` * resolves, the build is green, and the screen quietly wears the system tint. * Xcode emits no warning. * * **And a SECOND appearance is a defect of its own now.** The app is * light-only — no theme choice, no dark mode, no system-appearance switch — so * a `luminosity: dark` entry is a colour nothing can ever resolve to, shipped * in every build, and the first screen to acquire one would be repainted by a * phone in dark mode with nothing in the source to say why. * * So this guard reads the catalogue the app ships, and asserts three things * per colorset: it parses, it declares exactly one colour, and that colour * names no appearance. * * THE PATH THIS READS BELONGS TO ANOTHER LANE — `apps/cotton/` is the * phone's — so the read fails LOUDLY, naming the path, before any assertion. * A sweep over a directory that moved is a guard that passes over nothing, * which is the failure mode this whole file exists to prevent. */import { existsSync, readdirSync, readFileSync } from "node:fs";import { dirname, join, resolve } from "node:path";import { fileURLToPath } from "node:url";import { describe, expect, it } from "vitest";const REPO_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "../..");/** The phone's generated palette catalogue. `cotton-gen` writes it. */const CATALOGUE = join(  REPO_ROOT,  "apps",  "loom",  "Sources",  "CottonPaper",  "Generated",  "Palette.xcassets",);interface ColorsetEntry {  appearances?: { appearance: string; value: string }[];  color?: { "color-space"?: string; components?: Record<string, string> };  idiom?: string;}interface Colorset {  colors?: ColorsetEntry[];}function colorsets(): { name: string; path: string }[] {  return readdirSync(CATALOGUE, { withFileTypes: true })    .filter((entry) => entry.isDirectory() && entry.name.endsWith(".colorset"))    .map((entry) => ({      name: entry.name.replace(/\.colorset$/, ""),      path: join(CATALOGUE, entry.name, "Contents.json"),    }));}describe("the phone's colour catalogue", () => {  it("is where this guard thinks it is", () => {    // Not a formality. If `cotton-gen`'s write target moves and this file is    // not moved with it, every assertion below runs over an empty list and    // reports success — the exact shape AGENTS.md's by-path-guard rule names.    expect(      existsSync(CATALOGUE),      `${CATALOGUE} is missing — the phone lane owns this path (cotton-gen ` +        `writes it, see crates/cotton-gen/src/files.rs). Move this guard with it.`,    ).toBe(true);  });  it("holds more than a handful of colorsets, so a truncated write is loud", () => {    expect(colorsets().length).toBeGreaterThanOrEqual(10);  });  for (const set of colorsets()) {    it(`${set.name}: declares ONE colour and names no appearance`, () => {      const raw = readFileSync(set.path, "utf8");      expect(raw.length, `${set.path} is empty`).toBeGreaterThan(0);      const parsed = JSON.parse(raw) as Colorset;      const entries = parsed.colors ?? [];      // An empty `colors` array IS the v4-shape defect, transposed: a colorset      // that resolves and paints nothing.      expect(        entries.length,        `${set.name}.colorset declares no colours — an EMPTY colorset builds ` +          `green and wears the system tint`,      ).toBeGreaterThan(0);      expect(        entries.length,        `${set.name}.colorset declares ${entries.length} colours — the app is ` +          `light-only, so a colorset carries exactly one`,      ).toBe(1);      const base = entries[0];      expect(        base?.color?.components,        `${set.name}.colorset has no colour with components`,      ).toBeTruthy();      expect(        base?.appearances ?? [],        `${set.name}.colorset names an appearance — the app is light-only and ` +          `nothing can resolve to a second value`,      ).toEqual([]);    });  }});