| index e16ed66..a57fc0f 100644 |
| --- a/native/apple/Tests/DocsKitTests/DocsDeletionTests.swift |
| +++ b/native/apple/Tests/DocsKitTests/DocsDeletionTests.swift |
| @@ -87,8 +87,8 @@ import XCTest |
| func testDeletedDropsTheLaneAndTheReplica() async throws { |
| let (model, _, key) = try await open([.respond(status: 200, body: ok, headers: json)]) |
| XCTAssertNil(model.deleteBlock(offline: false)) |
| - let outcome = await model.deleteDocument() |
| - XCTAssertEqual(outcome, .deleted) |
| + let result = await model.deleteDocument() |
| + XCTAssertEqual(result, .init(outcome: .deleted, wasOpen: true)) |
| XCTAssertFalse(try model.offlineAccount.replicaCache.hasBase(key), "the replica is gone") |
| XCTAssertNil(try model.offlineAccount.metadata(key), "and its metadata") |
| let lane = await model.offlineAccount.outbox.summary.only("doc:" + key) |
| @@ -99,8 +99,8 @@ import XCTest |
| func testNotDeletedKeepsTheLaneAndTheReplica() async throws { |
| let (model, server, key) = try await open([.respond(status: 404)]) |
| await server.answerDetail(200) |
| - let outcome = await model.deleteDocument() |
| - XCTAssertEqual(outcome, .stillThere) |
| + let result = await model.deleteDocument() |
| + XCTAssertEqual(result?.outcome, .stillThere) |
| XCTAssertTrue(try model.offlineAccount.replicaCache.hasBase(key), "the replica is kept") |
| XCTAssertNotNil(try model.offlineAccount.metadata(key)) |
| XCTAssertEqual(model.detail?.sqid, "doc", "the document stays open") |
| @@ -109,6 +109,50 @@ import XCTest |
| await model.stop() |
| } |
| |
| + /// Fable on #178, finding 1: the answer comes after another document |
| + /// was opened (back to the library, open B, on a slow link). Only A's |
| + /// lane, replica and metadata go; B, open now, keeps all of its own, and |
| + /// the model stays on B. |
| + func testADeleteAnsweredAfterAnotherDocumentOpenedDropsOnlyItsOwn() async throws { |
| + let (model, server, keyA) = try await open([.respond(status: 200, body: ok, headers: json)]) |
| + try await server.b.seed([.object(["type": .string("paragraph"), "children": .array([ |
| + .object(["type": .string("text"), "runs": .array([.object(["insert": .string("B's text"), "marks": .object([:])])])])])])]) |
| + await server.holdDelete() |
| + let deleting = Task { await model.deleteDocument() } |
| + let deadline = Date().addingTimeInterval(5) |
| + while !(await server.deleteWaiting) { |
| + guard Date() < deadline else { return XCTFail("the delete never reached the server") } |
| + try await Task.sleep(for: .milliseconds(5)) |
| + } |
| + await model.open("b") |
| + XCTAssertEqual(model.detail?.sqid, "b", "B is open while A's delete waits") |
| + let keyB = try model.offlineAccount.key(for: "b") |
| + XCTAssertNotEqual(keyA, keyB) |
| + // An unsent change in B: its lane must survive A's delete. |
| + XCTAssertTrue(model.enqueue(["op": .string("display_edit"), "at": .number(0), "length": .number(0), "text": .string("x")]), |
| + "B takes typing: A's delete does not hold it") |
| + let typedDeadline = Date().addingTimeInterval(5) |
| + while await model.offlineAccount.outbox.summary.only("doc:" + keyB).isEmpty { |
| + guard Date() < typedDeadline else { return XCTFail("B's change never reached its lane") } |
| + try await Task.sleep(for: .milliseconds(5)) |
| + } |
| + await model.persistReplica() |
| + |
| + await server.releaseDelete() |
| + let result = await deleting.value |
| + XCTAssertEqual(result, .init(outcome: .deleted, wasOpen: false), "A was deleted, and it was no longer the open document") |
| + XCTAssertEqual(model.detail?.sqid, "b", "the model stays on B") |
| + XCTAssertTrue(try model.offlineAccount.replicaCache.hasBase(keyB), "B's replica survives") |
| + XCTAssertNotNil(try model.offlineAccount.metadata(keyB), "and its metadata") |
| + let laneB = await model.offlineAccount.outbox.summary.only("doc:" + keyB) |
| + XCTAssertFalse(laneB.isEmpty, "and its unsent lane") |
| + XCTAssertFalse(try model.offlineAccount.replicaCache.hasBase(keyA), "A's replica is gone") |
| + XCTAssertNil(try model.offlineAccount.metadata(keyA), "and A's metadata") |
| + let laneA = await model.offlineAccount.outbox.summary.only("doc:" + keyA) |
| + XCTAssertTrue(laneA.isEmpty, "and A's lane") |
| + await model.stop() |
| + } |
| + |
| /// Owners only; never a draft; refused while anything is unsent. |
| func testDeleteIsRefusedWithUnsentChangesAndForNonOwners() async throws { |
| let (model, _, _) = try await open([]) |
| @@ -130,16 +174,33 @@ import XCTest |
| actor DeleteServer: NativeTransport { |
| nonisolated let origin = URL(string: "https://docs.delete.invalid")! |
| let inner = LoopServer() |
| + /// A second document, "b": the same LoopServer routes under /d/b. Its |
| + /// update POSTs are refused (503), so what B types stays in its lane. |
| + let b = LoopServer() |
| private var deleteSteps: [ScriptedTransport.Step] |
| private var detailStatus = 200 |
| private(set) var deleteCalls = 0 |
| + private var holding = false |
| + private var gate: CheckedContinuation<Void, Never>? |
| |
| init(_ steps: [ScriptedTransport.Step]) { deleteSteps = steps } |
| func answerDetail(_ status: Int) { detailStatus = status } |
| + func holdDelete() { holding = true } |
| + var deleteWaiting: Bool { gate != nil } |
| + func releaseDelete() { holding = false; gate?.resume(); gate = nil } |
| |
| func responseDetails(_ path: String, method: String, body: Data?, contentType: String, maxBytes: Int) async throws -> NativeResponse { |
| + if path == "/d/b" || path.hasPrefix("/d/b/") || path.hasPrefix("/d/b?") { |
| + if method == "GET", path == "/d/b" { |
| + let detail = DocumentDetail(sqid: "b", title: "B", guid: "render-loop", can_write: true, can_share: true) |
| + return NativeResponse(data: try JSONEncoder().encode(detail), status: 200, headers: [:]) |
| + } |
| + if method == "POST" { return NativeResponse(data: Data(), status: 503, headers: [:]) } |
| + return try await b.responseDetails("/d/doc" + path.dropFirst(4), method: method, body: body, contentType: contentType, maxBytes: maxBytes) |
| + } |
| if method == "POST", path == "/d/doc/delete" { |
| deleteCalls += 1 |
| + if holding { await withCheckedContinuation { gate = $0 } } |
| guard !deleteSteps.isEmpty else { throw NativeClientError.message("DeleteServer: no scripted answer left for the delete") } |
| switch deleteSteps.removeFirst() { |
| case .respond(let status, let body, let headers): |
| @@ -157,6 +218,7 @@ actor DeleteServer: NativeTransport { |
| return try await inner.responseDetails(path, method: method, body: body, contentType: contentType, maxBytes: maxBytes) |
| } |
| func stream(_ path: String, onOpen: @Sendable () async throws -> Void, onLine: @Sendable (String) async throws -> Void) async throws { |
| + if path.hasPrefix("/d/b/") { return try await b.stream("/d/doc" + path.dropFirst(4), onOpen: onOpen, onLine: onLine) } |
| try await inner.stream(path, onOpen: onOpen, onLine: onLine) |
| } |
| func download(_ path: String, to destination: URL) async throws { throw NativeClientError.notFound } |