/** * LOOM BUILDS WITH NO CORE. * * Loom (`apps/loom`) is the faces' own package: the plain values a face draws * from (`CottonValues`), the look (`CottonPaper`) and the faces * (`CottonFaces`), so a face, the Loom app and their tests build where no Rust * library has been built — a bare clone, a face's own test on a Mac (Loom, * 27 Oct 2026). One `import CottonKit` or one outside package in it and that * stops being true without a single test failing elsewhere: every other build * has those modules on the search path. So, read the way the compiler reads * it: * * - every Swift file under `apps/loom` imports only the SDK's own frameworks * named below and the Loom targets its own target depends on — never * `CottonKit`, `CottonFFI`, `CottonUI` or a package from outside the SDK; * - `CottonValues` imports Foundation alone (the Swift standard library needs * no import) and depends on nothing; * - `apps/loom/Package.swift` declares no package dependency, no binary * target, and no target dependency but its own targets, in the order of * the layers: values, then the look, then the faces, then a specimen drawn; * * This guard replaces the two it folds (`cotton-values-imports`, * `paper-imports`): one package, one rule. A framework the faces genuinely * need from the SDK is one line in `PLATFORM`. */ import { execFileSync } from "node:child_process"; import { readFileSync } from "node:fs"; import { dirname, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import { describe, expect, it } from "vitest"; const REPO_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "../.."); const LOOM = "apps/loom/"; const MANIFEST = "apps/loom/Package.swift"; /** The SDK's frameworks a face and its look draw with. */ const PLATFORM = new Set(["Foundation", "SwiftUI", "UIKit", "AppKit", "CoreGraphics", "CoreText", "os", "Observation", "XCTest"]); /** Each Loom target, and the Loom targets it may stand on: the layers, bottom up. */ const LAYERS: Record = { CottonValues: [], CottonPaper: [], CottonFaces: ["CottonValues", "CottonPaper"], CottonSpecimens: ["CottonFaces", "CottonPaper", "CottonValues"], CottonFacesTests: ["CottonSpecimens", "CottonFaces", "CottonPaper", "CottonValues"], }; /** The one target CottonValues' rule is stricter for: Foundation alone. */ const VALUES_ONLY = new Set(["Foundation"]); const read = (path: string) => readFileSync(resolve(REPO_ROOT, path), "utf8"); function tracked(prefix: string): string[] { return execFileSync("git", ["ls-files", "-z", "--cached", "--others", "--exclude-standard", "--", prefix], { cwd: REPO_ROOT, encoding: "utf8", }) .split("\0") .filter(Boolean); } /** The source with comments and string literals blanked (newlines kept, so line numbers hold). */ function code(source: string): string[] { const blank = (text: string) => text.replace(/[^\n]/g, " "); return source .replace(/\/\*[\s\S]*?\*\//g, blank) .replace(/"""[\s\S]*?"""/g, blank) .replace(/"(?:\\.|[^"\\\n])*"/g, '""') .split("\n") .map((line) => line.replace(/\/\/.*$/, "")); } /** * The module each import line names, in any spelling: attributes * (`@testable`, `@_exported`, `@preconcurrency`), access levels, a kind * (`import struct X.Y` imports `X`), indented under an `#if` or not. */ export function importsOf(source: string): { line: number; module: string }[] { const found: { line: number; module: string }[] = []; code(source).forEach((text, index) => { const hit = text.match( /^\s*(?:@[A-Za-z_]\w*(?:\([^)]*\))?\s+)*(?:(?:public|package|internal|fileprivate|private)\s+)?import\s+(?:(?:typealias|struct|class|enum|protocol|let|var|func)\s+)?([A-Za-z_]\w*)/, ); if (hit) found.push({ line: index + 1, module: hit[1] }); }); return found; } /** The manifest with its comments blanked and its strings kept: a name in a comment is not a dependency. */ function uncommented(source: string): string { let out = ""; for (let i = 0; i < source.length; i++) { const c = source[i]; if (c === '"') { const close = source.indexOf('"', i + 1); out += source.slice(i, close + 1); i = close; } else if (c === "/" && source[i + 1] === "/") { const eol = source.indexOf("\n", i); i = (eol < 0 ? source.length : eol) - 1; } else if (c === "/" && source[i + 1] === "*") { const close = source.indexOf("*/", i + 2); out += source.slice(i, close + 2).replace(/[^\n]/g, " "); i = close + 1; } else out += c; } return out; } /** The balanced `( … )` of the call that starts at `at`. */ function balanced(source: string, at: number, what: string): string { let depth = 0; for (let i = source.indexOf("(", at); i < source.length; i++) { const c = source[i]; if (c === '"') { i = source.indexOf('"', i + 1); continue; } if (c === "(") depth++; if (c === ")" && --depth === 0) return source.slice(at, i + 1); } throw Error(`${MANIFEST}: ${what}'s parentheses do not close`); } /** Every `.target` / `.testTarget` the manifest declares: its name, its path and the names its `dependencies:` lists. */ export function targetsOf(manifest: string): { name: string; path?: string; dependencies: string[] }[] { const source = uncommented(manifest); return [...source.matchAll(/\.(?:target|testTarget)\(\s*name:\s*"([^"]+)"/g)].map((hit) => { const target = balanced(source, hit.index!, hit[1]); const list = target.match(/dependencies:\s*\[([\s\S]*?)\]\s*,/)?.[1] ?? ""; // `.product(name: "X", package: "P")`, `.target(name: "X")` and `.byName(name: "X")` are each the one name X. const named = list.replace(/\.(?:product|target|byName)\(\s*name:\s*"([^"]+)"[^)]*\)/g, '"$1"'); return { name: hit[1], path: target.match(/path:\s*"([^"]+)"/)?.[1], dependencies: [...named.matchAll(/"([^"]+)"/g)].map((name) => name[1]), }; }); } describe("Loom builds with no core", () => { const manifest = read(MANIFEST); const targets = targetsOf(manifest); /// RULE: Loom (apps/loom) depends on no package and no binary: the faces, the look and the values build where no Rust library has been built (Loom, 27 Oct 2026). it("declares no package dependency and no binary target", () => { const body = uncommented(manifest); expect(body, "Loom depends on no package: the faces are drawn from the SDK and their own targets").not.toMatch(/\.package\s*\(/); expect(body, "Loom links no binary: no Rust, no XCFramework").not.toMatch(/\.binaryTarget\s*\(/); expect(targets.map((target) => target.name).sort()).toEqual(Object.keys(LAYERS).sort()); }); /// RULE: Each Loom target depends only on the Loom targets beneath it — values, then the look, then the faces — so nothing in Loom reaches the app. it("lets each target depend only on the layers beneath it", () => { const strays = targets.flatMap(({ name, dependencies }) => dependencies.filter((dependency) => !LAYERS[name]?.includes(dependency)).map((dependency) => `${name} depends on ${dependency}`), ); expect(strays).toEqual([]); }); /// RULE: Every Swift file under apps/loom imports only the SDK's own frameworks and the Loom targets its own target stands on — never CottonKit, CottonFFI, CottonUI or an outside package; what the app knows that a face does not, the app says to it in the face's own plain types. it("imports nothing of the app's and nothing from outside the SDK", () => { const files = tracked(LOOM).filter((path) => path.endsWith(".swift") && path !== MANIFEST); expect(files.length, "the sweep read no Swift files under apps/loom").toBeGreaterThan(30); const strays = files.flatMap((path) => { const target = targets.find((candidate) => candidate.path && path.startsWith(`${LOOM}${candidate.path}/`)); if (!target) return [`${path} belongs to no Loom target`]; const allowed = target.name === "CottonValues" ? VALUES_ONLY : new Set([...PLATFORM, ...(LAYERS[target.name] ?? [])]); return importsOf(read(path)) .filter(({ module }) => !allowed.has(module)) .map(({ line, module }) => `${path}:${line} imports ${module}`); }); expect(strays, "say it to the face in its own plain types, or leave the file in CottonUI").toEqual([]); }); it("reads an import line the way the compiler does", () => { const source = [ "// import CottonKit — a comment is not an import", "/* import CottonUI", " still a comment */", 'let said = "import SwiftUI"', "import Foundation", "@preconcurrency import SwiftTerm", "#if canImport(UIKit)", " import UIKit", "#endif", "@_exported import CottonValues", "import struct CottonKit.JSONValue", "public import CottonPaper", "@testable import CottonFaces", ].join("\n"); expect(importsOf(source)).toEqual([ { line: 5, module: "Foundation" }, { line: 6, module: "SwiftTerm" }, { line: 8, module: "UIKit" }, { line: 10, module: "CottonValues" }, { line: 11, module: "CottonKit" }, { line: 12, module: "CottonPaper" }, { line: 13, module: "CottonFaces" }, ]); }); it("reads a target's dependencies the way SwiftPM does", () => { const sample = [ ".target(", ' name: "CottonKit",', ' dependencies: ["CottonFFIBinary"],', ' path: "Sources/CottonKit"', "),", ".target(", ' name: "CottonPaper",', ' path: "Sources/CottonPaper",', ' resources: [.copy("Resources/Fonts")]', "),", ".testTarget(", ' name: "CottonUITests",', " dependencies: [", ' "CottonKit", // a comment, "CottonUI"', ' .product(name: "SwiftTerm", package: "SwiftTerm"),', " ],", ' path: "Tests/CottonUITests"', "),", ].join("\n"); expect(targetsOf(sample)).toEqual([ { name: "CottonKit", path: "Sources/CottonKit", dependencies: ["CottonFFIBinary"] }, { name: "CottonPaper", path: "Sources/CottonPaper", dependencies: [] }, { name: "CottonUITests", path: "Tests/CottonUITests", dependencies: ["CottonKit", "SwiftTerm"] }, ]); }); });