|
| 1 | +/** |
| 2 | + * Unit tests for lib/commands/app.ts: back, forward, getTitle, setWindowRect |
| 3 | + */ |
| 4 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 5 | +import { back, forward, title, setWindowRect } from '../../../lib/commands/app'; |
| 6 | +import { createMockDriver } from '../../fixtures/driver'; |
| 7 | +import { Key } from '../../../lib/enums'; |
| 8 | + |
| 9 | +vi.mock('../../../lib/winapi/user32', () => ({ |
| 10 | + getWindowAllHandlesForProcessIds: vi.fn().mockReturnValue([]), |
| 11 | + trySetForegroundWindow: vi.fn().mockReturnValue(true), |
| 12 | + keyDown: vi.fn(), |
| 13 | + keyUp: vi.fn(), |
| 14 | +})); |
| 15 | + |
| 16 | +const ELEMENT_ID = '1.2.3.4.5'; |
| 17 | + |
| 18 | +describe('back', () => { |
| 19 | + beforeEach(() => vi.clearAllMocks()); |
| 20 | + |
| 21 | + it('sends Alt+Left when a window is active', async () => { |
| 22 | + const { keyDown, keyUp } = await import('../../../lib/winapi/user32'); |
| 23 | + const driver = createMockDriver() as any; |
| 24 | + driver.sendPowerShellCommand.mockResolvedValue(ELEMENT_ID); |
| 25 | + |
| 26 | + await back.call(driver); |
| 27 | + |
| 28 | + expect(keyDown).toHaveBeenNthCalledWith(1, Key.ALT); |
| 29 | + expect(keyDown).toHaveBeenNthCalledWith(2, Key.LEFT); |
| 30 | + expect(keyUp).toHaveBeenNthCalledWith(1, Key.LEFT); |
| 31 | + expect(keyUp).toHaveBeenNthCalledWith(2, Key.ALT); |
| 32 | + }); |
| 33 | + |
| 34 | + it('throws NoSuchWindowError when no active window', async () => { |
| 35 | + const driver = createMockDriver() as any; |
| 36 | + driver.sendPowerShellCommand.mockResolvedValue(''); |
| 37 | + |
| 38 | + await expect(back.call(driver)).rejects.toThrow('No active window found'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('performs exactly one PS call (window check) before sending keys', async () => { |
| 42 | + const driver = createMockDriver() as any; |
| 43 | + driver.sendPowerShellCommand.mockResolvedValue(ELEMENT_ID); |
| 44 | + |
| 45 | + await back.call(driver); |
| 46 | + |
| 47 | + expect(driver.sendPowerShellCommand).toHaveBeenCalledTimes(1); |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
| 51 | +describe('forward', () => { |
| 52 | + beforeEach(() => vi.clearAllMocks()); |
| 53 | + |
| 54 | + it('sends Alt+Right when a window is active', async () => { |
| 55 | + const { keyDown, keyUp } = await import('../../../lib/winapi/user32'); |
| 56 | + const driver = createMockDriver() as any; |
| 57 | + driver.sendPowerShellCommand.mockResolvedValue(ELEMENT_ID); |
| 58 | + |
| 59 | + await forward.call(driver); |
| 60 | + |
| 61 | + expect(keyDown).toHaveBeenNthCalledWith(1, Key.ALT); |
| 62 | + expect(keyDown).toHaveBeenNthCalledWith(2, Key.RIGHT); |
| 63 | + expect(keyUp).toHaveBeenNthCalledWith(1, Key.RIGHT); |
| 64 | + expect(keyUp).toHaveBeenNthCalledWith(2, Key.ALT); |
| 65 | + }); |
| 66 | + |
| 67 | + it('throws NoSuchWindowError when no active window', async () => { |
| 68 | + const driver = createMockDriver() as any; |
| 69 | + driver.sendPowerShellCommand.mockResolvedValue(''); |
| 70 | + |
| 71 | + await expect(forward.call(driver)).rejects.toThrow('No active window found'); |
| 72 | + }); |
| 73 | +}); |
| 74 | + |
| 75 | +describe('title (getTitle)', () => { |
| 76 | + beforeEach(() => vi.clearAllMocks()); |
| 77 | + |
| 78 | + it('returns the window title from the Name property', async () => { |
| 79 | + const driver = createMockDriver() as any; |
| 80 | + driver.sendPowerShellCommand |
| 81 | + .mockResolvedValueOnce(ELEMENT_ID) // window check |
| 82 | + .mockResolvedValueOnce('Untitled - Notepad'); // Name property |
| 83 | + |
| 84 | + const result = await title.call(driver); |
| 85 | + |
| 86 | + expect(result).toBe('Untitled - Notepad'); |
| 87 | + expect(driver.sendPowerShellCommand).toHaveBeenCalledTimes(2); |
| 88 | + }); |
| 89 | + |
| 90 | + it('returns an empty string when the window has no title', async () => { |
| 91 | + const driver = createMockDriver() as any; |
| 92 | + driver.sendPowerShellCommand |
| 93 | + .mockResolvedValueOnce(ELEMENT_ID) |
| 94 | + .mockResolvedValueOnce(''); |
| 95 | + |
| 96 | + const result = await title.call(driver); |
| 97 | + |
| 98 | + expect(result).toBe(''); |
| 99 | + }); |
| 100 | + |
| 101 | + it('throws NoSuchWindowError when no active window', async () => { |
| 102 | + const driver = createMockDriver() as any; |
| 103 | + driver.sendPowerShellCommand.mockResolvedValue(''); |
| 104 | + |
| 105 | + await expect(title.call(driver)).rejects.toThrow('No active window found'); |
| 106 | + }); |
| 107 | +}); |
| 108 | + |
| 109 | +describe('setWindowRect', () => { |
| 110 | + beforeEach(() => vi.clearAllMocks()); |
| 111 | + |
| 112 | + const MOCK_RECT = { x: 100, y: 100, width: 800, height: 600 }; |
| 113 | + |
| 114 | + function createDriverWithRect(windowRect = MOCK_RECT) { |
| 115 | + const driver = createMockDriver() as any; |
| 116 | + driver.sendPowerShellCommand.mockResolvedValue(ELEMENT_ID); |
| 117 | + driver.getWindowRect = vi.fn().mockResolvedValue(windowRect); |
| 118 | + return driver; |
| 119 | + } |
| 120 | + |
| 121 | + it('calls Move and Resize when all four values are provided', async () => { |
| 122 | + const driver = createDriverWithRect(); |
| 123 | + |
| 124 | + const result = await setWindowRect.call(driver, 100, 100, 800, 600); |
| 125 | + |
| 126 | + // PS calls: window check + Move + Resize = 3 |
| 127 | + expect(driver.sendPowerShellCommand).toHaveBeenCalledTimes(3); |
| 128 | + expect(driver.getWindowRect).toHaveBeenCalledTimes(1); |
| 129 | + expect(result).toEqual(MOCK_RECT); |
| 130 | + }); |
| 131 | + |
| 132 | + it('calls only Move when width and height are null', async () => { |
| 133 | + const driver = createDriverWithRect(); |
| 134 | + |
| 135 | + await setWindowRect.call(driver, 50, 75, null, null); |
| 136 | + |
| 137 | + // PS calls: window check + Move = 2 |
| 138 | + expect(driver.sendPowerShellCommand).toHaveBeenCalledTimes(2); |
| 139 | + }); |
| 140 | + |
| 141 | + it('calls only Resize when x and y are null', async () => { |
| 142 | + const driver = createDriverWithRect(); |
| 143 | + |
| 144 | + await setWindowRect.call(driver, null, null, 1024, 768); |
| 145 | + |
| 146 | + // PS calls: window check + Resize = 2 |
| 147 | + expect(driver.sendPowerShellCommand).toHaveBeenCalledTimes(2); |
| 148 | + }); |
| 149 | + |
| 150 | + it('skips Move and Resize when all arguments are null', async () => { |
| 151 | + const driver = createDriverWithRect(); |
| 152 | + |
| 153 | + await setWindowRect.call(driver, null, null, null, null); |
| 154 | + |
| 155 | + // PS calls: window check only = 1 |
| 156 | + expect(driver.sendPowerShellCommand).toHaveBeenCalledTimes(1); |
| 157 | + }); |
| 158 | + |
| 159 | + it('returns the new window rect from getWindowRect', async () => { |
| 160 | + const expectedRect = { x: 200, y: 300, width: 1024, height: 768 }; |
| 161 | + const driver = createDriverWithRect(expectedRect); |
| 162 | + |
| 163 | + const result = await setWindowRect.call(driver, 200, 300, 1024, 768); |
| 164 | + |
| 165 | + expect(result).toEqual(expectedRect); |
| 166 | + }); |
| 167 | + |
| 168 | + it('throws InvalidArgumentError for negative width', async () => { |
| 169 | + const driver = createDriverWithRect(); |
| 170 | + |
| 171 | + await expect(setWindowRect.call(driver, 0, 0, -1, 600)).rejects.toThrow('width must be a non-negative integer'); |
| 172 | + }); |
| 173 | + |
| 174 | + it('throws InvalidArgumentError for negative height', async () => { |
| 175 | + const driver = createDriverWithRect(); |
| 176 | + |
| 177 | + await expect(setWindowRect.call(driver, 0, 0, 800, -1)).rejects.toThrow('height must be a non-negative integer'); |
| 178 | + }); |
| 179 | + |
| 180 | + it('throws NoSuchWindowError when no active window', async () => { |
| 181 | + const driver = createMockDriver() as any; |
| 182 | + driver.sendPowerShellCommand.mockResolvedValue(''); |
| 183 | + driver.getWindowRect = vi.fn(); |
| 184 | + |
| 185 | + await expect(setWindowRect.call(driver, 0, 0, 800, 600)).rejects.toThrow('No active window found'); |
| 186 | + }); |
| 187 | + |
| 188 | + it('the Move PS command contains TransformPattern and Move', async () => { |
| 189 | + const driver = createDriverWithRect(); |
| 190 | + |
| 191 | + await setWindowRect.call(driver, 10, 20, null, null); |
| 192 | + |
| 193 | + const moveCmdCall = driver.sendPowerShellCommand.mock.calls[1][0] as string; |
| 194 | + const decoded = moveCmdCall.replace(/FromBase64String\('([^']+)'\)/g, (_, b64) => |
| 195 | + Buffer.from(b64, 'base64').toString('utf8') |
| 196 | + ); |
| 197 | + expect(decoded).toContain('TransformPattern'); |
| 198 | + expect(decoded).toContain('Move'); |
| 199 | + }); |
| 200 | + |
| 201 | + it('the Resize PS command contains TransformPattern and Resize', async () => { |
| 202 | + const driver = createDriverWithRect(); |
| 203 | + |
| 204 | + await setWindowRect.call(driver, null, null, 800, 600); |
| 205 | + |
| 206 | + const resizeCmdCall = driver.sendPowerShellCommand.mock.calls[1][0] as string; |
| 207 | + const decoded = resizeCmdCall.replace(/FromBase64String\('([^']+)'\)/g, (_, b64) => |
| 208 | + Buffer.from(b64, 'base64').toString('utf8') |
| 209 | + ); |
| 210 | + expect(decoded).toContain('TransformPattern'); |
| 211 | + expect(decoded).toContain('Resize'); |
| 212 | + }); |
| 213 | +}); |
0 commit comments