test(main): add overlay modal runtime fallback open-state regression coverage

This commit is contained in:
2026-02-26 12:54:50 -08:00
parent a14c9da139
commit 643f8eb958

View File

@@ -12,6 +12,7 @@ type MockWindow = {
hideCount: number; hideCount: number;
sent: unknown[][]; sent: unknown[][];
loading: boolean; loading: boolean;
url: string;
loadCallbacks: Array<() => void>; loadCallbacks: Array<() => void>;
}; };
@@ -19,7 +20,8 @@ function createMockWindow(): MockWindow & {
isDestroyed: () => boolean; isDestroyed: () => boolean;
isVisible: () => boolean; isVisible: () => boolean;
isFocused: () => boolean; isFocused: () => boolean;
setIgnoreMouseEvents: (ignore: boolean) => void; getURL: () => string;
setIgnoreMouseEvents: (ignore: boolean, options?: { forward?: boolean }) => void;
getShowCount: () => number; getShowCount: () => number;
getHideCount: () => number; getHideCount: () => number;
show: () => void; show: () => void;
@@ -28,6 +30,7 @@ function createMockWindow(): MockWindow & {
webContents: { webContents: {
focused: boolean; focused: boolean;
isLoading: () => boolean; isLoading: () => boolean;
getURL: () => string;
send: (channel: string, payload?: unknown) => void; send: (channel: string, payload?: unknown) => void;
isFocused: () => boolean; isFocused: () => boolean;
once: (event: 'did-finish-load', cb: () => void) => void; once: (event: 'did-finish-load', cb: () => void) => void;
@@ -44,6 +47,7 @@ function createMockWindow(): MockWindow & {
hideCount: 0, hideCount: 0,
sent: [], sent: [],
loading: false, loading: false,
url: 'file:///overlay/index.html?layer=modal',
loadCallbacks: [], loadCallbacks: [],
}; };
return { return {
@@ -51,7 +55,8 @@ function createMockWindow(): MockWindow & {
isDestroyed: () => state.destroyed, isDestroyed: () => state.destroyed,
isVisible: () => state.visible, isVisible: () => state.visible,
isFocused: () => state.focused, isFocused: () => state.focused,
setIgnoreMouseEvents: (ignore: boolean) => { getURL: () => state.url,
setIgnoreMouseEvents: (ignore: boolean, _options?: { forward?: boolean }) => {
state.ignoreMouseEvents = ignore; state.ignoreMouseEvents = ignore;
}, },
getShowCount: () => state.showCount, getShowCount: () => state.showCount,
@@ -69,6 +74,7 @@ function createMockWindow(): MockWindow & {
}, },
webContents: { webContents: {
isLoading: () => state.loading, isLoading: () => state.loading,
getURL: () => state.url,
send: (channel, payload) => { send: (channel, payload) => {
if (payload === undefined) { if (payload === undefined) {
state.sent.push([channel]); state.sent.push([channel]);
@@ -93,7 +99,6 @@ test('sendToActiveOverlayWindow targets modal window with full geometry and trac
const calls: string[] = []; const calls: string[] = [];
const runtime = createOverlayModalRuntimeService({ const runtime = createOverlayModalRuntimeService({
getMainWindow: () => null, getMainWindow: () => null,
getInvisibleWindow: () => null,
getModalWindow: () => window as never, getModalWindow: () => window as never,
createModalWindow: () => { createModalWindow: () => {
calls.push('create-modal-window'); calls.push('create-modal-window');
@@ -111,6 +116,8 @@ test('sendToActiveOverlayWindow targets modal window with full geometry and trac
assert.equal(sent, true); assert.equal(sent, true);
assert.equal(runtime.getRestoreVisibleOverlayOnModalClose().has('runtime-options'), true); assert.equal(runtime.getRestoreVisibleOverlayOnModalClose().has('runtime-options'), true);
assert.deepEqual(calls, ['bounds:10,20,300,200']); assert.deepEqual(calls, ['bounds:10,20,300,200']);
assert.equal(window.getShowCount(), 0);
runtime.notifyOverlayModalOpened('runtime-options');
assert.equal(window.getShowCount(), 1); assert.equal(window.getShowCount(), 1);
assert.equal(window.isFocused(), true); assert.equal(window.isFocused(), true);
assert.deepEqual(window.sent, [['runtime-options:open']]); assert.deepEqual(window.sent, [['runtime-options:open']]);
@@ -121,7 +128,6 @@ test('sendToActiveOverlayWindow creates modal window lazily when absent', () =>
let modalWindow: ReturnType<typeof createMockWindow> | null = null; let modalWindow: ReturnType<typeof createMockWindow> | null = null;
const runtime = createOverlayModalRuntimeService({ const runtime = createOverlayModalRuntimeService({
getMainWindow: () => null, getMainWindow: () => null,
getInvisibleWindow: () => null,
getModalWindow: () => modalWindow as never, getModalWindow: () => modalWindow as never,
createModalWindow: () => { createModalWindow: () => {
modalWindow = window; modalWindow = window;
@@ -135,14 +141,47 @@ test('sendToActiveOverlayWindow creates modal window lazily when absent', () =>
runtime.sendToActiveOverlayWindow('jimaku:open', undefined, { restoreOnModalClose: 'jimaku' }), runtime.sendToActiveOverlayWindow('jimaku:open', undefined, { restoreOnModalClose: 'jimaku' }),
true, true,
); );
assert.equal(window.getShowCount(), 0);
runtime.notifyOverlayModalOpened('jimaku');
assert.equal(window.getShowCount(), 1);
assert.deepEqual(window.sent, [['jimaku:open']]); assert.deepEqual(window.sent, [['jimaku:open']]);
}); });
test('sendToActiveOverlayWindow waits for blank modal URL before sending open command', () => {
const window = createMockWindow();
window.url = '';
window.loading = true;
const runtime = createOverlayModalRuntimeService({
getMainWindow: () => null,
getModalWindow: () => window as never,
createModalWindow: () => {
throw new Error('modal window should not be created when already present');
},
getModalGeometry: () => ({ x: 10, y: 20, width: 300, height: 200 }),
setModalWindowBounds: () => {},
});
const sent = runtime.sendToActiveOverlayWindow('runtime-options:open', undefined, {
restoreOnModalClose: 'runtime-options',
});
assert.equal(sent, true);
assert.deepEqual(window.sent, []);
assert.equal(window.loadCallbacks.length, 1);
window.loading = false;
window.url = 'file:///overlay/index.html?layer=modal';
window.loadCallbacks[0]!();
runtime.notifyOverlayModalOpened('runtime-options');
assert.deepEqual(window.sent, [['runtime-options:open']]);
assert.equal(window.getShowCount(), 1);
});
test('handleOverlayModalClosed hides modal window only after all pending modals close', () => { test('handleOverlayModalClosed hides modal window only after all pending modals close', () => {
const window = createMockWindow(); const window = createMockWindow();
const runtime = createOverlayModalRuntimeService({ const runtime = createOverlayModalRuntimeService({
getMainWindow: () => null, getMainWindow: () => null,
getInvisibleWindow: () => null,
getModalWindow: () => window as never, getModalWindow: () => window as never,
createModalWindow: () => window as never, createModalWindow: () => window as never,
getModalGeometry: () => ({ x: 0, y: 0, width: 400, height: 300 }), getModalGeometry: () => ({ x: 0, y: 0, width: 400, height: 300 }),
@@ -169,7 +208,6 @@ test('modal runtime notifies callers when modal input state becomes active/inact
const runtime = createOverlayModalRuntimeService( const runtime = createOverlayModalRuntimeService(
{ {
getMainWindow: () => null, getMainWindow: () => null,
getInvisibleWindow: () => null,
getModalWindow: () => window as never, getModalWindow: () => window as never,
createModalWindow: () => window as never, createModalWindow: () => window as never,
getModalGeometry: () => ({ x: 0, y: 0, width: 400, height: 300 }), getModalGeometry: () => ({ x: 0, y: 0, width: 400, height: 300 }),
@@ -201,7 +239,6 @@ test('handleOverlayModalClosed hides modal window for single kiku modal', () =>
const window = createMockWindow(); const window = createMockWindow();
const runtime = createOverlayModalRuntimeService({ const runtime = createOverlayModalRuntimeService({
getMainWindow: () => null, getMainWindow: () => null,
getInvisibleWindow: () => null,
getModalWindow: () => window as never, getModalWindow: () => window as never,
createModalWindow: () => window as never, createModalWindow: () => window as never,
getModalGeometry: () => ({ x: 0, y: 0, width: 400, height: 300 }), getModalGeometry: () => ({ x: 0, y: 0, width: 400, height: 300 }),
@@ -216,3 +253,36 @@ test('handleOverlayModalClosed hides modal window for single kiku modal', () =>
assert.equal(window.getHideCount(), 1); assert.equal(window.getHideCount(), 1);
assert.equal(runtime.getRestoreVisibleOverlayOnModalClose().size, 0); assert.equal(runtime.getRestoreVisibleOverlayOnModalClose().size, 0);
}); });
test('modal fallback reveal keeps mouse events ignored until modal confirms open', async () => {
const window = createMockWindow();
const runtime = createOverlayModalRuntimeService({
getMainWindow: () => null,
getModalWindow: () => window as never,
createModalWindow: () => {
throw new Error('modal window should not be created when already present');
},
getModalGeometry: () => ({ x: 0, y: 0, width: 400, height: 300 }),
setModalWindowBounds: () => {},
});
window.loading = true;
window.url = '';
const sent = runtime.sendToActiveOverlayWindow('jimaku:open', undefined, {
restoreOnModalClose: 'jimaku',
});
assert.equal(sent, true);
assert.equal(window.ignoreMouseEvents, false);
await new Promise<void>((resolve) => {
setTimeout(resolve, 260);
});
assert.equal(window.getShowCount(), 1);
assert.equal(window.ignoreMouseEvents, true);
runtime.notifyOverlayModalOpened('jimaku');
assert.equal(window.ignoreMouseEvents, false);
});