|
| 1 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 2 | +import { existsSync, statSync, rmSync } from 'node:fs'; |
| 3 | +import { join } from 'node:path'; |
| 4 | +import { tmpdir } from 'node:os'; |
| 5 | +import { startTestServer, type TestServer } from './fixtures/server.js'; |
| 6 | +import { demo as demoFn } from './fixtures/demo-script.js'; |
| 7 | +import { runScriptAndRecord } from '../../packages/core/src/recorder/playwright-runner.js'; |
| 8 | +import { postProcess } from '../../packages/core/src/recorder/post-processor.js'; |
| 9 | + |
| 10 | +// The script as a string — as it would come from the LLM |
| 11 | +const DEMO_SCRIPT = ` |
| 12 | +import type { Page } from '@playwright/test'; |
| 13 | +
|
| 14 | +export async function demo(page: Page): Promise<void> { |
| 15 | + await page.waitForLoadState('networkidle'); |
| 16 | + await page.waitForTimeout(500); |
| 17 | +
|
| 18 | + await page.click('#add-to-cart'); |
| 19 | + await page.waitForTimeout(400); |
| 20 | + await page.click('#add-to-cart'); |
| 21 | + await page.waitForTimeout(600); |
| 22 | +
|
| 23 | + await page.click('#try-on-btn'); |
| 24 | + await page.waitForSelector('#modal.open', { state: 'visible' }); |
| 25 | + await page.waitForTimeout(800); |
| 26 | +
|
| 27 | + await page.click('#close-modal'); |
| 28 | + await page.waitForTimeout(400); |
| 29 | +} |
| 30 | +`; |
| 31 | + |
| 32 | +const RECORDING_CONFIG = { |
| 33 | + viewport: { width: 1280, height: 720 }, |
| 34 | + format: 'gif' as const, |
| 35 | + maxDuration: 30, |
| 36 | + deviceScaleFactor: 1, |
| 37 | +}; |
| 38 | + |
| 39 | +let server: TestServer; |
| 40 | +let outputDir: string; |
| 41 | + |
| 42 | +beforeAll(async () => { |
| 43 | + server = await startTestServer(); |
| 44 | + outputDir = join(tmpdir(), `git-glimpse-test-${Date.now()}`); |
| 45 | +}, 15000); |
| 46 | + |
| 47 | +afterAll(async () => { |
| 48 | + await server.close(); |
| 49 | + if (existsSync(outputDir)) { |
| 50 | + rmSync(outputDir, { recursive: true, force: true }); |
| 51 | + } |
| 52 | +}); |
| 53 | + |
| 54 | +describe('Playwright recorder', () => { |
| 55 | + it('records a webm video from the demo script', async () => { |
| 56 | + const result = await runScriptAndRecord({ |
| 57 | + script: DEMO_SCRIPT, |
| 58 | + baseUrl: server.url, |
| 59 | + recording: RECORDING_CONFIG, |
| 60 | + outputDir, |
| 61 | + }); |
| 62 | + |
| 63 | + expect(result.videoPath).toMatch(/\.webm$/); |
| 64 | + expect(existsSync(result.videoPath)).toBe(true); |
| 65 | + expect(statSync(result.videoPath).size).toBeGreaterThan(1000); |
| 66 | + expect(result.duration).toBeGreaterThan(0); |
| 67 | + expect(result.duration).toBeLessThan(30); |
| 68 | + }, 30000); |
| 69 | +}); |
| 70 | + |
| 71 | +describe('Post-processor', () => { |
| 72 | + it('converts webm to gif', async () => { |
| 73 | + // First record |
| 74 | + const recording = await runScriptAndRecord({ |
| 75 | + script: DEMO_SCRIPT, |
| 76 | + baseUrl: server.url, |
| 77 | + recording: RECORDING_CONFIG, |
| 78 | + outputDir, |
| 79 | + }); |
| 80 | + |
| 81 | + // Then convert |
| 82 | + const processed = await postProcess({ |
| 83 | + inputPath: recording.videoPath, |
| 84 | + outputDir, |
| 85 | + format: 'gif', |
| 86 | + viewport: RECORDING_CONFIG.viewport, |
| 87 | + }); |
| 88 | + |
| 89 | + expect(processed.outputPath).toMatch(/\.gif$/); |
| 90 | + expect(existsSync(processed.outputPath)).toBe(true); |
| 91 | + expect(processed.sizeMB).toBeGreaterThan(0); |
| 92 | + expect(processed.sizeMB).toBeLessThan(20); // sanity cap |
| 93 | + }, 60000); |
| 94 | + |
| 95 | + it('converts webm to mp4', async () => { |
| 96 | + const recording = await runScriptAndRecord({ |
| 97 | + script: DEMO_SCRIPT, |
| 98 | + baseUrl: server.url, |
| 99 | + recording: RECORDING_CONFIG, |
| 100 | + outputDir, |
| 101 | + }); |
| 102 | + |
| 103 | + const processed = await postProcess({ |
| 104 | + inputPath: recording.videoPath, |
| 105 | + outputDir, |
| 106 | + format: 'mp4', |
| 107 | + viewport: RECORDING_CONFIG.viewport, |
| 108 | + }); |
| 109 | + |
| 110 | + expect(processed.outputPath).toMatch(/\.mp4$/); |
| 111 | + expect(existsSync(processed.outputPath)).toBe(true); |
| 112 | + expect(processed.sizeMB).toBeGreaterThan(0); |
| 113 | + }, 60000); |
| 114 | +}); |
| 115 | + |
| 116 | +describe('Full recording pipeline (no LLM)', () => { |
| 117 | + it('produces a gif under 5MB from start to finish', async () => { |
| 118 | + const pipelineOutputDir = join(tmpdir(), `git-glimpse-pipeline-${Date.now()}`); |
| 119 | + |
| 120 | + try { |
| 121 | + const recording = await runScriptAndRecord({ |
| 122 | + script: DEMO_SCRIPT, |
| 123 | + baseUrl: server.url, |
| 124 | + recording: RECORDING_CONFIG, |
| 125 | + outputDir: pipelineOutputDir, |
| 126 | + }); |
| 127 | + |
| 128 | + const processed = await postProcess({ |
| 129 | + inputPath: recording.videoPath, |
| 130 | + outputDir: pipelineOutputDir, |
| 131 | + format: 'gif', |
| 132 | + viewport: RECORDING_CONFIG.viewport, |
| 133 | + }); |
| 134 | + |
| 135 | + expect(existsSync(processed.outputPath)).toBe(true); |
| 136 | + expect(processed.sizeMB).toBeLessThan(5); |
| 137 | + } finally { |
| 138 | + if (existsSync(pipelineOutputDir)) { |
| 139 | + rmSync(pipelineOutputDir, { recursive: true, force: true }); |
| 140 | + } |
| 141 | + } |
| 142 | + }, 60000); |
| 143 | +}); |
0 commit comments