oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1import Foundation
2import XCTest
3@testable import DocsKit
4
5/// The block index under optimistic typing (spec 2026-11-22 §7 Design 4,
6/// §11): typing, Return, Backspace at a block's start and a paste go
7/// through the real text view with no publication. The index always equals
8/// the `.docsTopBlock` runs read afresh from the storage, provisional
9/// blocks included; when the publication then arrives, the storage is a
10/// whole render's.
11@MainActor final class BlockIndexTests: XCTestCase {
12 func testIndexFollowsTypingBeforeAnyPublication() async throws {
13 let replica = CRDTReplica(guid: "block-index")
14 try await replica.open(update: Data())
15 _ = try await replica.command(["op": .string("insert_element"), "at": .number(0), "type": .string("paragraph")])
16 _ = try await replica.command(["op": .string("display_paste"), "at": .number(0), "length": .number(0), "blocks": .array(BlockRenderEquivalenceTests.fixture)])
17 let state = SurfaceState(width: 640)
18 let text = state.text
19 func publish() async throws {
20 await replica.retainGenerations([state.surface.generation])
21 let answer = try await replica.presentation(source: state.surface.generation, selection: NSRange(location: 0, length: 0), since: state.surface.appliedRevision)
22 let presented = try XCTUnwrap(answer)
23 state.allowed = presented.generation
24 state.surface.apply(DocumentSurface.Publication(generation: presented.generation, projection: presented.projection, revision: presented.revision, dirty: presented.dirty))
25 }
26 try await publish()
27 func checkIndex(_ step: String, file: StaticString = #filePath, line: UInt = #line) {
28 XCTAssertEqual(state.surface.index.runs, DocumentBlockIndex.runs(in: state.surface.content), "after \(step)", file: file, line: line)
29 }
30 checkIndex("opening")
31 let shown = state.surface.content.string as NSString
32 let survey = shown.range(of: "The survey began")
33 Keyboard.select(NSRange(location: NSMaxRange(survey), length: 0), in: text)
34 Keyboard.type(" Typed", in: text)
35 checkIndex("typing")
36 // An optimistic Return: the new paragraph keeps its block's id.
37 Keyboard.type("\nA new line", in: text)
38 checkIndex("Return")
39 let provisional = state.surface.index.runs.contains { run in
40 run.id != nil && (state.surface.content.string as NSString).substring(with: run.range).dropLast().contains("\n")
41 }
42 XCTAssertTrue(provisional, "a Return before any publication leaves one id over two paragraphs")
43 // Backspace at the start of the next block: one paragraph, two runs.
44 let next = (state.surface.content.string as NSString).range(of: "Run make check")
45 Keyboard.select(NSRange(location: next.location, length: 0), in: text)
46 Keyboard.backspace(in: text)
47 checkIndex("Backspace at a block's start")
48 let merged = (state.surface.content.string as NSString).paragraphRange(for: NSRange(location: next.location - 1, length: 0))
49 XCTAssertGreaterThan(DocumentBlockIndex.runs(in: state.surface.content, range: merged).count, 1, "the merged paragraph holds two runs")
50 // A paste of two blocks.
51 let end = (state.surface.content.string as NSString).range(of: "The end.")
52 Keyboard.select(NSRange(location: NSMaxRange(end), length: 0), in: text)
53 Keyboard.paste(BlockRenderEquivalenceTests.pasted, in: text, surface: state.surface)
54 checkIndex("a paste")
55 XCTAssertEqual(state.acknowledgements.count, 1, "no publication was drawn while typing")
56
57 // The model's queue: every command, in order, then a publication.
58 for command in state.commands { _ = try await replica.command(command) }
59 state.pending = false
60 try await publish()
61 checkIndex("the publication")
62 SurfaceFixture.assertMatchesWholeRender(state.surface.content, try await replica.projection(), "after the publication")
63 XCTAssertTrue(state.surface.content.string.contains("The survey began Typed\nA new line in spring."))
64 await replica.close()
65 }
66}
67