importCottonKitimportCottonValuesimportFoundationimportSwiftUIimport XCTest@testable importCottonUI@MainActorfinalclassUsageRecordingTests: XCTestCase {privatestructSource: SettingsTableSource {let table = "example"func records(_ core: any CottonCalling) asyncthrows -> [TableRecord] { [TableRecord(id: "invented-one", values: ["title": .text("Invented")]), TableRecord(id: "invented-two", values: ["title": .text("Invented second")])] }func perform(_ action: String, record: TableRecord?, core: any CottonCalling) asyncthrows -> SettingsActed { .outcome(.said("Done")) } }privatefunc setup() throws -> (ScriptedCore, SettingsRecords, TableDefinition) {let definition = tryTableDefinition.decode(Data(""" {"id":"example","name":"Example","plural":"Examples","builtin":false, "columns":[{"key":"title","type":"text"}],"title":"title", "slip":{"title":"title"},"tile":{"face":"slip-list"},"page":[{"heading":null,"columns":["title"]}]} """.utf8))let core = ScriptedCore([CottonMethod.usageRecord: .ok("{\"recorded\":true}")])return (core, SettingsRecords(core: core, definitions: [definition], sources: [Source()]), definition) }privatefunc events(_ core: ScriptedCore) asyncthrows -> [[String: String]] {tryawait core.calls.filter { $0.method == CottonMethod.usageRecord }.map { call inlet raw = try JSONSerialization.jsonObject(with: Data(call.requestJSON.utf8)) as! [String: Any]return raw.compactMapValues { $0 as? String } } }/// RULE: A painted page is not an open and a fan's turn is not an open: a list logs its open only when the person sees it — never painted for its picture, never mounted by the fan (whose settled card says its own) — and a record route never logs a view (294, walker293: 423 opens in 37 min, none made).func testAPaintedOrTurnedPastPageIsNotAnOpen() {let list = Route.notes XCTAssertTrue(RouteScreen.countsOpen(list, paintsPicture: false, countsOpens: true), "a list the person opened") XCTAssertFalse(RouteScreen.countsOpen(list, paintsPicture: true, countsOpens: true), "painted for its picture") XCTAssertFalse(RouteScreen.countsOpen(list, paintsPicture: false, countsOpens: false), "mounted by the fan") XCTAssertFalse(RouteScreen.countsOpen(.notesNote(id: "invented-one"), paintsPicture: false, countsOpens: true), "a record's page logs its own open") }/// RULE: A record route leaves its open to the successfully loaded page; logging it as a view too would inflate the same open on both hosts.func testViewRoutingLeavesRecordOpensToTheirPage() asyncthrows {let (core, store, _) = try setup()await store.recordViewOpened(.notesNote(id: "invented-one"))await store.recordViewOpened(.tableRecord(table: "example", id: "invented-one"))await store.recordViewOpened(.todosTodo(id: "invented-todo"))let records = tryawait events(core) XCTAssertTrue(records.isEmpty)await store.recordViewOpened(.notes)let views = tryawait events(core) XCTAssertEqual(views.map { $0["kind"] }, ["view_opened"]) }}