import CottonKit import CottonValues import XCTest @testable import CottonUI final class DatabasePickingTests: XCTestCase { /// RULE: A command that reaches into another database offers only the person's own other databases — never the open one, never one somebody else owns (one this device was removed from is not held at all). func testOnlyThePersonsOwnOtherDatabasesAreOffered() { let own = HeldDatabase(databaseId: "db-own", name: "Personal", kind: .personal) let kitchen = HeldDatabase(databaseId: "db-kitchen", name: "Kitchen", kind: .shared) var theirs = HeldDatabase(databaseId: "db-theirs", name: "Their studio", kind: .shared) theirs.rights = .member let offered = DatabasePicking.choices([own, kitchen, theirs], open: "db-own") XCTAssertEqual(offered.map(\.databaseId), ["db-kitchen"]) XCTAssertEqual(DatabasePicking.choices([own, kitchen], open: "db-kitchen").map(\.databaseId), ["db-own"]) } /// RULE: The Domains definition Cotton ships decodes on the phone with its three acts — Register and Hand over ask first, in words that say what is charged and what is given, and Hand over picks a database. /// /// The acts are drawn from this file. A command the phone cannot decode is /// a page with no way to do the act, and one that did not ask first would /// buy a domain on one tap. func testTheShippedDomainsDefinitionCarriesItsActsAndAsksFirst() throws { let file = SourceSweep.repoRoot.appendingPathComponent("crates/cotton-tools/src/connections/domains/tables/domain.json") let domain = try TableDefinition.decode(Data(contentsOf: file)) let commands = try XCTUnwrap(domain.commands) func act(_ id: String) throws -> RecordCommand { try XCTUnwrap(commands.first { $0.id == id }, "no \(id) act") } let check = try act("check_price"), register = try act("register"), handOver = try act("hand_over") XCTAssertFalse(check.asksFirst, "asking a price changes nothing") XCTAssertTrue(register.asksFirst) XCTAssertFalse(try XCTUnwrap(register.confirm).isEmpty, "registering asks nothing before charging") XCTAssertEqual(register.bindings?["price"], "price", "the price sent is the price on the row") XCTAssertTrue(handOver.asksFirst) XCTAssertFalse(try XCTUnwrap(handOver.confirm).isEmpty, "handing over asks nothing before giving") XCTAssertEqual(handOver.pick?.kind, .database) XCTAssertEqual(handOver.pick?.argument, "database") XCTAssertNil(handOver.pick?.table) } /// RULE: A domain's page draws "Reach this household here" under Points to, without asking first, and the act writes how the household is reached with that domain as its address — its Undo is the write's own way back, sent back as the core answered it; nothing the page says reads "door". func testADomainCanBeWhereTheHouseholdIsReachedAndItsUndoIsTheWritesOwn() throws { let file = SourceSweep.repoRoot.appendingPathComponent("crates/cotton-tools/src/connections/domains/tables/domain.json") let domain = try TableDefinition.decode(Data(contentsOf: file)) let reach = try XCTUnwrap(domain.commands?.first { $0.id == "reach_here" }) XCTAssertEqual(reach.method, "workspace.setReach") XCTAssertEqual(reach.bindings?["address"], "name", "the address is the domain's own name") XCTAssertFalse(reach.asksFirst, "drawn on the page, taken back by its Undo") let answered = #"{"command":"reach_here","kind":"update","receipt":"Reached directly","undo":null,"answer":{"through":"direct"},"open":null,"undo_call":{"method":"workspace.setReach","args":{"through":"relay","address":null}}}"# let outcome = try JSONDecoder().decode(CommandOutcome.self, from: Data(answered.utf8)) let change = try outcome.change(table: "domain", record: "dom-1") XCTAssertTrue(change.undoable) XCTAssertEqual(change.said?.said, "Reached directly") let call = try XCTUnwrap(change.command?.undoCall) XCTAssertEqual(call.method, "workspace.setReach") let args = try JSONDecoder().decode(JSONValue.self, from: Data(call.args.utf8)) XCTAssertEqual(args, .object(["through": .string("relay"), "address": .null])) } }