importCottonKitimportCottonValuesimport XCTest@testable importCottonUI@MainActorfinalclassYouRecordTests: 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() asyncthrows {let core = ScriptedCore([CottonMethod.readYou: .ok(#"{"database_id":"mine","person_id":"me","name":"ExamplePerson","print":null,"sign_in":["This phone was admitted on 7October"],"devices_count":2}"#),CottonMethod.recordsGet: .ok(#"{"id":"me","data":{"full_name":"ExamplePerson","avatar_key":"mine/people/me/print.jpg"}}"#),"records.update": .ok(#"{"change":{"id":"change-1","record_id":"me","before":{"full_name":"ExamplePerson"},"after":{"full_name":"RenamedPerson"},"undoable":true},"record":{"id":"me","data":{"full_name":"RenamedPerson","avatar_key":"mine/people/me/print.jpg"}}}"#), ])let store = PersonPageStores().store(core: DatabaseScopedCore(underlying: core, database: "shared"), database: "mine", bundle: tryTestResources.bundle()).recordslet row = tryawait store.get(table: "person", id: "me") XCTAssertEqual(row?["avatar_key"].text, "mine/people/me/print.jpg") _ = tryawait store.update(table: "person", id: "me", patch: ["full_name": .text("Renamed Person")])let calls = await core.callsfor 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.","NoMac serves this database yet."],"devices_count":0}"#let identity = try JSONDecoder().decode(YouRead.self, from: Data(json.utf8)) XCTAssertNil(identity.personId) XCTAssertNil(identity.name) }privatestaticlet linked = #"{"database_id":"mine","owner":"linked","person_id":"me","name":"ExamplePerson","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}"#)])guardcase .notYet = awaitMeOpening.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)])guardcase .person(let you) = awaitMeOpening.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.")])guardcase .refused(let words) = awaitMeOpening.read(refused) else { return XCTFail("a refusal is said") } XCTAssertEqual(words, "This phone's copy could not be read.") }}