|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import type { BrowserWindow } from 'electron'; |
| 3 | +import { |
| 4 | + markCorrectedBounds, |
| 5 | + trackSettingsWindowMovement, |
| 6 | +} from './trackWindowMovement'; |
| 7 | +import { writeData } from './storage/storage'; |
| 8 | + |
| 9 | +vi.mock('./storage/storage', () => ({ writeData: vi.fn() })); |
| 10 | + |
| 11 | +const DEBOUNCE_MS = 200; |
| 12 | + |
| 13 | +/** |
| 14 | + * Minimal stand-in for the parts of BrowserWindow this module uses: it records |
| 15 | + * the handlers registered for 'moved' and 'resized' so a test can raise them, |
| 16 | + * and returns whatever bounds the test last set. |
| 17 | + */ |
| 18 | +function fakeWindow(initial: Electron.Rectangle) { |
| 19 | + const handlers: Record<string, (() => void)[]> = {}; |
| 20 | + let bounds = initial; |
| 21 | + |
| 22 | + const win = { |
| 23 | + on: (event: string, handler: () => void) => { |
| 24 | + (handlers[event] ??= []).push(handler); |
| 25 | + return win; |
| 26 | + }, |
| 27 | + getBounds: () => bounds, |
| 28 | + }; |
| 29 | + |
| 30 | + return { |
| 31 | + win: win as unknown as BrowserWindow, |
| 32 | + setBounds: (next: Electron.Rectangle) => { |
| 33 | + bounds = next; |
| 34 | + }, |
| 35 | + emit: (event: string) => (handlers[event] ?? []).forEach((h) => h()), |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +const ON_SCREEN = { x: 100, y: 100, width: 800, height: 700 }; |
| 40 | +const OFF_SCREEN = { x: -20000, y: 250, width: 800, height: 700 }; |
| 41 | +const RESCUED = { x: 622, y: 200, width: 800, height: 700 }; |
| 42 | + |
| 43 | +describe('trackSettingsWindowMovement', () => { |
| 44 | + beforeEach(() => { |
| 45 | + vi.useFakeTimers(); |
| 46 | + vi.mocked(writeData).mockClear(); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + vi.useRealTimers(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('saves bounds after the user moves the window', () => { |
| 54 | + const { win, setBounds, emit } = fakeWindow(ON_SCREEN); |
| 55 | + trackSettingsWindowMovement(win); |
| 56 | + |
| 57 | + setBounds({ ...ON_SCREEN, x: 300 }); |
| 58 | + emit('moved'); |
| 59 | + vi.advanceTimersByTime(DEBOUNCE_MS); |
| 60 | + |
| 61 | + expect(writeData).toHaveBeenCalledWith('settingsWindowBounds', { |
| 62 | + ...ON_SCREEN, |
| 63 | + x: 300, |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('saves bounds after the user resizes the window', () => { |
| 68 | + const { win, setBounds, emit } = fakeWindow(ON_SCREEN); |
| 69 | + trackSettingsWindowMovement(win); |
| 70 | + |
| 71 | + setBounds({ ...ON_SCREEN, width: 900 }); |
| 72 | + emit('resized'); |
| 73 | + vi.advanceTimersByTime(DEBOUNCE_MS); |
| 74 | + |
| 75 | + expect(writeData).toHaveBeenCalledOnce(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('debounces a burst of events into one save', () => { |
| 79 | + const { win, setBounds, emit } = fakeWindow(ON_SCREEN); |
| 80 | + trackSettingsWindowMovement(win); |
| 81 | + |
| 82 | + setBounds({ ...ON_SCREEN, x: 200 }); |
| 83 | + emit('moved'); |
| 84 | + emit('moved'); |
| 85 | + emit('moved'); |
| 86 | + vi.advanceTimersByTime(DEBOUNCE_MS); |
| 87 | + |
| 88 | + expect(writeData).toHaveBeenCalledOnce(); |
| 89 | + }); |
| 90 | + |
| 91 | + it('does not persist a position the app corrected to', () => { |
| 92 | + // The window was saved off-screen, so startup rescues it. That rescue must |
| 93 | + // not overwrite the saved position, or reconnecting the monitor would no |
| 94 | + // longer bring the window back to where the user had put it. |
| 95 | + const { win, setBounds, emit } = fakeWindow(OFF_SCREEN); |
| 96 | + trackSettingsWindowMovement(win); |
| 97 | + |
| 98 | + markCorrectedBounds(win, RESCUED); |
| 99 | + setBounds(RESCUED); |
| 100 | + // Raised explicitly. Electron does not currently emit these for a |
| 101 | + // programmatic setBounds, so the saved position survives today by accident; |
| 102 | + // this asserts it survives even if that changes. |
| 103 | + emit('moved'); |
| 104 | + emit('resized'); |
| 105 | + vi.advanceTimersByTime(DEBOUNCE_MS); |
| 106 | + |
| 107 | + expect(writeData).not.toHaveBeenCalled(); |
| 108 | + }); |
| 109 | + |
| 110 | + it('resumes saving once the user moves the window themselves', () => { |
| 111 | + const { win, setBounds, emit } = fakeWindow(OFF_SCREEN); |
| 112 | + trackSettingsWindowMovement(win); |
| 113 | + |
| 114 | + markCorrectedBounds(win, RESCUED); |
| 115 | + setBounds(RESCUED); |
| 116 | + emit('moved'); |
| 117 | + vi.advanceTimersByTime(DEBOUNCE_MS); |
| 118 | + expect(writeData).not.toHaveBeenCalled(); |
| 119 | + |
| 120 | + // The user drags it somewhere of their own choosing. |
| 121 | + const chosen = { ...RESCUED, x: 900, y: 400 }; |
| 122 | + setBounds(chosen); |
| 123 | + emit('moved'); |
| 124 | + vi.advanceTimersByTime(DEBOUNCE_MS); |
| 125 | + |
| 126 | + expect(writeData).toHaveBeenCalledWith('settingsWindowBounds', chosen); |
| 127 | + }); |
| 128 | + |
| 129 | + it('only suppresses the window that was corrected', () => { |
| 130 | + const a = fakeWindow(ON_SCREEN); |
| 131 | + const b = fakeWindow(ON_SCREEN); |
| 132 | + trackSettingsWindowMovement(a.win); |
| 133 | + trackSettingsWindowMovement(b.win); |
| 134 | + |
| 135 | + markCorrectedBounds(a.win, RESCUED); |
| 136 | + a.setBounds(RESCUED); |
| 137 | + b.setBounds(RESCUED); |
| 138 | + |
| 139 | + a.emit('moved'); |
| 140 | + b.emit('moved'); |
| 141 | + vi.advanceTimersByTime(DEBOUNCE_MS); |
| 142 | + |
| 143 | + expect(writeData).toHaveBeenCalledOnce(); |
| 144 | + expect(writeData).toHaveBeenCalledWith('settingsWindowBounds', RESCUED); |
| 145 | + }); |
| 146 | +}); |
0 commit comments