e2eElectronWindowRecorder.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { access, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import { startElectronWindowRecording } from "./e2e/helpers/electron-window-recorder.js";
  6. import { runElectronRecordingCommand } from "./e2e/helpers/electron-window-recorder-timeout.js";
  7. const tempDirs: string[] = [];
  8. afterEach(async () => {
  9. await Promise.all(tempDirs.splice(0).map((dir) => rm(dir, { force: true, recursive: true })));
  10. });
  11. describe("Electron window recorder", () => {
  12. it("records renderer frames through Electron capturePage metadata", async () => {
  13. const tempDir = await mkdtemp(join(tmpdir(), "zcode-electron-recorder-"));
  14. tempDirs.push(tempDir);
  15. const outputPath = join(tempDir, "electron-repro.webm");
  16. const encodedFrameCounts: number[] = [];
  17. const session = await startElectronWindowRecording({
  18. captureFrame: async () => Buffer.from("fake-png-frame"),
  19. encodeVideo: async ({ framePaths, outputPath: encodedOutputPath }) => {
  20. encodedFrameCounts.push(framePaths.length);
  21. await readFile(framePaths[0]);
  22. await writeFile(encodedOutputPath, "fake-webm");
  23. },
  24. frameIntervalMs: 60_000,
  25. outputPath,
  26. });
  27. const result = await session.stop();
  28. expect(result.captureMode).toBe("electron_capture_page");
  29. expect(result.videoPath).toBe(outputPath);
  30. expect(result.frameCount).toBeGreaterThanOrEqual(2);
  31. expect(encodedFrameCounts).toEqual([result.frameCount]);
  32. await expect(readFile(outputPath, "utf8")).resolves.toBe("fake-webm");
  33. });
  34. it("discards passing-case frames without invoking the encoder", async () => {
  35. const tempDir = await mkdtemp(join(tmpdir(), "zcode-electron-recorder-"));
  36. tempDirs.push(tempDir);
  37. const outputPath = join(tempDir, "electron-repro.webm");
  38. const frameDir = join(tempDir, "frames");
  39. const encodeVideo = vi.fn();
  40. const session = await startElectronWindowRecording({
  41. captureFrame: async () => Buffer.from("fake-png-frame"),
  42. encodeVideo,
  43. frameDir,
  44. frameIntervalMs: 60_000,
  45. outputPath,
  46. });
  47. await session.discard();
  48. expect(encodeVideo).not.toHaveBeenCalled();
  49. await expect(access(frameDir)).rejects.toMatchObject({ code: "ENOENT" });
  50. await expect(access(outputPath)).rejects.toMatchObject({ code: "ENOENT" });
  51. });
  52. it("collects a pre-roll before allowing the case to start", async () => {
  53. const tempDir = await mkdtemp(join(tmpdir(), "zcode-electron-recorder-"));
  54. tempDirs.push(tempDir);
  55. const startedAt = Date.now();
  56. const session = await startElectronWindowRecording({
  57. captureFrame: async () => Buffer.from("fake-png-frame"),
  58. frameIntervalMs: 100,
  59. outputPath: join(tempDir, "electron-repro.webm"),
  60. preRollDurationMs: 120,
  61. });
  62. expect(Date.now() - startedAt).toBeGreaterThanOrEqual(100);
  63. await session.discard();
  64. });
  65. it("keeps the video timeline aligned with the case when later captures are missed", async () => {
  66. const tempDir = await mkdtemp(join(tmpdir(), "zcode-electron-recorder-"));
  67. tempDirs.push(tempDir);
  68. const outputPath = join(tempDir, "electron-repro.webm");
  69. const encodedFrameCounts: number[] = [];
  70. let captureAttempts = 0;
  71. const session = await startElectronWindowRecording({
  72. captureFrame: async () => {
  73. captureAttempts += 1;
  74. if (captureAttempts === 1) {
  75. return Buffer.from("first-frame");
  76. }
  77. throw new Error("capturePage became unavailable");
  78. },
  79. encodeVideo: async ({ framePaths, outputPath: encodedOutputPath }) => {
  80. encodedFrameCounts.push(framePaths.length);
  81. await writeFile(encodedOutputPath, "fake-webm");
  82. },
  83. frameIntervalMs: 100,
  84. outputPath,
  85. });
  86. await new Promise((resolveWait) => setTimeout(resolveWait, 240));
  87. const result = await session.stop();
  88. expect(result.frameCount).toBeGreaterThanOrEqual(3);
  89. expect(encodedFrameCounts).toEqual([result.frameCount]);
  90. expect(result.captureError).toContain("capturePage became unavailable");
  91. });
  92. it("bounds a capturePage request that never settles", async () => {
  93. const tempDir = await mkdtemp(join(tmpdir(), "zcode-electron-recorder-"));
  94. tempDirs.push(tempDir);
  95. const startedAt = Date.now();
  96. await expect(
  97. startElectronWindowRecording({
  98. captureFrame: () => new Promise<Buffer>(() => undefined),
  99. captureTimeoutMs: 30,
  100. outputPath: join(tempDir, "electron-repro.webm"),
  101. }),
  102. ).rejects.toThrow("frame capture timed out after 30ms");
  103. expect(Date.now() - startedAt).toBeLessThan(1_000);
  104. });
  105. it("bounds a custom encoder that never settles", async () => {
  106. const tempDir = await mkdtemp(join(tmpdir(), "zcode-electron-recorder-"));
  107. tempDirs.push(tempDir);
  108. const session = await startElectronWindowRecording({
  109. captureFrame: async () => Buffer.from("fake-png-frame"),
  110. encodeTimeoutMs: 30,
  111. encodeVideo: () => new Promise<void>(() => undefined),
  112. frameIntervalMs: 60_000,
  113. outputPath: join(tempDir, "electron-repro.webm"),
  114. stopTimeoutMs: 100,
  115. });
  116. await expect(session.stop()).rejects.toThrow("video encoding timed out after 30ms");
  117. });
  118. it("bounds the complete stop path even while tail recording", async () => {
  119. const tempDir = await mkdtemp(join(tmpdir(), "zcode-electron-recorder-"));
  120. tempDirs.push(tempDir);
  121. const session = await startElectronWindowRecording({
  122. captureFrame: async () => Buffer.from("fake-png-frame"),
  123. frameIntervalMs: 60_000,
  124. outputPath: join(tempDir, "electron-repro.webm"),
  125. stopTimeoutMs: 30,
  126. });
  127. await expect(session.stop({ tailDurationMs: 1_000 })).rejects.toThrow(
  128. "recording stop timed out after 30ms",
  129. );
  130. });
  131. it("kills a recording command that exceeds its timeout", async () => {
  132. await expect(
  133. runElectronRecordingCommand(process.execPath, ["-e", "setInterval(() => undefined, 1000)"], {
  134. timeoutMs: 100,
  135. }),
  136. ).rejects.toThrow("timed out after 100ms");
  137. });
  138. });