Repository Public
1import AIAssistant2import AppKit3import SessionStore4import WindowManager5import WorkspaceKit6import XCTest7@testable import SPTerm89/// Mutable set of simulated displays shared with the model under test.10@MainActor11final class FakeDisplays {12 var screens: [ScreenDescriptor]13 init(_ screens: [ScreenDescriptor]) { self.screens = screens }1415 static let laptop = ScreenDescriptor(id: 1, name: "Built-in Retina Display", frame: CGRect(x: 0, y: 0, width: 1512, height: 982),16 visibleFrame: CGRect(x: 0, y: 70, width: 1512, height: 874), isMain: true)17 static let external = ScreenDescriptor(id: 2, name: "Studio Display", frame: CGRect(x: 1512, y: -200, width: 2560, height: 1440),18 visibleFrame: CGRect(x: 1512, y: -200, width: 2560, height: 1415), isMain: false)19}2021@MainActor22final class Answers {23 var confirm = true24 var asked: [String] = []25}2627/// Records every request; never touches the network.28final class MockTransport: HTTPTransport, @unchecked Sendable {29 private let lock = NSLock()30 private var _requests: [HTTPRequest] = []31 var reply: String32 init(reply: String) { self.reply = reply }33 var requests: [HTTPRequest] { lock.withLock { _requests } }3435 func send(_ request: HTTPRequest) async throws -> HTTPResponse {36 lock.withLock { _requests.append(request) }37 let body: [String: Any] = ["id": "msg_1", "type": "message", "role": "assistant", "model": "test-model", "stop_reason": "end_turn",38 "content": [["type": "text", "text": reply]], "usage": ["input_tokens": 12, "output_tokens": 34]]39 return HTTPResponse(statusCode: 200, headers: [:], body: try JSONSerialization.data(withJSONObject: body))40 }41}4243@MainActor44class AppTestCase: XCTestCase {45 var root: URL!46 var displays: FakeDisplays!47 var answers: Answers!48 var secrets: InMemorySecretStore!4950 override func setUp() async throws {51 root = FileManager.default.temporaryDirectory.appendingPathComponent("spterm-apptests-\(UUID().uuidString)")52 displays = FakeDisplays([FakeDisplays.laptop])53 answers = Answers()54 secrets = InMemorySecretStore()55 }5657 override func tearDown() async throws {58 try? FileManager.default.removeItem(at: root)59 }6061 func makeModel(persists: Bool = true) -> AppModel {62 let displays = displays!, answers = answers!63 let environment = AppEnvironment(locations: StoreLocations(root: root), secrets: secrets, screens: { displays.screens },64 showsWindows: false, confirm: { title, _, _ in65 answers.asked.append(title)66 return answers.confirm67 }, persists: persists)68 let model = AppModel(environment: environment)69 // Deterministic sessions: plain sh, no user configuration, no confirmation noise.70 model.settings.shellPath = "/bin/sh"71 model.settings.loginShell = false72 model.settings.startupDirectory = "/tmp"73 addTeardownBlock { @MainActor in74 model.terminateAllSessions()75 for window in model.windows { window.controller?.closeWithoutPrompt() }76 }77 return model78 }7980 func wait(_ timeout: TimeInterval = 8, file: StaticString = #filePath, line: UInt = #line, _ condition: @MainActor () -> Bool) async -> Bool {81 let deadline = Date().addingTimeInterval(timeout)82 while Date() < deadline {83 if condition() { return true }84 try? await Task.sleep(for: .milliseconds(30))85 }86 return condition()87 }8889 /// Starts the process of every pane of a window and waits for the shells to be running.90 func start(_ window: WindowState, in model: AppModel) async {91 for pane in window.allPanes { model.startIfNeeded(pane) }92 let ok = await wait { window.allPanes.allSatisfy { model.session(for: $0)?.status.isRunning == true } }93 XCTAssertTrue(ok, "sessions did not start")94 }9596 func text(_ pane: PaneID, _ model: AppModel) -> String { model.session(for: pane)?.bufferText() ?? "" }97}98