Cotton code
73 lines · 4.6 KB · TypeScriptRaw
import { readFileSync } from "node:fs";import { describe, expect, it } from "vitest";import { actLabelFault, cardTextFault, NAMED_ACTS, parseCardGeometry, type Theme } from "../gen-cards/card-label";const cardSource = readFileSync(new URL("../../apps/cotton/Sources/CottonUI/Cards/CardStock.swift", import.meta.url), "utf8");const themes: Theme[] = ["paper", "plain"].map((name) =>  JSON.parse(readFileSync(new URL(`../../crates/cotton-core/kits/shell/themes/${name}.json`, import.meta.url), "utf8")),);// RULE: Card labels wrap at whole words into at most two lines using the app's geometry and theme font; counting words rejects ordinary acts that fit and admits long labels that overflow.describe("card label wrapping", () => {  it("accepts acts with several words when they fit two lines", () => {    for (const label of ["Make the link", "Send a report", "Serve from this Mac", "Re-key this device", "Hand over to…"]) {      expect(actLabelFault(label, themes)).toBeNull();    }  });  // RULE: "Hand over" is an imperative verb, so ownership handover must fit  // the same card vocabulary as other acts without rewriting its definition.  it("accepts hand over as an act verb", () => {    expect(actLabelFault("Hand over to…", themes)).toBeNull();  });  it("requires a verb at the start of an act", () => {    expect(actLabelFault("Report send", themes)).toBeNull(); // report is also a verb    expect(actLabelFault("All devices", themes)).toContain("begins with a verb");    expect(actLabelFault("Storage", themes)).toContain("begins with a verb");    // Which record it acts on is a field line, never a placeholder in the verb.    expect(actLabelFault("Sign in on {serving_mac}", themes)).toContain("carries no placeholder");    expect(actLabelFault("Open {x}", themes)).toContain("carries no placeholder");    expect(cardTextFault("Storage", "key", themes)).toBeNull();  });  // RULE: "Fetch" is an act verb, and an act whose label is a name (a shelf, a part of the card) is excused only by NAMED_ACTS, by its table and act with the label as it stands, each with its reason.  it("takes fetch as a verb and excuses a named act only by its key and its label", () => {    expect(actLabelFault("Fetch the sound", themes)).toBeNull();    expect(actLabelFault("Finished", themes)).toContain("begins with a verb");    expect(actLabelFault("Finished", themes, "book.finish")).toBeNull();    expect(actLabelFault("Finished", themes, "todo.finish")).toContain("begins with a verb");    expect(actLabelFault("Done", themes, "book.finish")).toContain("begins with a verb");    for (const [act, { label, why }] of Object.entries(NAMED_ACTS)) {      expect(actLabelFault(label, themes, act), act).toBeNull();      expect(why.length, act).toBeGreaterThan(20);    }  });  it("refuses an oversized word and a third line", () => {    expect(cardTextFault("Open notifications", "line", themes)).toContain("wider");    expect(cardTextFault("Serve from this Mac today", "line", themes)).toContain("3 lines");  });  it("uses the app's label geometry and the theme's font", () => {    const theme = structuredClone(themes[0]);    const geometry = parseCardGeometry(cardSource);    geometry.cardLineWidthInsetMultiplier = 6;    expect(cardTextFault("Serve from this Mac", "line", [theme], geometry)).toContain("lines");    geometry.cardLineWidthInsetMultiplier = parseCardGeometry(cardSource).cardLineWidthInsetMultiplier;    geometry.cardLineSizeMin *= 2;    geometry.cardLineSizeMax *= 2;    geometry.cardLineSizeFraction *= 2;    expect(cardTextFault("Serve from this Mac", "line", [theme], geometry)).not.toBeNull();    theme.type.title = "unknown";    expect(() => cardTextFault("Serve", "line", [theme])).toThrow("character advance");  });  // RULE: The card measurer reads the app's named constants and refuses missing or nonliteral values; a Swift rename must fail loudly instead of silently accepting overflowing labels.  it("refuses renamed, missing, duplicated and nonliteral card constants", () => {    expect(() => parseCardGeometry(cardSource.replace("enum CardStockKnobs", "enum RenamedKnobs"))).toThrow("CardStockKnobs");    for (const name of Object.keys(parseCardGeometry(cardSource))) {      const declaration = new RegExp(`static let ${name}: CGFloat = [^\\n]+`);      const original = cardSource.match(declaration)![0];      for (const replacement of ["", original.replace(name, `${name}Renamed`), `${original}\n    ${original}`, original.replace(/= .*/, "= otherConstant"), original.replace(/= .*/, "= 0")]) {        expect(() => parseCardGeometry(cardSource.replace(declaration, replacement))).toThrow(`CardStockKnobs.${name}`);      }    }  });});