Cotton code
55 lines · 3.2 KB · SwiftRaw
import CottonKitimport CottonValuesimport Foundationimport SwiftUIimport XCTest@testable import CottonUI@MainActorfinal class UsageRecordingTests: XCTestCase {    private struct Source: SettingsTableSource {        let table = "example"        func records(_ core: any CottonCalling) async throws -> [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) async throws -> SettingsActed { .outcome(.said("Done")) }    }    private func setup() throws -> (ScriptedCore, SettingsRecords, TableDefinition) {        let definition = try TableDefinition.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)    }    private func events(_ core: ScriptedCore) async throws -> [[String: String]] {        try await core.calls.filter { $0.method == CottonMethod.usageRecord }.map { call in            let 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() async throws {        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 = try await events(core)        XCTAssertTrue(records.isEmpty)        await store.recordViewOpened(.notes)        let views = try await events(core)        XCTAssertEqual(views.map { $0["kind"] }, ["view_opened"])    }}