#if os(macOS) import Foundation import Observation import RastrilloApple import RastrilloNative import RastrilloTestSupport import Synchronization import XCTest @testable import DocsKit /// The merge hazard Fable named on #169: S1a takes `DocsModel.projection` /// out of observation, and three S2a readers (the undo/redo pill, Tools ▸ /// Word count's enabled state, the word-count sheet) would then silently /// stop updating. These pin each one changing after an edit, through the /// observation SwiftUI uses, so a merge that loses it goes red. @MainActor final class DocsHistoryObservationTests: XCTestCase { private func open() async throws -> DocsModel { let server = AccessServer() try await server.inner.seed([.object(["type": .string("paragraph"), "children": .array([ .object(["type": .string("text"), "runs": .array([.object(["insert": .string("Hello"), "marks": .object([:])])])])])])]) let connectivity = NativeConnectivity(source: ScriptedPathSource(.satisfied)) let account = DocsOfflineAccount(store: EphemeralDocsStore(), accountID: UUID().uuidString, connectivity: connectivity, transport: { server }) let model = DocsModel(transport: { server }, connectivity: connectivity, offlineAccount: account) await model.open("doc") return model } private func type(_ model: DocsModel) async throws { XCTAssertTrue(model.enqueue(["op": .string("display_edit"), "at": .number(0), "length": .number(0), "text": .string("X")])) let deadline = Date().addingTimeInterval(5) while !model.history.canUndo { guard Date() < deadline else { return XCTFail("the edit never became undoable") } try await Task.sleep(for: .milliseconds(10)) } } /// Whether evaluating `read` registered a dependency that `change` fires. private func observes(_ read: () -> Void, after change: () async throws -> Void) async throws -> Bool { let fired = Mutex(false) withObservationTracking(read) { fired.withLock { $0 = true } } try await change() return fired.withLock { $0 } } func testTheUndoPillFollowsAnEdit() async throws { let model = try await open() XCTAssertFalse(model.history.canUndo) let pill = DocsUndoRedoPill(model: model) let followed = try await observes({ _ = pill.body }) { try await type(model) } XCTAssertTrue(followed, "the pill's body re-evaluates when undo becomes possible") XCTAssertTrue(model.history.canUndo) await model.stop() } func testWordCountsEnabledStateFollowsTheDocumentOpening() async throws { let server = AccessServer() try await server.inner.seed([]) let model = DocsModel(transport: { server }, connectivity: NativeConnectivity(source: ScriptedPathSource(.satisfied))) XCTAssertFalse(model.history.loaded, "nothing open: Word count is disabled") let followed = try await observes({ _ = model.history.loaded }) { await model.open("doc") } XCTAssertTrue(followed) XCTAssertTrue(model.history.loaded) await model.stop() } func testTheWordCountSheetFollowsAnEdit() async throws { let model = try await open() // The count reads the text view's own storage and recounts after // each draw into it (S1a's contentRevision), so the document is in // a text view, as it is whenever the sheet can be open. let text = DocsTextView(frame: CGRect(x: 0, y: 0, width: 640, height: 800)) model.surface.attach(text) try await Task.sleep(for: .milliseconds(20)) let sheet = DocsLiveWordCountSheet(model: model) {} let followed = try await observes({ _ = sheet.body }) { try await type(model) } XCTAssertTrue(followed, "the open sheet recounts as the document changes") await model.stop() } } #endif