// The Swift package's guards that need no toolchain — so they run on the // `checks` runner, which has neither cargo nor Swift. // // • the header and the Rust source list the same six functions; // • the Swift module map reads the committed header where it actually is; // • the git ladder is one decision in Rust and in the scripts. // // Every read here is BY PATH into another lane's tree, so each one fails loudly // when the file is missing rather than passing over an empty string — the rule // AGENTS.md writes down after #332. import { existsSync, readFileSync } from "node:fs"; import { join } from "node:path"; import { describe, expect, it } from "vitest"; const ROOT = join(__dirname, "..", ".."); /** The Rust lane owns these two. */ const HEADER = "crates/cotton-ffi/include/cotton_ffi.h"; const FFI_SOURCE = "crates/cotton-ffi/src/ffi.rs"; /** The package's. */ const MODULE_MAP = "apps/cotton/Sources/CottonFFI/module.modulemap"; const PACKAGE_SWIFT = "apps/cotton/Package.swift"; /** Read a file, failing with its path rather than with a null dereference. */ function read(relative: string): string { const path = join(ROOT, relative); expect(existsSync(path), `${relative} is missing — a guard that reads it must go red, not quiet`).toBe(true); return readFileSync(path, "utf8"); } /** The ABI, in the order the design names it. */ const FUNCTIONS = [ "cotton_open", "cotton_close", "cotton_call", "cotton_subscribe", "cotton_version", "cotton_string_free", ]; describe("the FFI header and its Rust source", () => { // RULE: The bridge between the Mac app and the core is six functions and no seventh. it("declare the same six functions, in the same order", () => { const header = read(HEADER); const source = read(FFI_SOURCE); const exported = [...source.matchAll(/pub (?:unsafe )?extern "C" fn (\w+)\(/g)].map((m) => m[1]); expect(exported, "the ABI is six functions and stays six (design M1)").toEqual(FUNCTIONS); const declared = [...header.matchAll(/^(?:[A-Za-z]+ )*\**(cotton_\w+)\(/gm)].map((m) => m[1]); expect(declared).toEqual(FUNCTIONS); }); it("keep the handle opaque, so no C caller can reach inside it", () => { expect(read(HEADER)).toContain("typedef struct CottonHandle CottonHandle;"); }); it("say they are generated, and say what regenerates them", () => { const header = read(HEADER); expect(header).toContain("GENERATED"); expect(header).toContain("gen:ffi"); }); }); describe("the Swift package's view of the core", () => { it("reads the header through the XCFramework, which the build script copies from the committed one on every run", () => { // ONE module map since H2, the XCFramework-relative one: an archive takes // a headers DIRECTORY, so the map names `cotton_ffi.h` inside it, and // `build-xcframework.sh` copies the committed header (and this map) into // `.ffi/include/` from their one source each — the archive is a gitignored // build product, so the copy inside it cannot outlive the header. const moduleMap = read(MODULE_MAP); expect(moduleMap).toMatch(/header "cotton_ffi\.h"/); expect(moduleMap).toContain(HEADER); const script = read("scripts/build-xcframework.sh"); expect(script).toContain("Sources/CottonFFI/module.modulemap"); expect(script).toContain("cotton-ffi/include/cotton_ffi.h"); }); it("links the ONE XCFramework the build script writes", () => { const manifest = read(PACKAGE_SWIFT); expect(manifest).toContain('.binaryTarget(name: "CottonFFIBinary", path: ".ffi/CottonFFI.xcframework")'); expect(manifest).not.toContain(".ffi/lib"); expect(manifest).not.toContain('.linkedLibrary("cotton_ffi")'); // `build-rust-ffi.sh` survives as the cargo half the XCFramework script // calls per slice; it stages nothing. const cargoHalf = read("scripts/build-rust-ffi.sh"); expect(cargoHalf).not.toContain(".ffi/lib"); expect(cargoHalf).not.toContain("apps/cotton"); }); }); /** * THE ONE RUST DECLARATION of the git ladder. * * It used to be `crates/cotton-server/src/repos/git.rs`, which was one of * THREE copies: the server's, `cotton-client`'s and Swift's. This guard held * only the server ⇄ Swift pair, so the client's could drift in silence. The * list now lives in `cotton-core` and both crates `pub use` it — so this guard * moved with it, and the two crate-side re-exports are asserted below. */ const CORE_GIT = "crates/cotton-core/src/git.rs"; /** The crate that must READ the ladder and never restate it. */ const CLIENT_GIT = "crates/cotton-client/src/git/discover.rs"; /** The paths inside a `[ … ]` list of quoted strings, in order. */ function quotedList(text: string, after: string): string[] { const at = text.indexOf(after); expect(at, `${after} is not in the file any more — this guard must go red, not quiet`).toBeGreaterThan(-1); // From the `=`, never from `at`: the Rust declaration's own type is // `[&str; 4]`, and a search for the first `[` would read the annotation // instead of the list and answer "no candidates at all". const equals = text.indexOf("=", at); expect(equals, `${after} declares no value`).toBeGreaterThan(at); const open = text.indexOf("[", equals); const close = text.indexOf("]", open); expect(close, "the list is not closed").toBeGreaterThan(open); return [...text.slice(open, close).matchAll(/"([^"]+)"/g)].map((m) => m[1]); } describe("the git ladder", () => { // RULE: The core declares one ordered, non-empty ladder of absolute git paths. // Why: a ladder with a relative entry runs whatever git the caller's PATH holds. it("is a non-empty absolute-path ladder in Rust", () => { const rust = quotedList(read(CORE_GIT), "pub const CANDIDATES"); expect(rust, "the core declares at least one git candidate").not.toEqual([]); for (const path of rust) expect(path.startsWith("/"), `${path} is not absolute`).toBe(true); }); it("is declared ONCE in Rust — both crates read the core's list", () => { // The defect this closes: three Rust copies of the ladder and a guard over // one pair. A crate that restated the list could drift from the one the // Mac exports, and nothing would go red. for (const file of [CLIENT_GIT]) { const text = read(file); expect(text, `${file} restates the git ladder instead of reading the core's`) .not.toMatch(/pub const [A-Z_]*CANDIDATES[A-Z_]*\s*:\s*\[/); expect(text, `${file} no longer reads cotton_core::git::CANDIDATES`) .toContain("cotton_core::git::CANDIDATES"); } }); it("is never /usr/bin/git — the shim is refused by name", () => { const rust = read(CORE_GIT); expect(quotedList(rust, "pub const CANDIDATES")).not.toContain("/usr/bin/git"); // And it says so out loud, so a future edit that added it back has to // delete a stated rule rather than a comment. expect(rust).toContain('pub const SHIM: &str = "/usr/bin/git"'); }); it("travels in the one variable the core names", () => { expect(read(CORE_GIT)).toContain('pub const PROGRAM_ENV: &str = "COTTON_GIT"'); }); });