Cotton code
108 lines · 5.9 KB · SwiftRaw
import XCTest@testable import CottonKit/// THE GUARD: Cotton Dev resolves no identifier of the real household.////// The real identifiers are written out as literals on purpose — a change that/// drags `.dev` along with `.real` must go red here rather than agree with/// itself. The Rust half (data folder, port, relay name, server) is/// `crates/cotton-core/src/flavor_tests.rs`; the build half (bundle ids, app/// groups, names, scheme in `project.yml` and `build-app.sh`) is the/// app-flavor test.final class AppFlavorTests: XCTestCase {    /// Every identifier the frozen CottonDB owns.    private let realIdentifiers: Set<String> = [        "com.cottondb.cotton",        "com.cottondb.cotton.share-extension",        "group.com.cottondb.cotton",        "com.decemberfm.cottondb.mac",        "M7V3QVXZQH.com.decemberfm.cotton",        "com.decemberfm.cotton.mac",        "com.decemberfm.cotton.session-host",        "CottonDB",        "/Applications/CottonDB.app",        "tinmen-cotton",    ]    private func identifiers(of flavor: AppFlavor) -> [String: String] {        [            "phoneAppID": flavor.phoneAppID,            "phoneAppGroup": flavor.phoneAppGroup,            "phoneKeychainService": flavor.phoneKeychainService,            "macBundleID": flavor.macBundleID,            "macAppGroup": flavor.macAppGroup,            "macKeychainService": flavor.macKeychainService,            "sessionHostLabel": flavor.sessionHostLabel,            "macAppName": flavor.macAppName,            "installedPath": flavor.installedPath,            "dataFolderName": flavor.dataFolderName,            "urlScheme": flavor.urlScheme,            "phoneShareExtensionGroup": CottonDefaults.appGroup(bundleIdentifier: flavor.phoneAppID + ".share-extension", family: .phone),        ]    }    func testADevBuildResolvesNoRealIdentifier() {        for (name, value) in identifiers(of: .dev) {            XCTAssertFalse(realIdentifiers.contains(value), "Cotton Dev's \(name) is the real household's \(value)")            XCTAssertFalse(value.isEmpty, name)        }    }    func testTheRealBuildKeepsTheFrozenIdentity() {        let real = identifiers(of: .real)        XCTAssertEqual(real["phoneAppID"], "com.cottondb.cotton")        XCTAssertEqual(real["phoneAppGroup"], "group.com.cottondb.cotton")        XCTAssertEqual(real["phoneKeychainService"], "com.cottondb.cotton")        XCTAssertEqual(real["macBundleID"], "com.decemberfm.cottondb.mac")        XCTAssertEqual(real["macAppGroup"], "M7V3QVXZQH.com.decemberfm.cotton")        XCTAssertEqual(real["macKeychainService"], "com.decemberfm.cotton.mac")        XCTAssertEqual(real["sessionHostLabel"], "com.decemberfm.cotton.session-host")        XCTAssertEqual(real["installedPath"], "/Applications/CottonDB.app")        XCTAssertEqual(real["dataFolderName"], "CottonDB")        XCTAssertEqual(real["urlScheme"], "tinmen-cotton")        XCTAssertEqual(real["phoneShareExtensionGroup"], "group.com.cottondb.cotton")    }    func testAnythingButTheWordRealIsDev() {        for word in [nil, "", " ", "Real", "REAL", "prod", "release", "dev"] as [String?] {            XCTAssertEqual(AppFlavor.declared(word), .dev, String(describing: word))        }        XCTAssertEqual(AppFlavor.declared("real"), .real)        XCTAssertEqual(AppFlavor.declared(" real\n"), .real)    }    /// `swift test` and `pnpm ios:test` run unstamped or stamped dev — never    /// as the real app.    func testTheTestProcessIsNeverTheRealApp() {        XCTAssertEqual(AppFlavor.current, .dev)    }    /// RULE (owner, 29 Oct 2026): Cotton signs in at cottondb.com and Cotton Dev at the dev Worker (`apps/business/wrangler.jsonc` `env.dev`), the one manifest saying both sides; a build reads its own side, the test process the dev one; a two-sided server that names no side for the build does not open; and a one-sided server (a business's) is the same on both.    func testCottonDevSignsInAtTheDevWorker() throws {        let file = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()            .appendingPathComponent("App/CottonManifest/flavour.json")        // The source names its dev side as `https://{dev-host}`: the build fills in config/dev-server.json's host        // (scripts/ios-spec-gate.ts), so here the token stands for the dev Worker.        let data = try Data(contentsOf: file)        func read(_ side: AppFlavor?) throws -> FlavourManifest {            let decoder = JSONDecoder()            if let side { decoder.userInfo[BusinessAddress.side] = side }            return try decoder.decode(FlavourManifest.self, from: data)        }        XCTAssertEqual(try read(.real).personalObject, BusinessAddress(server: "https://cottondb.com", window: ["message": 90]))        XCTAssertEqual(try read(.dev).personalObject, BusinessAddress(server: "https://{dev-host}", window: ["message": 90]))        XCTAssertEqual(try read(nil).personalObject?.server, "https://{dev-host}", "the test process reads the dev side")        let crossed = try JSONDecoder().decode(JSONValue.self, from: JSONEncoder().encode(try read(.dev)))        XCTAssertEqual(crossed["business"]?["server"], .string("https://{dev-host}"), "the core is handed one address")        let oneSide = Data(#"{"server": {"real": "https://cottondb.com"}}"#.utf8)        XCTAssertThrowsError(try JSONDecoder().decode(BusinessAddress.self, from: oneSide), "Cotton Dev with no dev server opens nowhere, never at the real one")        let business = Data(#"{"server": "https://cotton-business.synthetic.workers.dev", "slug": "studio"}"#.utf8)        for side in AppFlavor.allCases {            let decoder = JSONDecoder()            decoder.userInfo[BusinessAddress.side] = side            XCTAssertEqual(try decoder.decode(BusinessAddress.self, from: business).server, "https://cotton-business.synthetic.workers.dev")        }    }}