test: mock win32 in overlay runtime init test

This commit is contained in:
2026-04-10 03:12:26 -07:00
parent bd5275fbf8
commit f79e6bde3b

View File

@@ -2,6 +2,23 @@ import assert from 'node:assert/strict';
import test from 'node:test'; import test from 'node:test';
import { initializeOverlayAnkiIntegration, initializeOverlayRuntime } from './overlay-runtime-init'; import { initializeOverlayAnkiIntegration, initializeOverlayRuntime } from './overlay-runtime-init';
function withPlatform(platform: NodeJS.Platform, run: () => void): void {
const originalPlatform = process.platform;
Object.defineProperty(process, 'platform', {
value: platform,
configurable: true,
});
try {
run();
} finally {
Object.defineProperty(process, 'platform', {
value: originalPlatform,
configurable: true,
});
}
}
test('initializeOverlayRuntime skips Anki integration when ankiConnect.enabled is false', () => { test('initializeOverlayRuntime skips Anki integration when ankiConnect.enabled is false', () => {
let createdIntegrations = 0; let createdIntegrations = 0;
let startedIntegrations = 0; let startedIntegrations = 0;
@@ -548,59 +565,61 @@ test('initializeOverlayRuntime hides overlay windows when tracker loses the targ
}); });
test('initializeOverlayRuntime preserves visible overlay on Windows tracker loss when target is not minimized', () => { test('initializeOverlayRuntime preserves visible overlay on Windows tracker loss when target is not minimized', () => {
const calls: string[] = []; withPlatform('win32', () => {
const tracker = { const calls: string[] = [];
onGeometryChange: null as ((...args: unknown[]) => void) | null, const tracker = {
onWindowFound: null as ((...args: unknown[]) => void) | null, onGeometryChange: null as ((...args: unknown[]) => void) | null,
onWindowLost: null as (() => void) | null, onWindowFound: null as ((...args: unknown[]) => void) | null,
onWindowFocusChange: null as ((focused: boolean) => void) | null, onWindowLost: null as (() => void) | null,
isTargetWindowMinimized: () => false, onWindowFocusChange: null as ((focused: boolean) => void) | null,
start: () => {}, isTargetWindowMinimized: () => false,
}; start: () => {},
const overlayWindows = [ };
{ const overlayWindows = [
hide: () => calls.push('hide-visible'), {
}, hide: () => calls.push('hide-visible'),
]; },
];
initializeOverlayRuntime({ initializeOverlayRuntime({
backendOverride: null, backendOverride: null,
createMainWindow: () => {}, createMainWindow: () => {},
registerGlobalShortcuts: () => {}, registerGlobalShortcuts: () => {},
updateVisibleOverlayBounds: () => {}, updateVisibleOverlayBounds: () => {},
isVisibleOverlayVisible: () => true, isVisibleOverlayVisible: () => true,
updateVisibleOverlayVisibility: () => { updateVisibleOverlayVisibility: () => {
calls.push('update-visible'); calls.push('update-visible');
}, },
refreshCurrentSubtitle: () => {}, refreshCurrentSubtitle: () => {},
getOverlayWindows: () => overlayWindows as never, getOverlayWindows: () => overlayWindows as never,
syncOverlayShortcuts: () => { syncOverlayShortcuts: () => {
calls.push('sync-shortcuts'); calls.push('sync-shortcuts');
}, },
setWindowTracker: () => {}, setWindowTracker: () => {},
getMpvSocketPath: () => '/tmp/mpv.sock', getMpvSocketPath: () => '/tmp/mpv.sock',
createWindowTracker: () => tracker as never, createWindowTracker: () => tracker as never,
getResolvedConfig: () => ({ getResolvedConfig: () => ({
ankiConnect: { enabled: false } as never, ankiConnect: { enabled: false } as never,
}), }),
getSubtitleTimingTracker: () => null, getSubtitleTimingTracker: () => null,
getMpvClient: () => null, getMpvClient: () => null,
getRuntimeOptionsManager: () => null, getRuntimeOptionsManager: () => null,
setAnkiIntegration: () => {}, setAnkiIntegration: () => {},
showDesktopNotification: () => {}, showDesktopNotification: () => {},
createFieldGroupingCallback: () => async () => ({ createFieldGroupingCallback: () => async () => ({
keepNoteId: 1, keepNoteId: 1,
deleteNoteId: 2, deleteNoteId: 2,
deleteDuplicate: false, deleteDuplicate: false,
cancelled: false, cancelled: false,
}), }),
getKnownWordCacheStatePath: () => '/tmp/known-words-cache.json', getKnownWordCacheStatePath: () => '/tmp/known-words-cache.json',
});
calls.length = 0;
tracker.onWindowLost?.();
assert.deepEqual(calls, ['sync-shortcuts']);
}); });
calls.length = 0;
tracker.onWindowLost?.();
assert.deepEqual(calls, ['sync-shortcuts']);
}); });
test('initializeOverlayRuntime restores overlay bounds and visibility when tracker finds the target window again', () => { test('initializeOverlayRuntime restores overlay bounds and visibility when tracker finds the target window again', () => {