// THE GUARD THAT MAKES THE LOGO ONE SOURCE. // // The constellation-C exists in two forms and they have to be one mark: // // * the ART — `assets/brand/logo-master.png`, amber french knots // joined by steel-blue thread. Every RASTER form derives from it (the // phone's app icon, the Mac's `.icns`, the Android densities), because the // thread's texture is what makes it that picture. // * the GEOMETRY — `cotton-core`'s `design/mark.rs`, emitted as `mark.json`. // Every VECTOR form derives from THAT (the phone's launch screen, the Mac's // menu-bar icon, the OAuth pages, the site's SVGs), because a 22-point // menu-bar icon downsampled from a photograph of embroidery is a smudge. // Until step 5i a TypeScript copy of the numbers lived in `packages/core` // and this guard held the two equal; that copy went with the package, so // `mark.rs` is the ONE declaration, `mark_tests.rs` pins `mark.json` to it // and checks the SVG it draws, and this guard reads the JSON. // // Two derivations from two files is two sources, and two sources drift — which // is exactly what happened: the app icon worked nine knots, the phone's splash // and the site drew a seven-star polyline, and the OAuth page inlined a base64 // PNG of a third thing. The owner saw all three on 2026-09-10. // // So this test OPENS THE ART and checks the numbers against it: every declared // knot sits on an amber knot in the master, every declared thread runs over // inked pixels of it, and the master holds no knot the geometry has forgotten. // Change the art and this goes red until the numbers follow; change the numbers // and it goes red until the art does. // // TWO PATHS BELONG TO OTHER LANES and both reads fail LOUDLY before any // comparison (AGENTS.md's rule for a guard that reads a file by path): // `crates/cotton-core/src/design/mark.json` is the Rust core's, written by // `COTTON_DESIGN_WRITE_JSON=1 cargo test -p cotton-core design::mark`; // `assets/brand/logo-master.png` is the brand assets'. A regex or a comparison // over an empty read is how a moved file turns a guard green. import { readFileSync } from "node:fs"; import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; import sharp from "sharp"; import { beforeAll, describe, expect, it } from "vitest"; const ROOT = join(dirname(fileURLToPath(import.meta.url)), "..", ".."); const JSON_PATH = join( ROOT, "crates/cotton-core/src/design/mark.json", ); // The brand master lives at the ROOT `assets/brand/`: it is read by the Mac's // icon script and by this guard, and a master two apps share does not belong // inside one app. const MASTER_PATH = join(ROOT, "assets/brand/logo-master.png"); interface EmittedMark { viewWidth: number; viewHeight: number; knotRadius: number; threadWidth: number; knotInk: string; threadInk: string; ground: string; points: { x: number; y: number; kind: "knot" | "turn" }[]; threads: [number, number][]; } function emitted(): EmittedMark { let text: string | null = null; try { text = readFileSync(JSON_PATH, "utf8"); } catch { text = null; } expect( text, `could not read ${JSON_PATH} — did it move? That path is the Rust core lane's; ` + "it is written by `COTTON_DESIGN_WRITE_JSON=1 cargo test -p cotton-core design::mark`. " + "Grep it repo-wide (__tests__ and every open worktree included) before changing this guard.", ).not.toBeNull(); const parsed = JSON.parse(text as string) as EmittedMark; expect(parsed.points.length, `${JSON_PATH} declares no points`).toBeGreaterThan(4); return parsed; } // The numbers every test below works in — read from the emitted JSON once, // loudly, before any test runs. let MARK_VIEW_WIDTH = 0; let MARK_VIEW_HEIGHT = 0; let MARK_KNOT_RADIUS = 0; let MARK_THREAD_WIDTH = 0; let MARK_POINTS: EmittedMark["points"] = []; let MARK_THREADS: EmittedMark["threads"] = []; beforeAll(() => { const json = emitted(); MARK_VIEW_WIDTH = json.viewWidth; MARK_VIEW_HEIGHT = json.viewHeight; MARK_KNOT_RADIUS = json.knotRadius; MARK_THREAD_WIDTH = json.threadWidth; MARK_POINTS = json.points; MARK_THREADS = json.threads; }); /** The master's pixels, with an `alpha`/`isAmber` reader over them. */ async function master(): Promise<{ width: number; height: number; alphaAt: (x: number, y: number) => number; isAmberAt: (x: number, y: number) => boolean; }> { let bytes: Buffer | null = null; try { bytes = readFileSync(MASTER_PATH); } catch { bytes = null; } expect( bytes, `could not read ${MASTER_PATH} — did the logo master move? That path is the brand ` + "assets' lane; every icon in the repo derives from it. Grep it repo-wide before " + "changing this guard.", ).not.toBeNull(); const { data, info } = await sharp(bytes as Buffer) .ensureAlpha() .raw() .toBuffer({ resolveWithObject: true }); const { width, height, channels } = info; const at = (x: number, y: number) => (Math.round(y) * width + Math.round(x)) * channels; const inside = (x: number, y: number) => x >= 0 && y >= 0 && x < width && y < height; return { width, height, alphaAt: (x, y) => (inside(x, y) ? data[at(x, y) + 3] : 0), // Amber, not blue: red well above blue, green between them. The master's // knots are shaded embroidery, so this is a family test and not a hex. isAmberAt: (x, y) => { if (!inside(x, y)) return false; const i = at(x, y); const [r, g, b, a] = [data[i], data[i + 1], data[i + 2], data[i + 3]]; return a > 128 && r > 120 && r > b + 50 && g > b + 10 && g < r; }, }; } describe("the emitted geometry and the master art", () => { it("the master art is the grid the coordinates are written in", async () => { // If the master is re-exported at another size the numbers below stop // meaning pixels, and every comparison in this file silently loosens. const art = await master(); const json = emitted(); expect(art.width).toBe(json.viewWidth); expect(art.height).toBe(json.viewHeight); }); }); describe("the geometry against the app icon's own art", () => { it("every declared knot sits on an amber knot in the master", async () => { const art = await master(); for (const [index, point] of MARK_POINTS.entries()) { if (point.kind !== "knot") continue; expect( art.isAmberAt(point.x, point.y), `point ${index} (${point.x}, ${point.y}) is declared a knot but the master has no ` + "amber there — the geometry and the art have come apart", ).toBe(true); } }); it("a declared turn carries thread and NOT a knot", async () => { // The three turns are the detail a lookalike always gets wrong: draw a disc // on one and the logo grows three dots the icon has not got. const art = await master(); for (const [index, point] of MARK_POINTS.entries()) { if (point.kind !== "turn") continue; // The apex sits ON the left edge, so look a little way inside it. const x = Math.min(Math.max(point.x, 2), art.width - 3); const y = Math.min(Math.max(point.y, 2), art.height - 3); expect(art.alphaAt(x, y), `point ${index} is a turn but the master is empty there`) .toBeGreaterThan(100); expect( art.isAmberAt(x, y), `point ${index} is declared a TURN but the master has a knot there`, ).toBe(false); } }); it("the master holds no knot the geometry has forgotten", async () => { // The other direction, and the one that catches a knot dropped from the // list: count the amber blobs in the art and match them to declared knots. const art = await master(); const seen = new Uint8Array(art.width * art.height); const centres: { x: number; y: number; size: number }[] = []; for (let y = 0; y < art.height; y += 1) { for (let x = 0; x < art.width; x += 1) { const start = y * art.width + x; if (seen[start] || !art.isAmberAt(x, y)) { seen[start] = 1; continue; } const stack = [start]; seen[start] = 1; let count = 0; let sumX = 0; let sumY = 0; while (stack.length) { const at = stack.pop() as number; const px = at % art.width; const py = (at - px) / art.width; count += 1; sumX += px; sumY += py; for (const [dx, dy] of [ [1, 0], [-1, 0], [0, 1], [0, -1], ]) { const nx = px + dx; const ny = py + dy; if (nx < 0 || ny < 0 || nx >= art.width || ny >= art.height) continue; const next = ny * art.width + nx; if (seen[next]) continue; seen[next] = 1; if (art.isAmberAt(nx, ny)) stack.push(next); } } // A blob smaller than a fifth of a knot's disc is antialiasing. if (count > Math.PI * MARK_KNOT_RADIUS ** 2 * 0.2) { centres.push({ x: sumX / count, y: sumY / count, size: count }); } } } const declared = MARK_POINTS.filter((point) => point.kind === "knot"); expect( centres.length, `the master works ${centres.length} knots and the geometry declares ${declared.length}`, ).toBe(declared.length); for (const centre of centres) { const nearest = Math.min( ...declared.map((point) => Math.hypot(point.x - centre.x, point.y - centre.y)), ); expect( nearest, `the master has a knot at (${centre.x.toFixed(1)}, ${centre.y.toFixed(1)}) that no ` + "declared point is on", ).toBeLessThan(2); } }); it("every declared thread runs over ink the whole way", async () => { const art = await master(); for (const [from, to] of MARK_THREADS) { const a = MARK_POINTS[from]; const b = MARK_POINTS[to]; const steps = Math.max(2, Math.round(Math.hypot(b.x - a.x, b.y - a.y))); let tested = 0; let inked = 0; for (let step = 0; step <= steps; step += 1) { const t = step / steps; const x = a.x + (b.x - a.x) * t; const y = a.y + (b.y - a.y) * t; // Skip the knot discs: they are ink either way and prove nothing about // the run between them. const nearKnot = MARK_POINTS.some( (point) => point.kind === "knot" && Math.hypot(point.x - x, point.y - y) < MARK_KNOT_RADIUS * 1.3, ); if (nearKnot) continue; tested += 1; let hit = false; for (let dx = -2; dx <= 2 && !hit; dx += 1) { for (let dy = -2; dy <= 2 && !hit; dy += 1) { if (art.alphaAt(x + dx, y + dy) > 100) hit = true; } } if (hit) inked += 1; } if (tested === 0) continue; expect( inked / tested, `the thread ${from}-${to} is only ${((inked / tested) * 100).toFixed(0)}% over ink in ` + "the master — the geometry and the art have come apart", ).toBeGreaterThan(0.97); } }); // RULE: The logo is the constellation mark, derived from one source, and every part of it is accounted for. it("nothing in the master is left unexplained by the threads and the knots", async () => { // Without this the geometry could quietly LOSE a thread: every declared one // would still be over ink, and a whole stroke of the icon would be missing // from every vector surface. const art = await master(); const half = MARK_THREAD_WIDTH / 2 + 3; const near = (x: number, y: number) => { for (const point of MARK_POINTS) { if (point.kind !== "knot") continue; if (Math.hypot(point.x - x, point.y - y) < MARK_KNOT_RADIUS + 3) return true; } for (const [from, to] of MARK_THREADS) { const a = MARK_POINTS[from]; const b = MARK_POINTS[to]; const dx = b.x - a.x; const dy = b.y - a.y; const length = dx * dx + dy * dy; const t = length ? Math.max(0, Math.min(1, ((x - a.x) * dx + (y - a.y) * dy) / length)) : 0; if (Math.hypot(x - (a.x + dx * t), y - (a.y + dy * t)) < half) return true; } return false; }; let ink = 0; let stray = 0; for (let y = 0; y < art.height; y += 2) { for (let x = 0; x < art.width; x += 2) { if (art.alphaAt(x, y) <= 100) continue; ink += 1; if (!near(x, y)) stray += 1; } } expect(ink, "the master read as empty").toBeGreaterThan(1000); // A few per cent is the round joins and the embroidery's fuzz; a MISSING // stroke is tens of per cent. expect( stray / ink, `${((stray / ink) * 100).toFixed(1)}% of the master's ink is not on any declared thread ` + "or knot — the geometry is missing part of the drawing", ).toBeLessThan(0.05); }); });