import CottonKit import CottonValues import XCTest @testable import CottonUI @MainActor final class YouRecordTests: XCTestCase { // RULE: You opens the native canonical person in its explicit personal database; a shared database's namesake is never looked up or changed. func testYouUsesTheCanonicalIDAndScopesThePersonReadAndNameWrite() async throws { let core = ScriptedCore([ CottonMethod.readYou: .ok(#"{"database_id":"mine","person_id":"me","name":"Example Person","print":null,"sign_in":["This phone was admitted on 7 October"],"devices_count":2}"#), CottonMethod.recordsGet: .ok(#"{"id":"me","data":{"full_name":"Example Person","avatar_key":"mine/people/me/print.jpg"}}"#), "records.update": .ok(#"{"change":{"id":"change-1","record_id":"me","before":{"full_name":"Example Person"},"after":{"full_name":"Renamed Person"},"undoable":true},"record":{"id":"me","data":{"full_name":"Renamed Person","avatar_key":"mine/people/me/print.jpg"}}}"#), ]) let store = PersonPageStores().store(core: DatabaseScopedCore(underlying: core, database: "shared"), database: "mine", bundle: try TestResources.bundle()).records let row = try await store.get(table: "person", id: "me") XCTAssertEqual(row?["avatar_key"].text, "mine/people/me/print.jpg") _ = try await store.update(table: "person", id: "me", patch: ["full_name": .text("Renamed Person")]) let calls = await core.calls for call in calls where call.method == CottonMethod.recordsGet || call.method == "records.update" { let body = try JSONDecoder().decode([String: RecordValue].self, from: Data(call.requestJSON.utf8)) XCTAssertEqual(body["database"], .text("mine")) XCTAssertEqual(body["id"], .text("me")) } XCTAssertFalse(calls.contains { $0.method == CottonMethod.recordsList }, "You never searches by name") XCTAssertFalse(calls.contains { $0.method == "tools.list" }, "the built-in person never depends on imported tools") } // RULE: An unnamed database or an archived owner answers null identity; // never replace that absence with a namesake. func testAnAbsentOwnerDecodes() throws { let json = #"{"database_id":"mine","person_id":null,"name":null,"print":null,"sign_in":["This device is not admitted to this database.","No device is admitted.","No Mac serves this database yet."],"devices_count":0}"# let identity = try JSONDecoder().decode(YouRead.self, from: Data(json.utf8)) XCTAssertNil(identity.personId) XCTAssertNil(identity.name) } private static let linked = #"{"database_id":"mine","owner":"linked","person_id":"me","name":"Example Person","print":null,"sign_in":[],"devices_count":1}"# // RULE: The object names who you are (the person carrying the sign-in number) and nothing on the phone chooses: read.you's answer `owner: "none_yet"` — a copy that does not hold that person yet, never a refusal — says so in one shell line until the next pass (never a blank page), a linked person opens as themselves, and a refusal is said as it came. No method on the phone links you. func testMeOpensThePersonTheObjectNamesAndWaitsWhileTheCopyHasNone() async { let unlinked = ScriptedCore([CottonMethod.readYou: .ok(#"{"database_id":"mine","owner":"none_yet","person_id":null,"name":null,"print":null,"sign_in":[],"devices_count":1}"#)]) guard case .notYet = await MeOpening.read(unlinked) else { return XCTFail("a copy without you yet waits") } XCTAssertNotEqual(MeWords.notYet, "record.me.not_yet", "the wait is said in a shipped line") let linked = ScriptedCore([CottonMethod.readYou: .ok(Self.linked)]) guard case .person(let you) = await MeOpening.read(linked) else { return XCTFail("the linked person opens") } XCTAssertEqual(you.personId, "me") XCTAssertEqual(you.databaseId, "mine") let sent = await linked.calls XCTAssertEqual(sent.map(\.method), [CottonMethod.readYou]) let refused = ScriptedCore([CottonMethod.readYou: .refuse(code: "unreadable", message: "This phone's copy could not be read.")]) guard case .refused(let words) = await MeOpening.read(refused) else { return XCTFail("a refusal is said") } XCTAssertEqual(words, "This phone's copy could not be read.") } }