import CottonKit import XCTest /// A page act's answer read as the core gave it (Spec 11 (c)): its receipt, /// and the undo it named — called as named, never rebuilt by the app. final class ActAnswerTests: XCTestCase { private func json(_ text: String) throws -> JSONValue { try JSONDecoder().decode(JSONValue.self, from: Data(text.utf8)) } /// RULE: An act's answer that says undoable false offers no undo whatever else it carries; one with no undoable key offers the undo it names; a blank receipt is no receipt. func testUndoableFalseOffersNoUndoAndANamedUndoIsKeptAsGiven() throws { let removed = ActAnswer(try json(#"{"receipt":"Removed Studio Mac","undoable":false,"undo":null}"#)) XCTAssertEqual(removed.receipt, "Removed Studio Mac") XCTAssertNil(removed.undo) let said = ActAnswer(try json(#"{"receipt":"x","undoable":false,"undo":{"method":"devices.rename","args":{}}}"#)) XCTAssertNil(said.undo, "undoable false wins over an undo") let renamed = ActAnswer(try json(#"{"receipt":"Cap colour teal","undo":{"method":"databases.recolour","args":{"databaseId":"db-1","colour":null}}}"#)) XCTAssertEqual(renamed.undo, ActUndo(method: "databases.recolour", args: try json(#"{"databaseId":"db-1","colour":null}"#))) XCTAssertNil(ActAnswer(try json(#"{"receipt":""}"#)).receipt) XCTAssertEqual(ActAnswer(try json("{}")), .none) } /// RULE: Taking an act back calls the undo's method with its arguments exactly, and answers that call's own receipt. func testTakingBackCallsTheNamedMethodWithItsArgs() async throws { let core = ScriptedCore(["devices.rename": .ok(#"{"receipt":"Renamed to Example Mac","undoable":true,"undo":{"method":"devices.rename","args":{"deviceId":"d-1","name":"Desk"}}}"#)]) let undone = try await core.takeBack(ActUndo(method: "devices.rename", args: try json(#"{"deviceId":"d-1","name":"Example Mac"}"#))) XCTAssertEqual(undone.receipt, "Renamed to Example Mac") let calls = await core.calls XCTAssertEqual(calls.map(\.method), ["devices.rename"]) let sent = try json(try XCTUnwrap(calls.first).requestJSON) XCTAssertEqual(sent, try json(#"{"deviceId":"d-1","name":"Example Mac"}"#)) } }