| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- import { access, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
- import { tmpdir } from "node:os";
- import { join } from "node:path";
- import { afterEach, describe, expect, it, vi } from "vitest";
- import { startElectronWindowRecording } from "./e2e/helpers/electron-window-recorder.js";
- import { runElectronRecordingCommand } from "./e2e/helpers/electron-window-recorder-timeout.js";
- const tempDirs: string[] = [];
- afterEach(async () => {
- await Promise.all(tempDirs.splice(0).map((dir) => rm(dir, { force: true, recursive: true })));
- });
- describe("Electron window recorder", () => {
- it("records renderer frames through Electron capturePage metadata", async () => {
- const tempDir = await mkdtemp(join(tmpdir(), "zcode-electron-recorder-"));
- tempDirs.push(tempDir);
- const outputPath = join(tempDir, "electron-repro.webm");
- const encodedFrameCounts: number[] = [];
- const session = await startElectronWindowRecording({
- captureFrame: async () => Buffer.from("fake-png-frame"),
- encodeVideo: async ({ framePaths, outputPath: encodedOutputPath }) => {
- encodedFrameCounts.push(framePaths.length);
- await readFile(framePaths[0]);
- await writeFile(encodedOutputPath, "fake-webm");
- },
- frameIntervalMs: 60_000,
- outputPath,
- });
- const result = await session.stop();
- expect(result.captureMode).toBe("electron_capture_page");
- expect(result.videoPath).toBe(outputPath);
- expect(result.frameCount).toBeGreaterThanOrEqual(2);
- expect(encodedFrameCounts).toEqual([result.frameCount]);
- await expect(readFile(outputPath, "utf8")).resolves.toBe("fake-webm");
- });
- it("discards passing-case frames without invoking the encoder", async () => {
- const tempDir = await mkdtemp(join(tmpdir(), "zcode-electron-recorder-"));
- tempDirs.push(tempDir);
- const outputPath = join(tempDir, "electron-repro.webm");
- const frameDir = join(tempDir, "frames");
- const encodeVideo = vi.fn();
- const session = await startElectronWindowRecording({
- captureFrame: async () => Buffer.from("fake-png-frame"),
- encodeVideo,
- frameDir,
- frameIntervalMs: 60_000,
- outputPath,
- });
- await session.discard();
- expect(encodeVideo).not.toHaveBeenCalled();
- await expect(access(frameDir)).rejects.toMatchObject({ code: "ENOENT" });
- await expect(access(outputPath)).rejects.toMatchObject({ code: "ENOENT" });
- });
- it("collects a pre-roll before allowing the case to start", async () => {
- const tempDir = await mkdtemp(join(tmpdir(), "zcode-electron-recorder-"));
- tempDirs.push(tempDir);
- const startedAt = Date.now();
- const session = await startElectronWindowRecording({
- captureFrame: async () => Buffer.from("fake-png-frame"),
- frameIntervalMs: 100,
- outputPath: join(tempDir, "electron-repro.webm"),
- preRollDurationMs: 120,
- });
- expect(Date.now() - startedAt).toBeGreaterThanOrEqual(100);
- await session.discard();
- });
- it("keeps the video timeline aligned with the case when later captures are missed", async () => {
- const tempDir = await mkdtemp(join(tmpdir(), "zcode-electron-recorder-"));
- tempDirs.push(tempDir);
- const outputPath = join(tempDir, "electron-repro.webm");
- const encodedFrameCounts: number[] = [];
- let captureAttempts = 0;
- const session = await startElectronWindowRecording({
- captureFrame: async () => {
- captureAttempts += 1;
- if (captureAttempts === 1) {
- return Buffer.from("first-frame");
- }
- throw new Error("capturePage became unavailable");
- },
- encodeVideo: async ({ framePaths, outputPath: encodedOutputPath }) => {
- encodedFrameCounts.push(framePaths.length);
- await writeFile(encodedOutputPath, "fake-webm");
- },
- frameIntervalMs: 100,
- outputPath,
- });
- await new Promise((resolveWait) => setTimeout(resolveWait, 240));
- const result = await session.stop();
- expect(result.frameCount).toBeGreaterThanOrEqual(3);
- expect(encodedFrameCounts).toEqual([result.frameCount]);
- expect(result.captureError).toContain("capturePage became unavailable");
- });
- it("bounds a capturePage request that never settles", async () => {
- const tempDir = await mkdtemp(join(tmpdir(), "zcode-electron-recorder-"));
- tempDirs.push(tempDir);
- const startedAt = Date.now();
- await expect(
- startElectronWindowRecording({
- captureFrame: () => new Promise<Buffer>(() => undefined),
- captureTimeoutMs: 30,
- outputPath: join(tempDir, "electron-repro.webm"),
- }),
- ).rejects.toThrow("frame capture timed out after 30ms");
- expect(Date.now() - startedAt).toBeLessThan(1_000);
- });
- it("bounds a custom encoder that never settles", async () => {
- const tempDir = await mkdtemp(join(tmpdir(), "zcode-electron-recorder-"));
- tempDirs.push(tempDir);
- const session = await startElectronWindowRecording({
- captureFrame: async () => Buffer.from("fake-png-frame"),
- encodeTimeoutMs: 30,
- encodeVideo: () => new Promise<void>(() => undefined),
- frameIntervalMs: 60_000,
- outputPath: join(tempDir, "electron-repro.webm"),
- stopTimeoutMs: 100,
- });
- await expect(session.stop()).rejects.toThrow("video encoding timed out after 30ms");
- });
- it("bounds the complete stop path even while tail recording", async () => {
- const tempDir = await mkdtemp(join(tmpdir(), "zcode-electron-recorder-"));
- tempDirs.push(tempDir);
- const session = await startElectronWindowRecording({
- captureFrame: async () => Buffer.from("fake-png-frame"),
- frameIntervalMs: 60_000,
- outputPath: join(tempDir, "electron-repro.webm"),
- stopTimeoutMs: 30,
- });
- await expect(session.stop({ tailDurationMs: 1_000 })).rejects.toThrow(
- "recording stop timed out after 30ms",
- );
- });
- it("kills a recording command that exceeds its timeout", async () => {
- await expect(
- runElectronRecordingCommand(process.execPath, ["-e", "setInterval(() => undefined, 1000)"], {
- timeoutMs: 100,
- }),
- ).rejects.toThrow("timed out after 100ms");
- });
- });
|