Cotton code
36 lines · 2.3 KB · SwiftRaw
import CottonKitimport CottonValuesimport Foundationimport XCTest@testable import CottonUIfinal class CommandPickScopeTests: XCTestCase {    /// RULE: A declared picker database comes from the origin record, never    /// the window's later selection, for both list and get calls.    func testOriginDatabaseIsCapturedAndOverridesOuterWindowScope() async throws {        let pick = try JSONDecoder().decode(RecordCommandPick.self, from: Data(#"{"kind":"record","table":"project","argument":"project","database_by":"$id"}"#.utf8))        let origin = TableRecord(id: "origin-D", values: [:])        XCTAssertEqual(try pick.database(in: origin), "origin-D")        let core = ScriptedCore(["records.list": .ok(#"{"records":[],"next_page":null}"#), "records.get": .ok("{}")])        let selected = ShownDatabase("window-E")        let scoped = DatabaseScopedCore(underlying: DatabaseScopedCore(underlying: core, shown: selected), database: try pick.database(in: origin))        _ = try await scoped.callJSON("records.list", ["table": JSONValue.string("project")])        selected.id = "window-F"        _ = try await scoped.callJSON("records.get", ["table": JSONValue.string("project"), "id": JSONValue.string("project-in-D")])        let calls = try await core.calls.map { try JSONDecoder().decode(JSONValue.self, from: Data($0.requestJSON.utf8)) }        XCTAssertTrue(calls.allSatisfy { $0["database"]?.string == "origin-D" })        XCTAssertThrowsError(try pick.database(in: nil))    }    // RULE: The app submits one native command containing the complete ordered answer list.    func testOrderedAnswersUseOneNativeCall() async throws {        let core = ScriptedCore(["records.command": .ok(#"{"command":"book","kind":"create","receipt":"Booked"}"#)])        let command = RecordCommand(id: "book", label: "Book", verb: "reserve_record", kind: .create)        _ = try await core.recordCommand(table: "synthetic_class", id: "synthetic-slot", command: command,            pickedValue: .list([.text("synthetic-member"), .text("3")]))        let calls = await core.calls        XCTAssertEqual(calls.count, 1)        let request = try JSONDecoder().decode(JSONValue.self, from: Data(calls[0].requestJSON.utf8))        XCTAssertEqual(request["picked"], .array([.string("synthetic-member"), .string("3")]))    }}