Docs native: tests for incremental presentations, recovery from base plus updates
CRDTIncrementalTests: 120 steps of typing through generations (reused and fresh sources), peers' updates, undo and redo, each presentation compared with a fresh replica of the same bytes; a peer fed only the returned updates converges. Recovery after a poisoned authority replays the base and 300 updates. Projections cannot be asked for as commands.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+102
−0
native/apple/Tests/DocsKitTests/CRDTIncrementalTests.swift+102 −0
diff --git a/native/apple/Tests/DocsKitTests/CRDTIncrementalTests.swift b/native/apple/Tests/DocsKitTests/CRDTIncrementalTests.swift| new file mode 100644 |
| index 0000000..047c256 |
| --- /dev/null |
| +++ b/native/apple/Tests/DocsKitTests/CRDTIncrementalTests.swift |
| @@ -0,0 +1,102 @@ |
| +import Foundation |
| +import XCTest |
| +@testable import DocsKit |
| + |
| +/// The replica no longer snapshots or projects per command: its state is a |
| +/// base plus the updates since, and presentations patch only changed blocks. |
| +/// Each must stay exactly what a fresh replica of the same bytes shows. |
| +@MainActor final class CRDTIncrementalTests: XCTestCase { |
| + private func describe(_ nodes: [CRDTNode]) -> String { |
| + nodes.map { node in |
| + let runs = (node.runs ?? []).map { run in "\(run.insert)|\((run.marks ?? [:]).keys.sorted())" }.joined(separator: ",") |
| + return "\(node.id):\(node.type):\((node.attrs ?? [:]).sorted { $0.key < $1.key })[\(runs)]{\(describe(node.children ?? []))}" |
| + }.joined(separator: ";") |
| + } |
| + private func truth(_ replica: CRDTReplica) async throws -> String { |
| + let state = try await replica.persistentState().state |
| + let fresh = CRDTReplica(guid: "incremental") |
| + try await fresh.open(update: state) |
| + let projection = try await fresh.projection() |
| + await fresh.close() |
| + return describe(projection.children) |
| + } |
| + private func paragraphs(_ count: Int) -> [String: JSONValue] { |
| + let blocks: [JSONValue] = (0..<count).map { index in |
| + .object(["type": .string(index % 4 == 0 ? "heading" : "paragraph"), "attrs": .object(index % 4 == 0 ? ["level": .string("2")] : [:]), |
| + "children": .array([.object(["type": .string("text"), "runs": .array([.object(["insert": .string("Paragraph \(index) with some words in it."), "marks": .object([:])])])])])]) |
| + } |
| + return ["op": .string("display_paste"), "at": .number(0), "length": .number(0), "blocks": .array(blocks)] |
| + } |
| + |
| + func testPresentationsMatchAWholeProjectionThroughTypingPeersAndUndo() async throws { |
| + let replica = CRDTReplica(guid: "incremental"), peer = CRDTReplica(guid: "incremental") |
| + try await replica.open(update: Data()) |
| + _ = try await replica.command(["op": .string("insert_element"), "at": .number(0), "type": .string("paragraph")]) |
| + _ = try await replica.command(paragraphs(40)) |
| + try await peer.open(update: try await replica.persistentState().state) |
| + let first = try await replica.presentation(source: 0, selection: NSRange(location: 0, length: 0)) |
| + var (source, projection, _) = try XCTUnwrap(first) |
| + var random = SystemRandomNumberGenerator() |
| + var rendered = source |
| + for step in 0..<120 { |
| + let text = projection.children.map(\.plainText).joined(separator: "\n") |
| + let at = Int.random(in: 0..<max(1, text.utf16.count - 2), using: &random) |
| + switch step % 10 { |
| + case 7: |
| + let update = try await peer.command(["op": .string("display_edit"), "at": .number(Double(at / 2)), "length": .number(0), "text": .string("peer")]) |
| + try await replica.apply(update) |
| + case 8: try await peer.apply(try await replica.command(["op": .string("undo")])) |
| + case 9: try await peer.apply(try await replica.command(["op": .string("redo")])) |
| + default: |
| + let update = try await replica.command(["op": .string("display_edit"), "generation": .number(Double(rendered)), "at": .number(Double(at)), |
| + "length": .number(step % 3 == 0 ? 1 : 0), "text": .string(step % 5 == 0 ? "x\ny" : "typed")]) |
| + try await peer.apply(update) |
| + } |
| + await replica.retainGenerations([rendered, source]) |
| + guard let next = try await replica.presentation(source: rendered, selection: NSRange(location: at, length: 0), allowReuse: rendered == source) else { continue } |
| + (source, projection) = (next.0, next.1) |
| + // Acknowledge most renders, so both the reused and fresh paths run. |
| + if step % 4 != 3 { rendered = source } |
| + let expected = try await truth(replica) |
| + XCTAssertEqual(describe(projection.children), expected, "step \(step)") |
| + } |
| + // The peer saw every local update through its own bytes alone. |
| + let mine = try await replica.projection().children.map(\.plainText) |
| + let theirs = try await peer.projection().children.map(\.plainText) |
| + XCTAssertEqual(mine, theirs) |
| + await replica.close(); await peer.close() |
| + } |
| + |
| + func testRecoveryReplaysTheBaseAndEveryUpdateSince() async throws { |
| + let replica = CRDTReplica(guid: "incremental") |
| + try await replica.open(update: Data()) |
| + _ = try await replica.command(["op": .string("insert_element"), "at": .number(0), "type": .string("paragraph")]) |
| + _ = try await replica.command(paragraphs(10)) |
| + for index in 0..<300 { |
| + _ = try await replica.command(["op": .string("display_edit"), "at": .number(Double(index)), "length": .number(0), "text": .string("k")]) |
| + } |
| + let before = try await replica.projection().children.map(\.plainText) |
| + // A rejected update poisons the authority; it is rebuilt from state. |
| + do { try await replica.apply(Data([7, 7, 7])); XCTFail("Malformed update must fail") } catch { } |
| + let after = try await replica.projection().children.map(\.plainText) |
| + XCTAssertEqual(after, before) |
| + XCTAssertTrue(after.joined().hasPrefix(String(repeating: "k", count: 300))) |
| + let state = try await replica.persistentState().state |
| + let reopened = CRDTReplica(guid: "incremental") |
| + try await reopened.open(update: state) |
| + let reread = try await reopened.projection().children.map(\.plainText) |
| + XCTAssertEqual(reread, before) |
| + await replica.close(); await reopened.close() |
| + } |
| + |
| + func testProjectionsCannotBeRequestedAsCommands() async throws { |
| + let replica = CRDTReplica(guid: "incremental") |
| + try await replica.open(update: Data()) |
| + for op in ["project", "project_delta", "open"] { |
| + do { _ = try await replica.command(["op": .string(op)]); XCTFail(op) } catch { } |
| + } |
| + let snapshot = try await replica.command(["op": .string("snapshot")]) |
| + XCTAssertFalse(snapshot.isEmpty) |
| + await replica.close() |
| + } |
| +} |