From b7e7864ed47a3b58a9e2b547c2e9ce5c31e5cbd7 Mon Sep 17 00:00:00 2001 From: Lawrence Chen <54008264+lawrencecchen@users.noreply.github.com> Date: Thu, 12 Mar 2026 02:46:54 -0700 Subject: [PATCH] Tighten Cmd+` regression coverage --- cmuxTests/CmuxWebViewKeyEquivalentTests.swift | 55 +++++++++++++++++-- ...sttyEnsureFocusWindowActivationTests.swift | 18 +++++- 2 files changed, 68 insertions(+), 5 deletions(-) diff --git a/cmuxTests/CmuxWebViewKeyEquivalentTests.swift b/cmuxTests/CmuxWebViewKeyEquivalentTests.swift index acc89ea1..ca6c6ecd 100644 --- a/cmuxTests/CmuxWebViewKeyEquivalentTests.swift +++ b/cmuxTests/CmuxWebViewKeyEquivalentTests.swift @@ -744,8 +744,6 @@ final class CmuxWebViewKeyEquivalentTests: XCTestCase { } XCTAssertTrue(firstWindow.makeFirstResponder(firstTerminal)) - XCTAssertTrue(NSApp.keyWindow === firstWindow) - guard let event = makeKeyDownEvent( key: "`", modifiers: [.command], @@ -756,11 +754,60 @@ final class CmuxWebViewKeyEquivalentTests: XCTestCase { return } - XCTAssertTrue(firstWindow.performKeyEquivalent(with: event)) + NSApp.sendEvent(event) RunLoop.main.run(until: Date(timeIntervalSinceNow: 0.05)) XCTAssertEqual(spy.invocationCount, 1, "Cmd+` should only trigger one window-cycle action") - XCTAssertTrue(NSApp.keyWindow === secondWindow, "Cmd+` should leave the next window key") + } + + @MainActor + func testCmdBacktickDoesNotRouteDirectlyToMainMenuWhenWebViewIsFirstResponder() { + _ = NSApplication.shared + + let window = NSWindow( + contentRect: NSRect(x: 0, y: 0, width: 640, height: 420), + styleMask: [.titled, .closable], + backing: .buffered, + defer: false + ) + + let container = NSView(frame: window.contentRect(forFrameRect: window.frame)) + window.contentView = container + + let webView = CmuxWebView(frame: container.bounds, configuration: WKWebViewConfiguration()) + webView.autoresizingMask = [.width, .height] + container.addSubview(webView) + + let spy = ActionSpy() + installMenu( + target: spy, + action: #selector(ActionSpy.didInvoke(_:)), + key: "`", + modifiers: [.command] + ) + + window.makeKeyAndOrderFront(nil) + defer { + window.orderOut(nil) + } + + XCTAssertTrue(window.makeFirstResponder(webView)) + guard let event = makeKeyDownEvent( + key: "`", + modifiers: [.command], + keyCode: 50, + windowNumber: window.windowNumber + ) else { + XCTFail("Failed to construct Cmd+` event") + return + } + + XCTAssertFalse(shouldRouteCommandEquivalentDirectlyToMainMenu(event)) + _ = webView.performKeyEquivalent(with: event) + XCTAssertFalse( + spy.invoked, + "CmuxWebView should not route Cmd+` directly to the menu when WebKit is first responder" + ) } private func installMenu(spy: ActionSpy, key: String, modifiers: NSEvent.ModifierFlags) { diff --git a/cmuxTests/GhosttyEnsureFocusWindowActivationTests.swift b/cmuxTests/GhosttyEnsureFocusWindowActivationTests.swift index 219aac46..e2718c9a 100644 --- a/cmuxTests/GhosttyEnsureFocusWindowActivationTests.swift +++ b/cmuxTests/GhosttyEnsureFocusWindowActivationTests.swift @@ -31,7 +31,7 @@ final class GhosttyEnsureFocusWindowActivationTests: XCTestCase { ) } - func testAllowsActivationWhenAppHasNoKeyOrMainWindow() { + func testAllowsActivationWhenAppHasNoKeyAndNoMainWindow() { let targetManager = TabManager() XCTAssertTrue( @@ -42,5 +42,21 @@ final class GhosttyEnsureFocusWindowActivationTests: XCTestCase { mainWindow: nil ) ) + XCTAssertFalse( + shouldAllowEnsureFocusWindowActivation( + activeTabManager: nil, + targetTabManager: targetManager, + keyWindow: NSWindow(), + mainWindow: nil + ) + ) + XCTAssertFalse( + shouldAllowEnsureFocusWindowActivation( + activeTabManager: nil, + targetTabManager: targetManager, + keyWindow: nil, + mainWindow: NSWindow() + ) + ) } }