import CottonKit import CottonValues import Foundation import XCTest @testable import CottonUI /// Acts as cards in the tray (owner's ruling, 12 Oct 2026): tap fires, tap /// asks, hold confirms — and what the tray hands back is always a value a card /// can show. final class ActCardTests: XCTestCase { /// RULE: Numeric tray answers become numeric act arguments only inside the declared range; words, nonfinite values and out-of-range answers cannot run an act. func testNumericActAnswersRespectTheDeclaredRange() { let command = RecordCommand(id: "book", label: "Book", verb: "reserve_record", kind: .create, pick: RecordCommandPick(argument: "data.quantity", kind: .number, min: 1, defaultValue: 2, max: 5)) for value in [FieldValue.text("2"), .number(5)] { let picked = RecordCardPage.picked(value, for: command) XCTAssertNotNil(picked.flatMap(Double.init)) } for value in [FieldValue.text("three"), .number(0), .number(6), .number(.infinity)] { XCTAssertNil(RecordCardPage.picked(value, for: command)) } } private static let calendar: Calendar = { var calendar = Calendar(identifier: .gregorian) calendar.timeZone = TimeZone(identifier: "Asia/Kolkata")! return calendar }() /// RULE: a hold fires an irreversible act only after the full duration (three quarters of a second) — a release any earlier fires nothing and ends the hold, so an act that cannot be taken back is never fired by a tap, a brush or a hold let go in time. func testAHoldFiresOnlyAfterTheFullDuration() { let start = Date(timeIntervalSince1970: 1_000) let duration = Double(ActCardKnobs.cardHoldDurationSeconds) var hold = ActHold(duration: duration) XCTAssertFalse(hold.completes(at: start), "nothing held, nothing fires") hold.press(at: start) for early in [0, 0.1, 0.5, duration - 0.001] { XCTAssertFalse(hold.completes(at: start.addingTimeInterval(early)), "\(early)s is not the whole hold") } XCTAssertTrue(hold.completes(at: start.addingTimeInterval(duration))) XCTAssertEqual(hold.progress(at: start.addingTimeInterval(duration / 2)), 0.5, accuracy: 0.001) var early = ActHold(duration: duration) early.press(at: start) XCTAssertFalse(early.release(at: start.addingTimeInterval(duration - 0.01)), "let go early") XCTAssertFalse(early.isHeld) XCTAssertFalse(early.completes(at: start.addingTimeInterval(10)), "and time passing later fires nothing") // A second press while held does not restart the clock or shorten it. var twice = ActHold(duration: duration) twice.press(at: start) twice.press(at: start.addingTimeInterval(-5)) XCTAssertFalse(twice.completes(at: start.addingTimeInterval(0.1))) XCTAssertTrue(twice.release(at: start.addingTimeInterval(duration))) } /// Whether one record can be offered both acts: neither is the other's /// way back, and some values of the columns their `when`s name satisfy /// both. A tray act no definition names (nil) is offered with anything. private static func together(_ a: RecordCommand?, _ b: RecordCommand?) -> Bool { guard let a, let b else { return true } if a.undo == b.id || b.undo == a.id { return false } let conditions = (a.when ?? [:]).merging(b.when ?? [:]) { first, _ in first } var choices: [String: [RecordValue]] = [:] for key in Set(conditions.keys) { var values: [RecordValue] = [.null, .text("\u{1}another")] for when in [a.when?[key], b.when?[key]].compactMap({ $0 }) { var named = when if case .object(let object) = when, let excluded = object["not"] { named = excluded } if case .list(let list) = named { values += list } else { values.append(named) } } choices[key] = values } var records: [[String: RecordValue]] = [[:]] for (key, values) in choices { records = records.flatMap { record in values.map { record.merging([key: $0]) { _, new in new } } } } let filled = [a.opens, b.opens, a.copies, b.copies].compactMap { $0 } return records.contains { values in var record = TableRecord(id: "synthetic", values: values) for column in filled where record.values[column] == nil { record.values[column] = .text("synthetic") } return a.applies(to: record) && b.applies(to: record) } } func testARaisedTrayIsAnythingButRest() { XCTAssertFalse(TrayState.rest.isRaised) XCTAssertTrue(TrayState.pulled.isRaised) XCTAssertTrue(TrayState.offering(TrayOffer(id: "m", title: "This message", acts: [])).isRaised) } }