diff --git a/README.md b/README.md index 3b402a4..75370d2 100644 --- a/README.md +++ b/README.md @@ -165,6 +165,8 @@ export default { Run `npx e2e run` with `DEVICE_CLOUD_API_KEY` set. The provider uploads `appPath` once per run and each session installs it, so e2e installs nothing itself. To use a build you already uploaded, pass `deviceCloud({ appBinaryId })`. It also takes `model`, `os`, `maxDurationMin`, and `locale` (such as `en_US`) for the device's language and region, set before your build is installed. Each session's console link is printed as the run starts, and the session is stopped when e2e is done with it. +With e2e's `video` option on (any setting but `'off'`), each test's video in e2e's report is a console link to that test's part of the session's recording. The session is recorded as one video, so the link plays the part from the test's start to its end; it works once the session has ended and its video has uploaded, for as long as the session's files are kept. + ## Development diff --git a/src/e2e/index.ts b/src/e2e/index.ts index 80726c7..febcb7d 100644 --- a/src/e2e/index.ts +++ b/src/e2e/index.ts @@ -8,7 +8,8 @@ * const engine = mobile({ platform: 'android', device: deviceCloud() }); * * Each e2e worker gets its own DeviceCloud device session, which shows in the - * console, is recorded, and is billed like any other. Reads + * console, is recorded, and is billed like any other. With `video` on, each + * test's video is a link to its part of the session in the console. Reads * `DEVICE_CLOUD_API_KEY` from the run's environment. Early access: the * organization needs it turned on by DeviceCloud. * @@ -28,6 +29,9 @@ export type { DeviceCloudProvider, E2eDeviceLease, E2eDeviceRequest, + E2eRecordContext, + E2eRecording, + E2eRecordingResult, E2eReleaseContext, } from './provider.js'; diff --git a/src/e2e/provider.ts b/src/e2e/provider.ts index a4295ea..96e4a78 100644 --- a/src/e2e/provider.ts +++ b/src/e2e/provider.ts @@ -12,6 +12,12 @@ * is. e2e then drives the device directly, so its calls do not pass through * the API's command queue. `release` stops the session. * + * When a test records video, `record` gives e2e a link for it instead of a + * file: the session's page in the DeviceCloud console, at that test's part of + * the session's own recording (`?from=&to=`, clock times). The recording is + * one video for the whole session, finished when the session ends, so no + * per-test file exists when the test does. + * * The types below describe e2e's provider contract (`DeviceProvider` in * `@e2e-dev/mobile`) by shape, for the fields this provider uses, so the * package does not depend on e2e. An object of this type can be passed to @@ -54,10 +60,40 @@ export interface E2eReleaseContext { readonly log: (line: string) => void; } +/** Handed to `record`, once per test attempt that records video. */ +export interface E2eRecordContext { + readonly attemptId: string; + readonly env: Readonly>; + readonly runId: string; + readonly signal: AbortSignal; + readonly targetName: string; +} + +/** What a stopped recording became: here always a link, with its media type. */ +export interface E2eRecordingResult { + readonly mediaType: string; + readonly url: string; +} + +/** A test attempt's recording, as `record` starts it (`ProviderRecording` in `@e2e-dev/core/engine`). */ +export interface E2eRecording { + /** When it started, as an ISO time. */ + readonly startedAt: string; + stop(context: { + readonly dir: string; + readonly signal: AbortSignal; + }): Promise; +} + /** A source of DeviceCloud devices for e2e, the shape of `@e2e-dev/mobile`'s `DeviceProvider`. */ export interface DeviceCloudProvider { readonly name: string; acquire(request: E2eDeviceRequest): Promise; + /** A test attempt's video: a console link to its part of the session's recording. */ + record( + lease: E2eDeviceLease, + context: E2eRecordContext, + ): Promise; release(lease: E2eDeviceLease, context: E2eReleaseContext): Promise; } @@ -105,6 +141,8 @@ export interface ProviderDeps { pollMs: number; /** How long a session may take to be ready: queued, starting, installing. */ startTimeoutMs: number; + /** The clock, in ms. `Date.now` without. */ + now?: () => number; } const DEFAULT_API_URL = 'https://api.devicecloud.dev'; @@ -217,6 +255,10 @@ export function createProvider( const locale = deviceLocale(options.locale); // One upload per build per run: e2e acquires every worker slot at once. const uploads = new Map>(); + const now = deps.now ?? Date.now; + // Each lease's page in the console, read once per worker process: e2e + // records in the worker, which has the lease's id but not the session. + const pages = new Map>(); const client = (apiKey: string, apiUrl: string) => @@ -426,6 +468,46 @@ export function createProvider( name: 'devicecloud', + async record(lease, context) { + // The test starts once this returns: from now on is its part. + const startedAt = new Date(now()).toISOString(); + let page = pages.get(lease.id); + if (!page) { + page = (async () => { + const { apiKey, apiUrl } = connection(options, context.env, () => {}); + const read = await client(apiKey, apiUrl)( + 'GET', + `/device/sessions/${encodeURIComponent(lease.id)}`, + undefined, + context.signal, + ); + if (read.status >= 300 || !read.json?.session) + throw failure(`reading device session ${lease.id}`, read); + const { monitor_url: monitorUrl } = read.json.session as SessionBlock; + if (!monitorUrl) + throw new Error( + `DeviceCloud: session ${lease.id} has no console page to link a test's video to`, + ); + return monitorUrl; + })(); + pages.set(lease.id, page); + // A failed read is tried again by the next attempt, not remembered. + page.catch(() => pages.delete(lease.id)); + } + + const monitorUrl = await page; + return { + startedAt, + // No request here: stopping can't fail once the test is over. + async stop() { + const url = new URL(monitorUrl); + url.searchParams.set('from', startedAt); + url.searchParams.set('to', new Date(now()).toISOString()); + return { mediaType: 'text/html', url: url.href }; + }, + }; + }, + async release(lease, context) { const { apiKey, apiUrl } = connection(options, context.env, context.log); const stopped = await client(apiKey, apiUrl)( diff --git a/test/unit/e2e-provider.test.ts b/test/unit/e2e-provider.test.ts index a43343f..fbae704 100644 --- a/test/unit/e2e-provider.test.ts +++ b/test/unit/e2e-provider.test.ts @@ -496,4 +496,108 @@ describe('the e2e device provider', () => { ); expect(error.message).to.include('Something broke.'); }); + + describe('a test that records video', () => { + const context = (attemptId = 'attempt-1') => ({ + attemptId, + env: { DEVICE_CLOUD_API_KEY: 'key-from-env' }, + runId: 'run-1', + signal: new AbortController().signal, + targetName: 'android', + }); + const stopContext = { + dir: '/tmp/video', + signal: new AbortController().signal, + }; + const at = (iso: string) => () => Date.parse(iso); + + it("gets a console link to its part of the session's recording", async () => { + let clock = Date.parse('2026-11-25T10:00:10.000Z'); + const { api, provider } = setup( + { + 'GET /device/sessions/brave-teal-otter': [ + { body: session('active'), status: 200 }, + ], + }, + {}, + { now: () => clock }, + ); + + const recording = await provider.record( + { id: 'brave-teal-otter' }, + context(), + ); + expect(recording.startedAt).to.equal('2026-11-25T10:00:10.000Z'); + clock = Date.parse('2026-11-25T10:00:45.500Z'); + const result = await recording.stop(stopContext); + + expect(result).to.deep.equal({ + mediaType: 'text/html', + url: 'https://console.example/device/brave-teal-otter?from=2026-11-25T10%3A00%3A10.000Z&to=2026-11-25T10%3A00%3A45.500Z', + }); + // The session was read with the run's key; stopping asked for nothing. + expect(api.calls).to.have.length(1); + expect(api.calls[0].headers['x-app-api-key']).to.equal('key-from-env'); + }); + + it('reads the session once per lease, however many tests record', async () => { + const { api, provider } = setup( + { + 'GET /device/sessions/brave-teal-otter': [ + { body: session('active'), status: 200 }, + ], + }, + {}, + { now: at('2026-11-25T10:00:00.000Z') }, + ); + for (const attempt of ['attempt-1', 'attempt-2', 'attempt-3']) { + const recording = await provider.record( + { id: 'brave-teal-otter' }, + context(attempt), + ); + await recording.stop(stopContext); + } + + expect(api.calls.map((c) => `${c.method} ${c.path}`)).to.deep.equal([ + 'GET /device/sessions/brave-teal-otter', + ]); + }); + + it('fails, named, when the session cannot be read, and reads it again next time', async () => { + const { api, provider } = setup( + { + 'GET /device/sessions/brave-teal-otter': [ + { body: { message: 'Device session not found.' }, status: 404 }, + { + body: session('active', { monitor_url: undefined }), + status: 200, + }, + { body: session('active'), status: 200 }, + ], + }, + {}, + { now: at('2026-11-25T10:00:00.000Z') }, + ); + + const gone = await rejection( + provider.record({ id: 'brave-teal-otter' }, context()), + ); + expect(gone.message).to.include( + 'reading device session brave-teal-otter', + ); + expect(gone.message).to.include('Device session not found.'); + const noPage = await rejection( + provider.record({ id: 'brave-teal-otter' }, context()), + ); + expect(noPage.message).to.include('has no console page'); + const recording = await provider.record( + { id: 'brave-teal-otter' }, + context(), + ); + expect((await recording.stop(stopContext)).url).to.match( + /^https:\/\/console\.example\/device\/brave-teal-otter\?from=/, + ); + expect(api.calls).to.have.length(3); + }); + }); });