// The Swift push table, held TOTAL against the crate's own vocabulary. // // Design P14 asks for *"the same guard that a new server type lands with its // entry and its test in the same change"*. The crate half declares the // vocabulary once (`cotton_core::rules::notifications`) and holds the SERVER // to it; this is the other half — the APP held to it, so a type the server // composes and this table has no entry for is a RED test rather than a push // that opens the app and goes nowhere. // // **It reaches the crate through `routes.list`, which is a METHOD.** That is // deliberate and it is the shape to prefer: a guard that reads another // package's file by path goes QUIET, not red, when the file moves — and // `apps/cotton` and `packages/*` are a deletion target. A method cannot go // quiet: a build that stopped answering fails here, and so does a build that // answers a different list. It also proves the linked archive actually answers // it, which no amount of reading source can. // // The known holes travel WITH the list, and there are NONE left. // `pushTypesUnmapped` is the crate's `UNMAPPED_BY_THE_APP`, which was // `share_revoked` and `share_stale` — both emitted by a server and mapped by no // client — and both went with the multi-person sharing data plane (the code is // on the branch `archive/sharing-v1`). The baseline is EMPTY now, and the case // below is what keeps it that way: every type the server sends is mapped, and // every type this table maps is sent. import CottonKit import XCTest final class PushVocabularyTests: XCTestCase { private struct RoutesAnswer: Decodable { let routes: [String] let count: Int let pushTypes: [String] let pushTypesUnmapped: [String] } /// RULE: The app understands every notification the server can send, and claims none the server cannot. func testTheSwiftTableIsTotalOverTheCratesVocabulary() throws { // The linked module identifies the real core; the stub has no module. // A real build still runs every assertion below. guard CottonRuntime.linksFFI else { throw XCTSkip("The module-less FFI stub cannot answer routes.list; this guard requires the real XCFramework.") } let answered = try askRoutes() // Anti-vacuity before any comparison: an empty list would make every // assertion below pass while proving nothing. XCTAssertGreaterThan( answered.pushTypes.count, 0, "read \(answered.pushTypes.count) push types out of `routes.list` — the comparison below would prove nothing" ) let crate = Set(answered.pushTypes) let swift = Set(NotificationRoutes.mappedTypes) XCTAssertEqual( crate.subtracting(swift).sorted(), [], """ the crate names these push types and the Swift table has no entry for them. \ A new server type lands with its entry and its test in the SAME change. """ ) XCTAssertEqual( swift.subtracting(crate).sorted(), [], "the Swift table invents these push types; the vocabulary is the crate's" ) } func testThereAreNoKnownHolesLeftInEitherDirection() throws { // The linked module identifies the real core; the stub has no module. // A real build still runs every assertion below. guard CottonRuntime.linksFFI else { throw XCTSkip("The module-less FFI stub cannot answer routes.list; this guard requires the real XCFramework.") } let answered = try askRoutes() XCTAssertEqual( answered.pushTypesUnmapped, [], """ a type the server emits has no entry in the Swift table. The baseline is \ EMPTY on purpose: a new server type lands with its route-map entry and its \ test in the SAME change, rather than joining a list of known holes. """ ) // And nothing this table maps is unreachable either, which is the // mirror hole (`EMITTED_BY_NOBODY`, also empty). for mapped in NotificationRoutes.mappedTypes { XCTAssertTrue( answered.pushTypes.contains(mapped), "\(mapped) is mapped here and nothing emits it" ) } } /// `CottonCalling` is async and `XCTestCase` here is not, so the one call is /// pumped through a semaphore rather than making every case async for it. /// `routes.list` is SHARED and a table read: no device secret, no server, /// nothing opened. private func askRoutes() throws -> RoutesAnswer { let container = FileManager.default.temporaryDirectory .appendingPathComponent("cotton-pushtypes-\(UUID().uuidString)") try FileManager.default.createDirectory(at: container, withIntermediateDirectories: true) defer { try? FileManager.default.removeItem(at: container) } let core = try CottonRuntime.open(CottonConfig(containerPath: container.path)) let box = Box>() let waiting = DispatchSemaphore(value: 0) Task { do { box.value = .success( try await core.call(CottonMethod.routesList, as: RoutesAnswer.self) ) } catch { box.value = .failure(error) } waiting.signal() } XCTAssertEqual(waiting.wait(timeout: .now() + 30), .success, "routes.list never answered") return try XCTUnwrap(box.value).get() } /// A one-slot box, because the closure above is `@Sendable` and a captured /// `var` would not be. private final class Box: @unchecked Sendable { private let lock = NSLock() private var stored: T? var value: T? { get { lock.lock() defer { lock.unlock() } return stored } set { lock.lock() defer { lock.unlock() } stored = newValue } } } }