oficina / base Public

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

Plain git — no account needed to clone.

Download

Download this file

1import BaseKit
2import Foundation
3import RastrilloApple
4import RastrilloSnapshots
5import RastrilloTestSupport
6import SwiftUI
7import XCTest
8
9/// Ask, drawn (Task 11): the approved mockups' thread (mac-main-b,
10/// phone-ask) on the Mac and the iPhone, the job view, and Ask's other
11/// states, in light and dark, and on iPhone at AX3. Fixed fixtures; no
12/// server. The window's bar and sidebar are Tasks 13 and 14's: the frame
13/// here is only enough to compare with the mockups.
14final class AskSnapshots: XCTestCase {
15 @MainActor func testAsk() async throws {
16 try XCTSkipUnless(ProcessInfo.processInfo.environment["RASTRILLO_SNAPSHOTS"] != nil, "Run with native/scripts/snapshots.sh")
17 BaseFonts.register()
18 await assertSnapshots(of: Self.catalog)
19 }
20
21 @MainActor static var catalog: SnapshotCatalog {
22 SnapshotCatalog(app: "base-ask", settings: SnapshotSettings(tint: LookSnapshots.tint), screens: [
23 SnapshotScreen("thread", mac: .fixed(width: 1000, height: 720), iphone: .fixed(width: 402, height: 874)) {
24 AskFrame { AskView(model: AskFixtures.thread(), layout: frameLayout, timeZone: AskFixtures.zone, actions: AskActions {}) }
25 },
26 SnapshotScreen("job-view", mac: .width(320), iphone: .width(402)) {
27 JobView(job: AskFixtures.job, timeZone: AskFixtures.zone, close: {}, now: SnapshotSettings.now)
28 },
29 // Review finding 9: many ended cards in the job view's real
30 // bounds (the popover's 320 × 520; half the iPhone, the sheet's
31 // medium detent): the details scroll, Close stays in reach.
32 SnapshotScreen("job-view-long", mac: .fixed(width: 320, height: 520), iphone: .fixed(width: 402, height: 437)) {
33 JobView(job: AskFixtures.longJob, timeZone: AskFixtures.zone, close: {}, now: SnapshotSettings.now)
34 },
35 // Clearer paused work (2026-11-24): a paused job's pill (the
36 // clock on indigo, "Paused until 21:00") beside a stopped one
37 // (its square) and one working; and the paused job's view with
38 // its sentence and Carry on now.
39 SnapshotScreen("paused", mac: .fixed(width: 640, height: 360), iphone: .fixed(width: 402, height: 420)) {
40 AskFrame { AskView(model: AskFixtures.paused(), layout: frameLayout, timeZone: AskFixtures.zone, actions: AskActions {}) }
41 },
42 SnapshotScreen("job-view-paused", mac: .width(320), iphone: .width(402)) {
43 JobView(job: AskFixtures.pausedJob, timeZone: AskFixtures.zone, close: {}, now: SnapshotSettings.now)
44 },
45 // Fable's review S3: a failed Carry on now says so in the view.
46 SnapshotScreen("job-view-paused-failed", mac: .width(320), iphone: .width(402)) {
47 JobView(job: AskFixtures.pausedJob, timeZone: AskFixtures.zone, close: {}, now: SnapshotSettings.now,
48 carryOnError: BaseText.text("ask.not_sent"))
49 },
50 // The person's own words in the Assistant's little Markdown
51 // (2026-11-26): bold, italics, a list with a link, lines kept;
52 // and a plain message, exactly as written.
53 SnapshotScreen("person-markdown", mac: .fixed(width: 640, height: 520), iphone: .fixed(width: 402, height: 620)) {
54 AskFrame { AskView(model: AskFixtures.personMarkdown(), layout: frameLayout, timeZone: AskFixtures.zone, actions: AskActions {}) }
55 },
56 SnapshotScreen("norunner", mac: .fixed(width: 640, height: 420), iphone: .fixed(width: 402, height: 520)) {
57 AskFrame { AskView(model: AskFixtures.model(state: "norunner", extra: AskFixtures.shortThread), layout: frameLayout, timeZone: AskFixtures.zone, actions: AskActions {}) }
58 },
59 SnapshotScreen("connect", mac: .fixed(width: 640, height: 300), iphone: .fixed(width: 402, height: 360)) {
60 AskFrame { AskView(model: AskFixtures.model(state: "connect"), layout: frameLayout, timeZone: AskFixtures.zone, actions: AskActions {}) }
61 },
62 SnapshotScreen("not-linked", mac: .fixed(width: 640, height: 300), iphone: .fixed(width: 402, height: 360)) {
63 AskFrame { AskView(model: AskFixtures.notLinked(), layout: frameLayout, timeZone: AskFixtures.zone, actions: AskActions {}) }
64 },
65 ])
66 }
67}
68
69let frameLayout: AskLayout = {
70 #if os(macOS)
71 .window
72 #else
73 .phone
74 #endif
75}()
76
77/// The sky and the conversation's sheet, with its title bar, as the
78/// mockups frame Ask.
79struct AskFrame<Content: View>: View {
80 @ViewBuilder let content: Content
81 @Environment(\.colorScheme) private var scheme
82 var body: some View {
83 let p = Palette(scheme)
84 ZStack {
85 Sky()
86 VStack(spacing: 0) {
87 HStack(spacing: 9) {
88 BaseMark(size: 16)
89 Text(BaseText.text("native.sidebar.ask")).font(BaseFont.heading(17, .semibold))
90 Spacer()
91 }
92 .padding(.horizontal, 20).frame(height: 52)
93 .overlay(alignment: .bottom) { Rectangle().fill(p[.rule]).frame(height: 1) }
94 content.frame(maxWidth: 760)
95 }
96 .foregroundStyle(p[.frost])
97 .glassSheet(cornerRadius: 18)
98 .padding(10)
99 }
100 .skyFrozen()
101 }
102}
103
104@MainActor enum AskFixtures {
105 static let zone = TimeZone(identifier: "Europe/Dublin")!
106
107 static func model(state: String, extra: String = "") -> AskModel {
108 let m = AskModel(client: BaseClient(transport: ScriptedTransport()) {}, schedule: { _, _ in })
109 let json = #"{"state":"\#(state)","messages":[\#(extra)],"jobs":[],"cards":[],"turn":null,"status":"","cursor":1,"equipo_origin":"https://equipo.fennel.example"}"#
110 m.applyForPreview(try! JSONDecoder().decode(AskSnapshot.self, from: Data(json.utf8)))
111 return m
112 }
113
114 static let shortThread = #"{"id":"m1","role":"person","text":"What's on tomorrow?","turn_id":"t1"}"#
115
116 static func notLinked() -> AskModel {
117 let m = model(state: "connect")
118 m.show(.notLinked)
119 return m
120 }
121
122 /// The mockups' thread: the bakery website on its way, a question on
123 /// the Christmas order form, invoice reminders ready, and tomorrow's
124 /// plan streaming in.
125 static func thread() -> AskModel {
126 let m = AskModel(client: BaseClient(transport: ScriptedTransport()) {}, schedule: { _, _ in })
127 let json = #"""
128 {"state":"ready","cursor":40,"status":"","turn":null,"cards":[],
129 "messages":[
130 {"id":"m1","role":"person","text":"Can you make us a website for the bakery? Use the menu in our Sheets.","turn_id":"t1"},
131 {"id":"m2","role":"assistant","text":"On it. You'll see a first look in a couple of minutes, with the menu from your Sheets.","turn_id":"t1"}],
132 "jobs":[
133 {"id":"j1","revision":1,"kind":"job","state":"working","question":"Bakery website","result":"Building a first look","options":[],"links":[]},
134 {"id":"j2","revision":1,"kind":"job","state":"asking","question":"Christmas order form","result":"One question for you","options":[],"links":[],
135 "cards":[{"id":"c1","revision":1,"kind":"question","state":"open","question":"When should Christmas orders close?","job":"j2",
136 "options":[{"value":"fri","label":"Fri 18 Dec"},{"value":"tue","label":"Tue 22 Dec"},{"value":"else","label":"Something else"}],"links":[]}]},
137 {"id":"j3","revision":1,"kind":"job","state":"ready","question":"Invoice reminders","result":"Ready to try","options":[],
138 "links":[{"label":"Try it","url":"https://fennel.base.example/apps/reminders"}]}]}
139 """#
140 m.applyForPreview(try! JSONDecoder().decode(AskSnapshot.self, from: Data(json.utf8)))
141 m.apply(.message(AskMessage(id: "m3", role: "person", text: "What's on tomorrow?", turnID: "t2")))
142 m.apply(.delta(AskDelta(turnID: "t2", offset: 0, text: "Three things tomorrow:\n- 08:00 Flour delivery\n- 11:30 Ana, about the Christmas menu\n- 15:00 Call with the accountant\n")))
143 return m
144 }
145
146 /// Five jobs waiting on the person, each with a decision under it.
147 static func manyWaiting() -> AskModel {
148 let m = thread()
149 for n in 1...5 {
150 m.apply(.card(AskCard(id: "w\(n)", revision: 1, kind: "job", state: n % 2 == 0 ? "stopped" : "asking",
151 question: "Waiting job \(n)", result: n % 2 == 0 ? "Stopped: needs you" : "One question for you")))
152 m.apply(.card(AskCard(id: "wc\(n)", revision: 1, kind: "confirm", state: "open", question: "Go ahead with job \(n)?",
153 options: [.init(value: "yes", label: "Go ahead"), .init(value: "no", label: "Not yet")], job: "w\(n)")))
154 }
155 return m
156 }
157
158 static func personMarkdown() -> AskModel {
159 model(state: "ready", extra: #"""
160 {"id":"m1","role":"person","text":"**Christmas menu** for the bakery, *draft two*:\n- Mince pies, [see the recipe](https://fennel.example/menu/pies)\n- **Stollen**\n\nAna signs it off\non Friday.","turn_id":"t1"},
161 {"id":"m2","role":"assistant","text":"Got it. I'll add **both** to the *Christmas* menu.","turn_id":"t1"},
162 {"id":"m3","role":"person","text":"Two lines,\n2*3 and a_b_c stay as typed.","turn_id":"t2"}
163 """#)
164 }
165
166 /// A paused job that offers Carry on now, a stopped one and one working.
167 static func paused() -> AskModel {
168 let m = model(state: "ready", extra: #"{"id":"m1","role":"person","text":"Can you put together a simple website for the bakery?","turn_id":"t1"}"#)
169 m.apply(.card(pausedJob.card))
170 m.apply(.card(AskCard(id: "j5", revision: 2, kind: "job", state: "stopped", question: "Invoice reminders", result: "Stopped: needs you")))
171 m.apply(.card(AskCard(id: "j6", revision: 1, kind: "job", state: "working", question: "Christmas menu", result: "Building a first look")))
172 return m
173 }
174
175 static let pausedJob = AskModel.Job(
176 card: AskCard(id: "j4", revision: 3, kind: "job", state: "paused", question: "Bakery website",
177 links: [.init(label: "Open in Equipo", url: URL(string: "https://equipo.fennel.example/tasks/tk4"), rel: "equipo")],
178 result: "Paused until 21:00",
179 detail: "It has reached the limit on its plan for now. It will carry on by itself at 21:00 (in 9 hours).",
180 startedAt: SnapshotSettings.now.addingTimeInterval(-2 * 3600), carryOn: true))
181
182 static let longJob = AskModel.Job(card: job.card, open: [], ended: (1...6).map { n in
183 AskCard(id: "e\(n)", kind: "confirm", state: "answered", question: "Decision \(n): use the autumn menu's prices for the Christmas boxes?",
184 result: n % 2 == 0 ? "You said yes" : "You said no", job: "j2")
185 })
186
187 static let job = AskModel.Job(
188 card: AskCard(id: "j2", revision: 3, kind: "job", state: "asking", question: "Christmas order form",
189 links: [.init(label: "Open in Equipo", url: URL(string: "https://equipo.fennel.example/tasks/tk2"), rel: "equipo")],
190 result: "One question for you",
191 detail: "The form is built and works. It needs a closing date before it goes on the website.",
192 startedAt: SnapshotSettings.now.addingTimeInterval(-18 * 3600)),
193 open: [],
194 ended: [AskCard(id: "c0", kind: "confirm", state: "answered", question: "Use the autumn menu's prices?", result: "You said yes", job: "j2")])
195}
196