-
Notifications
You must be signed in to change notification settings - Fork 1
π§ͺ Test: add unit tests for browser-capture service #486
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,149 @@ | ||||||||||
| import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; | ||||||||||
| import { | ||||||||||
| startBrowserCapture, | ||||||||||
| stopBrowserCapture, | ||||||||||
| isBrowserCaptureRunning, | ||||||||||
| hasFrameFile, | ||||||||||
| FRAME_FILE | ||||||||||
| } from './browser-capture'; | ||||||||||
| import puppeteer from 'puppeteer-core'; | ||||||||||
| import * as fs from 'node:fs'; | ||||||||||
|
|
||||||||||
| vi.mock('puppeteer-core'); | ||||||||||
| vi.mock('node:fs', () => { | ||||||||||
| return { | ||||||||||
| writeFileSync: vi.fn(), | ||||||||||
| existsSync: vi.fn(), | ||||||||||
| }; | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| describe('Browser Capture Service', () => { | ||||||||||
| let mockPage: any; | ||||||||||
| let mockCdp: any; | ||||||||||
| let mockBrowser: any; | ||||||||||
|
|
||||||||||
| beforeEach(() => { | ||||||||||
| mockCdp = { | ||||||||||
| on: vi.fn(), | ||||||||||
| send: vi.fn(), | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| mockPage = { | ||||||||||
| setViewport: vi.fn(), | ||||||||||
| evaluateOnNewDocument: vi.fn(), | ||||||||||
| goto: vi.fn(), | ||||||||||
| createCDPSession: vi.fn().mockResolvedValue(mockCdp), | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| mockBrowser = { | ||||||||||
| newPage: vi.fn().mockResolvedValue(mockPage), | ||||||||||
| close: vi.fn(), | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| vi.mocked(puppeteer.launch).mockResolvedValue(mockBrowser as any); | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| afterEach(async () => { | ||||||||||
| vi.clearAllMocks(); | ||||||||||
| await stopBrowserCapture(); | ||||||||||
|
Comment on lines
+47
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is recommended to call
Suggested change
|
||||||||||
| }); | ||||||||||
|
Comment on lines
+46
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test Isolation and Cleanup: Recommended solution: afterEach(async () => {
vi.clearAllMocks();
if (isBrowserCaptureRunning()) {
await stopBrowserCapture();
}
}); |
||||||||||
|
|
||||||||||
| it('should start browser capture with correct parameters', async () => { | ||||||||||
| const config = { | ||||||||||
| url: 'http://localhost:3000/stream', | ||||||||||
| width: 1920, | ||||||||||
| height: 1080, | ||||||||||
| quality: 80, | ||||||||||
| overlayLayout: '{"test":true}', | ||||||||||
| theme: 'haxor', | ||||||||||
| avatarIndex: 2, | ||||||||||
| destinationId: 'dest-1' | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| await startBrowserCapture(config); | ||||||||||
|
|
||||||||||
| expect(puppeteer.launch).toHaveBeenCalledWith(expect.objectContaining({ | ||||||||||
| headless: true, | ||||||||||
| args: expect.arrayContaining([ | ||||||||||
| '--window-size=1920,1080', | ||||||||||
| '--no-sandbox', | ||||||||||
| '--use-gl=swiftshader', | ||||||||||
| ]), | ||||||||||
| })); | ||||||||||
|
|
||||||||||
| expect(mockPage.setViewport).toHaveBeenCalledWith({ | ||||||||||
| width: 1920, | ||||||||||
| height: 1080, | ||||||||||
| deviceScaleFactor: 1 | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| expect(mockPage.evaluateOnNewDocument).toHaveBeenCalledWith( | ||||||||||
| expect.any(Function), | ||||||||||
| '{"test":true}', | ||||||||||
| 'haxor', | ||||||||||
| 2, | ||||||||||
| 'dest-1' | ||||||||||
| ); | ||||||||||
|
|
||||||||||
| // Should append popout | ||||||||||
| expect(mockPage.goto).toHaveBeenCalledWith('http://localhost:3000/stream?popout=', expect.objectContaining({ | ||||||||||
| waitUntil: 'networkidle0' | ||||||||||
| })); | ||||||||||
|
|
||||||||||
| expect(mockCdp.send).toHaveBeenCalledWith('Page.startScreencast', { | ||||||||||
| format: 'jpeg', | ||||||||||
| quality: 80, | ||||||||||
| maxWidth: 1920, | ||||||||||
| maxHeight: 1080, | ||||||||||
| everyNthFrame: 2, | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| expect(isBrowserCaptureRunning()).toBe(true); | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| it('should not start a second instance if already running', async () => { | ||||||||||
| await startBrowserCapture({ url: 'http://localhost' }); | ||||||||||
| expect(puppeteer.launch).toHaveBeenCalledTimes(1); | ||||||||||
|
|
||||||||||
| await startBrowserCapture({ url: 'http://localhost' }); | ||||||||||
| expect(puppeteer.launch).toHaveBeenCalledTimes(1); // Still 1 | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| it('should handle URL popout logic correctly', async () => { | ||||||||||
| await startBrowserCapture({ url: 'http://localhost:3000/stream#view?' }); | ||||||||||
| expect(mockPage.goto).toHaveBeenCalledWith('http://localhost:3000/stream#view?&popout', expect.any(Object)); | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| it('should handle screencast frames correctly', async () => { | ||||||||||
| await startBrowserCapture({ url: 'http://localhost' }); | ||||||||||
|
|
||||||||||
| // Simulate frame | ||||||||||
| const onCall = mockCdp.on.mock.calls.find((call: any) => call[0] === 'Page.screencastFrame'); | ||||||||||
| expect(onCall).toBeDefined(); | ||||||||||
|
|
||||||||||
| const handler = onCall[1]; | ||||||||||
|
|
||||||||||
| await handler({ | ||||||||||
| data: Buffer.from('test frame').toString('base64'), | ||||||||||
| sessionId: 123 | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| expect(vi.mocked(fs.writeFileSync)).toHaveBeenCalledWith(FRAME_FILE, expect.any(Buffer)); | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of using
Suggested change
|
||||||||||
| expect(mockCdp.send).toHaveBeenCalledWith('Page.screencastFrameAck', { sessionId: 123 }); | ||||||||||
| }); | ||||||||||
|
Comment on lines
+117
to
+133
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Frame File Content Verification: Recommended solution: expect(vi.mocked(fs.writeFileSync)).toHaveBeenCalledWith(FRAME_FILE, Buffer.from('test frame')); |
||||||||||
|
|
||||||||||
| it('should stop capture correctly', async () => { | ||||||||||
| await startBrowserCapture({ url: 'http://localhost' }); | ||||||||||
| expect(isBrowserCaptureRunning()).toBe(true); | ||||||||||
|
|
||||||||||
| await stopBrowserCapture(); | ||||||||||
| expect(mockBrowser.close).toHaveBeenCalled(); | ||||||||||
| expect(isBrowserCaptureRunning()).toBe(false); | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| it('hasFrameFile should return true if file exists', () => { | ||||||||||
| vi.mocked(fs.existsSync).mockReturnValue(true); | ||||||||||
| expect(hasFrameFile()).toBe(true); | ||||||||||
| expect(vi.mocked(fs.existsSync)).toHaveBeenCalledWith(FRAME_FILE); | ||||||||||
| }); | ||||||||||
| }); | ||||||||||
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.
Using
anyfor mock objects bypasses TypeScript's type checking. It is recommended to use specific types frompuppeteer-core(e.g.,Page,CDPSession,Browser) combined with Vitest's mocking types to ensure the mocks align with the actual API and provide better type safety.