Cotton code
94 lines · 5.5 KB · TypeScriptRaw
// 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.<role>.uiColor"],    [String.raw`\bPalettes\s*\.`, "theme.colors"],    [String.raw`\bCottonFonts\s*\.`, "theme.type"],    [String.raw`(?<![A-Za-z])Metrics\s*\.`, "theme.scale"],    [String.raw`\bPaperMetrics\s*\.`, "theme.scale, theme.tilt(seed:)"],    [String.raw`\bShadows\s*\.`, "theme.texture.shadow"],    [String.raw`\bColor\s*\(\s*(red|white|hue)\s*:`, "a palette role"],    [String.raw`\bColor\s*\(\s*\.sRGB`, "a palette role"],    [      String.raw`\bColor\s*\.\s*(black|white|gray|grey|red|orange|yellow|green|mint|teal|cyan|blue|indigo|purple|pink|brown|primary|secondary)\b`,      "a palette role",    ],    [      String.raw`\.(foregroundStyle|foregroundColor|fill|stroke|strokeBorder|background|tint)\s*\(\s*\.(white|black|gray|grey|red|blue|green|orange|yellow|pink|purple|primary|secondary|tertiary)\b`,      "a palette role",    ],    [String.raw`\bFont\s*\.\s*(system|custom)\s*\(`, "theme.type"],    [String.raw`\.font\s*\(\s*\.(system|custom|body|caption2?|title[23]?|headline|footnote|subheadline|callout|largeTitle)\b`, "theme.type"],    [String.raw`\b(UIFont|NSFont)\s*\.`, "theme.type"],    [String.raw`\bPaperInk\b[^\n]*\.color\b`, "theme.print(_:)"],    [String.raw`\bPaperInk\s*\.\s*blend\b`, "theme.printBlend"],    [String.raw`\bTextInk\b[^\n]*\.color\b`, "theme.text(_:)"],    [String.raw`(?<![.\w])ink\.color\b`, "theme.print(_:) or theme.text(_:)"],    [String.raw`\bPaperPaint\s*\.`, "theme.printFrame, theme.printWindow"],    [String.raw`\bPaperDye\b[^\n]*\.fill\b`, "theme.sheet(_:)"],  ];  // RULE: no file under Faces/, Surfaces/PageStyles/, Paper/ or Cards/ reads a colour, font or metric constant (CottonColors, CottonFonts, Metrics, PaperMetrics, PaperPaint, a dye's fill, a Color or Font literal, a PaperInk's static colour) — every face takes its skin from the `\.theme` environment value, or a database's theme cannot reach it.  it("no face reads a skin constant", () => {    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([]);  });});