oficina / docs Public

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

Plain git — no account needed to clone.

Download

Download this file

1#if os(iOS)
2import Foundation
3import RastrilloApple
4import RastrilloTestSupport
5import SwiftUI
6import UIKit
7import XCTest
8@testable import DocsKit
9
10/// The iPhone document (S3, spec 2026-11-22 §3, §11), in the real
11/// `DocsWorkspace` on an in-memory server, in a window.
12@MainActor final class PhoneDocumentTests: XCTestCase {
13 private var window: UIWindow?
14 override func tearDown() async throws {
15 window?.rootViewController = nil; window?.isHidden = true; window = nil
16 }
17
18 private func open() async throws -> (DocsTextView, DocumentSurface, LoopServer) {
19 DocsFonts.register()
20 let server = LoopServer()
21 try await server.seed(LoopFixture.everything)
22 let connectivity = NativeConnectivity(source: ScriptedPathSource(.satisfied))
23 let workspace = DocsWorkspace(client: NativeHTTP(origin: server.origin), transport: { server }, signOut: {}, documentID: "doc", team: "Tito",
24 connectivity: connectivity)
25 let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 402, height: 874))
26 if let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene { window.windowScene = scene }
27 window.rootViewController = UIHostingController(rootView: workspace)
28 window.makeKeyAndVisible()
29 self.window = window
30 func find(_ view: UIView) -> DocsTextView? { (view as? DocsTextView) ?? view.subviews.lazy.compactMap(find).first }
31 try await until("the document is shown") { find(window).map { $0.textStorage.string.contains(LoopFixture.middle) } ?? false }
32 let text = try XCTUnwrap(find(window))
33 let surface = try XCTUnwrap(text.delegate as? DocumentSurface)
34 try await until("open settled") { surface.newest.map { $0.revision == surface.appliedRevision } ?? false }
35 try await Task.sleep(for: .milliseconds(300))
36 return (text, surface, server)
37 }
38
39 private func until(_ what: String, _ condition: () -> Bool) async throws {
40 let deadline = Date().addingTimeInterval(10)
41 while !condition() {
42 guard Date() < deadline else { throw Failure(what: what) }
43 try await Task.sleep(for: .milliseconds(10))
44 }
45 }
46 private struct Failure: Error, CustomStringConvertible { let what: String; var description: String { "timed out: \(what)" } }
47
48 /// Spec §11 (Low 18): with the caret mid-paragraph, a peer's edit to
49 /// another block moves neither the caret nor the scroll, so the
50 /// VoiceOver cursor, which follows them, stays where it was.
51 func testAPeerEditElsewhereMovesNeitherTheCaretNorTheScroll() async throws {
52 let (text, surface, server) = try await open()
53 let content = text.textStorage.string as NSString
54 let middle = content.range(of: LoopFixture.middle)
55 let caret = NSRange(location: middle.location + 10, length: 0)
56 text.selectedRange = caret
57 _ = text.becomeFirstResponder()
58 try await Task.sleep(for: .milliseconds(200))
59 let offset = text.contentOffset
60 // The peer types into the last block, after the caret's.
61 let after = (text.textStorage.string as NSString).range(of: LoopFixture.afterRule).location
62 try await server.peerInsert("A peer’s note. ", at: after)
63 try await until("the peer's note is drawn") { surface.content.string.contains("A peer’s note.") }
64 try await Task.sleep(for: .milliseconds(200))
65 XCTAssertEqual(text.selectedRange, caret, "the caret did not move")
66 XCTAssertEqual(text.contentOffset.y, offset.y, accuracy: 0.5, "nor did the scroll")
67 }
68
69 /// The hosted chrome lays itself out without the window's safe area
70 /// (Fable on #180): the large title scrolls under the bar inside the
71 /// text view, and the keyboard bar sits on the bottom edge over the home
72 /// indicator when a hardware keyboard is connected. Neither is to be
73 /// padded by insets that belong to the screen.
74 func testTheHostedChromeIgnoresTheSafeArea() async throws {
75 let (text, _, _) = try await open()
76 let header = try XCTUnwrap(text.header)
77 XCTAssertEqual(header.controller.safeAreaRegions, [], "the large title")
78 let bar = try XCTUnwrap(text.inputAccessoryView)
79 let host = try XCTUnwrap(DocsPhoneEditorChrome.host(of: bar))
80 XCTAssertEqual(host.safeAreaRegions, [], "the keyboard bar")
81 }
82
83 /// A text view the surface attaches while the Format sheet is open (the
84 /// editor built again: a new `editorIdentity`) gets the sheet's state:
85 /// no keyboard, and the text clear of the sheet (Fable on #180).
86 func testANewTextViewUnderTheSheetKeepsItsState() {
87 let surface = DocumentSurface()
88 surface.isEditable = true
89 let first = DocsTextView(frame: CGRect(x: 0, y: 0, width: 402, height: 874))
90 surface.attach(first)
91 surface.suppressKeyboard(true)
92 surface.sheetCovers(300)
93 let second = DocsTextView(frame: CGRect(x: 0, y: 0, width: 402, height: 874))
94 surface.attach(second)
95 XCTAssertTrue(second.keyboardSuppressed, "the keyboard stays away under the sheet")
96 XCTAssertEqual(second.bottomObstruction, 300, "and the text clear of it")
97 surface.suppressKeyboard(false)
98 surface.sheetCovers(0)
99 let third = DocsTextView(frame: CGRect(x: 0, y: 0, width: 402, height: 874))
100 surface.attach(third)
101 XCTAssertFalse(third.keyboardSuppressed)
102 XCTAssertEqual(third.bottomObstruction, 0)
103 }
104
105 /// Spec §3, "Header and scrolling": the title folds into the bar on the
106 /// keyboard and with the Format sheet, and unfolds only at the top with
107 /// the keyboard down and the sheet closed.
108 func testTheTitleFoldsOnTheKeyboardAndTheSheet() async throws {
109 let (text, _, _) = try await open()
110 let state = try XCTUnwrap(text.header?.state)
111 XCTAssertFalse(state.collapsed, "at the top, the large title")
112 XCTAssertTrue(text.becomeFirstResponder())
113 try await until("the keyboard folds the title") { state.collapsed }
114 XCTAssertTrue(text.resignFirstResponder())
115 try await until("the keyboard down at the top unfolds it") { !state.collapsed }
116 state.sheetOpen = true
117 XCTAssertTrue(state.collapsed, "the sheet folds it")
118 state.sheetOpen = false
119 XCTAssertFalse(state.collapsed)
120 }
121
122 /// The keyboard bar is a writer's, rides on the text view as its input
123 /// accessory (so a hardware keyboard shows it too, at the bottom), and
124 /// goes while the Format sheet is open, when the keyboard goes but the
125 /// text view keeps first responder and its selection.
126 func testTheKeyboardBarAndTheFormatSheetsKeyboard() async throws {
127 let (text, surface, _) = try await open()
128 XCTAssertNotNil(text.inputAccessoryView, "a writer's text view carries the keyboard bar")
129 text.selectedRange = NSRange(location: 3, length: 5)
130 XCTAssertTrue(text.becomeFirstResponder())
131 surface.suppressKeyboard(true)
132 XCTAssertTrue(text.isFirstResponder, "the Format sheet keeps first responder")
133 XCTAssertEqual(text.selectedRange, NSRange(location: 3, length: 5), "and the selection")
134 XCTAssertNotNil(text.inputView, "the keyboard is an empty input view meanwhile")
135 XCTAssertNil(text.inputAccessoryView, "and no keyboard bar under the sheet")
136 surface.suppressKeyboard(false)
137 XCTAssertNil(text.inputView, "the keyboard comes back as it was")
138 XCTAssertNotNil(text.inputAccessoryView)
139 surface.isEditable = false
140 XCTAssertNil(text.inputAccessoryView, "a reader's text view has none")
141 }
142}
143#endif
144