#if os(iOS) import Foundation import RastrilloApple import RastrilloSnapshots import RastrilloTestSupport import SwiftUI import UIKit import XCTest @testable import DocsKit /// The S3 iPhone document (spec 2026-11-22 §3, §11): the real /// `DocsWorkspace` opening a fixture document on the editor snapshots' /// in-memory server and pushing it, as a writer and as a reader; and the /// Format sheet and the Share sheet as the sheet draws them. Light and dark /// at the default size and AX3; the document, the sheets also in Arabic and /// Russian. final class DocsPhoneDocumentSnapshots: XCTestCase { @MainActor func testPhoneDocumentScreens() async throws { try XCTSkipUnless(ProcessInfo.processInfo.environment["RASTRILLO_SNAPSHOTS"] != nil, "Run with native/scripts/snapshots.sh") DocsFonts.register() let settings = SnapshotSettings(tint: OficinaPalette.accent(.docs)) await assertSnapshots(of: SnapshotCatalog(app: "docs-phone", variants: [.init(.light, .standard), .init(.dark, .standard)], settings: settings, screens: Self.screens + Self.languageScreens)) await assertSnapshots(of: SnapshotCatalog(app: "docs-phone-ax3", variants: [.init(.light, .accessibility), .init(.dark, .accessibility)], settings: settings, screens: Self.screens)) await PhoneDocumentScreen.stopAll() } static let phone = SnapshotSize.fixed(width: 402, height: 874) static let sheet = SnapshotSize.width(402) @MainActor static var screens: [SnapshotScreen] { [ SnapshotScreen("phone-document", mac: nil, iphone: phone) { PhoneDocumentScreen(reader: false) }, SnapshotScreen("phone-reader", mac: nil, iphone: phone) { PhoneDocumentScreen(reader: true) }, SnapshotScreen("phone-format-sheet", mac: nil, iphone: sheet) { PhoneFormatSheetScreen(language: "en") }, SnapshotScreen("phone-share-sheet", mac: nil, iphone: sheet) { PhoneShareSheetScreen(language: "en") }, ] } @MainActor static var languageScreens: [SnapshotScreen] { ["ar", "ru"].flatMap { language in [ SnapshotScreen("phone-document-" + language, mac: nil, iphone: phone) { PhoneDocumentScreen(reader: false, language: language) }, SnapshotScreen("phone-format-sheet-" + language, mac: nil, iphone: sheet) { PhoneFormatSheetScreen(language: language) }, SnapshotScreen("phone-share-sheet-" + language, mac: nil, iphone: sheet) { PhoneShareSheetScreen(language: language) }, ] } } } /// The document as the iPhone shows it: the workspace opening "doc" and /// pushing it, until its text is drawn and its title measured. @MainActor struct PhoneDocumentScreen: View { let reader: Bool var language = "en" @State private var server: EditorServer? @State private var ready = false private let probe = ViewProbe() private static var opened: [EditorServer] = [] static func stopAll() async { opened.removeAll() } var body: some View { ZStack(alignment: .topLeading) { if let server { DocsWorkspace(client: NativeHTTP(origin: server.origin), transport: { server }, signOut: {}, documentID: "doc", team: "Tito", connectivity: NativeConnectivity(source: ScriptedPathSource(.satisfied))) .background { PhoneProbeView(probe: probe) } } if !ready { PhoneOpeningCounter() } } .environment(\.locale, Locale(identifier: language)) .environment(\.layoutDirection, language == "ar" ? .rightToLeft : .leftToRight) .task { await open() } } private func open() async { do { let server = EditorServer() if reader { await server.readOnly() } let fixture: EditorFixture = language == "ar" ? .arabic : language == "ru" ? .russian : .millbrook try await server.seed(fixture.blocks) Self.opened.append(server) self.server = server let deadline = ContinuousClock.now + .seconds(10) while ContinuousClock.now < deadline { if let text = probe.textView, text.textStorage.length > 100, text.headerHeight > 0, await server.openStreams == 1 { break } try await Task.sleep(for: .milliseconds(20)) } guard let text = probe.textView, text.textStorage.length > 100 else { throw EditorScreenFailure("the document did not open") } text.setContentOffset(CGPoint(x: 0, y: -text.adjustedContentInset.top), animated: false) text.layoutIfNeeded() try await Task.sleep(for: .milliseconds(400)) ready = true } catch { print("phone snapshot did not open: \(error)") } } } /// The Format sheet on its own, as the sheet's medium detent shows it. @MainActor struct PhoneFormatSheetScreen: View { let language: String @State private var model = DocsModel(transport: { nil }) var body: some View { DocsFormatSheet(model: model, close: {}) .frame(height: 430, alignment: .top) .background(Color(uiColor: .systemBackground)) .environment(\.locale, Locale(identifier: language)) .environment(\.layoutDirection, language == "ar" ? .rightToLeft : .leftToRight) } } /// The Share sheet's panel, as the sheet shows it: two people, the team /// rung on, and the link published with the once-only URL showing. @MainActor struct PhoneShareSheetScreen: View { let language: String var body: some View { let information = DocsShareInformation(sqid: "doc", title: "A History of the Millbrook Waterworks", grants: [DocsShareGrant(address: "bob@example.com", role: "writer"), DocsShareGrant(address: "carol.long-address@example.org", role: "reader")], published: true, public_url: "", visibility: "team", error: "") let model = DocsSharePanelModel(sqid: "doc", title: information.title, information: information, onceURL: URL(string: "https://docs.oficina.works/p/Hx7kQ2mVb9tLr4sN")) DocsSharePanel(model: model, me: "alice@example.com", focusOnAppear: false, close: {}) .background(Color(uiColor: .systemBackground)) .environment(\.locale, Locale(identifier: language)) .environment(\.layoutDirection, language == "ar" ? .rightToLeft : .leftToRight) } } @MainActor private struct PhoneOpeningCounter: View { @State private var frame = 0 var body: some View { Text(verbatim: "opening \(frame)").padding(4).background(Color.red).foregroundStyle(.white) .task { while !Task.isCancelled { frame &+= 1; try? await Task.sleep(for: .milliseconds(10)) } } } } private struct PhoneProbeView: UIViewRepresentable { let probe: ViewProbe func makeUIView(context: Context) -> UIView { let view = UIView(); probe.view = view; return view } func updateUIView(_ view: UIView, context: Context) {} } #endif