|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest' |
| 2 | +import DevToolsHookService from '../src/index.js' |
| 3 | + |
| 4 | +const fakeFrame = { |
| 5 | + getFileName: () => '/test/specs/fake.spec.ts', |
| 6 | + getLineNumber: () => 1, |
| 7 | + getColumnNumber: () => 1 |
| 8 | +} |
| 9 | +// Create mock instance that will be returned by SessionCapturer constructor |
| 10 | +vi.mock('stack-trace', () => ({ |
| 11 | + parse: () => [fakeFrame] |
| 12 | +})) |
| 13 | +const mockSessionCapturerInstance = { |
| 14 | + afterCommand: vi.fn(), |
| 15 | + sendUpstream: vi.fn(), |
| 16 | + injectScript: vi.fn().mockResolvedValue(undefined), |
| 17 | + commandsLog: [], |
| 18 | + sources: new Map(), |
| 19 | + mutations: [], |
| 20 | + traceLogs: [], |
| 21 | + consoleLogs: [], |
| 22 | + isReportingUpstream: false |
| 23 | +} |
| 24 | + |
| 25 | +vi.mock('../src/session.js', () => ({ |
| 26 | + SessionCapturer: vi.fn(function (this: any) { |
| 27 | + return mockSessionCapturerInstance |
| 28 | + }) |
| 29 | +})) |
| 30 | + |
| 31 | +describe('DevtoolsService - Internal Command Filtering', () => { |
| 32 | + let service: DevToolsHookService |
| 33 | + const mockBrowser = { |
| 34 | + isBidi: true, |
| 35 | + sessionId: 'test-session', |
| 36 | + scriptAddPreloadScript: vi.fn().mockResolvedValue(undefined), |
| 37 | + takeScreenshot: vi.fn().mockResolvedValue('screenshot'), |
| 38 | + execute: vi.fn().mockResolvedValue({ |
| 39 | + width: 1200, |
| 40 | + height: 800, |
| 41 | + offsetLeft: 0, |
| 42 | + offsetTop: 0 |
| 43 | + }) |
| 44 | + } as any |
| 45 | + |
| 46 | + // Helper to execute a command (before + after) |
| 47 | + const executeCommand = ( |
| 48 | + cmd: string, |
| 49 | + args: any[] = [], |
| 50 | + result: any = undefined |
| 51 | + ) => { |
| 52 | + service.beforeCommand(cmd as any, args) |
| 53 | + service.afterCommand(cmd as any, args, result) |
| 54 | + } |
| 55 | + |
| 56 | + beforeEach(() => { |
| 57 | + vi.clearAllMocks() |
| 58 | + mockSessionCapturerInstance.afterCommand.mockClear() |
| 59 | + mockSessionCapturerInstance.sendUpstream.mockClear() |
| 60 | + service = new DevToolsHookService() |
| 61 | + }) |
| 62 | + |
| 63 | + describe('beforeCommand', () => { |
| 64 | + it('should not add internal commands to command stack', () => { |
| 65 | + const internalCommands = [ |
| 66 | + 'getTitle', |
| 67 | + 'waitUntil', |
| 68 | + 'getUrl', |
| 69 | + 'execute', |
| 70 | + 'findElement' |
| 71 | + ] |
| 72 | + internalCommands.forEach((cmd) => service.beforeCommand(cmd as any, [])) |
| 73 | + expect(true).toBe(true) |
| 74 | + }) |
| 75 | + |
| 76 | + it('should add user commands to command stack', () => { |
| 77 | + ;['click', 'url', 'getText'].forEach((cmd, i) => { |
| 78 | + const args = [['.button', 'https://example.com', '.result'][i]] |
| 79 | + service.beforeCommand(cmd as any, args) |
| 80 | + }) |
| 81 | + expect(true).toBe(true) |
| 82 | + }) |
| 83 | + }) |
| 84 | + |
| 85 | + describe('afterCommand - internal command filtering', () => { |
| 86 | + beforeEach(async () => { |
| 87 | + await service.before({} as any, [], mockBrowser) |
| 88 | + vi.clearAllMocks() |
| 89 | + mockSessionCapturerInstance.afterCommand.mockClear() |
| 90 | + }) |
| 91 | + |
| 92 | + it('should filter mixed internal and user commands correctly', () => { |
| 93 | + // Execute mix of user and internal commands |
| 94 | + executeCommand('url', ['https://example.com']) |
| 95 | + executeCommand('getTitle', [], 'Page Title') // internal |
| 96 | + executeCommand('click', ['.button']) |
| 97 | + executeCommand('waitUntil', [expect.any(Function)], true) // internal |
| 98 | + executeCommand('getText', ['.result'], 'Success') |
| 99 | + |
| 100 | + // Only user commands (url, click, getText) should be captured |
| 101 | + expect(mockSessionCapturerInstance.afterCommand).toHaveBeenCalledTimes(3) |
| 102 | + |
| 103 | + const capturedCommands = |
| 104 | + mockSessionCapturerInstance.afterCommand.mock.calls.map( |
| 105 | + (call) => call[1] |
| 106 | + ) |
| 107 | + expect(capturedCommands).toEqual(['url', 'click', 'getText']) |
| 108 | + expect(capturedCommands).not.toContain('getTitle') |
| 109 | + expect(capturedCommands).not.toContain('waitUntil') |
| 110 | + }) |
| 111 | + }) |
| 112 | +}) |
0 commit comments