-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
fix(stage-tamagotchi): add renewable click-through lease for window interactivity #2455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
0xSelenicDove
wants to merge
2
commits into
moeru-ai:main
Choose a base branch
from
0xSelenicDove:0xSelenicDove/fix/window-interactivity-lease
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
226 changes: 226 additions & 0 deletions
226
apps/stage-tamagotchi/src/main/services/electron/window-interactivity.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,226 @@ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| interface MockContext { | ||
| emit: ReturnType<typeof vi.fn> | ||
| invokeHandlers: Map<string, (payload: unknown, options?: unknown) => unknown> | ||
| } | ||
|
|
||
| interface MockEmitter { | ||
| emit: (event: string, ...args: unknown[]) => void | ||
| on: ReturnType<typeof vi.fn> | ||
| } | ||
|
|
||
| function createEmitter(): MockEmitter { | ||
| const handlers = new Map<string, Array<(...args: unknown[]) => void>>() | ||
| return { | ||
| on: vi.fn((event: string, handler: (...args: unknown[]) => void) => { | ||
| const eventHandlers = handlers.get(event) ?? [] | ||
| eventHandlers.push(handler) | ||
| handlers.set(event, eventHandlers) | ||
| }), | ||
| emit(event, ...args) { | ||
| for (const handler of handlers.get(event) ?? []) | ||
| handler(...args) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| function createMockContext(): MockContext { | ||
| return { | ||
| emit: vi.fn(), | ||
| invokeHandlers: new Map(), | ||
| } | ||
| } | ||
|
|
||
| async function setupWindowService(options: { manageWindowInteractivity?: boolean } = {}) { | ||
| const rendererLoop = { | ||
| start: vi.fn(), | ||
| stop: vi.fn(), | ||
| } | ||
|
|
||
| vi.doMock('@moeru/eventa', async (importOriginal) => { | ||
| const actual = await importOriginal<typeof import('@moeru/eventa')>() | ||
| return { | ||
| ...actual, | ||
| defineInvokeHandler: ( | ||
| context: MockContext, | ||
| eventa: { sendEvent: { id: string } }, | ||
| handler: (payload: unknown, options?: unknown) => unknown, | ||
| ) => { | ||
| context.invokeHandlers.set(eventa.sendEvent.id.replace(/-send$/, ''), handler) | ||
| }, | ||
| } | ||
| }) | ||
|
|
||
| vi.doMock('@proj-airi/electron-eventa', () => ({ | ||
| bounds: { id: 'bounds' }, | ||
| startLoopGetBounds: { sendEvent: { id: 'start-loop-send' } }, | ||
| })) | ||
|
|
||
| vi.doMock('@proj-airi/electron-vueuse/main', () => ({ | ||
| createRendererLoop: () => rendererLoop, | ||
| safeClose: vi.fn(), | ||
| })) | ||
|
|
||
| vi.doMock('../../../shared/eventa', () => ({ | ||
| electron: { | ||
| window: { | ||
| getBounds: { sendEvent: { id: 'get-bounds-send' } }, | ||
| resize: { sendEvent: { id: 'resize-send' } }, | ||
| setBackgroundMaterial: { sendEvent: { id: 'set-background-material-send' } }, | ||
| setBounds: { sendEvent: { id: 'set-bounds-send' } }, | ||
| setIgnoreMouseEvents: { sendEvent: { id: 'set-ignore-mouse-events-send' } }, | ||
| setVibrancy: { sendEvent: { id: 'set-vibrancy-send' } }, | ||
| }, | ||
| }, | ||
| electronGetWindowLifecycleState: { sendEvent: { id: 'get-lifecycle-send' } }, | ||
| electronWindowClose: { sendEvent: { id: 'close-send' } }, | ||
| electronWindowLifecycleChanged: { id: 'lifecycle-changed' }, | ||
| electronWindowSetAlwaysOnTop: { sendEvent: { id: 'set-always-on-top-send' } }, | ||
| })) | ||
|
|
||
| vi.doMock('../../libs/bootkit/lifecycle', () => ({ | ||
| onAppBeforeQuit: vi.fn(), | ||
| onAppWindowAllClosed: vi.fn(), | ||
| })) | ||
|
|
||
| vi.doMock('../../windows/shared/window', () => ({ | ||
| resizeWindowByDelta: vi.fn(), | ||
| })) | ||
|
|
||
| vi.doMock('std-env', () => ({ isWindows: process.platform === 'win32' })) | ||
|
|
||
| const windowEvents = createEmitter() | ||
| const webContentsEvents = createEmitter() | ||
| const setIgnoreMouseEvents = vi.fn() | ||
| const window = { | ||
| ...windowEvents, | ||
| getBounds: vi.fn(() => ({ height: 600, width: 800, x: 0, y: 0 })), | ||
| isFocused: vi.fn(() => true), | ||
| isMinimized: vi.fn(() => false), | ||
| isVisible: vi.fn(() => true), | ||
| setAlwaysOnTop: vi.fn(), | ||
| setBackgroundMaterial: vi.fn(), | ||
| setBounds: vi.fn(), | ||
| setIgnoreMouseEvents, | ||
| setVibrancy: vi.fn(), | ||
| webContents: { | ||
| ...webContentsEvents, | ||
| id: 42, | ||
| }, | ||
| } | ||
| const context = createMockContext() | ||
| const { createWindowService } = await import('./window') | ||
| createWindowService({ | ||
| context: context as never, | ||
| window: window as never, | ||
| manageWindowInteractivity: options.manageWindowInteractivity, | ||
| }) | ||
|
|
||
| return { | ||
| context, | ||
| setIgnoreMouseEvents, | ||
| webContents: window.webContents, | ||
| } | ||
| } | ||
|
|
||
| function sameWindowOptions() { | ||
| return { | ||
| raw: { | ||
| ipcMainEvent: { | ||
| sender: { id: 42 }, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| describe('window interactivity recovery', () => { | ||
| beforeEach(() => { | ||
| vi.useFakeTimers() | ||
| vi.resetModules() | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| // https://github.com/moeru-ai/airi/issues/2160 | ||
| it('starts each managed window with mouse input enabled (Issue #2160)', async () => { | ||
| // ROOT CAUSE: | ||
| // | ||
| // The main process does not establish a fail-open input state when it registers a window. | ||
| // A recreated native window can therefore depend on stale renderer state. | ||
| // The fix must make the main process set the initial input state. | ||
| const service = await setupWindowService() | ||
|
|
||
| expect(service.setIgnoreMouseEvents).toHaveBeenCalledWith(false) | ||
| }) | ||
|
|
||
| it('preserves input isolation for a visual-only overlay', async () => { | ||
| const service = await setupWindowService({ manageWindowInteractivity: false }) | ||
|
|
||
| expect(service.setIgnoreMouseEvents).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| // https://github.com/moeru-ai/airi/issues/2160 | ||
| it('restores mouse input when the renderer process exits (Issue #2160)', async () => { | ||
| // ROOT CAUSE: | ||
| // | ||
| // The renderer owns the native click-through state. | ||
| // A renderer exit stops the code that can call `setIgnoreMouseEvents(false)`. | ||
| // The fix must make the main process restore input after a renderer exit. | ||
| const service = await setupWindowService() | ||
| const handler = service.context.invokeHandlers.get('set-ignore-mouse-events') | ||
|
|
||
| expect(handler).toBeDefined() | ||
| handler!([true, { forward: true }], sameWindowOptions()) | ||
| service.webContents.emit('render-process-gone', {}, { reason: 'crashed' }) | ||
|
|
||
| expect(service.setIgnoreMouseEvents).toHaveBeenLastCalledWith(false) | ||
| }) | ||
|
|
||
| // https://github.com/moeru-ai/airi/issues/2160 | ||
| it('restores mouse input when the renderer becomes unresponsive (Issue #2160)', async () => { | ||
| // ROOT CAUSE: | ||
| // | ||
| // An unresponsive renderer cannot send the request that restores native mouse input. | ||
| // The fix must let the main process restore input without renderer cooperation. | ||
| const service = await setupWindowService() | ||
| const handler = service.context.invokeHandlers.get('set-ignore-mouse-events') | ||
|
|
||
| expect(handler).toBeDefined() | ||
| handler!([true, { forward: true }], sameWindowOptions()) | ||
| service.webContents.emit('unresponsive') | ||
|
|
||
| expect(service.setIgnoreMouseEvents).toHaveBeenLastCalledWith(false) | ||
| }) | ||
|
|
||
| // https://github.com/moeru-ai/airi/issues/2160 | ||
| it('restores mouse input before renderer navigation (Issue #2160)', async () => { | ||
| // ROOT CAUSE: | ||
| // | ||
| // Renderer navigation disposes the code that owns the current click-through state. | ||
| // The fix must restore input before the old renderer lifecycle ends. | ||
| const service = await setupWindowService() | ||
| const handler = service.context.invokeHandlers.get('set-ignore-mouse-events') | ||
|
|
||
| expect(handler).toBeDefined() | ||
| handler!([true, { forward: true }], sameWindowOptions()) | ||
| service.webContents.emit('did-start-navigation', {}, 'file:///app/index.html', false, true) | ||
|
|
||
| expect(service.setIgnoreMouseEvents).toHaveBeenLastCalledWith(false) | ||
| }) | ||
|
|
||
| // https://github.com/moeru-ai/airi/issues/2160 | ||
| it('restores mouse input when a click-through lease expires (Issue #2160)', async () => { | ||
| // ROOT CAUSE: | ||
| // | ||
| // A click-through request has no lifetime and remains active until another renderer request changes it. | ||
| // The fix must give this transient state a bounded lease that fails open. | ||
| const service = await setupWindowService() | ||
| const handler = service.context.invokeHandlers.get('set-ignore-mouse-events') | ||
|
|
||
| expect(handler).toBeDefined() | ||
| handler!([true, { forward: true }], sameWindowOptions()) | ||
| await vi.advanceTimersByTimeAsync(2001) | ||
|
|
||
| expect(service.setIgnoreMouseEvents).toHaveBeenLastCalledWith(false) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mouseInputLeaseDurationand the renderer'smouseInputLeaseRenewIntervalform one cross-process timing contract, but neither constant documents that renewals must precede expiration. If either value changes independently, click-through can expire between heartbeats and make the window intercept clicks. Document the relationship beside both constants, or define it once in a shared contract.AGENTS.md reference: AGENTS.md:L317-L319
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 618c25a: documented the cross-process timing contract at both constants (mouseInputLeaseDuration in window.ts, mouseInputLeaseRenewInterval in the composable), noting renewal must happen strictly more often than expiration.