| 1 | import Foundation |
| 2 | import XCTest |
| 3 | @testable import DocsKit |
| 4 | |
| 5 | /// The replica's revisions and dirty log (spec 2026-11-22 §7 Design 1–2, |
| 6 | /// §11 "Replica dirty log"). |
| 7 | final class ReplicaDirtyLogTests: XCTestCase { |
| 8 | private func replica(limit: Int = 512) async throws -> CRDTReplica { |
| 9 | let replica = CRDTReplica(guid: "dirty-log", dirtyLogLimit: limit) |
| 10 | try await replica.open(update: Data()) |
| 11 | _ = try await replica.command(["op": .string("insert_element"), "at": .number(0), "type": .string("paragraph")]) |
| 12 | let blocks: [JSONValue] = (0..<4).map { index in |
| 13 | .object(["type": .string("paragraph"), "children": .array([.object(["type": .string("text"), "runs": .array([.object(["insert": .string("Block \(index)"), "marks": .object([:])])])])])]) |
| 14 | } |
| 15 | _ = try await replica.command(["op": .string("display_paste"), "at": .number(0), "length": .number(0), "blocks": .array(blocks)]) |
| 16 | return replica |
| 17 | } |
| 18 | private func type(_ text: String, inBlock block: Int, of replica: CRDTReplica) async throws { |
| 19 | let projection = try await replica.projection() |
| 20 | let offset = projection.children.prefix(block).reduce(0) { $0 + $1.plainText.utf16.count + 1 } |
| 21 | _ = try await replica.command(["op": .string("display_edit"), "at": .number(Double(offset)), "length": .number(0), "text": .string(text)]) |
| 22 | } |
| 23 | |
| 24 | func testRevisionsAdvanceOnEveryProjectionWhoeverAsks() async throws { |
| 25 | let replica = try await replica() |
| 26 | let start = await replica.presentedRevision |
| 27 | _ = try await replica.projection() |
| 28 | _ = try await replica.projection() |
| 29 | let presented = try await XCTUnwrapAsync(try await replica.presentation(source: 0, selection: NSRange(location: 0, length: 0))) |
| 30 | let after = await replica.presentedRevision |
| 31 | XCTAssertEqual(after, start + 3) |
| 32 | XCTAssertEqual(presented.revision, after) |
| 33 | } |
| 34 | |
| 35 | func testDirtyCoversExactlyTheRevisionsSince() async throws { |
| 36 | let replica = try await replica() |
| 37 | let opened = try await XCTUnwrapAsync(try await replica.presentation(source: 0, selection: NSRange(location: 0, length: 0))) |
| 38 | let ids = opened.projection.children.map(\.id) |
| 39 | // The first projection is a whole one: only a full comparison says. |
| 40 | XCTAssertNil(opened.dirty) |
| 41 | try await type("x", inBlock: 1, of: replica) |
| 42 | let first = try await XCTUnwrapAsync(try await replica.presentation(source: opened.generation, selection: NSRange(location: 0, length: 0), since: opened.revision)) |
| 43 | XCTAssertEqual(first.dirty, [ids[1]]) |
| 44 | try await type("y", inBlock: 3, of: replica) |
| 45 | // An extra projection between presentations: still in the log. |
| 46 | _ = try await replica.projection() |
| 47 | let second = try await XCTUnwrapAsync(try await replica.presentation(source: first.generation, selection: NSRange(location: 0, length: 0), since: first.revision)) |
| 48 | XCTAssertEqual(second.dirty, [ids[3]]) |
| 49 | // A text view still at the first revision gets the union. |
| 50 | let again = await replica.dirty(since: opened.revision, through: second.revision) |
| 51 | XCTAssertEqual(again, [ids[1], ids[3]]) |
| 52 | // Nothing since the latest. |
| 53 | let none = await replica.dirty(since: second.revision, through: second.revision) |
| 54 | XCTAssertEqual(none, []) |
| 55 | // Spanning the whole projection at open: unknown. |
| 56 | let fromStart = await replica.dirty(since: 0, through: second.revision) |
| 57 | XCTAssertNil(fromStart) |
| 58 | } |
| 59 | |
| 60 | func testStructuralEditsNameTheirBlocks() async throws { |
| 61 | let replica = try await replica() |
| 62 | let opened = try await XCTUnwrapAsync(try await replica.presentation(source: 0, selection: NSRange(location: 0, length: 0))) |
| 63 | let ids = opened.projection.children.map(\.id) |
| 64 | // A Return in block 1 splits it: block 1 changes and a new one appears. |
| 65 | let offset = opened.projection.children.prefix(1).reduce(0) { $0 + $1.plainText.utf16.count + 1 } + 3 |
| 66 | _ = try await replica.command(["op": .string("display_edit"), "at": .number(Double(offset)), "length": .number(0), "text": .string("\n")]) |
| 67 | let split = try await XCTUnwrapAsync(try await replica.presentation(source: opened.generation, selection: NSRange(location: 0, length: 0), since: opened.revision)) |
| 68 | let inserted = Set(split.projection.children.map(\.id)).subtracting(ids) |
| 69 | XCTAssertEqual(inserted.count, 1) |
| 70 | XCTAssertEqual(split.dirty, Set([ids[1]]).union(inserted)) |
| 71 | } |
| 72 | |
| 73 | func testPastTheLogIsUnknown() async throws { |
| 74 | let replica = try await replica(limit: 4) |
| 75 | let opened = try await XCTUnwrapAsync(try await replica.presentation(source: 0, selection: NSRange(location: 0, length: 0))) |
| 76 | var latest = opened |
| 77 | for step in 0..<10 { |
| 78 | try await type("z", inBlock: step % 4, of: replica) |
| 79 | await replica.retainGenerations([latest.generation]) |
| 80 | latest = try await XCTUnwrapAsync(try await replica.presentation(source: latest.generation, selection: NSRange(location: 0, length: 0), since: latest.revision)) |
| 81 | XCTAssertNotNil(latest.dirty, "one revision back is always in the log") |
| 82 | } |
| 83 | let overflowed = await replica.dirty(since: opened.revision + 1, through: latest.revision) |
| 84 | XCTAssertNil(overflowed, "a span older than the log cannot be answered") |
| 85 | let recent = await replica.dirty(since: latest.revision - 2, through: latest.revision) |
| 86 | XCTAssertNotNil(recent) |
| 87 | } |
| 88 | |
| 89 | func testAWholeProjectionIsUnknownWhateverTheReason() async throws { |
| 90 | // Recovery: an update that will not apply replaces the authority. |
| 91 | let replica = try await replica() |
| 92 | let opened = try await XCTUnwrapAsync(try await replica.presentation(source: 0, selection: NSRange(location: 0, length: 0))) |
| 93 | do { try await replica.apply(Data([0x01, 0xff, 0xff, 0xff, 0xff])); XCTFail("garbage applied") } catch {} |
| 94 | let recovered = try await XCTUnwrapAsync(try await replica.presentation(source: opened.generation, selection: NSRange(location: 0, length: 0), since: opened.revision)) |
| 95 | XCTAssertNil(recovered.dirty, "after recovery the projection is whole") |
| 96 | |
| 97 | // A delta that does not apply to what the replica presented. |
| 98 | try await type("q", inBlock: 2, of: replica) |
| 99 | await replica.retainGenerations([recovered.generation]) |
| 100 | let before = try await XCTUnwrapAsync(try await replica.presentation(source: recovered.generation, selection: NSRange(location: 0, length: 0), since: recovered.revision)) |
| 101 | XCTAssertNotNil(before.dirty) |
| 102 | await replica.replacePresentedForTesting { projection in |
| 103 | var changed = projection; changed.children.append(projection.children[0]); return changed |
| 104 | } |
| 105 | try await type("r", inBlock: 2, of: replica) |
| 106 | await replica.retainGenerations([before.generation]) |
| 107 | let mismatched = try await XCTUnwrapAsync(try await replica.presentation(source: before.generation, selection: NSRange(location: 0, length: 0), since: before.revision)) |
| 108 | XCTAssertNil(mismatched.dirty, "delta.applied returned nil: the full branch ran") |
| 109 | XCTAssertEqual(mismatched.projection.children.count, before.projection.children.count) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | func XCTUnwrapAsync<T>(_ value: @autoclosure () async throws -> T?, file: StaticString = #filePath, line: UInt = #line) async throws -> T { |
| 114 | let unwrapped = try await value() |
| 115 | return try XCTUnwrap(unwrapped, file: file, line: line) |
| 116 | } |
| 117 | |