Cotton code
118 lines · 4.5 KB · SwiftRaw
import XCTest@testable import CottonKit/// The three rules AGENTS.md states about what a build is allowed to say.////// Every one of them is a decision `reportingPlan` makes from values, which is/// the whole reason the decision is a function and not a branch inside the SDK/// wiring: this file can reach it and the wiring's file cannot be tested at all.final class ReportingTests: XCTestCase {    private func facts(        dsn: String = "https://key@bugsink.example.test/9",        variant: String = "Release",        packaged: Bool = true,        version: String = "1.0.0",        build: String = "131"    ) -> BuildFacts {        BuildFacts(dsn: dsn, variant: variant, packaged: packaged, version: version, build: build)    }    // MARK: - The three "no"s    func testADebugBuildNeverSendsHoweverWellConfiguredItIs() {        // The `__DEV__` rule. Everything else about this build says "send".        let plan = reportingPlan(facts(), debugBuild: true)        XCTAssertFalse(plan.sends)        XCTAssertEqual(plan.dsn, "", "a plan that does not send carries no address")        XCTAssertFalse(plan.reason.isEmpty)    }    func testARunThatIsNotOneOfTheAppsOwnBundlesNeverSends() {        // An XCTest host, a preview, anything that is not the app or one of its        // three extensions.        let plan = reportingPlan(facts(packaged: false), debugBuild: false)        XCTAssertFalse(plan.sends)        XCTAssertFalse(plan.reason.isEmpty)    }    func testABuildWithNoBugsinkAddressSendsNothingAndSaysSo() {        // Every build today, until the sink follow-up lands. It is not broken —        // and the reason says which of the three "no"s it is.        let plan = reportingPlan(facts(dsn: ""), debugBuild: false)        XCTAssertFalse(plan.sends)        XCTAssertFalse(plan.reason.isEmpty)    }    // MARK: - The label is build-variant truth    // MARK: - The flavor rides on every event    // MARK: - What leaves the phone    func testTheHomeDirectoryIsFoldedOutOfEveryMessageAndEveryContextValue() {        let home = "/var/mobile/Containers/Data/Application/DEAD-BEEF"        let scrubbed = Reporting.scrub(            Report(                kind: .unreadableAnswer,                message: "could not read \(home)/Documents/cotton.db",                context: ["path": "\(home)/tmp/snapshot", "method": "read.home"]            ),            home: home        )        XCTAssertEqual(scrubbed.message, "could not read ~/Documents/cotton.db")        XCTAssertEqual(scrubbed.context["path"], "~/tmp/snapshot")        XCTAssertEqual(scrubbed.context["method"], "read.home", "nothing else is rewritten")    }    func testTheHomeDirectoryIsFoldedOutOfTheLogLinesToo() {        let home = "/var/mobile/Containers/Data/Application/DEAD-BEEF"        let scrubbed = Reporting.scrub(            Report(kind: .uncaught, message: "x", log: ["10:00:00.000 opened \(home)/life.db"]),            home: home        )        XCTAssertEqual(scrubbed.log, ["10:00:00.000 opened ~/life.db"])    }    // MARK: - What every report is dressed with    private let surroundings = Surroundings(        release: "cotton-ios@1.0.0+142",        device: Hardware(model: "iPhone16,2", os: "iOS 18.6.1"),        route: "/messages/thread/abc",        log: ["10:00:00.000 route /", "10:00:01.000 route /messages/thread/abc"]    )    // MARK: - Which errors are worth an event    // MARK: - The seam    func testTheDefaultSinkDropsAndAnInstalledSinkReceivesScrubbedReports() {        final class Spy: ReportSink, @unchecked Sendable {            let lock = NSLock()            var seen: [Report] = []            func send(_ report: Report) {                lock.lock()                defer { lock.unlock() }                seen.append(report)            }        }        // The default drops — a build with nothing configured has ONE silent        // path, and it is this one.        XCTAssertTrue(Reporting.current() is DroppingSink)        let spy = Spy()        Reporting.install(spy)        defer { Reporting.install(DroppingSink()) }        Reporting.report(            Report(kind: .unexpectedRefusal, message: "at \(NSHomeDirectory())/x", context: [:])        )        XCTAssertEqual(spy.seen.count, 1)        XCTAssertEqual(spy.seen.first?.kind, .unexpectedRefusal)        XCTAssertEqual(            spy.seen.first?.message,            "at ~/x",            "a sink cannot forget to scrub, because it never sees the unscrubbed report"        )    }}