| 1 | #if os(macOS) |
| 2 | import Foundation |
| 3 | import Observation |
| 4 | import RastrilloApple |
| 5 | import RastrilloNative |
| 6 | import RastrilloTestSupport |
| 7 | import Synchronization |
| 8 | import XCTest |
| 9 | @testable import DocsKit |
| 10 | |
| 11 | /// The merge hazard Fable named on #169: S1a takes `DocsModel.projection` |
| 12 | /// out of observation, and three S2a readers (the undo/redo pill, Tools ▸ |
| 13 | /// Word count's enabled state, the word-count sheet) would then silently |
| 14 | /// stop updating. These pin each one changing after an edit, through the |
| 15 | /// observation SwiftUI uses, so a merge that loses it goes red. |
| 16 | @MainActor final class DocsHistoryObservationTests: XCTestCase { |
| 17 | private func open() async throws -> DocsModel { |
| 18 | let server = AccessServer() |
| 19 | try await server.inner.seed([.object(["type": .string("paragraph"), "children": .array([ |
| 20 | .object(["type": .string("text"), "runs": .array([.object(["insert": .string("Hello"), "marks": .object([:])])])])])])]) |
| 21 | let connectivity = NativeConnectivity(source: ScriptedPathSource(.satisfied)) |
| 22 | let account = DocsOfflineAccount(store: EphemeralDocsStore(), accountID: UUID().uuidString, connectivity: connectivity, transport: { server }) |
| 23 | let model = DocsModel(transport: { server }, connectivity: connectivity, offlineAccount: account) |
| 24 | await model.open("doc") |
| 25 | return model |
| 26 | } |
| 27 | |
| 28 | private func type(_ model: DocsModel) async throws { |
| 29 | XCTAssertTrue(model.enqueue(["op": .string("display_edit"), "at": .number(0), "length": .number(0), "text": .string("X")])) |
| 30 | let deadline = Date().addingTimeInterval(5) |
| 31 | while !model.history.canUndo { |
| 32 | guard Date() < deadline else { return XCTFail("the edit never became undoable") } |
| 33 | try await Task.sleep(for: .milliseconds(10)) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | /// Whether evaluating `read` registered a dependency that `change` fires. |
| 38 | private func observes(_ read: () -> Void, after change: () async throws -> Void) async throws -> Bool { |
| 39 | let fired = Mutex(false) |
| 40 | withObservationTracking(read) { fired.withLock { $0 = true } } |
| 41 | try await change() |
| 42 | return fired.withLock { $0 } |
| 43 | } |
| 44 | |
| 45 | func testTheUndoPillFollowsAnEdit() async throws { |
| 46 | let model = try await open() |
| 47 | XCTAssertFalse(model.history.canUndo) |
| 48 | let pill = DocsUndoRedoPill(model: model) |
| 49 | let followed = try await observes({ _ = pill.body }) { try await type(model) } |
| 50 | XCTAssertTrue(followed, "the pill's body re-evaluates when undo becomes possible") |
| 51 | XCTAssertTrue(model.history.canUndo) |
| 52 | await model.stop() |
| 53 | } |
| 54 | |
| 55 | func testWordCountsEnabledStateFollowsTheDocumentOpening() async throws { |
| 56 | let server = AccessServer() |
| 57 | try await server.inner.seed([]) |
| 58 | let model = DocsModel(transport: { server }, connectivity: NativeConnectivity(source: ScriptedPathSource(.satisfied))) |
| 59 | XCTAssertFalse(model.history.loaded, "nothing open: Word count is disabled") |
| 60 | let followed = try await observes({ _ = model.history.loaded }) { await model.open("doc") } |
| 61 | XCTAssertTrue(followed) |
| 62 | XCTAssertTrue(model.history.loaded) |
| 63 | await model.stop() |
| 64 | } |
| 65 | |
| 66 | func testTheWordCountSheetFollowsAnEdit() async throws { |
| 67 | let model = try await open() |
| 68 | // The count reads the text view's own storage and recounts after |
| 69 | // each draw into it (S1a's contentRevision), so the document is in |
| 70 | // a text view, as it is whenever the sheet can be open. |
| 71 | let text = DocsTextView(frame: CGRect(x: 0, y: 0, width: 640, height: 800)) |
| 72 | model.surface.attach(text) |
| 73 | try await Task.sleep(for: .milliseconds(20)) |
| 74 | let sheet = DocsLiveWordCountSheet(model: model) {} |
| 75 | let followed = try await observes({ _ = sheet.body }) { try await type(model) } |
| 76 | XCTAssertTrue(followed, "the open sheet recounts as the document changes") |
| 77 | await model.stop() |
| 78 | } |
| 79 | } |
| 80 | #endif |
| 81 | |