|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest' |
| 2 | +import { ScreencastRecorder } from '../src/screencast.js' |
| 3 | + |
| 4 | +vi.mock('@wdio/logger', () => { |
| 5 | + const mockLogger = { |
| 6 | + info: vi.fn(), |
| 7 | + warn: vi.fn(), |
| 8 | + debug: vi.fn(), |
| 9 | + error: vi.fn() |
| 10 | + } |
| 11 | + return { default: vi.fn(() => mockLogger) } |
| 12 | +}) |
| 13 | + |
| 14 | +/** Helper: create a CDP-backed recorder with a frame handler ref. */ |
| 15 | +function createCdpSetup() { |
| 16 | + let frameHandler: (event: any) => void |
| 17 | + const cdpSession = { |
| 18 | + send: vi.fn().mockResolvedValue(undefined), |
| 19 | + on: vi.fn((event: string, handler: any) => { |
| 20 | + if (event === 'Page.screencastFrame') { |
| 21 | + frameHandler = handler |
| 22 | + } |
| 23 | + }) |
| 24 | + } |
| 25 | + const browser = { |
| 26 | + getPuppeteer: vi.fn().mockResolvedValue({ |
| 27 | + pages: vi |
| 28 | + .fn() |
| 29 | + .mockResolvedValue([ |
| 30 | + { createCDPSession: vi.fn().mockResolvedValue(cdpSession) } |
| 31 | + ]) |
| 32 | + }) |
| 33 | + } as any |
| 34 | + |
| 35 | + const pushFrame = (data: string, timestampSec: number, sessionId = 1) => |
| 36 | + frameHandler({ data, metadata: { timestamp: timestampSec }, sessionId }) |
| 37 | + |
| 38 | + return { browser, cdpSession, pushFrame } |
| 39 | +} |
| 40 | + |
| 41 | +describe('ScreencastRecorder', () => { |
| 42 | + beforeEach(() => { |
| 43 | + vi.clearAllMocks() |
| 44 | + vi.restoreAllMocks() |
| 45 | + }) |
| 46 | + |
| 47 | + it('CDP mode: start → collect frames with acks → stop', async () => { |
| 48 | + const { browser, cdpSession, pushFrame } = createCdpSetup() |
| 49 | + const recorder = new ScreencastRecorder() |
| 50 | + |
| 51 | + // Start |
| 52 | + await recorder.start(browser) |
| 53 | + expect(recorder.isRecording).toBe(true) |
| 54 | + expect(cdpSession.send).toHaveBeenCalledWith( |
| 55 | + 'Page.startScreencast', |
| 56 | + expect.objectContaining({ format: 'jpeg', quality: 70 }) |
| 57 | + ) |
| 58 | + |
| 59 | + // Collect frames — timestamps are converted from seconds to ms |
| 60 | + pushFrame('frame1', 1.0, 1) |
| 61 | + pushFrame('frame2', 2.5, 2) |
| 62 | + expect(recorder.frames).toHaveLength(2) |
| 63 | + expect(recorder.frames[0]).toEqual({ data: 'frame1', timestamp: 1000 }) |
| 64 | + expect(recorder.frames[1]).toEqual({ data: 'frame2', timestamp: 2500 }) |
| 65 | + expect(cdpSession.send).toHaveBeenCalledWith('Page.screencastFrameAck', { |
| 66 | + sessionId: 1 |
| 67 | + }) |
| 68 | + |
| 69 | + // Duration |
| 70 | + expect(recorder.duration).toBe(1500) |
| 71 | + |
| 72 | + // Stop |
| 73 | + await recorder.stop() |
| 74 | + expect(recorder.isRecording).toBe(false) |
| 75 | + expect(cdpSession.send).toHaveBeenCalledWith('Page.stopScreencast') |
| 76 | + |
| 77 | + // Stop again — no-op, no throw |
| 78 | + await recorder.stop() |
| 79 | + }) |
| 80 | + |
| 81 | + it('polling mode: fallback when CDP unavailable → collect at interval → stop', async () => { |
| 82 | + vi.useFakeTimers() |
| 83 | + const browser = { |
| 84 | + getPuppeteer: vi.fn().mockRejectedValue(new Error('No puppeteer')), |
| 85 | + takeScreenshot: vi |
| 86 | + .fn() |
| 87 | + .mockResolvedValueOnce('shot1') |
| 88 | + .mockResolvedValueOnce('shot2') |
| 89 | + .mockResolvedValueOnce('shot3') |
| 90 | + } as any |
| 91 | + |
| 92 | + const recorder = new ScreencastRecorder({ pollIntervalMs: 200 }) |
| 93 | + await recorder.start(browser) |
| 94 | + |
| 95 | + // Immediate first frame + recording started |
| 96 | + expect(recorder.isRecording).toBe(true) |
| 97 | + expect(recorder.frames).toHaveLength(1) |
| 98 | + expect(recorder.frames[0].data).toBe('shot1') |
| 99 | + |
| 100 | + // Interval ticks collect more frames |
| 101 | + await vi.advanceTimersByTimeAsync(200) |
| 102 | + expect(recorder.frames).toHaveLength(2) |
| 103 | + await vi.advanceTimersByTimeAsync(200) |
| 104 | + expect(recorder.frames).toHaveLength(3) |
| 105 | + |
| 106 | + await recorder.stop() |
| 107 | + expect(recorder.isRecording).toBe(false) |
| 108 | + vi.useRealTimers() |
| 109 | + }) |
| 110 | + |
| 111 | + it('polling: screenshot failure stops timer, initial failure skips recording', async () => { |
| 112 | + // Mid-polling failure — timer cleared, no more frames |
| 113 | + vi.useFakeTimers() |
| 114 | + const failBrowser = { |
| 115 | + getPuppeteer: vi.fn().mockRejectedValue(new Error('No puppeteer')), |
| 116 | + takeScreenshot: vi |
| 117 | + .fn() |
| 118 | + .mockResolvedValueOnce('ok') |
| 119 | + .mockRejectedValueOnce(new Error('Session ended')) |
| 120 | + .mockResolvedValueOnce('should-not-appear') |
| 121 | + } as any |
| 122 | + |
| 123 | + const rec1 = new ScreencastRecorder({ pollIntervalMs: 200 }) |
| 124 | + await rec1.start(failBrowser) |
| 125 | + await vi.advanceTimersByTimeAsync(200) // failure tick |
| 126 | + const countAfterError = rec1.frames.length |
| 127 | + await vi.advanceTimersByTimeAsync(200) // timer should be cleared |
| 128 | + expect(rec1.frames).toHaveLength(countAfterError) |
| 129 | + vi.useRealTimers() |
| 130 | + |
| 131 | + // Initial screenshot fails — recording never starts |
| 132 | + const noBrowser = { |
| 133 | + getPuppeteer: vi.fn().mockRejectedValue(new Error('No puppeteer')), |
| 134 | + takeScreenshot: vi.fn().mockRejectedValue(new Error('Not supported')) |
| 135 | + } as any |
| 136 | + const rec2 = new ScreencastRecorder() |
| 137 | + await rec2.start(noBrowser) |
| 138 | + expect(rec2.isRecording).toBe(false) |
| 139 | + expect(rec2.frames).toEqual([]) |
| 140 | + }) |
| 141 | + |
| 142 | + it('setStartMarker trims leading frames and is idempotent', async () => { |
| 143 | + const { browser, pushFrame } = createCdpSetup() |
| 144 | + const recorder = new ScreencastRecorder() |
| 145 | + await recorder.start(browser) |
| 146 | + |
| 147 | + // 2 blank frames before marker |
| 148 | + pushFrame('blank1', 1.0) |
| 149 | + pushFrame('blank2', 2.0) |
| 150 | + recorder.setStartMarker() |
| 151 | + |
| 152 | + // 2 meaningful frames |
| 153 | + pushFrame('page1', 5.0) |
| 154 | + recorder.setStartMarker() // second call — ignored |
| 155 | + pushFrame('page2', 8.0) |
| 156 | + |
| 157 | + // Only post-marker frames returned |
| 158 | + expect(recorder.frames).toHaveLength(2) |
| 159 | + expect(recorder.frames[0].data).toBe('page1') |
| 160 | + expect(recorder.frames[1].data).toBe('page2') |
| 161 | + |
| 162 | + // Duration based on trimmed frames: 8000 - 5000 |
| 163 | + expect(recorder.duration).toBe(3000) |
| 164 | + }) |
| 165 | + |
| 166 | + it('stop is safe when never started', async () => { |
| 167 | + const recorder = new ScreencastRecorder() |
| 168 | + expect(recorder.duration).toBe(0) |
| 169 | + await expect(recorder.stop()).resolves.toBeUndefined() |
| 170 | + }) |
| 171 | +}) |
0 commit comments