import { existsSync, readFileSync, readdirSync } from"node:fs";import { expect, it } from"vitest";const read = (path: string) => readFileSync(path, "utf8");// Tools are records only (72nd amendment, 21 Oct 2026): the shipped table// definitions are the starter kit's, one `.cottontool` package per tool// (the personal kit's tools).const kit = "crates/cotton-core/kits/personal/tools";// A package of views alone (Artifacts, over Documents' table) has no tables// folder and contributes no table definition.const definitions = readdirSync(kit, { withFileTypes: true }) .filter(e => e.isDirectory() && existsSync(`${kit}/${e.name}/manifest.json`) && existsSync(`${kit}/${e.name}/tables`)) .flatMap(e => readdirSync(`${kit}/${e.name}/tables`).filter(p => p.endsWith(".json")).map(p => JSON.parse(read(`${kit}/${e.name}/tables/${p}`))));const boards = readdirSync("crates/cotton-core/kits/shell/boards").filter(p => p.endsWith(".json")).map(p => JSON.parse(read(`crates/cotton-core/kits/shell/boards/${p}`)));const routes = [...read("crates/cotton-ffi/src/phone/routes.rs").matchAll(/^ "(\/[^"\n]*)",$/gm)].map(m => m[1]);// Behaviour screens may use a path different from their definition's tool path.// The owner must still exist: deleting a definition removes its whole family.const families: Record<string, string> = {"/projects": "project", "/repos": "repository", "/sessions": "session","/memory": "memory", "/voice-words": "voice_word", "/placements": "placement_proposal","/imports": "import", "/calls": "call",};// Fixed application shell: boot/navigation, global activity/search/capture,// admission, legal/help and the generic definition and tool-board interpreters.const shell = newSet(["/", "/home", "/about", "/about/help", "/about/whats-new", "/activity", "/briefing","/capture", "/database", "/decide", "/device", "/device/[page]", "/history/[kind]/[id]","/keyboard-take", "/legal/privacy", "/legal/terms", "/lists/[table]", "/lists/[table]/[id]","/me", "/rejected", "/search", "/settings/join-request/[id]", "/take", "/tools", "/tools/board/[id]",]);function namedPaths(value: unknown): string[] {if (Array.isArray(value)) return value.flatMap(namedPaths);if (!value || typeof value !== "object") return [];returnObject.entries(value).flatMap(([key, child]) => ["route", "record_route", "opens"].includes(key) && typeof child === "string" && child.startsWith("/") ? [child] : namedPaths(child));}function matches(pattern: string, path: string): boolean {const a = pattern.split("/"), b = path.split("?")[0].split("/");return a.length === b.length && a.every((p, i) => p === b[i] || /^\[[^\]]+\]$/.test(p));}// RULE: Every definition or board destination exists, and every app route belongs to a live definition, board or fixed shell; deleting a definition must not leave a screen that can only fail to open.it("keeps the route table and its owners in both directions", () => { expect(routes.length).toBeGreaterThan(0);const named = [...definitions, ...boards].flatMap(namedPaths); expect(named.length).toBeGreaterThan(0); expect(named.filter(path => !routes.some(route => matches(route, path)))).toEqual([]);// The generic interpreter exists even after a table is deleted. Its// literal destinations must name a live table, rather than merely parse.const tableTargets = named.map(path => path.match(/^\/lists\/([^/?]+)/)?.[1]).filter((id): id is string => id !== undefined); expect(tableTargets.filter(id => !id.startsWith("[") && !definitions.some(d => d.id === id))).toEqual([]);const swift = read("apps/cotton/Sources/CottonMailbox/Route.swift");const table = swift.split("public static let allPatterns: [String] = [")[1].split("\n ]", 1)[0];const swiftRoutes = [...table.matchAll(/^ "(\/[^"\n]*)",$/gm)].map(m => m[1]); expect(newSet(swiftRoutes)).toEqual(newSet(routes));});