Cotton code
104 lines · 5.4 KB · SwiftRaw
// Every method name the app declares is one the crate answers.//// `BoundaryContractTests` holds a HAND-WRITTEN list (`answeredToday`) against// the crate's method table, and it closed the same hole for the `read.*` half// by SWEEPING the sources instead of listing them. The other half was left// listed: at the time this was written, sixteen method names the app calls —// `capture.create`, `capture.media`, `dictation.create`, `export.build`,// `media.ensure`, `media.viewed`, `meetings.create`, `page.save`,// `settings.mediaBudget`, `vault.place`, `vault.unplace`,// `vault.importDashlane` and the four `host.*` / `pairing.*` names — were// declared on `CottonMethod` and were on no list, so a crate-side rename of// any of them reached a person as an `unknown_method` at runtime with nothing// red anywhere. Both sides of that boundary are stringly typed and the// compiler can see neither.//// So the claim is SWEPT rather than asserted: every string literal declared// inside a `CottonMethod` enum or extension, anywhere under// `apps/cotton/Sources`, is held against `crates/cotton-ffi/src/methods.rs`.// The `read.<name>` family is dispatched by PREFIX in the crate, so those names// are not in that table at all — `BoundaryContractTests` holds them one layer// in, against `reads::READS` — and this sweep skips them by name and asserts it// found some, so the skip cannot become the whole answer.//// The universe is the CottonMethod declarations and nothing else, which is why// this file needs no exemption list: bundle identifiers, App Group defaults// keys and the two `terminal.*` EVENT names are declared in types of their own// (`TerminalEventKind`, `ShareMailboxStorage`, …) and are never swept.import Foundationimport XCTestfinal class BoundaryMethodNameSweepTests: XCTestCase {    /// The lane that owns the path this guard reads. Grep it repo-wide before    /// moving the file — `SourceSweep.mustRead` fails loudly, but loudly is    /// still a failure somebody has to read.    private static let lane = "the Rust FFI"    private static let methodsPath = "crates/cotton-ffi/src/methods.rs"    /// Every `static let … = "<a.b>"` declared inside a `CottonMethod` enum or    /// extension, as (symbol, wire name, file).    ///    /// A line split at top-level declarations rather than brace matching, for    /// the reason `SourceSweep.topLevelDeclarations` gives: every type in this    /// package is written at column 0, and a brace counter would have to    /// understand string interpolation to be right.    private func declaredMethodNames() throws -> [(symbol: String, wire: String, file: String)] {        let pattern = try NSRegularExpression(            pattern: #"static\s+let\s+(\w+)\s*(?::\s*String\s*)?=\s*"([a-zA-Z][A-Za-z0-9]*\.[A-Za-z0-9.]+)""#        )        var found: [(symbol: String, wire: String, file: String)] = []        for source in try SourceSweep.swiftSources() {            let code = SourceSweep.withoutComments(source.text)            for block in SourceSweep.topLevelDeclarations(in: code) where block.name == "CottonMethod" {                let range = NSRange(block.text.startIndex..., in: block.text)                for match in pattern.matches(in: block.text, range: range) {                    guard let symbol = Range(match.range(at: 1), in: block.text),                        let wire = Range(match.range(at: 2), in: block.text)                    else { continue }                    found.append(                        (                            symbol: String(block.text[symbol]),                            wire: String(block.text[wire]),                            file: source.name                        )                    )                }            }        }        return found    }    /// RULE: Every method name the phone app declares is one the database core answers.    ///    /// The failure it prevents: a name the core renamed, or a name nobody ever    /// answered, reaches the person as a screen that does nothing — the app    /// asks for a capability by a word the core does not know and the answer is    /// "unknown method", at run time, on their phone.    func testEveryMethodNameTheAppDeclaresIsOneTheCrateAnswers() throws {        let table = try SourceSweep.mustRead(            BoundaryMethodNameSweepTests.methodsPath,            ownedBy: BoundaryMethodNameSweepTests.lane        )        let declared = try declaredMethodNames()        // Anti-vacuity on the SWEEP: a broken walk finds nothing and passes.        XCTAssertFalse(declared.isEmpty, "the sweep found no method names — the walk is broken, not the app")        XCTAssertTrue(            declared.contains { $0.wire.hasPrefix("read.") },            "the sweep found no read names, so the skip below is doing all the work"        )        for method in declared where !method.wire.hasPrefix("read.") {            XCTAssertTrue(                table.contains("\"\(method.wire)\""),                """                CottonMethod.\(method.symbol) is "\(method.wire)" (declared in \                \(method.file)) and the crate's method table does not name it. \                Either the crate renamed it — in which case this app follows, \                because the crate wins — or the app is asking for something \                nobody answers, which is an unknown_method in front of a person.                """            )        }    }}