import XCTest @testable import CottonKit #if canImport(CottonFFI) import CottonFFI #endif /// **The wiring proof for sub-step 4a part 2.** One call, through the module /// map, into the Rust archive, on the simulator. /// /// It is deliberately the smallest possible thing: `cotton_version()` takes no /// arguments, opens nothing, touches no file and has no side effect — so a /// green here says exactly one thing and says it unambiguously. The XCFramework /// is linked, the module map resolves the committed header, the symbols are in /// the archive for THIS architecture, and a string crosses the boundary and is /// freed by the caller. Everything above that is 4b's. /// /// **It is compiled out when the XCFramework is absent, and that is not a /// silent skip.** `Package.swift` declares the `binaryTarget` unconditionally /// and `scripts/ffi-stub.sh` holds its path with a module-less STUB when the real /// archive is absent — SwiftPM refuses to RESOLVE a package whose binary target /// path does not exist. A stub's libraries are empty and carry no headers, so on a /// checkout that has not run `scripts/build-xcframework.sh` the module /// genuinely is not there and this file cannot reference it. What stops that /// from being a green run that proved nothing is `testTheFFIIsActuallyLinked` /// below, which FAILS rather than skipping — so "no XCFramework" reads as a red /// test naming the script to run, not as an absence nobody notices. final class FFISmokeTests: XCTestCase { func testTheFFIIsActuallyLinked() { #if canImport(CottonFFI) XCTAssertTrue(true) #else XCTFail( """ CottonFFI is not linked, so nothing below the boundary was tested. \ This build has the placeholder `scripts/ffi-stub.sh` writes, not the \ real archive. Run `bash scripts/build-xcframework.sh` from the repo \ root; nothing needs resetting afterwards. """ ) #endif } #if canImport(CottonFFI) func testCottonVersionCrossesTheBoundaryAndIsFreedByTheCaller() { guard let raw = cotton_version() else { return XCTFail("this ABI never answers a null string") } // Read it, then free it — the whole ownership contract of the two // string-returning functions, exercised once. let version = String(cString: raw) cotton_string_free(raw) XCTAssertFalse(version.isEmpty) // The crate's own version, so a mismatch reads as "you are linking // an old archive" rather than as a failure with no lead. XCTAssertTrue( version.first?.isNumber == true, "cotton_version answered \(version), which is not a version" ) } /// **The wiring proof for 4b.** A real `cotton_open`, a real /// `cotton_call`, and an answer with content in it. /// /// `routes.list` is chosen because it is the one method that answers /// the same thing on a fresh container as on a paired phone: the route /// table is a constant, so this test needs no server, no device key and /// no file — and it is also the method the `Route` enum's own totality /// test reads, so a green here says that comparison can actually be /// made. func testTheRouteTableCrossesTheBoundaryWithItsPathsIntact() throws { let container = FileManager.default.temporaryDirectory .appendingPathComponent("cotton-ffi-smoke-\(UUID().uuidString)") defer { try? FileManager.default.removeItem(at: container) } let config = #"{"containerPath":"\#(container.path)","appVersion":"0.0.0-test"}"# // `out_error` is a `char **`: on a refusal the core writes an // owned JSON string `{code, title, message}` there, and freeing it // is the caller's job like every other string this ABI answers. var failure: UnsafeMutablePointer? guard let handle = cotton_open(config, &failure) else { let reason = failure.map { raw -> String in let text = String(cString: raw) cotton_string_free(raw) return text } ?? "no reason given" return XCTFail("cotton_open refused: \(reason)") } defer { cotton_close(handle) } guard let raw = cotton_call(handle, "routes.list", "{}") else { return XCTFail("this ABI never answers a null string") } let answer = String(cString: raw) cotton_string_free(raw) let parsed = try XCTUnwrap( try JSONSerialization.jsonObject(with: Data(answer.utf8)) as? [String: Any] ) XCTAssertEqual(parsed["ok"] as? Bool, true, answer) let data = try XCTUnwrap(parsed["data"] as? [String: Any]) let routes = try XCTUnwrap(data["routes"] as? [String]) // The count AND a named member: a count alone would pass against a // list of a hundred and ten empty strings. The count is the SWIFT // table's own rather than a number typed again here — `RouteTests` // is the one place that states it, and a route added on one side // of the boundary alone is exactly what this call would miss. XCTAssertEqual(routes.count, Route.allPatterns.count) XCTAssertTrue(routes.contains("/capture")) XCTAssertTrue(routes.contains("/")) } func testTheCoreCanBeAskedForAVersionRepeatedlyWithoutLeakingTheHandle() { // Called from a loop because `cotton_string_free` is the caller's // job and a boundary that leaked would leak here first. Nothing // asserts bytes; what this proves is that the call is re-entrant // and side-effect-free, which is why it is safe as the smoke test. for _ in 0 ..< 32 { guard let raw = cotton_version() else { return XCTFail("this ABI never answers a null string") } cotton_string_free(raw) } } #endif }