SPB Git

Repository Public License

ScreenTests.swift 4.1 KB 75 lines swift

1import AppKit2import Foundation3import SwiftUI4import Testing5import PrismSecurity6import PrismTestKit7@testable import PrismStudio89@MainActor10func snapshot<V: View>(_ view: V, _ name: String, size: CGSize = CGSize(width: 1240, height: 780)) -> Int {11    let host = NSHostingView(rootView: view.frame(width: size.width, height: size.height).background(Color(nsColor: .windowBackgroundColor)))12    host.frame = CGRect(origin: .zero, size: size)13    let window = NSWindow(contentRect: host.frame, styleMask: [.borderless], backing: .buffered, defer: false)14    window.contentView = host15    host.layoutSubtreeIfNeeded()16    RunLoop.main.run(until: Date().addingTimeInterval(0.4))17    host.layoutSubtreeIfNeeded()18    guard let rep = host.bitmapImageRepForCachingDisplay(in: host.bounds) else { return 0 }19    host.cacheDisplay(in: host.bounds, to: rep)20    let dir = testRepoRoot.appendingPathComponent("build/snapshots")21    try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)22    try? rep.representation(using: .png, properties: [:])?.write(to: dir.appendingPathComponent("screen-\(name).png"))23    var seen = Set<Int>()24    for x in stride(from: 3, to: rep.pixelsWide, by: 9) {25        for y in stride(from: 3, to: rep.pixelsHigh, by: 9) {26            guard let c = rep.colorAt(x: x, y: y)?.usingColorSpace(.sRGB) else { continue }27            seen.insert(Int(c.redComponent * 15) << 8 | Int(c.greenComponent * 15) << 4 | Int(c.blueComponent * 15))28        }29    }30    return seen.count31}3233/// Every PRISM screen is laid out and drawn off-screen with real state. This catches crashes and blank screens;34/// it does not replace using the app.35@MainActor36@Suite(.serialized) struct ScreenTests {37    @Test func everyScreenRenders() async throws {38        let root = studioRoot()39        defer { try? FileManager.default.removeItem(at: root) }40        let transport = ScriptedTransport([.json(200, modelListJSON)])41        let studio = try makeStudio(root, transport: transport, secrets: InMemorySecretStore([SecretAccount.anthropicAPIKey: testKey]))42        _ = await studio.testConnection()4344        #expect(snapshot(WelcomeView(model: studio), "welcome") > 4)45        let session = try #require(studio.createFromTemplate("expense-tracker"))46        _ = session.runtime?.importCSV(from: testRepoRoot.appendingPathComponent("templates/expense-tracker/sample.csv"), into: "transactions")47        #expect(snapshot(PreviewPane(session: session), "preview") > 8)4849        session.designSelection = "monthly_chart"50        #expect(snapshot(DesignView(model: studio, session: session), "design") > 8)51        session.designSelection = nil52        #expect(snapshot(InspectorPane(model: studio, session: session), "design-theme", size: CGSize(width: 320, height: 600)) > 3)5354        transport.append(script(budgetAlertAndChartCalls, closing: "Added a budget alert and a chart by category."))55        session.submit("Add a budget alert and a new chart.")56        await session.waitForBuild()57        await session.runProposalChecks()58        #expect(session.proposal?.checksPassed == true)59        #expect(snapshot(EvolveView(model: studio, session: session), "evolve") > 6)60        #expect(snapshot(BuildView(model: studio, session: session), "build") > 6)6162        if studioHostTemplate != nil { session.preparePublish(directory: root.appendingPathComponent("Applications"), version: "1.0.0", signingIdentity: nil) }63        #expect(snapshot(PublishView(model: studio, session: session), "publish") > 4)64        #expect(snapshot(LibraryView(model: studio), "library") > 6)65        #expect(snapshot(MonitorView(model: studio), "monitor") > 3)66        #expect(snapshot(SettingsView(model: studio), "settings", size: CGSize(width: 560, height: 430)) > 3)67        #expect(snapshot(CommandPalette(model: studio, isPresented: .constant(true)), "palette", size: CGSize(width: 520, height: 360)) > 3)6869        // A draft with no key shows the honest "not connected" state with working ways forward.70        studio.removeKey()71        let draft = try #require(studio.createProject(prompt: "Track my workouts"))72        #expect(draft.isDraft && snapshot(BuildView(model: studio, session: draft), "draft-no-key") > 4)73    }74}75
Files main
Menu

Theme