| 1 | #if os(macOS) |
| 2 | @testable import BaseKit |
| 3 | import AppKit |
| 4 | import SwiftUI |
| 5 | import XCTest |
| 6 | |
| 7 | /// Astra's Task 16 finding 5: the global shortcut really opens the panel. |
| 8 | /// A real status item in the menu bar and a real panel, in this process as |
| 9 | /// an accessory app; the shortcut's handler, then the item's click. |
| 10 | @MainActor final class StatusPanelTests: XCTestCase { |
| 11 | override func setUp() { |
| 12 | _ = NSApplication.shared |
| 13 | NSApp.setActivationPolicy(.accessory) |
| 14 | } |
| 15 | |
| 16 | func testTheShortcutOpensThePanelUnderBasesItem() throws { |
| 17 | let controller = StatusPanelController(itemShown: true, accessibilityLabel: "Ask Base") { Text(verbatim: "Ask") } |
| 18 | defer { controller.hide(); controller.itemShown = false } |
| 19 | let button = try XCTUnwrap(controller.statusItem?.button, "no status item in the menu bar") |
| 20 | XCTAssertFalse(controller.panel.isVisible) |
| 21 | controller.shortcutPressed() |
| 22 | XCTAssertTrue(controller.panel.isVisible, "the shortcut didn't open the panel") |
| 23 | // Under the item. |
| 24 | if let window = button.window { |
| 25 | let item = window.convertToScreen(button.convert(button.bounds, to: nil)) |
| 26 | XCTAssertLessThanOrEqual(controller.panel.frame.maxY, item.minY) |
| 27 | } |
| 28 | // The item's click closes it, and opens it again. |
| 29 | controller.itemClicked() |
| 30 | XCTAssertFalse(controller.panel.isVisible) |
| 31 | controller.itemClicked() |
| 32 | XCTAssertTrue(controller.panel.isVisible) |
| 33 | } |
| 34 | |
| 35 | /// Fable's re-review finding 3: the shortcut toggles; Escape closes it, |
| 36 | /// with the Ask field focused. |
| 37 | func testTheShortcutTogglesAndEscapeCloses() throws { |
| 38 | let controller = StatusPanelController(itemShown: false, accessibilityLabel: "Ask Base") { EscapeProbe() } |
| 39 | defer { controller.hide() } |
| 40 | controller.shortcutPressed() |
| 41 | XCTAssertTrue(controller.panel.isVisible) |
| 42 | controller.shortcutPressed() |
| 43 | XCTAssertFalse(controller.panel.isVisible, "the shortcut again closes it") |
| 44 | controller.shortcutPressed() |
| 45 | XCTAssertTrue(controller.panel.isVisible) |
| 46 | RunLoop.main.run(until: Date().addingTimeInterval(0.3)) |
| 47 | let escape = try XCTUnwrap(NSEvent.keyEvent(with: .keyDown, location: .zero, modifierFlags: [], timestamp: 0, |
| 48 | windowNumber: controller.panel.windowNumber, context: nil, |
| 49 | characters: "\u{1b}", charactersIgnoringModifiers: "\u{1b}", isARepeat: false, keyCode: 53)) |
| 50 | controller.panel.sendEvent(escape) |
| 51 | XCTAssertFalse(controller.panel.isVisible, "Escape closes it") |
| 52 | } |
| 53 | |
| 54 | /// Fable's nit 7: each opening re-reads what changed while it was |
| 55 | /// closed (the app hands the shortcut's hint over here). |
| 56 | func testEachOpeningRunsOnShow() { |
| 57 | let controller = StatusPanelController(itemShown: false, accessibilityLabel: "Ask Base") { Text(verbatim: "Ask") } |
| 58 | defer { controller.hide() } |
| 59 | var shown = 0 |
| 60 | controller.onShow = { shown += 1 } |
| 61 | controller.shortcutPressed() |
| 62 | controller.shortcutPressed() |
| 63 | controller.shortcutPressed() |
| 64 | XCTAssertEqual(shown, 2, "twice opened, once closed") |
| 65 | } |
| 66 | |
| 67 | func testTheShortcutOpensThePanelWithTheItemOff() { |
| 68 | let controller = StatusPanelController(itemShown: false, accessibilityLabel: "Ask Base") { Text(verbatim: "Ask") } |
| 69 | defer { controller.hide() } |
| 70 | XCTAssertNil(controller.statusItem) |
| 71 | controller.shortcutPressed() |
| 72 | XCTAssertTrue(controller.panel.isVisible) |
| 73 | XCTAssertTrue(controller.panel.canBecomeKey, "the Ask field must be able to take the keyboard") |
| 74 | } |
| 75 | } |
| 76 | #endif |
| 77 | |
| 78 | #if os(macOS) |
| 79 | /// A field with the keyboard, as the panel's composer has it on opening. |
| 80 | private struct EscapeProbe: View { |
| 81 | @State private var text = "" |
| 82 | @FocusState private var focused: Bool |
| 83 | var body: some View { |
| 84 | TextField(text: $text) { Text(verbatim: "Ask") }.focused($focused).onAppear { focused = true } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /// Astra's Task 16 finding 6: however many jobs wait, the panel's top |
| 89 | /// takes at most its bound, and the composer still has room under it. |
| 90 | @MainActor final class MenuPanelTests: XCTestCase { |
| 91 | func testWaitingJobsAreBoundedAndScroll() { |
| 92 | let m = AskModel(client: BaseClient(transport: GatedTransport()) {}, schedule: { _, _ in }) |
| 93 | for n in 1...6 { |
| 94 | m.apply(.card(AskCard(id: "w\(n)", revision: 1, kind: "job", state: "asking", question: "Job \(n)", result: "One question for you"))) |
| 95 | m.apply(.card(AskCard(id: "c\(n)", revision: 1, kind: "confirm", state: "open", question: "Go ahead?", |
| 96 | options: [.init(value: "y", label: "Go ahead")], job: "w\(n)"))) |
| 97 | } |
| 98 | let host = NSHostingView(rootView: WaitingJobs(ask: m, timeZone: nil).frame(width: 390)) |
| 99 | host.frame = NSRect(x: 0, y: 0, width: 390, height: 2000) |
| 100 | host.layoutSubtreeIfNeeded() |
| 101 | XCTAssertLessThanOrEqual(host.fittingSize.height, WaitingJobs.maxHeight + 0.5) |
| 102 | XCTAssertLessThan(WaitingJobs.maxHeight, StatusPanelController.size.height / 2, "the conversation and composer keep most of the panel") |
| 103 | } |
| 104 | } |
| 105 | #endif |
| 106 | |
| 107 | #if os(macOS) |
| 108 | /// Astra's Task 16 finding 7: Send a new code comes back on time by |
| 109 | /// itself, with no typing, focus or other redraw. |
| 110 | @MainActor final class ResendTests: XCTestCase { |
| 111 | func testResendReenablesOnTimeByWaitingOnly() async throws { |
| 112 | _ = NSApplication.shared |
| 113 | NSApp.setActivationPolicy(.accessory) |
| 114 | var states: [Bool] = [] |
| 115 | let button = ResendButton(availableAt: Date().addingTimeInterval(1), busy: false, action: {}, probe: { states.append($0) }) |
| 116 | let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 320, height: 80), styleMask: [.borderless], backing: .buffered, defer: false) |
| 117 | window.contentView = NSHostingView(rootView: button.frame(width: 320)) |
| 118 | window.orderFrontRegardless() |
| 119 | defer { window.orderOut(nil) } |
| 120 | try await Task.sleep(for: .milliseconds(300)) |
| 121 | XCTAssertEqual(states.last, false, "disabled while the code is fresh") |
| 122 | // Only waiting: no typing, focus or other redraw. On time, give or |
| 123 | // take a busy machine's scheduling. |
| 124 | try await waitUntil(.seconds(6)) { states.last == true } |
| 125 | XCTAssertEqual(states.last, true, "enabled by waiting only") |
| 126 | } |
| 127 | } |
| 128 | #endif |
| 129 | |