Files
psysonic/src/app/windowKind.test.ts
T
Frank Stellmacher 0cd8998dc9 refactor(app): Phase B.1 — extract pre-React bootstrap into src/app/ (#555)
main.tsx shrinks from 56 -> 17 LOC. New module surface:

  - src/app/windowKind.ts: cached getWindowKind() detector,
    replaces the global __PSY_WINDOW_LABEL__ string everywhere
  - src/app/bootstrap.ts: pushUserAgentToBackend +
    pushLoggingModeToBackend + runPreReactBootstrap orchestrator

App.tsx + playerStore.ts now read getWindowKind() instead of poking
window.__PSY_WINDOW_LABEL__ directly. Behaviour-preserving.
2026-05-12 01:39:39 +02:00

67 lines
2.4 KiB
TypeScript

/**
* Tests for the cached Tauri window-kind detector.
*
* The cache is module-scoped, so each test must reset it via the
* `_resetWindowKindCacheForTest()` escape hatch — otherwise the first call
* locks the value for the rest of the file.
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
vi.mock('@tauri-apps/api/window', () => ({
getCurrentWindow: vi.fn(),
}));
import { getCurrentWindow } from '@tauri-apps/api/window';
import { _resetWindowKindCacheForTest, getWindowKind } from './windowKind';
beforeEach(() => {
_resetWindowKindCacheForTest();
vi.clearAllMocks();
});
afterEach(() => {
_resetWindowKindCacheForTest();
});
describe('getWindowKind', () => {
it('returns "main" when the current window label is "main"', () => {
vi.mocked(getCurrentWindow).mockReturnValue({ label: 'main' } as ReturnType<typeof getCurrentWindow>);
expect(getWindowKind()).toBe('main');
});
it('returns "mini" when the current window label is "mini"', () => {
vi.mocked(getCurrentWindow).mockReturnValue({ label: 'mini' } as ReturnType<typeof getCurrentWindow>);
expect(getWindowKind()).toBe('mini');
});
it('falls back to "main" for any other label', () => {
vi.mocked(getCurrentWindow).mockReturnValue({ label: 'something-else' } as ReturnType<typeof getCurrentWindow>);
expect(getWindowKind()).toBe('main');
});
it('falls back to "main" when getCurrentWindow throws (non-Tauri runtime)', () => {
vi.mocked(getCurrentWindow).mockImplementation(() => {
throw new Error('not in tauri');
});
expect(getWindowKind()).toBe('main');
});
it('caches the first result and does not call getCurrentWindow again', () => {
vi.mocked(getCurrentWindow).mockReturnValue({ label: 'mini' } as ReturnType<typeof getCurrentWindow>);
expect(getWindowKind()).toBe('mini');
expect(getWindowKind()).toBe('mini');
expect(getWindowKind()).toBe('mini');
expect(getCurrentWindow).toHaveBeenCalledTimes(1);
});
it('re-reads after the cache is reset', () => {
vi.mocked(getCurrentWindow).mockReturnValue({ label: 'main' } as ReturnType<typeof getCurrentWindow>);
expect(getWindowKind()).toBe('main');
_resetWindowKindCacheForTest();
vi.mocked(getCurrentWindow).mockReturnValue({ label: 'mini' } as ReturnType<typeof getCurrentWindow>);
expect(getWindowKind()).toBe('mini');
expect(getCurrentWindow).toHaveBeenCalledTimes(2);
});
});