#if DEBUG import SwiftUI import Observation import EquipoShareCore #if os(macOS) import AppKit #endif /// Offline review fixture for the real foreground response and confirmation. /// It does not register with APNs or contact a live account. public struct NotificationFixture: View { @State private var model = FixtureModel() public init() {} public var body: some View { Group { if let notice = model.dispatcher.notices.first { NotificationActionView(dispatcher: model.dispatcher, notice: notice) } else { ProgressView().accessibilityLabel(EquipoStrings.text("work.details.task")) } } .preferredColorScheme(.light) .task { await model.open() #if os(macOS) if let path = ProcessInfo.processInfo.environment["EQUIPO_FIXTURE_CAPTURE"] { try? await Task.sleep(for: .seconds(2)) if let window = NSApp.windows.first(where: { $0.isVisible && $0.isSheet }) ?? NSApp.windows.first(where: { $0.isVisible }), let view = window.contentView { view.layoutSubtreeIfNeeded(); window.displayIfNeeded() if let bitmap = view.bitmapImageRepForCachingDisplay(in: view.bounds) { view.cacheDisplay(in: view.bounds, to: bitmap) // AppKit's sheet background is composited by WindowServer // and absent from an NSView snapshot. Render that backing // here so the capture is opaque without screen permission. let capture = NSImage(size: view.bounds.size) capture.lockFocus() NSColor.white.setFill(); NSBezierPath(rect: NSRect(origin: .zero, size: view.bounds.size)).fill() let content = NSImage(size: view.bounds.size); content.addRepresentation(bitmap) content.draw(in: NSRect(origin: .zero, size: view.bounds.size), from: .zero, operation: .sourceOver, fraction: 1) capture.unlockFocus() if let tiff = capture.tiffRepresentation, let opaque = NSBitmapImageRep(data: tiff), let data = opaque.representation(using: .png, properties: [:]) { _ = FileManager.default.createFile(atPath: path, contents: data) } } } } #endif } .onDisappear { model.dispatcher.clear(); try? FileManager.default.removeItem(at: model.directory) } } @MainActor private final class FixtureModel { let session: EquipoSession let dispatcher: NotificationDispatcher let selection: NotificationSelection let directory = FileManager.default.temporaryDirectory.appendingPathComponent("equipo-notification-fixture-" + UUID().uuidString) init() { let transport = NotificationFixtureTransport() let accounts = DeviceAccounts(store: MemorySecretStore()) var account = DeviceAccount.new(origin: transport.origin, label: "Fixture") account.address = "paul@example.com"; account.teamName = "Oficina" account.credential = DeviceCredential(id: "fixture", token: "fixture", audience: "equipo.person.v1", instanceId: "fixture", expiresAt: .distantFuture) session = EquipoSession(account: account, accounts: accounts, transport: transport) selection = accounts.notificationSelection! dispatcher = NotificationDispatcher(journal: ActionJournal(directory: directory)) dispatcher.attach(session: session, selection: selection) } func open() async { await session.refresh() let question = ProcessInfo.processInfo.environment["EQUIPO_NOTIFICATION_FIXTURE"] == "question" let data: [AnyHashable: Any] = ["equipo": ["kind": "card", "card_id": "card_merge", "task_id": "tk_fixture", "actions": ["version": 1, "binding_id": selection.id, "notification_id": "22222222-2222-4222-8222-222222222222", "card_kind": question ? "question" : "merge", "card_revision": 3]]] if let response = NotificationResponse(userInfo: data, category: question ? "equipo.question.choices.v1" : "equipo.merge.v1", action: question ? "answer" : "merge", text: nil) { await dispatcher.receive(response) } } } } private struct NotificationFixtureTransport: EquipoTransport { let underlying = TaskViewFixtureTransport() var origin: URL { underlying.origin } func send(_ request: APIRequest) async throws -> APIResponse { if request.method == "POST" { throw EquipoAPIError.offline } if request.path == "/capabilities" { return APIResponse(status: 200, data: Data(#"{"notification_actions_v1":true}"#.utf8)) } if request.path.hasPrefix("/cards/") { let snapshot = try await send(APIRequest("GET", "/snapshot")) let data = try JSONSerialization.jsonObject(with: snapshot.data) as! [String: Any] let cards = data["cards"] as! [[String: Any]] return APIResponse(status: 200, data: try JSONSerialization.data(withJSONObject: cards[0])) } let response = try await underlying.send(request) if request.path == "/snapshot", ProcessInfo.processInfo.environment["EQUIPO_NOTIFICATION_FIXTURE"] == "question", var snapshot = try JSONSerialization.jsonObject(with: response.data) as? [String: Any], var cards = snapshot["cards"] as? [[String: Any]], !cards.isEmpty { cards[0]["kind"] = "question"; cards[0]["question"] = "Should the badge clear when you switch teams too?" cards[0]["options"] = ["Clear it when switching teams", "Keep each team’s count"] snapshot["cards"] = cards return APIResponse(status: 200, data: try JSONSerialization.data(withJSONObject: snapshot)) } return response } func lines(_ request: APIRequest) async throws -> AsyncThrowingStream { AsyncThrowingStream { $0.finish() } } } #endif