Docs native: test that an adopted sequence never outruns the stored replica
Review note on #145: the echo skip adopts the server's sequence for an own POST without applying anything, which is safe only because the send path stores the sent update in the replica before its entry is marked sent. The test types while offline and crashes before the replica is persisted (the edit only in the outbox), reopens, sends with the stream open so the POST's sequence is adopted (no catch-up GET), stops, and reopens offline from the stored bytes: the edit is there and the stored sequence is the adopted row.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+53
−0
native/apple/Tests/DocsKitTests/RealtimeSyncTests.swift+53 −0
diff --git a/native/apple/Tests/DocsKitTests/RealtimeSyncTests.swift b/native/apple/Tests/DocsKitTests/RealtimeSyncTests.swift| index 5b419c7..83046c3 100644 |
| --- a/native/apple/Tests/DocsKitTests/RealtimeSyncTests.swift |
| +++ b/native/apple/Tests/DocsKitTests/RealtimeSyncTests.swift |
| @@ -180,6 +180,59 @@ private final class MetadataReadCounter: BlobStore, @unchecked Sendable { |
| await model.discardAll() |
| } |
| + /// Review note on #145: the echo skip adopts the server's sequence for an |
| + /// own POST without applying anything, which is only safe because the |
| + /// send path stores the sent update in the replica before the entry is |
| + /// marked sent. Crash after typing (the edit only in the outbox), reopen, |
| + /// send and adopt the sequence, reopen offline: the edit must be there. |
| + func testAnAdoptedSequenceNeverOutrunsTheStoredReplica() async throws { |
| + let server = self.server! |
| + let disk = FaultingBlobStore(), id = UUID().uuidString |
| + let offline = NativeConnectivity(source: ScriptedPathSource(.unsatisfied)) |
| + let first = DocsOfflineAccount(store: disk, accountID: id, connectivity: offline, transport: { server }) |
| + let writer = DocsModel(transport: { server }, connectivity: offline, offlineAccount: first) |
| + await writer.open("doc") |
| + writer.didRender(writer.projectionGeneration) |
| + XCTAssertTrue(writer.enqueue(["op": .string("display_edit"), "at": .number(0), "length": .number(0), "text": .string("Kept")])) |
| + await until(5) { !writer.hasPendingInput } |
| + first.drainer.stop(); disk.crash(after: 0) |
| + writer.suspendLiveUpdates() // the dead process hears nothing more |
| + // A new process sends the lane and adopts the POST's sequence. |
| + let store = disk.committed |
| + // The network returns once the document is open, so it is this |
| + // replica's POST whose answer is adopted. |
| + // The outbox sends only once the document's stream is open, so the |
| + // POST's answer reaches this replica, which adopts its sequence. |
| + let path = ScriptedPathSource(.unsatisfied) |
| + let sending = NativeConnectivity(source: path) |
| + let second = DocsOfflineAccount(store: store, accountID: id, connectivity: sending, transport: { server }) |
| + let sender = DocsModel(transport: { server }, connectivity: NativeConnectivity(source: ScriptedPathSource(.satisfied)), offlineAccount: second) |
| + await sender.open("doc") |
| + sender.didRender(sender.projectionGeneration) |
| + XCTAssertEqual(plain(sender), "Kept") |
| + await until(5) { await server.openStreams == 1 } |
| + try await Task.sleep(for: .milliseconds(100)) |
| + path.send(.satisfied) |
| + await until(5) { sending.isPathSatisfied } |
| + second.drainer.kick() |
| + await until(5) { await second.outbox.summary.isEmpty } |
| + try await Task.sleep(for: .milliseconds(300)) |
| + let fetched = await server.catchUps |
| + XCTAssertEqual(fetched, 0, "the own save's sequence was adopted, not fetched") |
| + let row = await server.sequence() |
| + XCTAssertEqual(try second.replicaCache.load("doc")?.sequence ?? 0 <= row, true) |
| + await sender.stop(); second.drainer.stop() |
| + XCTAssertEqual(try second.replicaCache.load("doc")?.sequence, row, "the stored replica reached the adopted row") |
| + // Offline again, from the stored bytes only: the edit is there. |
| + let third = DocsOfflineAccount(store: MemoryBlobStore(store.contents), accountID: id, connectivity: offline, transport: { server }) |
| + let reader = DocsModel(transport: { server }, connectivity: offline, offlineAccount: third) |
| + await reader.open("doc") |
| + XCTAssertEqual(plain(reader), "Kept") |
| + let empty = await third.outbox.summary.isEmpty |
| + XCTAssertTrue(empty) |
| + await reader.stop(); third.drainer.stop() |
| + } |
| + |
| /// The web's guard: a POST whose sequence is not exactly the next row |
| /// must not move the cursor past the row someone else wrote first. |
| func testAPeerRowBeforeAnOwnSaveIsNeverSkipped() async throws { |