oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1import Foundation
2import XCTest
3@testable import DocsKit
4
5final class CRDTClipboardTests: XCTestCase {
6 @MainActor func testPasteThenImmediateTypingBeforeRenderPreservesMarksAndOffsets() async throws {
7 let replica = CRDTReplica(guid: "clipboard-typeahead")
8 _ = try await replica.open(update: Data())
9 _ = try await replica.command(["op": .string("insert_element"), "at": .number(0), "type": .string("paragraph")])
10 _ = try await replica.command(["op": .string("display_edit"), "at": .number(0), "length": .number(0), "text": .string("AB")])
11 let presentation = try await replica.presentation(source: 0, selection: NSRange(location: 1, length: 0))
12 let presented = try XCTUnwrap(presentation)
13 let (source, projection) = (presented.generation, presented.projection)
14 let content = NativeTextProjection.render(projection)
15 let blocks: [JSONValue] = [.object(["type": .string("paragraph"), "children": .array([.object(["type": .string("text"), "runs": .array([.object(["insert": .string("bold😀"), "marks": .object(["strong": .object([:])])])])])])])]
16 let pasted = try XCTUnwrap(DocumentClipboardInsertion.attributed(blocks: blocks, in: content, range: NSRange(location: 1, length: 0)))
17 _ = try await replica.command(["op": .string("display_paste"), "generation": .number(Double(source)), "at": .number(1), "length": .number(0), "blocks": .array(blocks)])
18 let native = NSMutableAttributedString(attributedString: content)
19 native.replaceCharacters(in: NSRange(location: 1, length: 0), with: pasted)
20 var typing = try XCTUnwrap(TextEditMapping.displayCommand(in: native, range: NSRange(location: 7, length: 0), replacement: "!"))
21 typing["generation"] = .number(Double(source))
22 _ = try await replica.command(typing)
23 let final = try await replica.projection()
24 XCTAssertEqual(final.children.map(\.plainText).joined(), "Abold😀!B")
25 let runs = final.children[0].children?.flatMap { $0.runs ?? [] } ?? []
26 XCTAssertTrue(runs.contains { $0.insert.contains("!") && $0.marks?["strong"] == .object([:]) })
27 await replica.close()
28 }
29 @MainActor func testRuleRevertAndRapidNewlineTypingUseSameDisplayedSource() async throws {
30 let replica = CRDTReplica(guid: "rules-typeahead")
31 _ = try await replica.open(update: Data())
32 let receiver = CRDTReplica(guid: "rules-typeahead")
33 _ = try await receiver.open(update: Data())
34 let initial = try await replica.command(["op": .string("insert_element"), "at": .number(0), "type": .string("paragraph")])
35 _ = try await receiver.apply(initial)
36 let presentation = try await replica.presentation(source: 0, selection: NSRange(location: 0, length: 0))
37 let source = try XCTUnwrap(presentation?.generation)
38 for command: [String: JSONValue] in [
39 ["op": .string("display_edit"), "at": .number(0), "length": .number(0), "text": .string("-")],
40 ["op": .string("display_autoformat"), "at": .number(0), "length": .number(1), "kind": .string("bullet_list"), "level": .number(0)],
41 ["op": .string("display_revert_rule"), "at": .number(0), "kind": .string("bullet_list"), "prior_type": .string("paragraph"), "prior_attrs": .object([:]), "text": .string("- ")],
42 ["op": .string("display_edit"), "at": .number(2), "length": .number(0), "text": .string("one\n")],
43 ["op": .string("display_edit"), "at": .number(6), "length": .number(0), "text": .string("two😀")]
44 ] {
45 var command = command; command["generation"] = .number(Double(source))
46 let update = try await replica.command(command)
47 _ = try await receiver.apply(update)
48 let local = try await replica.projection(); let remote = try await receiver.projection()
49 XCTAssertEqual(local.children.map(\.id), remote.children.map(\.id))
50 XCTAssertEqual(local.children.map(\.type), remote.children.map(\.type))
51 XCTAssertEqual(local.children.map(\.plainText), remote.children.map(\.plainText))
52 }
53 let final = try await replica.projection()
54 XCTAssertEqual(final.children.map(\.plainText), ["- one", "two😀"])
55 XCTAssertEqual(final.children.map(\.type), ["paragraph", "paragraph"])
56 let received = try await receiver.projection()
57 XCTAssertEqual(received.children.map(\.plainText), ["- one", "two😀"])
58 XCTAssertEqual(received.children.map(\.type), ["paragraph", "paragraph"])
59 await receiver.close()
60 await replica.close()
61 }
62}
63