Cotton code
62 lines · 4.0 KB · SwiftRaw
import CottonKitimport CottonValuesimport Foundationimport XCTest@testable import CottonUIfinal class CommandPickingTests: XCTestCase {    private func command(_ json: String) throws -> RecordCommand {        try JSONDecoder().decode(RecordCommand.self, from: Data(json.utf8))    }    /// RULE: A text command asks for nonblank trimmed words before writing; its declared argument and account binding survive decoding.    func testATextPickKeepsItsArgumentAndRequiresWords() throws {        let record = try command(#"{"id":"record","label":"Record","kind":"create","verb":"create_record","data":{"table":"transaction"},"bindings":{"data.money_account_id":"$id"},"pick":{"kind":"text","argument":"data.description"}}"#)        XCTAssertEqual(record.pick?.kind, .text)        XCTAssertEqual(record.pick?.argument, "data.description")        XCTAssertEqual(record.bindings?["data.money_account_id"], "$id")        XCTAssertTrue(record.needsPick)        XCTAssertEqual(try JSONDecoder().decode(RecordCommand.self, from: JSONEncoder().encode(record)), record)        for blank in ["", " ", "\n\t", "\u{00A0}"] {            XCTAssertNil(RecordCardPage.picked(.text(blank), for: record))        }        XCTAssertEqual(RecordCardPage.picked(.text(" \nspent 450 on groceries at quay street grocer\t "), for: record),                       "spent 450 on groceries at quay street grocer")    }    /// RULE: A record command keeps its complete native picker filter, including alternatives that override a table's default section.    func testARecordPickKeepsItsCategoryAndSectionFilter() throws {        let classify = try command(#"{"id":"classify","label":"Classify","kind":"update","verb":"classify_transaction","pick":{"kind":"record","table":"money_account","argument":"categoryAccountId","where":{"subtype":"category","section":["income","spending"]}}}"#)        XCTAssertEqual(classify.pick?.filter, ["subtype": .text("category"), "section": .list([.text("income"), .text("spending")])])        XCTAssertEqual(try JSONDecoder().decode(RecordCommand.self, from: JSONEncoder().encode(classify)), classify)    }    /// RULE: A typed command pick survives decoding and is never offered as a one-tap write; table picks keep their existing contract.    func testPickedCommandsKeepTheirArgumentAndTarget() throws {        let snooze = try command(#"{"id":"snooze","label":"Snooze","kind":"update","verb":"update_todo","pick":{"argument":"snoozedUntil","kind":"datetime"}}"#)        XCTAssertEqual(snooze.pick?.kind, .datetime)        XCTAssertEqual(snooze.pick?.argument, "snoozedUntil")        XCTAssertNil(snooze.pick?.table)        let move = try command(#"{"id":"move","label":"Move","kind":"update","verb":"update_record","pick":{"argument":"patch.project","kind":"record","table":"project"}}"#)        XCTAssertEqual(move.pick?.kind, .record)        XCTAssertEqual(move.pick?.argument, "patch.project")        XCTAssertEqual(move.pick?.table, "project")        let file = try command(#"{"id":"file","label":"File","pick_table":"as"}"#)        for picked in [snooze, move, file] {            XCTAssertTrue(picked.needsPick)            XCTAssertEqual(try JSONDecoder().decode(RecordCommand.self, from: JSONEncoder().encode(picked)), picked)        }        XCTAssertFalse(try command(#"{"id":"done","label":"Done"}"#).needsPick)        XCTAssertThrowsError(try command(#"{"id":"x","label":"X","pick":{"argument":"x","kind":"script"}}"#))    }    func testThePickedInstantKeepsTheExplicitCalendarsOffset() throws {        var calendar = Calendar(identifier: .gregorian)        calendar.timeZone = try XCTUnwrap(TimeZone(identifier: "Asia/Kolkata"))        let day = try XCTUnwrap(calendar.date(from: DateComponents(year: 2026, month: 10, day: 9)))        let picked = WhenValue(day: day, time: ClockTime(hour: 9, minute: 0)).stored(calendar: calendar)        XCTAssertEqual(picked, .text("2026-10-09T03:30:00.000Z"))        XCTAssertEqual(WhenValue.read(picked, calendar: calendar)?.time, ClockTime(hour: 9, minute: 0))    }}