oficina / docs Public

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

Plain git — no account needed to clone.

Download

Download this file

1import DocsKit
2import Foundation
3import RastrilloApple
4import RastrilloSnapshots
5import RastrilloTestSupport
6import SwiftUI
7import XCTest
8
9/// Docs' key screens for the screenshot gate (rastrillo/native
10/// RastrilloSnapshots): the library, Oficina ID onboarding as Docs shows it,
11/// Other ways to sign in, the team picker and, on the Mac, Settings. Drawn
12/// from fixtures with no server, keychain, App Group or network, in light and
13/// dark, and on iPhone at the default size and AX3.
14///
15/// `native/scripts/snapshots.sh` runs it (compare, or record); `swift test`
16/// alone skips it, because baselines match only the OS that drew them. The
17/// Mac half runs from `swift test`; the iPhone half is the same file in the
18/// `DocsPhoneSnapshots` target of `ios/project.yml`.
19///
20/// The editor's page is in EditorSnapshots.swift, with its own catalog:
21/// its document opens asynchronously, so its screens hold themselves
22/// unsettled until they are drawn. Bundled faces are registered first, or
23/// the baselines would record fallback faces.
24final class DocsSnapshots: XCTestCase {
25 @MainActor func testKeyScreens() async throws {
26 try XCTSkipUnless(ProcessInfo.processInfo.environment["RASTRILLO_SNAPSHOTS"] != nil,
27 "Run with native/scripts/snapshots.sh")
28 DocsFonts.register()
29 let fixtures = await Fixtures.make()
30 await assertSnapshots(of: Self.catalog(fixtures))
31 }
32
33 @MainActor static func catalog(_ fixtures: Fixtures) -> SnapshotCatalog {
34 var screens: [SnapshotScreen] = [
35 SnapshotScreen("library", mac: .fixed(width: 1000, height: 700), iphone: .fixed(width: 402, height: 874)) {
36 Fixtures.library(["Quarterly plan", "Meeting notes, 12 January", "Letter to the landlord", "Recipes"])
37 },
38 SnapshotScreen("library-empty", mac: .fixed(width: 1000, height: 700), iphone: .fixed(width: 402, height: 874)) {
39 Fixtures.library([])
40 },
41 SnapshotScreen("onboarding-welcome") {
42 OficinaOnboardingView(model: fixtures.welcome, otherWays: {})
43 },
44 SnapshotScreen("onboarding-code") {
45 OficinaOnboardingView(model: fixtures.code, otherWays: {})
46 },
47 SnapshotScreen("onboarding-teams") {
48 OficinaOnboardingView(model: fixtures.teams, otherWays: {})
49 },
50 SnapshotScreen("other-ways", mac: .fixed(width: 520, height: 480), iphone: .fixed(width: 402, height: 640)) {
51 RemountedOnScreen { DocsOtherWaysView(connection: Fixtures.connection()) {} }
52 },
53 SnapshotScreen("team-picker", mac: .fixed(width: 460, height: 540), iphone: .fixed(width: 402, height: 720)) {
54 DocsTeamPicker(model: fixtures.signedIn, current: fixtures.signedInState,
55 otherWays: {}, signOut: {}, done: {})
56 },
57 SnapshotScreen("team-picker-blocked", mac: .fixed(width: 460, height: 540), iphone: .fixed(width: 402, height: 720)) {
58 // Open documents or unsent changes: the switch and sign-out
59 // are disabled, and the line says why.
60 DocsTeamPicker(model: fixtures.signedIn, current: fixtures.signedInState, leaveCheck: { .sending },
61 otherWays: {}, signOut: {}, done: {})
62 },
63 ]
64 #if os(macOS)
65 screens.append(SnapshotScreen("settings", mac: .width(480), iphone: nil) {
66 DocsSettingsView(account: .oficinaID(identity: "paul@tito.io", team: "Paul", teamLabel: "paulca"), block: nil,
67 version: "0.1.0 (1)", manage: {}, signOut: {}, signIn: {})
68 })
69 screens.append(SnapshotScreen("settings-blocked", mac: .width(480), iphone: nil) {
70 DocsSettingsView(account: .oficinaID(identity: "paul@tito.io", team: "Paul", teamLabel: "paulca"), block: .openDocuments,
71 version: "0.1.0 (1)", manage: {}, signOut: {}, signIn: {})
72 })
73 screens.append(SnapshotScreen("settings-server", mac: .width(480), iphone: nil) {
74 DocsSettingsView(account: .server(name: "Tito", host: "tito.docs.oficina.works"), block: nil,
75 version: "0.1.0 (1)", manage: {}, signOut: {}, signIn: {})
76 })
77 #endif
78 return SnapshotCatalog(app: "docs", settings: SnapshotSettings(tint: OficinaPalette.accent(.docs)), screens: screens)
79 }
80}
81
82/// Oficina ID models walked to each step through the kit's in-memory Home,
83/// directory and instances, exactly as a person would get there: nothing
84/// touches the keychain, an App Group or the network.
85@MainActor final class Fixtures {
86 static let origin = URL(string: "https://docs.snapshot.invalid")!
87 let welcome: OficinaIDModel
88 let code: OficinaIDModel
89 let teams: OficinaIDModel
90 let signedIn: OficinaIDModel
91 let signedInState: OficinaSignedIn
92
93 private init(welcome: OficinaIDModel, code: OficinaIDModel, teams: OficinaIDModel, signedIn: OficinaIDModel, state: OficinaSignedIn) {
94 self.welcome = welcome; self.code = code; self.teams = teams; self.signedIn = signedIn; signedInState = state
95 }
96
97 static func make() async -> Fixtures {
98 let welcome = model(), code = model(), teams = model(), signedIn = model()
99 await welcome.start()
100 await walk(code, to: 1)
101 await walk(teams, to: 2)
102 await walk(signedIn, to: 2)
103 if case .teams(let list) = signedIn.step, let first = list.first { await signedIn.choose(first) }
104 guard case .signedIn(let state) = signedIn.step else { preconditionFailure("fixture did not sign in: \(signedIn.step)") }
105 return Fixtures(welcome: welcome, code: code, teams: teams, signedIn: signedIn, state: state)
106 }
107
108 private static func team(_ sqid: String, _ name: String, _ label: String) -> OficinaTeam {
109 OficinaTeam(sqid: sqid, label: label, name: name, role: "member",
110 apps: [OficinaTeamApp(app: "docs", origin: "https://\(label).docs.oficina.works", consumerID: "\(sqid)-docs")])
111 }
112
113 private static func model() -> OficinaIDModel {
114 let service = FakeOficinaIDService()
115 service.addPerson(email: "paul@tito.io", identityID: "id-paul")
116 let teams = [team("t1", "Paul", "paulca"), team("t2", "Tito", "tito"), team("t3", "Campbells", "campbells")]
117 service.setTeams(teams, for: "id-paul")
118 for team in teams { service.admit("id-paul", at: team.origin(for: "docs")!, as: .linked) }
119 let suite = "DocsSnapshots.\(UUID().uuidString)"
120 let defaults = UserDefaults(suiteName: suite)!
121 defaults.removePersistentDomain(forName: suite)
122 let store = OficinaIDStore(shared: OficinaMemoryStore(), appPrivate: OficinaMemoryStore(), group: defaults)
123 return OficinaIDModel(configuration: DocsOficinaSession.configuration, store: store, service: service,
124 passkeys: FakePasskeys(), makeKey: { OficinaSoftwareKey() },
125 now: { SnapshotSettings.now }, appDefaults: defaults)
126 }
127
128 /// 1: the code step. 2: the team step, after the code.
129 private static func walk(_ model: OficinaIDModel, to stage: Int) async {
130 await model.start()
131 model.continueWithEmail()
132 model.email = "paul@tito.io"
133 await model.submitEmail()
134 guard stage > 1 else { return }
135 model.code = FakeOficinaIDService.goodCode
136 await model.submitCode()
137 }
138
139 /// A workspace whose server answers the document list and nothing else.
140 static func library(_ titles: [String]) -> some View {
141 let documents = titles.enumerated().map { #"{"sqid":"d\#($0.offset)","title":"\#($0.element)","shared":\#($0.offset == 1)}"# }
142 let list = Data(#"{"documents":[\#(documents.joined(separator: ","))],"can_create":true}"#.utf8)
143 let server = ScriptedTransport(origin: origin) { call in
144 call.path.hasPrefix("/?") ? .respond(status: 200, body: list) : .respond(status: 404)
145 }
146 return DocsWorkspace(client: NativeHTTP(origin: origin), transport: { server }, accounts: {}, signOut: {},
147 accountsLabel: DocsOficinaSession.teamsTitle,
148 signOutLabel: OficinaIDText.text("signOut", "paul@tito.io"))
149 }
150
151 /// A connection on a throwaway defaults suite and an origin no app holds
152 /// a credential for, so `NativeConnection.init`'s legacy-credential
153 /// migration can never find (and move) a real item.
154 static func connection() -> NativeConnection {
155 let suite = "DocsSnapshots.connection.\(UUID().uuidString)"
156 let defaults = UserDefaults(suiteName: suite)!
157 defaults.removePersistentDomain(forName: suite)
158 let connection = NativeConnection(defaultServer: origin.absoluteString, defaults: defaults,
159 connectivity: NativeConnectivity(source: ScriptedPathSource(.satisfied)))
160 connection.serverAddress = "https://docs.oficina.works"
161 return connection
162 }
163}
164
165/// Builds its content again once it is on screen, and draws only that
166/// second build. On the Mac, "Other ways"'s rounded text fields (in the
167/// kit's `NativeSignInView`) are sometimes made 30 points tall instead of 24
168/// when the screen is first built, more often under load, and keep that
169/// height: about one gate run in twenty under a light load, and both
170/// pictures of that run (finding 4 on #161). Built again inside a window
171/// that is already showing, they came out 24 every time (50 runs under
172/// the same load, 200 fields).
173struct RemountedOnScreen<Content: View>: View {
174 @ViewBuilder let content: () -> Content
175 @State private var built = false
176 var body: some View {
177 content().id(built).task {
178 guard !built else { return }
179 await Task.yield()
180 built = true
181 }
182 }
183}
184