test(frontend): vitest framework bootstrap + hot-path coverage gate (#536)

* test(frontend): vitest framework bootstrap + hot-path file coverage gate

Adds the harness for component, hook and store tests on top of the existing
util tests in src/utils/. Mirrors the backend rust-tests rollout: jsdom env,
v8 coverage, soft hot-path file gate, dedicated CI workflow.

What's in:
- vitest.config.ts: jsdom environment, v8 coverage, alias @ -> src
- src/test/setup.ts: jest-dom, @testing-library cleanup, vi.mock for
  @tauri-apps/api/{core,event} + plugin-shell, Map-backed Storage polyfill
  for Node 25 + jsdom 26 (both ship a broken native localStorage)
- src/test/mocks/tauri.ts: programmable onInvoke() / emitTauriEvent() helpers,
  auto-reset between tests
- src/test/helpers/factories.ts: makeTrack / makeTracks
- src/test/helpers/renderWithProviders.tsx: render() wrapped with
  MemoryRouter + I18nextProvider
- src/test/README.md: conventions doc (where tests go, how to mock Tauri,
  what to never mock)

Sample tests showing the patterns:
- src/components/CoverLightbox.test.tsx: component, queries by role
- src/store/previewStore.test.ts: store characterization, event handlers
  + stopPreview (startPreview deferred until the cross-store provider
  strategy is decided)

CI:
- .github/workflows/frontend-tests.yml: jobs for vitest, tsc, coverage +
  hot-path gate. coverage job carries continue-on-error: true (soft).
- .github/frontend-hot-path-files.txt: initial list (3 utils at >=70%).
  playerStore + the unfinished half of previewStore are deferred until
  Phase 1 coverage work lands.
- scripts/check-frontend-hot-path-coverage.sh: mirror of the rust gate.

npm scripts:
- test: one-shot run (unchanged)
- test vitest in watch mode
- test:coverage: v8 coverage + html / lcov / json-summary reports

57 / 57 tests pass; tsc --noEmit clean.

* chore(nix): sync npmDepsHash with package-lock.json

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
Frank Stellmacher
2026-05-11 12:25:48 +02:00
committed by GitHub
parent 1cc43dc669
commit 02d533e949
15 changed files with 1893 additions and 4 deletions
+82
View File
@@ -0,0 +1,82 @@
/**
* Tauri test harness — programmable `invoke()` + `listen()` mocks.
*
* Usage in a test:
*
* import { onInvoke, emitTauriEvent } from '@/test/mocks/tauri';
*
* beforeEach(() => {
* onInvoke('audio_play', () => undefined);
* onInvoke('audio_get_state', () => ({ playing: true }));
* });
*
* it('emits progress', () => {
* emitTauriEvent('audio:progress', { id: 't1', currentTime: 42 });
* });
*
* Handlers are auto-cleared between tests (`beforeEach` hook below).
* Unhandled invokes throw — keeps tests honest about which commands they touch.
*/
import { beforeEach, vi } from 'vitest';
import { invoke } from '@tauri-apps/api/core';
import { listen } from '@tauri-apps/api/event';
export type InvokeHandler = (args: unknown) => unknown | Promise<unknown>;
export type EventCallback = (payload: unknown) => void;
const invokeHandlers = new Map<string, InvokeHandler>();
const eventListeners = new Map<string, EventCallback[]>();
// Tauri's typed signatures are strict (InvokeArgs / Event<T>). Tests don't
// need that level of precision — cast the mocks to `any` so the helpers
// accept simple `{ payload }` envelopes and plain object args.
const invokeMock = vi.mocked(invoke) as unknown as ReturnType<typeof vi.fn>;
const listenMock = vi.mocked(listen) as unknown as ReturnType<typeof vi.fn>;
invokeMock.mockImplementation(async (cmd: string, args?: unknown) => {
const handler = invokeHandlers.get(cmd);
if (!handler) {
throw new Error(
`Unhandled invoke('${cmd}'). Register via onInvoke('${cmd}', …) in the test.`,
);
}
return await handler(args);
});
listenMock.mockImplementation(
async (event: string, cb: (e: { payload: unknown }) => void) => {
const wrapped: EventCallback = (payload) =>
cb({ payload } as { payload: unknown });
const arr = eventListeners.get(event) ?? [];
arr.push(wrapped);
eventListeners.set(event, arr);
return () => {
const list = eventListeners.get(event);
if (!list) return;
const i = list.indexOf(wrapped);
if (i >= 0) list.splice(i, 1);
};
},
);
/** Register a handler for `invoke('<cmd>', …)`. Last-write-wins per command. */
export function onInvoke(cmd: string, handler: InvokeHandler): void {
invokeHandlers.set(cmd, handler);
}
/** Synchronously deliver an `<event>` payload to every active listener. */
export function emitTauriEvent(event: string, payload: unknown): void {
for (const cb of eventListeners.get(event) ?? []) cb(payload);
}
/** Clear all handlers + listeners + call counts. Wired to `beforeEach` below. */
export function resetTauriMocks(): void {
invokeHandlers.clear();
eventListeners.clear();
invokeMock.mockClear();
listenMock.mockClear();
}
export { invokeMock, listenMock };
beforeEach(resetTauriMocks);