| 1 | import DocsKit |
| 2 | import Foundation |
| 3 | import RastrilloApple |
| 4 | import SwiftUI |
| 5 | // This entry point is exclusively the review target. The production target |
| 6 | // compiles Sources/DocsApp and has no fixture/environment authentication path. |
| 7 | @main struct DocsReviewPhone: App { |
| 8 | private struct Configuration: Decodable { let origin: String; let credential: NativeSession } |
| 9 | private var client: NativeHTTP? { |
| 10 | let input = URL.documentsDirectory.appendingPathComponent("review.json") |
| 11 | guard let data = try? Data(contentsOf: input), |
| 12 | let config = try? JSONDecoder().decode(Configuration.self, from: data), |
| 13 | let origin = try? NativeHTTP.origin(config.origin), origin.scheme == "http", ["localhost", "127.0.0.1", "::1", "[::1]"].contains(origin.host ?? "") else { return nil } |
| 14 | return NativeHTTP(origin: origin, credential: config.credential) |
| 15 | } |
| 16 | // Every bundled face, registered before the first window, as the app does. |
| 17 | init() { DocsFonts.register() } |
| 18 | var body: some Scene { |
| 19 | WindowGroup { |
| 20 | if let client { |
| 21 | DocsWorkspace(client: client, signOut: {}) |
| 22 | .overlay(alignment: .bottom) { |
| 23 | if ProcessInfo.processInfo.environment["DOCS_REVIEW_COMMAND_PROBE"] == "1" { |
| 24 | ReviewCommandProbe() |
| 25 | } |
| 26 | if ProcessInfo.processInfo.environment["DOCS_REVIEW_DRAG_PROBE"] == "1" { |
| 27 | ReviewDragProbe() |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | else { Text("Review fixture unavailable").accessibilityIdentifier("fixture.missing") } |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | // Fixed-command review probe only. It inspects and dispatches the editor's real |
| 37 | // registered Command-K action through the first-responder chain, without HID. |
| 38 | import UIKit |
| 39 | private struct ReviewCommandProbe: View { |
| 40 | @State private var status = "Waiting for native editor focus" |
| 41 | var body: some View { |
| 42 | Text(status).font(.caption).accessibilityIdentifier("review.command.probe") |
| 43 | .task { |
| 44 | for _ in 0..<200 { |
| 45 | guard !Task.isCancelled else { return } |
| 46 | let scenes = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene } |
| 47 | let windows: [UIWindow] = scenes.flatMap { $0.windows } |
| 48 | var focused: UITextView? |
| 49 | for window in windows { |
| 50 | if let editor = focusedEditor(in: window) { focused = editor; break } |
| 51 | } |
| 52 | if let editor = focused { |
| 53 | let registered: [UIKeyCommand] = editor.keyCommands ?? [] |
| 54 | var found: UIKeyCommand? |
| 55 | for candidate in registered { |
| 56 | if candidate.input == "k" && candidate.modifierFlags == UIKeyModifierFlags.command { found = candidate; break } |
| 57 | } |
| 58 | guard let command = found, let selector = command.action, command.wantsPriorityOverSystemBehavior else { |
| 59 | status = "Missing native Command-K registration"; return |
| 60 | } |
| 61 | status = UIApplication.shared.sendAction(selector, to: nil, from: command, for: nil) |
| 62 | ? "Native Command-K dispatched" : "Native Command-K dispatch failed" |
| 63 | return |
| 64 | } |
| 65 | try? await Task.sleep(for: .milliseconds(100)) |
| 66 | } |
| 67 | status = "Native editor never became first responder" |
| 68 | } |
| 69 | } |
| 70 | private func focusedEditor(in view: UIView) -> UITextView? { |
| 71 | if let editor = view as? UITextView, editor.accessibilityIdentifier == "docs.editor", editor.isFirstResponder { return editor } |
| 72 | return view.subviews.lazy.compactMap { focusedEditor(in: $0) }.first |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // Review-only probe (Fable's delta review on #180, F5): does UIKit keep a |
| 77 | // contentOffset change made during a drag and during a fling, as the editor |
| 78 | // makes when a peer's edit lands above the screen? It fills the document, |
| 79 | // then while the test drags and flings, moves the offset 40pt once in each, |
| 80 | // and reports from the offsets it samples every frame whether the change |
| 81 | // was kept or undone on the next frame (a one-frame jump). |
| 82 | private struct ReviewDragProbe: View { |
| 83 | @State private var status = "Drag probe waiting for the editor" |
| 84 | var body: some View { |
| 85 | Text(status).font(.caption).accessibilityIdentifier("review.drag.probe") |
| 86 | .task { |
| 87 | var editor: UITextView? |
| 88 | for _ in 0..<300 where editor == nil { |
| 89 | editor = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.flatMap(\.windows).lazy |
| 90 | .compactMap { Self.editor(in: $0) }.first |
| 91 | if editor == nil { try? await Task.sleep(for: .milliseconds(100)) } |
| 92 | } |
| 93 | guard let editor else { status = "Drag probe found no editor"; return } |
| 94 | editor.selectedRange = NSRange(location: editor.textStorage.length, length: 0) |
| 95 | _ = editor.becomeFirstResponder() |
| 96 | editor.insertText(String(repeating: "\nA line of filler text for the scroll probe, long enough to wrap once on a phone.", count: 500)) |
| 97 | _ = editor.resignFirstResponder() |
| 98 | try? await Task.sleep(for: .milliseconds(300)) |
| 99 | editor.setContentOffset(CGPoint(x: 0, y: -editor.adjustedContentInset.top), animated: false) |
| 100 | try? await Task.sleep(for: .milliseconds(500)) |
| 101 | let sampler = ReviewDragSampler(editor) |
| 102 | status = "Drag probe ready" |
| 103 | while !sampler.done { try? await Task.sleep(for: .milliseconds(50)) } |
| 104 | status = "Drag probe: " + sampler.results.joined(separator: "; ") |
| 105 | + String(format: " [text %d characters, content %.0fpt]", editor.textStorage.length, editor.contentSize.height) |
| 106 | } |
| 107 | } |
| 108 | static func editor(in view: UIView) -> UITextView? { |
| 109 | if let editor = view as? UITextView, editor.accessibilityIdentifier == "docs.editor" { return editor } |
| 110 | return view.subviews.lazy.compactMap { editor(in: $0) }.first |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | @MainActor private final class ReviewDragSampler: NSObject { |
| 115 | private weak var view: UIScrollView? |
| 116 | private var link: CADisplayLink? |
| 117 | private var offsets: [CGFloat] = [] |
| 118 | private var phase = "drag" |
| 119 | private var injected: Int? |
| 120 | private var decelerationStopped = false |
| 121 | var results: [String] = [] |
| 122 | var done: Bool { results.count == 2 } |
| 123 | init(_ view: UIScrollView) { |
| 124 | self.view = view |
| 125 | super.init() |
| 126 | link = CADisplayLink(target: self, selector: #selector(tick)) |
| 127 | link?.add(to: .main, forMode: .common) |
| 128 | } |
| 129 | @objc private func tick() { |
| 130 | guard let view, !done else { link?.invalidate(); return } |
| 131 | let active = phase == "drag" ? view.isTracking && !view.isDecelerating : view.isDecelerating |
| 132 | if injected == nil { |
| 133 | if active { offsets.append(view.contentOffset.y) } else if !offsets.isEmpty, phase == "drag", !view.isTracking { offsets = [] } |
| 134 | // Twelve frames into a drag, six into a fling: the edit lands. |
| 135 | if offsets.count == (phase == "drag" ? 12 : 6) { |
| 136 | injected = offsets.count |
| 137 | view.contentOffset.y += 40 |
| 138 | offsets.append(view.contentOffset.y) |
| 139 | } |
| 140 | return |
| 141 | } |
| 142 | offsets.append(view.contentOffset.y) |
| 143 | if phase == "fling", !view.isDecelerating, offsets.count < (injected ?? 0) + 4 { decelerationStopped = true } |
| 144 | guard let at = injected, offsets.count >= at + 9 else { return } |
| 145 | let deltas = zip(offsets.dropFirst(), offsets).map { $0 - $1 } |
| 146 | let before = deltas[max(0, at - 5)..<(at - 1)] |
| 147 | let usual = before.sorted()[before.count / 2] |
| 148 | let after = Array(deltas[at...].prefix(7)) |
| 149 | let undone = after.contains { $0 < usual - 20 } |
| 150 | let list = after.map { String(format: "%.1f", $0) }.joined(separator: ",") |
| 151 | let room = view.contentSize.height - view.bounds.height + view.adjustedContentInset.bottom - offsets[at] |
| 152 | results.append("\(phase): \(undone ? "undone" : "kept")\(decelerationStopped ? ", deceleration stopped" : "") (deltas before ~\(String(format: "%.1f", usual)), after the +40 frame \(list); \(String(format: "%.0f", room))pt of scroll left below at the +40)") |
| 153 | phase = "fling"; injected = nil; offsets = []; decelerationStopped = false |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | |