Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 7 additions & 2 deletions e2e/support/cdpPages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)/

Expand Down Expand Up @@ -155,7 +156,7 @@ export class WebContentsPage {
}

private async clickElement(elementExpression: string): Promise<boolean> {
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})`
Expand All @@ -177,7 +178,11 @@ export class WebContentsPage {
return this.clickElement(`document.querySelectorAll(${JSON.stringify(selector)})[${index}]`)
}

async fill(selector: string, text: string): Promise<void> {
fill(selector: string, text: string): Promise<void> {
return pacedForVideo(() => this.fillNow(selector, text))
}

private async fillNow(selector: string, text: string): Promise<void> {
await evalWithRetry(async () => {
const focused = await this.wcEval<boolean>(`(() => {
const el = document.querySelector(${JSON.stringify(selector)})
Expand Down
5 changes: 4 additions & 1 deletion e2e/support/electronHarness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -220,9 +221,11 @@ export async function launchLauncherApp(options?: SeedOptions): Promise<Launcher
env['E2E_INSTALLATIONS_SEED'] = JSON.stringify(seedRecords)
}

const recordVideo = recordVideoOptions()
const application = await electron.launch({
args,
env
env,
...(recordVideo ? { recordVideo } : {})
})

const page = await application.firstWindow()
Expand Down
68 changes: 68 additions & 0 deletions e2e/support/videoRecording.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import path from 'node:path'
import { afterEach, describe, expect, it, vi } from 'vitest'
import {
pacedForVideo,
recordVideoOptions,
VIDEO_PACE_MS,
videoRecordingEnabled
} from './videoRecording'

const TEST_RESULTS_DIR = path.resolve(__dirname, '..', '..', 'test-results')

describe('videoRecordingEnabled', () => {
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')
})
})
42 changes: 42 additions & 0 deletions e2e/support/videoRecording.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
return new Promise((resolve) => setTimeout(resolve, ms))
}

async function withPausesAround<T>(action: () => Promise<T>): Promise<T> {
await pause(VIDEO_PACE_MS)
const result = await action()
await pause(VIDEO_PACE_MS)
return result
}

export function pacedForVideo<T>(
action: () => Promise<T>,
env: NodeJS.ProcessEnv = process.env
): Promise<T> {
if (!videoRecordingEnabled(env)) return action()
return withPausesAround(action)
}
1 change: 1 addition & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading