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 RastrilloApple
3import XCTest
4@testable import DocsKit
5
6final class DocumentSyncIntegrationTests: XCTestCase {
7 func testFailedMergeCannotPoisonEditorOrNextMerge() async throws {
8 let replica = CRDTReplica(guid: "merge-isolation")
9 _ = try await replica.open(update: Data())
10 _ = try await replica.command(["op": .string("insert_element"), "at": .number(0), "type": .string("paragraph")])
11 do {
12 _ = try await replica.mergeUpdates([Data([255]), Data([255])])
13 XCTFail("Malformed updates must fail")
14 } catch { }
15 _ = try await replica.projection()
16 let first = try await replica.command(["op": .string("display_edit"), "at": .number(0), "length": .number(0), "text": .string("a")])
17 let second = try await replica.command(["op": .string("display_edit"), "at": .number(1), "length": .number(0), "text": .string("b")])
18 let projection = try await replica.projection()
19 let merged = try await replica.mergeUpdates([first, second])
20 XCTAssertFalse(merged.isEmpty)
21 let after = try await replica.projection()
22 XCTAssertEqual(after.children.map(\.plainText), projection.children.map(\.plainText))
23 await replica.close()
24 }
25 @MainActor func testTypingDoesNotAccumulateOneRoundTripPerKeystroke() async throws {
26 guard let origin = ProcessInfo.processInfo.environment["DOCS_SYNC_TEST_ORIGIN"] else {
27 throw XCTSkip("Run native/scripts/test-sync-latency.py for the delayed HTTP fixture")
28 }
29 let client = NativeHTTP(origin: URL(string: origin)!)
30 let model = DocsModel(client: client)
31 await model.open("fixture")
32 XCTAssertNotNil(model.projection)
33 model.didRender(model.projectionGeneration)
34 for _ in 0..<20 {
35 XCTAssertTrue(model.enqueue(["op": .string("display_edit"), "at": .number(0), "length": .number(0), "text": .string("X")]))
36 while model.hasPendingInput { try await Task.sleep(for: .milliseconds(1)) }
37 model.didRender(model.projectionGeneration)
38 try await Task.sleep(for: .milliseconds(50))
39 }
40 let finishedTyping = ContinuousClock.now
41 let deadline = finishedTyping.advanced(by: .seconds(15))
42 while model.hasUnsavedChanges && ContinuousClock.now < deadline {
43 await model.flush()
44 try await Task.sleep(for: .milliseconds(10))
45 }
46 XCTAssertFalse(model.hasUnsavedChanges)
47 let lag = finishedTyping.duration(to: .now)
48 struct Stats: Decodable { let updates: [Data] }
49 let stats: Stats = try await client.request("/stats")
50 print("SYNC: 20 edits, 250ms/request, uploads=\(stats.updates.count), catch-up after typing=\(lag)")
51 XCTAssertLessThanOrEqual(stats.updates.count, 10, "Unsent edits must share a request")
52 XCTAssertLessThan(lag, .seconds(1), "Typing must not leave a growing serial-request backlog")
53 let (initial, _) = try await client.response("/d/fixture/updates")
54 let peer = CRDTReplica(guid: "sync-fixture")
55 _ = try await peer.open(update: initial)
56 for update in stats.updates { _ = try await peer.apply(update) }
57 let projection = try await peer.projection()
58 XCTAssertEqual(projection.children.map(\.plainText), model.projection?.children.map(\.plainText), "A receiver must get every edit")
59 await peer.close()
60 await model.stop()
61 }
62}
63