diff --git a/README.md b/README.md index 874577cfd..d3a092bb4 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,14 @@ pnpm test Main, preload, and shared code is tested in Node. Renderer tests run in headless Chromium through [Vitest browser mode](https://vitest.dev/guide/browser/). To watch them in a visible browser, run `pnpm exec vitest --project browser --browser.headless=false`. +To record a video of an e2e test for a reviewer, set `E2E_VIDEO` to any value other than empty or `0`: + +```bash +E2E_VIDEO=1 pnpm exec playwright test e2e/home.test.ts --project=macos -g "Auto start" +``` + +The videos land in `test-results/videos/` at 1280x800. Playwright writes one `.webm` per view; the page view is the largest file. While recording, each `click`, `clickNth`, `clickByText` and `fill` in `e2e/support/cdpPages.ts` pauses about 1 s before and after, so each change shows on screen. Dropdown helpers (`clickUntilVisible`, `selectOption`) and clicks made in the page's own script are not slowed down. + ## Releasing > Production builds go through [ToDesktop](https://www.todesktop.com/) in CI — nothing is built locally. Any maintainer can cut a release; it's one workflow run plus a PR merge. diff --git a/e2e/support/cdpPages.ts b/e2e/support/cdpPages.ts index f4cddd331..cd64b9bbd 100644 --- a/e2e/support/cdpPages.ts +++ b/e2e/support/cdpPages.ts @@ -8,6 +8,7 @@ import { } from './clickAim' import type { ClickAttempt, ClickOutcome } from './clickAim' import { evalWithRetry } from './evalRetry' +import { pacedForVideo } from './videoRecording' const PAGE_GONE_PATTERN = /webContents (not found|destroyed)/ @@ -155,7 +156,7 @@ export class WebContentsPage { } private async clickElement(elementExpression: string): Promise { - const outcome = await this.sendClick(elementExpression) + const outcome = await pacedForVideo(() => this.sendClick(elementExpression)) if (outcome === 'blocked') { throw new Error( `click: element is covered or has no interactive area (${elementExpression}, marker=${this.marker})` @@ -177,7 +178,11 @@ export class WebContentsPage { return this.clickElement(`document.querySelectorAll(${JSON.stringify(selector)})[${index}]`) } - async fill(selector: string, text: string): Promise { + fill(selector: string, text: string): Promise { + return pacedForVideo(() => this.fillNow(selector, text)) + } + + private async fillNow(selector: string, text: string): Promise { await evalWithRetry(async () => { const focused = await this.wcEval(`(() => { const el = document.querySelector(${JSON.stringify(selector)}) diff --git a/e2e/support/electronHarness.ts b/e2e/support/electronHarness.ts index b01a8bdf9..fbd4423d2 100644 --- a/e2e/support/electronHarness.ts +++ b/e2e/support/electronHarness.ts @@ -7,6 +7,7 @@ import { _electron as electron, type ElectronApplication } from 'playwright' import { evalWithRetry } from './evalRetry' import { FOREGROUND_PROJECTS, isBackgroundRun } from './focusMode' +import { recordVideoOptions } from './videoRecording' export interface LauncherAppHandle { application: ElectronApplication @@ -220,9 +221,11 @@ export async function launchLauncherApp(options?: SeedOptions): Promise { + it('is off when E2E_VIDEO is unset', () => { + expect(videoRecordingEnabled({})).toBe(false) + }) + + it.each(['', '0'])('is off when E2E_VIDEO is "%s"', (value) => { + expect(videoRecordingEnabled({ E2E_VIDEO: value })).toBe(false) + }) + + it.each(['1', 'true', 'yes'])('is on when E2E_VIDEO is %s', (value) => { + expect(videoRecordingEnabled({ E2E_VIDEO: value })).toBe(true) + }) +}) + +describe('recordVideoOptions', () => { + it('is undefined when recording is off', () => { + expect(recordVideoOptions({})).toBeUndefined() + expect(recordVideoOptions({ E2E_VIDEO: '0' })).toBeUndefined() + }) + + it('records at 1280x800 into a folder under test-results when on', () => { + const options = recordVideoOptions({ E2E_VIDEO: '1' }) + expect(options?.size).toEqual({ width: 1280, height: 800 }) + const relative = path.relative(TEST_RESULTS_DIR, options?.dir ?? '') + expect(relative.startsWith('..') || path.isAbsolute(relative)).toBe(false) + }) +}) + +describe('pacedForVideo', () => { + afterEach(() => { + vi.useRealTimers() + }) + + it('returns the action promise itself when recording is off', () => { + const actionResult = Promise.resolve('done') + expect(pacedForVideo(() => actionResult, {})).toBe(actionResult) + }) + + it('waits before and after the action when recording is on', async () => { + vi.useFakeTimers() + const action = vi.fn(() => Promise.resolve('done')) + let settled = false + const paced = pacedForVideo(action, { E2E_VIDEO: '1' }).then((value) => { + settled = true + return value + }) + + await vi.advanceTimersByTimeAsync(VIDEO_PACE_MS - 1) + expect(action).not.toHaveBeenCalled() + await vi.advanceTimersByTimeAsync(1) + expect(action).toHaveBeenCalledTimes(1) + await vi.advanceTimersByTimeAsync(VIDEO_PACE_MS - 1) + expect(settled).toBe(false) + await vi.advanceTimersByTimeAsync(1) + await expect(paced).resolves.toBe('done') + }) +}) diff --git a/e2e/support/videoRecording.ts b/e2e/support/videoRecording.ts new file mode 100644 index 000000000..dc2874a1f --- /dev/null +++ b/e2e/support/videoRecording.ts @@ -0,0 +1,42 @@ +import path from 'node:path' +import process from 'node:process' + +export const VIDEO_DIR = path.resolve(__dirname, '..', '..', 'test-results', 'videos') +export const VIDEO_SIZE = { width: 1280, height: 800 } as const +export const VIDEO_PACE_MS = 1000 + +export interface RecordVideoOptions { + dir: string + size: { width: number; height: number } +} + +export function videoRecordingEnabled(env: NodeJS.ProcessEnv = process.env): boolean { + const value = env['E2E_VIDEO'] + return value !== undefined && value !== '' && value !== '0' +} + +export function recordVideoOptions( + env: NodeJS.ProcessEnv = process.env +): RecordVideoOptions | undefined { + if (!videoRecordingEnabled(env)) return undefined + return { dir: VIDEO_DIR, size: { ...VIDEO_SIZE } } +} + +function pause(ms: number): Promise { + return new Promise((resolve) => setTimeout(resolve, ms)) +} + +async function withPausesAround(action: () => Promise): Promise { + await pause(VIDEO_PACE_MS) + const result = await action() + await pause(VIDEO_PACE_MS) + return result +} + +export function pacedForVideo( + action: () => Promise, + env: NodeJS.ProcessEnv = process.env +): Promise { + if (!videoRecordingEnabled(env)) return action() + return withPausesAround(action) +} diff --git a/vitest.config.ts b/vitest.config.ts index 9cb2dcb79..b76993310 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -43,6 +43,7 @@ export default defineConfig({ 'src/preload/**/*.test.ts', 'src/shared/**/*.test.ts', 'scripts/eslint/**/*.test.ts', + 'e2e/support/videoRecording.test.ts', ...RENDERER_TESTS_THAT_READ_FILES ], exclude: ['node_modules', ...INJECTED_PAGE_SCRIPT_TESTS]