Cotton code
80 lines · 4.5 KB · SwiftRaw
// What the Alarms conversion taught every table, each through a key drawn// once for any table (`Definitions/SwitchKeys.swift`): Delete on a long press// with its Undo, the next-alarm line, and the view a definition names above// its list. Every record is invented.import CottonKitimport CottonValuesimport Foundationimport XCTest@testable import CottonUI@MainActorfinal class SwitchKeysTests: XCTestCase {    private static var resources: Bundle? {        Bundle(url: SourceSweep.iosSources.appendingPathComponent("CottonUI/Resources"))    }    private func builtin(_ id: String) throws -> TableDefinition {        let loaded = FixtureDefinitions.load(from: try XCTUnwrap(Self.resources))        XCTAssertEqual(loaded.failures, [])        return try XCTUnwrap(loaded.definitions.first { $0.id == id })    }    private func todo(_ id: String) -> TableRecord {        TableRecord(id: id, values: ["title": .text("Example todo"), "status": .text("open")])    }    // ── Delete (item 9) ────────────────────────────────────────────────────    /// RULE: Delete on a slip's long press goes through the delete verb, takes the slip off and keeps its change for undo.    func testALongPressDeletesThroughTheVerbAndUndoBringsItBack() async throws {        let todos = try builtin("todo")        let store = FixtureRecords(definitions: [todos], records: ["todo": [todo("a"), todo("b")]])        let model = RecordListModel(definition: todos, store: store, core: ListCoreFake(), seeds: ListSeeds())        await model.reload(project: nil, query: "")        let first = try XCTUnwrap(model.records.first)        let change = try await model.delete(first)        XCTAssertNil(change.after)        XCTAssertFalse(model.records.contains { $0.id == first.id })        let gone = try await store.get(table: "todo", id: first.id)        XCTAssertNil(gone)        try await model.undo()        XCTAssertEqual(Set(model.records.map(\.id)), ["a", "b"])    }    // ── The next alarm (item 1) ────────────────────────────────────────────    /// RULE: The next line says the alarm's wall-clock time and how long until it, rounded down: "next: 6:30 am, in 7 h 12 m", "in 5 m", "in less than a minute".    func testTheNextLineSaysTheTimeAndTheCountdown() {        let now = Date(timeIntervalSince1970: 1_790_847_000)        XCTAssertEqual(            AlarmNextWords.line(hour: 6, minute: 30, at: now.addingTimeInterval(7 * 3600 + 12 * 60 + 59), now: now),            "next: 6:30 am, in 7 h 12 m")        XCTAssertEqual(AlarmNextWords.until(now.addingTimeInterval(5 * 60 + 30), now: now), "in 5 m")        XCTAssertEqual(AlarmNextWords.until(now.addingTimeInterval(59), now: now), "in less than a minute")        XCTAssertEqual(AlarmNextWords.clock(hour: 0, minute: 5), "12:05 am")        XCTAssertEqual(AlarmNextWords.clock(hour: 12, minute: 0), "12:00 pm")        XCTAssertEqual(AlarmNextWords.clock(hour: 19, minute: 30), "7:30 pm")    }    // ── Review: what an engine stands above its list ──    /// RULE: The view above a list is named by the table's definition (`list_head`, from the app's fixed vocabulary) and is drawn once by the list for any table — never looked up by an engine's or a table's name, never a header a route hands one table (architect, 13 Oct 2026).    /// The next-alarm line stays a view of its own because it reads device state no record holds (will this phone ring, the clock now), so a summary the engine answered would be stale and could not withhold itself.    func testTheViewAboveAListIsOneItsEngineRegisters() throws {        let todos = try builtin("todo")        XCTAssertFalse(EngineListHead.stands(for: todos))        let headed = 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":[],"list_head":"next_alarm"}            """.utf8))        XCTAssertTrue(EngineListHead.stands(for: headed), "the definition's list_head names the view")        // The engine's name decides nothing: a table naming no head stands none.        var unnamed = todos        unnamed.engine = "alarms"        XCTAssertFalse(EngineListHead.stands(for: unnamed), "a head is looked up by an engine's name")    }}