| 1 | import Foundation |
| 2 | import XCTest |
| 3 | @testable import DocsKit |
| 4 | |
| 5 | final class CRDTSourceReuseTests: XCTestCase { |
| 6 | func testAcknowledgedIdenticalSourceReuseBoundsClientsAndPreservesDivergence() async throws { |
| 7 | let replica = CRDTReplica(guid: "source-reuse-test") |
| 8 | _ = try await replica.open(update: Data()) |
| 9 | _ = try await replica.command(["op": .string("insert_element"), "at": .number(0), "type": .string("paragraph")]) |
| 10 | let initial = try await replica.presentation(source: 0, selection: NSRange(location: 0, length: 0)) |
| 11 | var source = try XCTUnwrap(initial?.0) |
| 12 | let original = source |
| 13 | for index in 0..<100 { |
| 14 | _ = try await replica.command(["op": .string("display_edit"), "generation": .number(Double(source)), "at": .number(Double(index)), "length": .number(0), "text": .string("a")]) |
| 15 | await replica.retainGenerations([source]) |
| 16 | let next = try await replica.presentation(source: source, selection: NSRange(location: index + 1, length: 0), allowReuse: true) |
| 17 | source = try XCTUnwrap(next?.0) |
| 18 | } |
| 19 | XCTAssertEqual(source, original) |
| 20 | let (clientData, typed) = try await replica.command(["op": .string("clients")]) |
| 21 | XCTAssertEqual(try JSONDecoder().decode([UInt64].self, from: clientData).count, 2) |
| 22 | XCTAssertEqual(typed.children.map(\.plainText).joined(), String(repeating: "a", count: 100)) |
| 23 | |
| 24 | // A candidate without explicit rendered acknowledgement must remain fresh. |
| 25 | await replica.retainGenerations([source]) |
| 26 | let unacknowledged = try await replica.presentation(source: source, selection: NSRange(location: 100, length: 0)) |
| 27 | XCTAssertNotEqual(unacknowledged?.0, source) |
| 28 | await replica.retainGenerations([source]) |
| 29 | |
| 30 | // A peer changes authority while displayed source stays at the old state. |
| 31 | let (snapshot, _) = try await replica.command(["op": .string("snapshot")]) |
| 32 | let peer = CRDTReplica(guid: "source-reuse-test") |
| 33 | _ = try await peer.open(update: snapshot) |
| 34 | let (peerUpdate, _) = try await peer.command(["op": .string("display_edit"), "at": .number(0), "length": .number(0), "text": .string("peer ")]) |
| 35 | _ = try await replica.apply(peerUpdate) |
| 36 | let divergent = try await replica.presentation(source: source, selection: NSRange(location: 100, length: 0), allowReuse: true) |
| 37 | let changed = try XCTUnwrap(divergent?.0) |
| 38 | XCTAssertNotEqual(changed, source) |
| 39 | XCTAssertEqual(divergent?.1.children.map(\.plainText).joined(), "peer " + String(repeating: "a", count: 100)) |
| 40 | XCTAssertEqual(divergent?.2.location, 105) |
| 41 | |
| 42 | // Reusing the newly acknowledged live source does not merge undo ownership. |
| 43 | await replica.retainGenerations([changed]) |
| 44 | _ = try await replica.command(["op": .string("display_edit"), "generation": .number(Double(changed)), "at": .number(105), "length": .number(0), "text": .string("!")]) |
| 45 | let local = try await replica.presentation(source: changed, selection: NSRange(location: 106, length: 0), allowReuse: true) |
| 46 | XCTAssertEqual(local?.0, changed) |
| 47 | _ = try await replica.command(["op": .string("undo")]) |
| 48 | let undone = try await replica.presentation(source: changed, selection: NSRange(location: 106, length: 0), allowReuse: true) |
| 49 | XCTAssertNotEqual(undone?.0, changed) |
| 50 | XCTAssertFalse(undone?.1.children.map(\.plainText).joined().hasSuffix("!") ?? true) |
| 51 | XCTAssertTrue(undone?.1.children.map(\.plainText).joined().hasPrefix("peer ") ?? false) |
| 52 | await peer.close(); await replica.close() |
| 53 | } |
| 54 | } |
| 55 | |