import Foundation import XCTest @testable import DocsKit final class CRDTGenerationTests: XCTestCase { @MainActor func testDelayedPresentationDoesNotReplaceSourceOfQueuedTyping() async throws { let replica = CRDTReplica(guid: "native-generation-test") _ = try await replica.open(update: Data()) _ = try await replica.command(["op": .string("insert_element"), "at": .number(0), "type": .string("paragraph")]) let first = try await replica.presentation(source: 0, selection: NSRange(location: 0, length: 0)) let source = try XCTUnwrap(first?.0) var published = source let characters = Array("Native text".unicodeScalars) for (index, scalar) in characters.enumerated() { _ = try await replica.command(["op": .string("display_edit"), "generation": .number(Double(source)), "at": .number(Double(index)), "length": .number(0), "text": .string(String(scalar))]) await replica.retainGenerations([source, published]) let candidate = try await replica.presentation(source: source, selection: NSRange(location: index + 1, length: 0)) published = try XCTUnwrap(candidate?.0) // Deliberately withhold render acknowledgement: the next callback // still belongs to the original displayed source, not this candidate. } let final = try await replica.projection() XCTAssertEqual(final.children.map(\.plainText).joined(), "Native text") XCTAssertTrue(final.canUndo) _ = try await replica.command(["op": .string("display_edit"), "generation": .number(Double(published)), "at": .number(11), "length": .number(0), "text": .string(" bold"), "marks": .object(["strong": .object([:])])]) let formatted = try await replica.projection() XCTAssertEqual(formatted.children.map(\.plainText).joined(), "Native text bold") XCTAssertEqual(formatted.children[0].children?.last?.runs?.last?.marks?["strong"], .object([:])) await replica.close() } }