import Foundation import XCTest @testable import DocsKit final class CRDTClipboardTests: XCTestCase { @MainActor func testPasteThenImmediateTypingBeforeRenderPreservesMarksAndOffsets() async throws { let replica = CRDTReplica(guid: "clipboard-typeahead") _ = try await replica.open(update: Data()) _ = try await replica.command(["op": .string("insert_element"), "at": .number(0), "type": .string("paragraph")]) _ = try await replica.command(["op": .string("display_edit"), "at": .number(0), "length": .number(0), "text": .string("AB")]) let presentation = try await replica.presentation(source: 0, selection: NSRange(location: 1, length: 0)) let (source, projection, _) = try XCTUnwrap(presentation) let content = NativeTextProjection.render(projection) let blocks: [JSONValue] = [.object(["type": .string("paragraph"), "children": .array([.object(["type": .string("text"), "runs": .array([.object(["insert": .string("bold😀"), "marks": .object(["strong": .object([:])])])])])])])] let pasted = try XCTUnwrap(DocumentClipboardInsertion.attributed(blocks: blocks, in: content, range: NSRange(location: 1, length: 0))) _ = try await replica.command(["op": .string("display_paste"), "generation": .number(Double(source)), "at": .number(1), "length": .number(0), "blocks": .array(blocks)]) let native = NSMutableAttributedString(attributedString: content) native.replaceCharacters(in: NSRange(location: 1, length: 0), with: pasted) var typing = try XCTUnwrap(TextEditMapping.displayCommand(in: native, range: NSRange(location: 7, length: 0), replacement: "!")) typing["generation"] = .number(Double(source)) _ = try await replica.command(typing) let final = try await replica.projection() XCTAssertEqual(final.children.map(\.plainText).joined(), "Abold😀!B") let runs = final.children[0].children?.flatMap { $0.runs ?? [] } ?? [] XCTAssertTrue(runs.contains { $0.insert.contains("!") && $0.marks?["strong"] == .object([:]) }) await replica.close() } @MainActor func testRuleRevertAndRapidNewlineTypingUseSameDisplayedSource() async throws { let replica = CRDTReplica(guid: "rules-typeahead") _ = try await replica.open(update: Data()) let receiver = CRDTReplica(guid: "rules-typeahead") _ = try await receiver.open(update: Data()) let (initial, _) = try await replica.command(["op": .string("insert_element"), "at": .number(0), "type": .string("paragraph")]) _ = try await receiver.apply(initial) let presentation = try await replica.presentation(source: 0, selection: NSRange(location: 0, length: 0)) let source = try XCTUnwrap(presentation?.0) for command: [String: JSONValue] in [ ["op": .string("display_edit"), "at": .number(0), "length": .number(0), "text": .string("-")], ["op": .string("display_autoformat"), "at": .number(0), "length": .number(1), "kind": .string("bullet_list"), "level": .number(0)], ["op": .string("display_revert_rule"), "at": .number(0), "kind": .string("bullet_list"), "prior_type": .string("paragraph"), "prior_attrs": .object([:]), "text": .string("- ")], ["op": .string("display_edit"), "at": .number(2), "length": .number(0), "text": .string("one\n")], ["op": .string("display_edit"), "at": .number(6), "length": .number(0), "text": .string("two😀")] ] { var command = command; command["generation"] = .number(Double(source)) let (update, _) = try await replica.command(command) _ = try await receiver.apply(update) let local = try await replica.projection(); let remote = try await receiver.projection() XCTAssertEqual(local.children.map(\.id), remote.children.map(\.id)) XCTAssertEqual(local.children.map(\.type), remote.children.map(\.type)) XCTAssertEqual(local.children.map(\.plainText), remote.children.map(\.plainText)) } let final = try await replica.projection() XCTAssertEqual(final.children.map(\.plainText), ["- one", "two😀"]) XCTAssertEqual(final.children.map(\.type), ["paragraph", "paragraph"]) let received = try await receiver.projection() XCTAssertEqual(received.children.map(\.plainText), ["- one", "two😀"]) XCTAssertEqual(received.children.map(\.type), ["paragraph", "paragraph"]) await receiver.close() await replica.close() } }