Cotton code
259 lines · 12.9 KB · SwiftRaw
// The boundary, held against the crate that answers it — and against the merge// that will complete it.//// `Boundary.swift` declares every method name this app asks for, and a name// there that no build answers is an `unknown_method` at RUNTIME: the ABI is six// C functions and a capability is a string inside one of them, so the compiler// cannot help. The first cut of that file was written to the design note rather// than to the crate and disagreed with it in six places; every one read as a// working app that quietly did nothing.//// **Both halves are on `main` now** (#422, #425), so the split this file used to// carry — "answered today" beside "awaited, with who owes it" — has collapsed to// the one list. The empty awaited list and the test that asserted its own// literal was empty went with the post-rewrite review: a method this app calls// ahead of the crate is caught by the list below going red, which is the// guard that was always doing the work.//// The paths belong to the RUST FFI lane. AGENTS.md's three rules for a by-path// guard are `SourceSweep.mustRead`'s: a read that finds nothing fails LOUDLY// with the path in the message, the lane is named here, and the paths are// grepped repo-wide before any of them moves.import CottonKitimport CottonUIimport Foundationimport XCTestfinal class BoundaryContractTests: XCTestCase {    private static let lane = "Rust FFI (the phone's method table)"    private static let methodsPath = "crates/cotton-ffi/src/methods.rs"    private static let readsPath = "crates/cotton-ffi/src/phone/reads.rs"    /// Every name this app calls. All of them are answered.    private static let answeredToday = [        CottonMethod.readBoot,        // The databases this device holds, a window's counted pin on the one        // it draws, and the make (`CottonKit/Databases.swift`).        CottonMethod.readDatabases,        // One database's page: its name, colour, where it is reached and what        // this device keeps of it.        CottonMethod.readDatabase,        // What each of the person's devices keeps of one database (Spec 10.5).        CottonMethod.readDatabaseStorage,        CottonMethod.readMembers,        // The serving Mac's backup, for the board's notices (`BackupNotice`).        CottonMethod.readServingBackup,        CottonMethod.databasesShow,        CottonMethod.databasesHide,        CottonMethod.databasesCreate,        CottonMethod.readSyncStatus,        CottonMethod.syncNow,        CottonMethod.syncForeground,        CottonMethod.syncBackground,        // The copy's compaction in a processing task (`BackgroundCatchUp`).        CottonMethod.syncCompact,        // The writes it parked rather than folded — asked by the app,        // answered on every build.        CottonMethod.syncRejected,        CottonMethod.syncRetryRejected,        // Start, the other half of the first screen. `join.nearbyOrigin` left        // this list with the browse that fed it, and the crate answers the name        // no more: nothing in either app asked it, so the method retired too.        CottonMethod.workspaceStartLocal,        // The boot gate, Home's stand, backup and sync, answered whole.        CottonMethod.shellHome,        CottonMethod.write,        CottonMethod.routesList,        // A person's photograph the owner chose in their own app: bytes into        // the store, the person's row updated to point at them.        CottonMethod.captureAvatar,        // ── J4 ── shake to report: one sentence, the screen, the facts, the        // log — to Cotton's own database, kept on the phone until it goes.        CottonMethod.feedbackSend,        CottonMethod.feedbackFlush,        // ── 4h ── the settings family, devices, Find and the Bin.        // Every name those nineteen screens call, so a rename in the crate        // fails HERE rather than as an `unknown_method` on the screen.        CottonMethod.devicesPending,        CottonMethod.devicesApprovePending,        CottonMethod.devicesDeclinePending,        // A tool's own act on its cards' fans: Connect Google Calendar.        CottonMethod.toolsAct,        // Cotton filling in a person's city from their zone, run on each        // foreground (`Boundary+PeopleCities.swift`).        CottonMethod.peopleFillCities,        CottonMethod.peopleUnfillCities,        // The reads. Dispatched by PREFIX, so these are held against        // `reads::READS` one layer in rather than against the method table.        CottonMethod.readAccount,        CottonMethod.readYou,        CottonMethod.readVoiceContext,        CottonMethod.readFind,        // 4d — Home, the two decision queues, the strip and the mic. Landed on        // main while this lane was in flight, and swept by the case below the        // moment the two met: a read named in a lane's own file and nowhere        // else is precisely what that sweep exists to find.        CottonMethod.readHome,        // 4e — the day and the person. Thirteen screen reads of its own and        // six more its screens call, listed here so the case below holds every        // one of them against `reads::READS`. A read named only beside its        // screen is a name this guard cannot see, which is how a crate-side        // rename reaches the app as an `unknown_method` instead of as a red        // test.        CottonMethod.readAlarms,        "read.day",        "read.month",        "read.event",        CottonMethod.readThoughts,        CottonMethod.readPeopleImport,        CottonMethod.readTodo,        // Not 4e's own, but called by 4e's screens.        CottonMethod.readRecord,        CottonMethod.readProjects,        // 4g — people's traffic and money. Twenty-seven reads, listed here        // for the reason 4e's are: this sweep is GLOBAL over `Sources/`, so a        // read named only inside its own lane's boundary file is a name it        // cannot see. 4g keeps its own method-to-screen table in        // `Traffic/BoundaryContract4gTests.swift`, which is the stronger        // check — this one is the completeness half, and the merge of the two        // lanes is what made it necessary.        CottonMethod.readEmail,        CottonMethod.readPhotos,        CottonMethod.readThreads,        CottonMethod.readStorage,        // What a phone keeps: its device row and the Keep on this phone act.        CottonMethod.keepRules,        CottonMethod.keepSet,        CottonMethod.keepClear,        // What a phone fetches by those rules: its cap and the core's lines.        CottonMethod.pushForget,        // `reminders.desired` is the crate's fourth 4j method and is        // deliberately NOT here: nothing in this app asks it. The scheduler        // asks `reminders.reconcile`, which answers the same set already        // diffed, and the desired set on its own serves a screen that shows        // what is coming — which this build does not have. `Boundary+4j.swift`        // names it in its table so a later lane finds it; declaring a constant        // with no caller would be the thing 4b's review struck.        CottonMethod.remindersReconcile,        // 4i — the path behind a face's photo: it answers where a photo's        // bytes are, and it is here because the sweep below found it declared        // beside the pass that asks it, which is exactly the invisibility this        // list exists to end.        CottonMethod.readMediaPath,        // The two reads asked BY NAME rather than through a route's        // `ScreenRead`: the Code tool's Machines list reads it through its source, and the        // todo's hand-to-agent sheet needs the Mac roster a todo's own read        // has no business carrying.        CottonMethod.readSessions,        // One session, for the session table's page and its conversation.        CottonMethod.readSession,        // One project's sessions of all time, for the session list narrowed        // to that project.        CottonMethod.readProjectSessions,        // The devices, off the file — asked by name because the Devices page        // joins it with the two above, and the settings sheet's row and        // Home's line read the same model.        CottonMethod.readDevices,        // One device's own page.        CottonMethod.readDevice,        // The serving Mac's sender on its row: its line and Test push.        // The Imports tool's past runs, the list's one read (Spec 9.3).        // 4k — the assistant. One read for `/activity` (a rename of        // `read.replicaFacts`, because a screen holds exactly one read and this        // lane adds no route) and one method that is not a tool call, because        // encrypting is a rail the phone build does not have.        CottonMethod.readActivity,        // 6 — the vault. Nine owner-signed methods and two screen reads;        // `secrets.set` retired with them, and the crate removed it in the same        // change, which is why this list carries the family rather than it.        CottonMethod.vaultPublishKey,        CottonMethod.vaultUnlock,        // The unlock window's card, its answers and its open windows        // (`Boot/UnlockAsks.swift`).        CottonMethod.unlockCard,        CottonMethod.unlockAllow,        CottonMethod.unlockDecline,        CottonMethod.unlockEnd,        CottonMethod.unlockWindows,        CottonMethod.unlockReads,        // Approve everywhere and recovery (e2e rulings R7, R9;        // `Boot/SealedKeys.swift`, `Boot/RecoveryCard.swift`).        CottonMethod.vaultApproval,        CottonMethod.vaultRefuseDevice,        CottonMethod.recoveryStatus,        CottonMethod.recoveryMake,        CottonMethod.recoveryShow,        CottonMethod.recoveryCheck,        CottonMethod.recoveryOpen,        CottonMethod.recoveryMakePlatform,        CottonMethod.recoveryOpenPlatform,        CottonMethod.recoveryTrust,        CottonMethod.recoveryAskHelp,        CottonMethod.recoveryHelped,        CottonMethod.recoveryHelpAsks,        CottonMethod.recoveryHelpApprove,        CottonMethod.recoveryHelpDecline,        CottonMethod.vaultReveal,        CottonMethod.vaultCreate,        CottonMethod.vaultUpdate,        CottonMethod.vaultTrash,        CottonMethod.vaultRestore,        CottonMethod.readSecrets,        CottonMethod.readSecret,        // The terminal's four verbs. They were spelled in a `TerminalMethod`        // enum beside the pane — invisible to this list, and therefore to the        // crate — which is the shape of the defect they actually shipped with:        // the NAMES were right and every FIELD was wrong, so every attach came        // back `bad_request`. The names live in `Boundary+Terminal.swift` now,        // this list holds them against the crate's own table, and        // `TerminalWireTests` holds the arguments against a fixture the crate        // feeds to the core.        CottonMethod.terminalAttach,        CottonMethod.terminalWrite,        CottonMethod.terminalResize,        CottonMethod.terminalDetach,        // The held card answered whole, and the When step's one write (`DeckEditor`).        CottonMethod.shellOpened,        // Send (`DeckSend`, `SendCard`): make, the inbox, Keep, Decline, Undo, trust, an offer's picture.        CottonMethod.sendMake,        CottonMethod.sendInbox,        CottonMethod.sendKeep,        CottonMethod.sendDecline,        CottonMethod.sendUndo,        CottonMethod.sendTrust,        CottonMethod.sendPicture,        CottonMethod.cardsWhen,    ]    private func methodTable() throws -> String {        try SourceSweep.mustRead(BoundaryContractTests.methodsPath, ownedBy: BoundaryContractTests.lane)    }    /// RULE: Every read name used by Swift is declared by the Rust registry, because method strings cross the ABI without a compiler check.    func testEveryReadNameInTheSourcesIsAnsweredByRust() throws {        let reads = try SourceSweep.mustRead(            BoundaryContractTests.readsPath,            ownedBy: BoundaryContractTests.lane        )        XCTAssertTrue(reads.contains("pub static READS"), "reads.rs did not read as itself")        let pattern = try NSRegularExpression(pattern: #""(read\.[A-Za-z][A-Za-z0-9]*)""#)        var found: [String: String] = [:]        for source in try SourceSweep.swiftSources() {            let code = SourceSweep.withoutComments(source.text)            let range = NSRange(code.startIndex..., in: code)            for match in pattern.matches(in: code, range: range) {                guard let hit = Range(match.range(at: 1), in: code) else { continue }                found[String(code[hit])] = source.name            }        }        XCTAssertFalse(found.isEmpty, "the source sweep found no read names")        for (method, file) in found.sorted(by: { $0.key < $1.key }) {            let name = String(method.dropFirst("read.".count))            XCTAssertTrue(                reads.contains("name: \"\(name)\""),                "\(method) is called from \(file) but Rust declares no such read"            )        }    }}