Cotton code
105 lines · 6.8 KB · TypeScriptRaw
// Rules about what a surface may read, write and draw, swept as text. These// once lived in CottonUITests/Surfaces (ReadErrorWordsSweepTests.swift,// NoAppWritesHomeTests.swift, NoMachineryHeadTests.swift, OneLayoutTests.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.import { describe, expect, it } from "vitest";import { matches, mustRead, swiftSources, trimSpaces, withoutComments } from "./helpers/swift-source-sweep";describe("ReadErrorWordsSweepTests", () => {  /** An error's own words: a refusal's message, a description, an interpolated error. They carry an address and a resolver's text. */  const ownWords = String.raw`localizedDescription|String\(describing:\s*error|\\\(error\)|CottonCallError\)\?\.message|\berror\.message\b`;  /** Files whose every caught error is a read's. */  const readFiles = [    "apps/cotton/Sources/CottonUI/Surfaces/RecordListModel.swift",    "apps/cotton/Sources/CottonUI/Scaffold/ScreenRead.swift",    "apps/cotton/Sources/CottonUI/Surfaces/Board/BoardNotices.swift",  ];  /** Files that also write: there, what a READ's refusal is set to. (Home's read says the core's line: it sets no refusal of its own.) */  const readRefusals: Record<string, string[]> = {    "apps/cotton/Sources/CottonUI/Surfaces/Databases/DatabasesModel.swift": ["readRefusal", "toolkitsRefusal"],  };  const offending = (line: string): boolean => !line.includes("Log.") && !line.includes("Reporting") && matches(ownWords, line).length > 0;  // RULE: no raw transport or error string reaches a page from a read (walker 294 row G, 17 Oct 2026): in the read paths — the list, the page, a screen's read, Home's read and its notices, the databases' list and toolkits reads — an error's `localizedDescription`, `String(describing: error)`, `"\(error)"` or `CottonCallError` message reaches nothing but `Log.` or `Reporting`; `CouldNotRead.swift` is the one place a caught read error becomes visible text, as a definition's line, and Home shows no toast of its own over the fan.  it("no read path draws an error's own words", () => {    const found: string[] = [];    for (const path of readFiles) {      const code = withoutComments(mustRead(path, "reads"));      for (const line of code.split("\n").filter(Boolean)) if (offending(line)) found.push(`${path}: ${trimSpaces(line)}`);    }    for (const [path, names] of Object.entries(readRefusals)) {      const code = withoutComments(mustRead(path, "reads"));      for (const name of names) {        const sets = matches(String.raw`\b${name}\s*=[^\n]*`, code);        expect(sets.length, `${path} no longer sets ${name} — did it move?`).toBeGreaterThan(0);        for (const line of sets) if (offending(line)) found.push(`${path}: ${line}`);      }    }    expect(found, "a read's error words reach a page; say CouldNotRead.line(for:) instead").toEqual([]);    const home = withoutComments(mustRead("apps/cotton/Sources/CottonUI/Surfaces/Board/HomeModel.swift", "home"));    expect(home.includes("could not be read just now"), "no toast of Home's own over the fan").toBe(false);    const one = withoutComments(mustRead("apps/cotton/Sources/CottonUI/Surfaces/CouldNotRead.swift", "reads"));    expect(one.includes("static func line(for error: Error"), "the one way a read error becomes text").toBe(true);  });});// The app once edited itself: Home's + opened "Add a tile", a tile's context// menu took it off Home, and the database's Name and colour row sent// `databases.rename` / `databases.recolour`. The owner ruled them out (16 Oct// 2026): card order, hidden tools, the Home arrangement and a database's look// are set over MCP only, and the app reads them.describe("NoAppWritesHomeTests", () => {  /** The method strings that write a board, the Home arrangement or a database's look, and the names of the controls that once sent them. */  const forbidden = ["settings.homeArrangement", "databases.rename", "databases.recolour", "headAdd", "HomeAddTileSheet", "writeTiles"];  // RULE: The app never edits itself (owner, 16 Oct 2026 17:50 IST): it has no code path that writes a board, the Home arrangement or a database's look — order, hidden tools and look are set by MCP only, so what is stored is MCP's by construction.  it("no code path writes Home or a database's look", () => {    const swept = swiftSources().filter(({ name }) => name.startsWith("CottonUI/") || name.startsWith("CottonKit/"));    expect(swept.some(({ name }) => name.startsWith("CottonUI/Surfaces/")), "the sweep found no CottonUI source").toBe(true);    expect(swept.some(({ name }) => name.startsWith("CottonKit/")), "the sweep found no CottonKit source").toBe(true);    const found: string[] = [];    for (const { name, text } of swept) {      const code = withoutComments(text);      for (const word of forbidden) if (code.includes(word)) found.push(`${name}: ${word}`);    }    expect(found, "these name a writer of what MCP alone sets").toEqual([]);  });});// Machinery never heads a list (architect's triage of walker291, 17 Oct 2026):// Notes headed its list with "the last sample was refused: name the database// (since …)". The refusal is a Feedback row and is read there.describe("NoMachineryHeadTests", () => {  // GUARD: no list reads or draws a refused write at its head.  // RULE: machinery never heads a list — a refused write is an open Feedback row, read in Feedback, never a line over a table's slips (walker291: Notes' head read "the last sample was refused: name the database (since …)").  it("no list is headed by a refused write", () => {    const sources = swiftSources();    expect(sources.length, "the sources were read").toBeGreaterThan(0);    const found: string[] = [];    for (const { name, text } of sources) {      const code = withoutComments(text);      if (code.includes("CottonMethod.recordsHead")) found.push(`${name} reads records.head`);      if (code.includes("RecordListHead(")) found.push(`${name} draws a refused write over a list`);    }    expect(found).toEqual([]);  });});// A list draws the one layout its definition names (owner, 15 Oct 2026).describe("OneLayoutTests", () => {  // RULE: A list's layout is read only from its definition and nothing on the device stores one (owner, 15 Oct 2026: "a tool shows one layout, chosen by its definition; the person never switches"): a remembered layout would let one phone draw a list its definition does not say, and the toggle that wrote it is gone.  it("no device store holds a layout", () => {    const storing = /(UserDefaults|@AppStorage|@SceneStorage)[^\n]*layout|layout[^\n]*(UserDefaults|@AppStorage|@SceneStorage)/i;    const found = swiftSources()      .filter(({ text }) => storing.test(withoutComments(text)))      .map(({ name }) => name);    expect(found, "these keep a layout on the device").toEqual([]);  });});