#if os(macOS) @testable import BaseKit import AppKit import SwiftUI import XCTest /// Astra's Task 16 finding 5: the global shortcut really opens the panel. /// A real status item in the menu bar and a real panel, in this process as /// an accessory app; the shortcut's handler, then the item's click. @MainActor final class StatusPanelTests: XCTestCase { override func setUp() { _ = NSApplication.shared NSApp.setActivationPolicy(.accessory) } func testTheShortcutOpensThePanelUnderBasesItem() throws { let controller = StatusPanelController(itemShown: true, accessibilityLabel: "Ask Base") { Text(verbatim: "Ask") } defer { controller.hide(); controller.itemShown = false } let button = try XCTUnwrap(controller.statusItem?.button, "no status item in the menu bar") XCTAssertFalse(controller.panel.isVisible) controller.shortcutPressed() XCTAssertTrue(controller.panel.isVisible, "the shortcut didn't open the panel") // Under the item. if let window = button.window { let item = window.convertToScreen(button.convert(button.bounds, to: nil)) XCTAssertLessThanOrEqual(controller.panel.frame.maxY, item.minY) } // The item's click closes it, and opens it again. controller.itemClicked() XCTAssertFalse(controller.panel.isVisible) controller.itemClicked() XCTAssertTrue(controller.panel.isVisible) } /// Fable's re-review finding 3: the shortcut toggles; Escape closes it, /// with the Ask field focused. func testTheShortcutTogglesAndEscapeCloses() throws { let controller = StatusPanelController(itemShown: false, accessibilityLabel: "Ask Base") { EscapeProbe() } defer { controller.hide() } controller.shortcutPressed() XCTAssertTrue(controller.panel.isVisible) controller.shortcutPressed() XCTAssertFalse(controller.panel.isVisible, "the shortcut again closes it") controller.shortcutPressed() XCTAssertTrue(controller.panel.isVisible) RunLoop.main.run(until: Date().addingTimeInterval(0.3)) let escape = try XCTUnwrap(NSEvent.keyEvent(with: .keyDown, location: .zero, modifierFlags: [], timestamp: 0, windowNumber: controller.panel.windowNumber, context: nil, characters: "\u{1b}", charactersIgnoringModifiers: "\u{1b}", isARepeat: false, keyCode: 53)) controller.panel.sendEvent(escape) XCTAssertFalse(controller.panel.isVisible, "Escape closes it") } /// Fable's nit 7: each opening re-reads what changed while it was /// closed (the app hands the shortcut's hint over here). func testEachOpeningRunsOnShow() { let controller = StatusPanelController(itemShown: false, accessibilityLabel: "Ask Base") { Text(verbatim: "Ask") } defer { controller.hide() } var shown = 0 controller.onShow = { shown += 1 } controller.shortcutPressed() controller.shortcutPressed() controller.shortcutPressed() XCTAssertEqual(shown, 2, "twice opened, once closed") } func testTheShortcutOpensThePanelWithTheItemOff() { let controller = StatusPanelController(itemShown: false, accessibilityLabel: "Ask Base") { Text(verbatim: "Ask") } defer { controller.hide() } XCTAssertNil(controller.statusItem) controller.shortcutPressed() XCTAssertTrue(controller.panel.isVisible) XCTAssertTrue(controller.panel.canBecomeKey, "the Ask field must be able to take the keyboard") } } #endif #if os(macOS) /// A field with the keyboard, as the panel's composer has it on opening. private struct EscapeProbe: View { @State private var text = "" @FocusState private var focused: Bool var body: some View { TextField(text: $text) { Text(verbatim: "Ask") }.focused($focused).onAppear { focused = true } } } /// Astra's Task 16 finding 6: however many jobs wait, the panel's top /// takes at most its bound, and the composer still has room under it. @MainActor final class MenuPanelTests: XCTestCase { func testWaitingJobsAreBoundedAndScroll() { let m = AskModel(client: BaseClient(transport: GatedTransport()) {}, schedule: { _, _ in }) for n in 1...6 { m.apply(.card(AskCard(id: "w\(n)", revision: 1, kind: "job", state: "asking", question: "Job \(n)", result: "One question for you"))) m.apply(.card(AskCard(id: "c\(n)", revision: 1, kind: "confirm", state: "open", question: "Go ahead?", options: [.init(value: "y", label: "Go ahead")], job: "w\(n)"))) } let host = NSHostingView(rootView: WaitingJobs(ask: m, timeZone: nil).frame(width: 390)) host.frame = NSRect(x: 0, y: 0, width: 390, height: 2000) host.layoutSubtreeIfNeeded() XCTAssertLessThanOrEqual(host.fittingSize.height, WaitingJobs.maxHeight + 0.5) XCTAssertLessThan(WaitingJobs.maxHeight, StatusPanelController.size.height / 2, "the conversation and composer keep most of the panel") } } #endif #if os(macOS) /// Astra's Task 16 finding 7: Send a new code comes back on time by /// itself, with no typing, focus or other redraw. @MainActor final class ResendTests: XCTestCase { func testResendReenablesOnTimeByWaitingOnly() async throws { _ = NSApplication.shared NSApp.setActivationPolicy(.accessory) var states: [Bool] = [] let button = ResendButton(availableAt: Date().addingTimeInterval(1), busy: false, action: {}, probe: { states.append($0) }) let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 320, height: 80), styleMask: [.borderless], backing: .buffered, defer: false) window.contentView = NSHostingView(rootView: button.frame(width: 320)) window.orderFrontRegardless() defer { window.orderOut(nil) } try await Task.sleep(for: .milliseconds(300)) XCTAssertEqual(states.last, false, "disabled while the code is fresh") // Only waiting: no typing, focus or other redraw. On time, give or // take a busy machine's scheduling. try await waitUntil(.seconds(6)) { states.last == true } XCTAssertEqual(states.last, true, "enabled by waiting only") } } #endif