Cotton code
53 lines · 3.5 KB · TypeScriptRaw
// Two rules about how the phone reads its definitions, which once lived in// CottonUITests/Definitions (NoBuiltinBranchTests.swift, NoSecondDecoderTests.swift)// and only ever read Swift as text — ported to vitest. The describe blocks keep// the Swift suites' names, so a comment that names one still finds its guard.// Reads apps/cotton/Sources and apps/cotton/App (lane pagedata owns these guards).import { readFileSync } from "node:fs";import { basename, join, relative } from "node:path";import { describe, expect, it } from "vitest";import { iosSources, matches, repoRoot, walk, withoutComments } from "./helpers/swift-source-sweep";describe("NoBuiltinBranchTests", () => {  // RULE: No CottonUI code reads a definition's `builtin` flag — a shipped tool and an imported one are the same thing to everything a person sees (owner, 12 Oct 2026); code that needs the data source asks for the table's `engine`, which the core refuses on any table a person defined.  it("no code branches on the builtin flag", () => {    const root = join(iosSources, "CottonUI");    const files = walk(root, "swift").filter((path) => basename(path) !== "TableDefinition.swift");    expect(files.length, `CottonUI's sources are at ${root}`).toBeGreaterThan(0);    const readers = files.filter((path) => matches(String.raw`\\?\.\s*builtin\b`, withoutComments(readFileSync(path, "utf8"))).length > 0).map((path) => basename(path));    expect(readers, "these read `builtin`; ask for `engine` where the source matters, or nothing").toEqual([]);  });});describe("NoSecondDecoderTests", () => {  /** The one file that reads the core's catalogue into the view types. */  const boundary = "CottonUI/Definitions/DefinitionCatalogue.swift";  // RULE: The core is the one decoder of the definition format, and `DefinitionCatalogue.swift` is the one place the app reads what the core handed over: no other Swift under Sources or App decodes a table or board definition (a `JSONDecoder` over `TableDefinition`, `BoardDefinition` or their generated storage, or a call of their `decode`), and no Swift carries a definition as a JSON literal to decode — two decoders of one format are two truths, and phone cut 273 crashed before its first screen on the second. A hit is deleted, never allowed.  it("no Swift decodes a definition outside the catalogue", () => {    const decodes = String.raw`\b(TableDefinition|BoardDefinition)\.decode\(|decode\(\s*\[?\s*(TableDefinition|BoardDefinition|DefinitionTableDefinition|DefinitionBoardDefinition)\b`;    const literal = String.raw`"columns"\s*:\s*\[`;    let read = 0;    let boundaryDecodes = 0;    const decoders: string[] = [];    const literals: string[] = [];    for (const folder of ["Sources", "App"]) {      const base = join(repoRoot, "apps/cotton", folder);      for (const path of walk(base, "swift")) {        read += 1;        const code = withoutComments(readFileSync(path, "utf8"));        const name = relative(base, path);        const found = matches(decodes, code).length;        if (name === boundary) boundaryDecodes = found;        else if (found > 0) decoders.push(`${folder}/${name}`);        if (matches(literal, code).length > 0) literals.push(`${folder}/${name}`);      }    }    expect(decoders, "these decode a definition outside the catalogue boundary").toEqual([]);    expect(literals, "these carry a definition as a JSON literal").toEqual([]);    expect(read, "the walk found the app's Swift").toBeGreaterThan(100);    expect(boundaryDecodes, "the boundary is where the catalogue is read").toBeGreaterThan(0);  });});