| 1 | import Foundation |
| 2 | import RastrilloApple |
| 3 | import XCTest |
| 4 | @testable import DocsKit |
| 5 | |
| 6 | /// The four deferred-flush triggers (spec 2026-11-22 §7 Design 6, finding |
| 7 | /// 2; inputPaused and didRender are not triggers, see the spec): |
| 8 | /// a publication arrives while something stops the text view taking it, |
| 9 | /// and *no later publication* follows. When that something clears, its |
| 10 | /// event alone must bring the storage to a whole render of the newest |
| 11 | /// projection. The production `DocsModel` and replica, an in-memory |
| 12 | /// server, and a real `DocsTextView`; the live stream is closed after open, |
| 13 | /// so nothing else publishes. |
| 14 | @MainActor final class FlushTriggerTests: XCTestCase { |
| 15 | private var server: LoopServer! |
| 16 | private var model: DocsModel! |
| 17 | private var text: DocsTextView! |
| 18 | |
| 19 | override func setUp() async throws { |
| 20 | DocsFonts.register() |
| 21 | server = LoopServer() |
| 22 | try await server.seed(LoopFixture.everything) |
| 23 | model = DocsModel(client: server) |
| 24 | text = DocsTextView(frame: CGRect(x: 0, y: 0, width: 640, height: 800)) |
| 25 | model.surface.attach(text) |
| 26 | await model.open("doc") |
| 27 | model.suspendLiveUpdates() |
| 28 | } |
| 29 | override func tearDown() async throws { |
| 30 | await model?.stop() |
| 31 | model = nil; text = nil; server = nil |
| 32 | } |
| 33 | |
| 34 | private var shown: NSAttributedString { model.surface.content } |
| 35 | private func assertDrawn(_ what: String, file: StaticString = #filePath, line: UInt = #line) throws { |
| 36 | let projection = try XCTUnwrap(model.projection, file: file, line: line) |
| 37 | SurfaceFixture.assertMatchesWholeRender(shown, projection, images: [:], what, file: file, line: line) |
| 38 | } |
| 39 | private func until(_ what: String, _ condition: () -> Bool) async throws { |
| 40 | let deadline = ContinuousClock.now + .seconds(5) |
| 41 | while !condition() { |
| 42 | guard ContinuousClock.now < deadline else { throw LoopFailure("timed out waiting for \(what)") } |
| 43 | try await Task.sleep(for: .milliseconds(2)) |
| 44 | } |
| 45 | } |
| 46 | private func typeInTheMiddle(_ characters: String) { |
| 47 | let line = (shown.string as NSString).range(of: LoopFixture.middle) |
| 48 | Keyboard.select(NSRange(location: NSMaxRange(line), length: 0), in: text) |
| 49 | Keyboard.type(characters, in: text) |
| 50 | } |
| 51 | /// A publication the replica did not make: block `index` changed. |
| 52 | private func fabricated(generation: UInt64, changing index: Int) throws -> DocumentSurface.Publication { |
| 53 | var projection = try XCTUnwrap(model.projection) |
| 54 | let node = projection.children[index] |
| 55 | projection.children[index] = SurfaceFixture.paragraph(node.id, "Changed while waiting.") |
| 56 | return DocumentSurface.Publication(generation: generation, projection: projection, revision: model.surface.appliedRevision + 1, dirty: [node.id]) |
| 57 | } |
| 58 | |
| 59 | /// (a) A publication applied, nothing in the way: drawn at once. |
| 60 | func testAPublicationIsDrawnWhenItArrives() throws { |
| 61 | try assertDrawn("open") |
| 62 | let publication = try fabricated(generation: model.projectionGeneration, changing: 1) |
| 63 | model.surface.apply(publication) |
| 64 | SurfaceFixture.assertMatchesWholeRender(shown, publication.projection, "a publication with nothing in the way") |
| 65 | } |
| 66 | |
| 67 | /// (b) The publication from typing arrives while the edit queue is |
| 68 | /// still draining; the queue's end alone draws it. A Return, so the |
| 69 | /// publication differs from what the text view shows optimistically: |
| 70 | /// the new paragraph gets its own block id only from it. |
| 71 | func testTheEditQueueDrainingDrawsTheWaitingPublication() async throws { |
| 72 | var deferred = 0 |
| 73 | model.surface.willApply = { [unowned self] _ in if model.hasPendingInput { deferred = model.surface.publicationsReceived } } |
| 74 | typeInTheMiddle(" typed\nNew") |
| 75 | try await until("the queue drained") { !model.hasPendingInput } |
| 76 | XCTAssertGreaterThan(deferred, 0, "a publication arrived while input was pending") |
| 77 | XCTAssertEqual(model.surface.publicationsReceived, deferred, "no later publication") |
| 78 | try assertDrawn("after the queue drained") |
| 79 | XCTAssertTrue(shown.string.contains(LoopFixture.middle + " typed\nNew")) |
| 80 | } |
| 81 | |
| 82 | /// (c) Composition starts as a publication arrives; ending it (with no |
| 83 | /// text, so nothing is sent) alone draws the publication. |
| 84 | func testCompositionEndingDrawsTheWaitingPublication() async throws { |
| 85 | var composed = false |
| 86 | model.surface.willApply = { [unowned self] _ in |
| 87 | guard !composed else { return } |
| 88 | composed = true |
| 89 | Self.mark("", in: text) |
| 90 | } |
| 91 | typeInTheMiddle("x") |
| 92 | try await until("the queue drained") { !model.hasPendingInput } |
| 93 | XCTAssertTrue(composed) |
| 94 | let received = model.surface.publicationsReceived |
| 95 | let waiting = try XCTUnwrap(model.surface.newest) |
| 96 | XCTAssertGreaterThan(waiting.revision, model.surface.appliedRevision, "the publication waits for the composition") |
| 97 | text.unmarkText() |
| 98 | XCTAssertEqual(model.surface.publicationsReceived, received, "no later publication") |
| 99 | XCTAssertEqual(model.surface.appliedRevision, waiting.revision) |
| 100 | try assertDrawn("after the composition ended") |
| 101 | } |
| 102 | |
| 103 | /// (d) A publication for a generation the model has not reached waits; |
| 104 | /// the generation catching up alone draws it. |
| 105 | func testTheGenerationCatchingUpDrawsTheWaitingPublication() throws { |
| 106 | let next = model.projectionGeneration + 1 |
| 107 | let publication = try fabricated(generation: next, changing: 1) |
| 108 | model.surface.apply(publication) |
| 109 | XCTAssertLessThan(model.surface.appliedRevision, publication.revision, "not the current generation yet") |
| 110 | model.projectionGeneration = next |
| 111 | SurfaceFixture.assertMatchesWholeRender(shown, publication.projection, "after the generation caught up") |
| 112 | } |
| 113 | |
| 114 | static func mark(_ word: String, in text: DocsTextView) { |
| 115 | #if os(macOS) |
| 116 | text.setMarkedText(word, selectedRange: NSRange(location: word.utf16.count, length: 0), replacementRange: NSRange(location: NSNotFound, length: 0)) |
| 117 | #else |
| 118 | text.setMarkedText(word, selectedRange: NSRange(location: word.utf16.count, length: 0)) |
| 119 | #endif |
| 120 | } |
| 121 | } |
| 122 | |