From 86306dc9de97e9543400be7ec9ccf34671f58d86 Mon Sep 17 00:00:00 2001 From: lucaronin Date: Sat, 11 Apr 2026 10:59:38 +0200 Subject: [PATCH] test: stabilize keyboard menu smoke bridge --- .codescene-thresholds | 4 +-- src/hooks/useMenuEvents.ts | 27 +++++++++++++++----- src/main.tsx | 14 ++++++---- tests/smoke/keyboard-command-routing.spec.ts | 11 +++++--- 4 files changed, 39 insertions(+), 17 deletions(-) diff --git a/.codescene-thresholds b/.codescene-thresholds index 8e645c78..af7a94c0 100644 --- a/.codescene-thresholds +++ b/.codescene-thresholds @@ -1,2 +1,2 @@ -HOTSPOT_THRESHOLD=9.68 -AVERAGE_THRESHOLD=9.33 +HOTSPOT_THRESHOLD=9.7 +AVERAGE_THRESHOLD=9.36 diff --git a/src/hooks/useMenuEvents.ts b/src/hooks/useMenuEvents.ts index a9aee77f..b94452fd 100644 --- a/src/hooks/useMenuEvents.ts +++ b/src/hooks/useMenuEvents.ts @@ -2,12 +2,19 @@ import { useEffect, useRef } from 'react' import { isTauri } from '../mock-tauri' import { APP_COMMAND_EVENT_NAME, - APP_MENU_EVENT_NAME, dispatchAppCommand, isAppCommandId, type AppCommandHandlers, } from './appCommandDispatcher' +declare global { + interface Window { + __laputaTest?: { + dispatchBrowserMenuCommand?: (id: string) => void + } + } +} + export interface MenuEventHandlers extends AppCommandHandlers { activeTabPath: string | null modifiedCount?: number @@ -90,12 +97,20 @@ export function useMenuEvents(handlers: MenuEventHandlers) { }, []) useEffect(() => { - const handleMenuCommandEvent = createWindowCommandListener((detail) => { - dispatchMenuEvent(detail, ref.current) - }) + const bridge = (id: string) => { + dispatchMenuEvent(id, ref.current) + } - window.addEventListener(APP_MENU_EVENT_NAME, handleMenuCommandEvent) - return () => window.removeEventListener(APP_MENU_EVENT_NAME, handleMenuCommandEvent) + window.__laputaTest = { + ...window.__laputaTest, + dispatchBrowserMenuCommand: bridge, + } + + return () => { + if (window.__laputaTest?.dispatchBrowserMenuCommand === bridge) { + delete window.__laputaTest.dispatchBrowserMenuCommand + } + } }, []) // Sync menu item enabled state when active note or git state changes diff --git a/src/main.tsx b/src/main.tsx index 703e2290..63c95d4e 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -5,7 +5,6 @@ import './index.css' import App from './App.tsx' import { APP_COMMAND_EVENT_NAME, - APP_MENU_EVENT_NAME, isAppCommandId, isNativeMenuCommandId, } from './hooks/appCommandDispatcher' @@ -14,6 +13,7 @@ declare global { interface Window { __laputaTest?: { dispatchAppCommand: (id: string) => void + dispatchBrowserMenuCommand?: (id: string) => void triggerMenuCommand?: (id: string) => Promise } } @@ -40,11 +40,15 @@ window.__laputaTest = { } if ('__TAURI__' in window || '__TAURI_INTERNALS__' in window) { - const { invoke } = await import('@tauri-apps/api/core') - return invoke('trigger_menu_command', { id }) - } + const { invoke } = await import('@tauri-apps/api/core') + return invoke('trigger_menu_command', { id }) + } - window.dispatchEvent(new CustomEvent(APP_MENU_EVENT_NAME, { detail: id })) + if (!window.__laputaTest?.dispatchBrowserMenuCommand) { + throw new Error('Laputa test bridge is missing dispatchBrowserMenuCommand') + } + + window.__laputaTest.dispatchBrowserMenuCommand(id) return undefined }, } diff --git a/tests/smoke/keyboard-command-routing.spec.ts b/tests/smoke/keyboard-command-routing.spec.ts index cda6857e..e097210a 100644 --- a/tests/smoke/keyboard-command-routing.spec.ts +++ b/tests/smoke/keyboard-command-routing.spec.ts @@ -1,11 +1,11 @@ -import { test, expect, type Page } from '@playwright/test' +import { test, expect } from '@playwright/test' import { triggerMenuCommand } from './testBridge' import { createFixtureVaultCopy, openFixtureVault, removeFixtureVaultCopy } from '../helpers/fixtureVault' let tempVaultDir: string -function untitledRow(page: Page, typeLabel: string) { - return page.getByText(new RegExp(`^Untitled ${typeLabel}(?: \\d+)?$`, 'i')).first() +function untitledNoteListMatcher(typeLabel: string) { + return new RegExp(`Untitled ${typeLabel}(?: \\d+)?`, 'i') } test.describe('keyboard command routing', () => { @@ -24,7 +24,10 @@ test.describe('keyboard command routing', () => { await openFixtureVault(page, tempVaultDir) await triggerMenuCommand(page, 'file-new-note') - await expect(untitledRow(page, 'note')).toBeVisible({ timeout: 5_000 }) + await expect(page.getByTestId('breadcrumb-filename-trigger')).toContainText(/untitled-note-\d+/i, { timeout: 5_000 }) + await expect( + page.locator('[data-testid="note-list-container"]').getByText(untitledNoteListMatcher('note')).first(), + ).toBeVisible({ timeout: 5_000 }) expect(errors).toEqual([]) })