// A face reads its skin from the theme, and code draws in the theme the person // wears, never the one that ships. These once lived in CottonUITests/Theme // (ThemeScanGuardTests.swift, WornThemeGuardTests.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 here. import { describe, expect, it } from "vitest"; import { matches, swiftSources, trimSpaces, withoutComments } from "./helpers/swift-source-sweep"; // The faces (`Faces/`), the page styles (`Surfaces/PageStyles/`), the paper kit // (`Paper/`) and the cards are what every definition is drawn with, and a // business's theme is drawn by them or not at all (design note 26203e21, // section 6). A colour, a face or a metric read off one of the app's constant // tables there is a piece of Cotton's own look that no theme row can change. // Plain numbers in a layout are structure and are not swept. describe("ThemeScanGuardTests", () => { /** The folders whose files draw definitions. */ const swept = ["CottonUI/Faces/", "CottonUI/Surfaces/PageStyles/", "CottonUI/Paper/", "CottonUI/Cards/"]; /** Each way a skin constant can be read, with the theme's word for it. */ const refused: Array<[pattern: string, use: string]> = [ [String.raw`\bCottonColors\s*\.`, "theme.colors"], [String.raw`\bCottonUIColors\s*\.`, "theme.colors..uiColor"], [String.raw`\bPalettes\s*\.`, "theme.colors"], [String.raw`\bCottonFonts\s*\.`, "theme.type"], [String.raw`(? { const files = swiftSources().filter((file) => swept.some((folder) => file.name.startsWith(folder))); expect(files.length, "the sweep read no files — it would pass on nothing").toBeGreaterThan(0); const found: string[] = []; for (const { name, text } of files) { withoutComments(text) .split("\n") .forEach((line, index) => { const hit = refused.find(([pattern]) => matches(pattern, line).length > 0); if (hit) found.push(`${name}:${index + 1} reads a skin constant — use ${hit[1]}: ${trimSpaces(line)}`); }); } expect(found, found.join("\n")).toEqual([]); }); }); // Code draws in the theme the person wears, never the one that ships. A // database's theme arrives as a row and is set at the root (`themed(_:)`); every // face reads it from `\.theme`. A static that reads `Theme.paper`, or a // `theme: Theme = .paper` default, draws the shipped theme whatever the person // chose. Only the theme loader names the shipped themes. describe("WornThemeGuardTests", () => { /** The theme loader: the one place the shipped themes are named. */ const loader = [ "CottonPaper/Theme/Theme.swift", "CottonPaper/Theme/ThemeRow.swift", "CottonUI/Theme/ThemeModel.swift", "CottonPaper/Theme/ThemeDefaults.swift", "CottonPaper/Theme/ThemeLook.swift", ]; const shippedReads = [String.raw`\bTheme\.(?:paper|plain|standard)\b`, String.raw`:\s*Theme\s*=\s*\.(?:paper|plain)\b`, String.raw`:\s*ThemeScale\s*=\s*Theme\b`]; // RULE: no code outside the theme loader reads the shipped theme — no `Theme.paper`/`.plain`/`.standard`, no `theme: Theme = .paper` default — so every part of every screen draws in the theme the person wears and a theme edit reaches all of it. it("nothing outside the loader reads the shipped theme", () => { const found: string[] = []; for (const source of swiftSources()) { if (loader.includes(source.name)) continue; const code = withoutComments(source.text); for (const pattern of shippedReads) for (const hit of matches(pattern, code)) found.push(`${source.name}: ${hit}`); } expect(found, "take the worn theme from `\\.theme` or the caller").toEqual([]); }); });