oficina / docs Public

Docs native S1a over S2a: tests for the chrome on the surface (#170, 4/5)

The rebase onto S2a (main 2704697) drives S2a's derived state from S1a's
surface: `history` is set with each projection the model publishes (so
undo and redo are current for a reader and while input is pending; the
iPhone toolbar now reads it too), and `contentRevision` moves after each
draw into the text view (the surface's didRender, never inside a view
update). The live word count counts from the projection's text and the
text view's storage, with no render. The Format inspector reads the
model's toolbarSelection and acts on the live storage; its side is an
input of DocumentEditorHost. ⌘K from the text view shows S2a's jump sheet.

SurfaceChromeTests pins what the Format menu, the history and the word
count read: the menu's enablement (DocumentCommands.isEnabled) follows a
key and the queue draining through observation; the history is current
at a publication with input pending and for a reader; the live count
recounts after a draw with no whole render. The last two failed with the
history's update or the count's no-render removed.
DocsHistoryObservationTests' word-count case attaches a text view, as
the editor does whenever the sheet can be open.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev a22b03b33439046b8e9b857d1ff8217236ef51be parent 7e19bbc
2 files changed, +118 −0
  • native/apple/Tests/DocsKitTests/DocsHistoryObservationTests.swift +6 −0
  • native/apple/Tests/DocsKitTests/SurfaceChromeTests.swift +112 −0
diff --git a/native/apple/Tests/DocsKitTests/DocsHistoryObservationTests.swift b/native/apple/Tests/DocsKitTests/DocsHistoryObservationTests.swift
index 6b55586..5341283 100644
--- a/native/apple/Tests/DocsKitTests/DocsHistoryObservationTests.swift
+++ b/native/apple/Tests/DocsKitTests/DocsHistoryObservationTests.swift
@@ -65,6 +65,12 @@ import XCTest
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")
diff --git a/native/apple/Tests/DocsKitTests/SurfaceChromeTests.swift b/native/apple/Tests/DocsKitTests/SurfaceChromeTests.swift
new file mode 100644
index 0000000..c33376b
--- /dev/null
+++ b/native/apple/Tests/DocsKitTests/SurfaceChromeTests.swift
@@ -0,0 +1,112 @@
+#if os(macOS)
+import AppKit
+import Foundation
+import Observation
+import RastrilloApple
+import RastrilloNative
+import RastrilloTestSupport
+import Synchronization
+import SwiftUI
+import XCTest
+@testable import DocsKit
+
+/// S2a's chrome over S1a's surface (Fable on #170, findings 4 and 5): what
+/// the Format menu, the toolbar, the pill and the live word count read
+/// must follow an edit through observation, though the model's projection
+/// and selection are no longer observed.
+@MainActor final class SurfaceChromeTests: XCTestCase {
+ private var model: DocsModel!
+ private var text: DocsTextView!
+ private var server: AccessServer!
+
+ override func setUp() async throws {
+ DocsFonts.register()
+ server = AccessServer()
+ try await server.inner.seed(LoopFixture.everything)
+ let connectivity = NativeConnectivity(source: ScriptedPathSource(.satisfied))
+ let account = DocsOfflineAccount(store: EphemeralDocsStore(), accountID: UUID().uuidString, connectivity: connectivity, transport: { [server] in server })
+ model = DocsModel(transport: { [server] in server }, connectivity: connectivity, offlineAccount: account)
+ text = DocsTextView(frame: CGRect(x: 0, y: 0, width: 640, height: 800))
+ model.surface.attach(text)
+ await model.open("doc")
+ model.suspendLiveUpdates()
+ }
+ override func tearDown() async throws { await model?.stop(); model = nil; text = nil; server = nil }
+
+ 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 }
+ }
+ private func until(_ what: String, _ condition: () -> Bool) async throws {
+ let deadline = ContinuousClock.now + .seconds(5)
+ while !condition() {
+ guard ContinuousClock.now < deadline else { throw LoopFailure("timed out waiting for \(what)") }
+ try await Task.sleep(for: .milliseconds(5))
+ }
+ }
+ private func typeInTheMiddle(_ characters: String) {
+ let line = (model.surface.content.string as NSString).range(of: LoopFixture.middle)
+ Keyboard.select(NSRange(location: NSMaxRange(line), length: 0), in: text)
+ Keyboard.type(characters, in: text)
+ }
+
+ /// The Format menu (DocsEditorCommands) is disabled from
+ /// `DocumentCommands.isEnabled`: a key makes it false and the queue
+ /// draining makes it true again, and each change reaches observation,
+ /// so the menu's enablement updates.
+ func testTheFormatMenusEnablementFollowsAnEdit() async throws {
+ let commands = { [unowned self] in DocumentCommands(model: model, content: model.surface.content) }
+ XCTAssertTrue(commands().isEnabled)
+ let disabled = try await observes({ _ = commands().isEnabled }) { typeInTheMiddle("x") }
+ XCTAssertTrue(disabled, "a key re-evaluates the menu")
+ XCTAssertFalse(commands().isEnabled, "disabled while input is pending")
+ let enabled = try await observes({ _ = commands().isEnabled }) { try await until("the queue drained") { !model.hasPendingInput } }
+ XCTAssertTrue(enabled, "the queue draining re-evaluates the menu")
+ try await until("enabled again") { commands().isEnabled }
+ }
+
+ /// Undo and redo read the observed history, set with each publication:
+ /// it is current while input is still pending (the toolbar selection
+ /// keeps its last state then), and for a reader, whose toolbar
+ /// selection is never computed.
+ func testHistoryIsCurrentWhileInputIsPendingAndForAReader() async throws {
+ XCTAssertFalse(model.history.canUndo)
+ var pendingAtPublication: Bool?, historyAtPublication: Bool?
+ model.surface.willApply = { [unowned self] _ in
+ if pendingAtPublication == nil { pendingAtPublication = model.hasPendingInput; historyAtPublication = model.history.canUndo }
+ }
+ typeInTheMiddle("y")
+ try await until("the queue drained") { !model.hasPendingInput }
+ XCTAssertEqual(pendingAtPublication, true, "the publication arrived with input pending")
+ XCTAssertEqual(historyAtPublication, true, "and the history was already current")
+
+ // A reader: access drops to read-only; a peer's edit still moves the history.
+ await server.answer(200, canWrite: false)
+ await model.revalidateAccess()
+ XCTAssertEqual(model.documentAccess, .readOnly)
+ XCTAssertNil(DocumentToolbarSelection(commands: DocumentCommands(model: model, content: model.surface.content)), "no toolbar selection for a reader")
+ XCTAssertTrue(model.history.loaded)
+ XCTAssertTrue(model.history.canUndo, "the reader's history is the projection's")
+ }
+
+ #if DOCS_PROBES
+ /// The live word count recounts after a draw without rendering the
+ /// document: it reads the projection's text and the text view's storage.
+ func testTheLiveWordCountDoesNoWholeRender() async throws {
+ try await Task.sleep(for: .milliseconds(20))
+ let sheet = DocsLiveWordCountSheet(model: model) {}
+ DocsProbes.reset()
+ let followed = try await observes({ _ = sheet.body }) {
+ typeInTheMiddle("z")
+ try await until("the queue drained") { !model.hasPendingInput }
+ try await Task.sleep(for: .milliseconds(20))
+ }
+ XCTAssertTrue(followed, "the sheet recounts after the draw")
+ _ = sheet.body
+ XCTAssertEqual(DocsProbes.wholeRenders, 0, "no whole render for the count")
+ }
+ #endif
+}
+#endif