import Foundation import XCTest @testable import DocsKit /// The block index under optimistic typing (spec 2026-11-22 §7 Design 4, /// §11): typing, Return, Backspace at a block's start and a paste go /// through the real text view with no publication. The index always equals /// the `.docsTopBlock` runs read afresh from the storage, provisional /// blocks included; when the publication then arrives, the storage is a /// whole render's. @MainActor final class BlockIndexTests: XCTestCase { func testIndexFollowsTypingBeforeAnyPublication() async throws { let replica = CRDTReplica(guid: "block-index") try await replica.open(update: Data()) _ = try await replica.command(["op": .string("insert_element"), "at": .number(0), "type": .string("paragraph")]) _ = try await replica.command(["op": .string("display_paste"), "at": .number(0), "length": .number(0), "blocks": .array(BlockRenderEquivalenceTests.fixture)]) let state = SurfaceState(width: 640) let text = state.text func publish() async throws { await replica.retainGenerations([state.surface.generation]) let answer = try await replica.presentation(source: state.surface.generation, selection: NSRange(location: 0, length: 0), since: state.surface.appliedRevision) let presented = try XCTUnwrap(answer) state.allowed = presented.generation state.surface.apply(DocumentSurface.Publication(generation: presented.generation, projection: presented.projection, revision: presented.revision, dirty: presented.dirty)) } try await publish() func checkIndex(_ step: String, file: StaticString = #filePath, line: UInt = #line) { XCTAssertEqual(state.surface.index.runs, DocumentBlockIndex.runs(in: state.surface.content), "after \(step)", file: file, line: line) } checkIndex("opening") let shown = state.surface.content.string as NSString let survey = shown.range(of: "The survey began") Keyboard.select(NSRange(location: NSMaxRange(survey), length: 0), in: text) Keyboard.type(" Typed", in: text) checkIndex("typing") // An optimistic Return: the new paragraph keeps its block's id. Keyboard.type("\nA new line", in: text) checkIndex("Return") let provisional = state.surface.index.runs.contains { run in run.id != nil && (state.surface.content.string as NSString).substring(with: run.range).dropLast().contains("\n") } XCTAssertTrue(provisional, "a Return before any publication leaves one id over two paragraphs") // Backspace at the start of the next block: one paragraph, two runs. let next = (state.surface.content.string as NSString).range(of: "Run make check") Keyboard.select(NSRange(location: next.location, length: 0), in: text) Keyboard.backspace(in: text) checkIndex("Backspace at a block's start") let merged = (state.surface.content.string as NSString).paragraphRange(for: NSRange(location: next.location - 1, length: 0)) XCTAssertGreaterThan(DocumentBlockIndex.runs(in: state.surface.content, range: merged).count, 1, "the merged paragraph holds two runs") // A paste of two blocks. let end = (state.surface.content.string as NSString).range(of: "The end.") Keyboard.select(NSRange(location: NSMaxRange(end), length: 0), in: text) Keyboard.paste(BlockRenderEquivalenceTests.pasted, in: text, surface: state.surface) checkIndex("a paste") XCTAssertEqual(state.acknowledgements.count, 1, "no publication was drawn while typing") // The model's queue: every command, in order, then a publication. for command in state.commands { _ = try await replica.command(command) } state.pending = false try await publish() checkIndex("the publication") SurfaceFixture.assertMatchesWholeRender(state.surface.content, try await replica.projection(), "after the publication") XCTAssertTrue(state.surface.content.string.contains("The survey began Typed\nA new line in spring.")) await replica.close() } }