import Foundation import XCTest @testable import DocsKit /// The surface across text views and model updates (Fable on #170, /// findings 2 and 3). @MainActor final class SurfaceLifecycleTests: XCTestCase { private func projection() -> CRDTProjection { SurfaceFixture.projection([SurfaceFixture.heading("h", "Title"), SurfaceFixture.paragraph("p", "Body text."), SurfaceFixture.list("l", "bullet_list", ["one", "two"])]) } /// The host is rebuilt without the model opening again (an iPad /// split collapsing, a restructured window): the new text view shows /// the document at once, with no further publication, and the old one /// no longer edits through the surface. func testASecondTextViewIsDrawnWhole() throws { let state = SurfaceState() state.publish(projection(), generation: 1, dirty: nil) let second = DocsTextView(frame: CGRect(x: 0, y: 0, width: 500, height: 700)) state.surface.attach(second) XCTAssertTrue(state.surface.textView === second) SurfaceFixture.assertMatchesWholeRender(Keyboard.storage(of: second), projection(), "the second text view") XCTAssertNil(state.text.delegate, "the first text view is let go") // And it keeps drawing: a later publication reaches the second view. var changed = projection() changed.children[1] = SurfaceFixture.paragraph("p", "Body text, changed.") state.publish(changed, generation: 1, dirty: ["p"]) SurfaceFixture.assertMatchesWholeRender(Keyboard.storage(of: second), changed, "a publication after the second attach") } /// The text view is attached inside SwiftUI's update (makeNSView / /// makeUIView). It is drawn there, so the first frame is final, but the /// model hears nothing until the next turn: no observed state is /// assigned during a view update (the 0.1.7/0.1.8 rule), not the /// rendered generation and not the document's image sources. func testAttachingTellsTheModelOnlyAfterTheUpdate() async throws { let image = CRDTNode(id: "i", type: "paragraph", children: [CRDTNode(id: "img", type: "image", attrs: ["src": "https://example.invalid/a.png", "alt": "A"])]) let document = SurfaceFixture.projection([SurfaceFixture.paragraph("p", "Before."), image]) let surface = DocumentSurface() var inUpdate = false var duringUpdate: [String] = [], after: [String] = [] var hooks = DocumentSurface.Hooks() hooks.didRender = { _ in (inUpdate ? { duringUpdate.append("didRender") } : { after.append("didRender") })() } hooks.imageSourcesChanged = { _ in (inUpdate ? { duringUpdate.append("imageSources") } : { after.append("imageSources") })() } surface.hooks = hooks surface.apply(DocumentSurface.Publication(generation: 1, projection: document, revision: 1, dirty: nil)) let text = DocsTextView(frame: CGRect(x: 0, y: 0, width: 500, height: 700)) inUpdate = true surface.attach(text) inUpdate = false XCTAssertEqual(duringUpdate, [], "nothing reaches the model inside the update") XCTAssertTrue(Keyboard.storage(of: text).string.hasPrefix("Before."), "drawn at once") for _ in 0..<20 where after.count < 2 { try await Task.sleep(for: .milliseconds(5)) } XCTAssertEqual(Set(after), ["didRender", "imageSources"], "the model hears both on the next turn") } /// A report deferred from an attach belongs to the document drawn then: /// if the model resets the surface (another document opens) in the same /// turn, the old document's image sources and generation never reach it /// (Fable on #170, final check, finding 2). func testADeferredReportIsDroppedAfterAReset() async throws { let image = CRDTNode(id: "i", type: "paragraph", children: [CRDTNode(id: "img", type: "image", attrs: ["src": "https://example.invalid/a.png", "alt": "A"])]) let surface = DocumentSurface() var sources: [[String]] = [], rendered: [UInt64] = [] var hooks = DocumentSurface.Hooks() hooks.didRender = { rendered.append($0) } hooks.imageSourcesChanged = { sources.append($0) } surface.hooks = hooks surface.apply(DocumentSurface.Publication(generation: 3, projection: SurfaceFixture.projection([image]), revision: 1, dirty: nil)) let text = DocsTextView(frame: CGRect(x: 0, y: 0, width: 500, height: 700)) surface.attach(text) surface.reset() for _ in 0..<10 { try await Task.sleep(for: .milliseconds(5)) } XCTAssertEqual(rendered, [], "the old document's generation is not reported after the reset") XCTAssertEqual(sources.last ?? [], [], "the last image sources the model hears are the reset's, none") } /// The old text view is already gone (its host released it) when the /// new one attaches: the new one is still drawn whole (Fable on #170, /// final check, finding 3). func testANewTextViewIsDrawnWholeWhenTheOldOneIsGone() async throws { let surface = DocumentSurface() var hooks = DocumentSurface.Hooks(); hooks.canRender = { _ in true } surface.hooks = hooks autoreleasepool { let first = DocsTextView(frame: CGRect(x: 0, y: 0, width: 500, height: 700)) surface.attach(first) surface.apply(DocumentSurface.Publication(generation: 1, projection: projection(), revision: 1, dirty: nil)) XCTAssertFalse(Keyboard.storage(of: first).string.isEmpty) } for _ in 0..<50 where surface.textView != nil { try await Task.sleep(for: .milliseconds(10)) } XCTAssertNil(surface.textView, "the first view is gone") let second = DocsTextView(frame: CGRect(x: 0, y: 0, width: 500, height: 700)) surface.attach(second) SurfaceFixture.assertMatchesWholeRender(Keyboard.storage(of: second), projection(), "a new view after the old one is gone") } }