| 1 | #if DEBUG |
| 2 | import SwiftUI |
| 3 | #if os(macOS) |
| 4 | import AppKit |
| 5 | #endif |
| 6 | |
| 7 | /// Production task screen and private WebKit sheet against the manual Go |
| 8 | /// screenshot fixture. The private config contains only a disposable test bearer. |
| 9 | public struct PreviewFixture: View { |
| 10 | @State private var session: EquipoSession? |
| 11 | public init() {} |
| 12 | public var body: some View { |
| 13 | Group { |
| 14 | if let session { |
| 15 | NavigationStack { TaskScreen(session: session, taskID: "tk_preview") } |
| 16 | } else { ProgressView() } |
| 17 | } |
| 18 | .preferredColorScheme(.light) |
| 19 | .task { |
| 20 | guard let path = ProcessInfo.processInfo.environment["EQUIPO_PREVIEW_FIXTURE"], |
| 21 | let data = try? Data(contentsOf: URL(fileURLWithPath: path)), |
| 22 | let config = try? JSONDecoder().decode([String: String].self, from: data), |
| 23 | let origin = config["origin"].flatMap(URL.init(string:)), let token = config["token"] else { return } |
| 24 | var account = DeviceAccount.new(origin: origin, label: "Preview fixture") |
| 25 | account.credential = DeviceCredential(id: "fixture", token: token, audience: "equipo.person.v1", instanceId: "fixture", expiresAt: .distantFuture) |
| 26 | let session = EquipoSession(account: account, accounts: DeviceAccounts(store: MemorySecretStore())) |
| 27 | self.session = session |
| 28 | await session.refresh() |
| 29 | if let task = try? await session.client.task("tk_preview") { |
| 30 | await session.previews.open(client: session.client, task: task) |
| 31 | #if os(macOS) |
| 32 | if let capture = ProcessInfo.processInfo.environment["EQUIPO_FIXTURE_CAPTURE"] { |
| 33 | try? await Task.sleep(for: .seconds(5)) |
| 34 | if let window = NSApp.windows.first(where: { $0.isSheet }), let view = window.contentView { |
| 35 | view.layoutSubtreeIfNeeded(); window.displayIfNeeded() |
| 36 | if let bitmap = view.bitmapImageRepForCachingDisplay(in: view.bounds) { |
| 37 | view.cacheDisplay(in: view.bounds, to: bitmap) |
| 38 | if let data = bitmap.representation(using: .png, properties: [:]) { |
| 39 | _ = FileManager.default.createFile(atPath: capture, contents: data) |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | #endif |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | #endif |
| 50 | |