e2eElectronWindowRecorder.test.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 {
  6. isElectronRecordingEncoderAvailable,
  7. startElectronWindowRecording,
  8. } from "./e2e/helpers/electron-window-recorder.js";
  9. import { runElectronRecordingCommand } from "./e2e/helpers/electron-window-recorder-timeout.js";
  10. const tempDirs: string[] = [];
  11. afterEach(async () => {
  12. await Promise.all(tempDirs.splice(0).map((dir) => rm(dir, { force: true, recursive: true })));
  13. });
  14. describe("Electron window recorder", () => {
  15. it("records renderer frames through Electron capturePage metadata", async () => {
  16. const tempDir = await mkdtemp(join(tmpdir(), "zcode-electron-recorder-"));
  17. tempDirs.push(tempDir);
  18. const outputPath = join(tempDir, "electron-repro.webm");
  19. const encodedFrameCounts: number[] = [];
  20. const session = await startElectronWindowRecording({
  21. captureFrame: async () => Buffer.from("fake-png-frame"),
  22. encodeVideo: async ({ framePaths, outputPath: encodedOutputPath }) => {
  23. encodedFrameCounts.push(framePaths.length);
  24. await readFile(framePaths[0]);
  25. await writeFile(encodedOutputPath, "fake-webm");
  26. },
  27. frameIntervalMs: 60_000,
  28. outputPath,
  29. });
  30. const result = await session.stop();
  31. expect(result.captureMode).toBe("electron_capture_page");
  32. expect(result.videoPath).toBe(outputPath);
  33. expect(result.frameCount).toBeGreaterThanOrEqual(2);
  34. expect(encodedFrameCounts).toEqual([result.frameCount]);
  35. await expect(readFile(outputPath, "utf8")).resolves.toBe("fake-webm");
  36. });
  37. it("discards passing-case frames without invoking the encoder", async () => {
  38. const tempDir = await mkdtemp(join(tmpdir(), "zcode-electron-recorder-"));
  39. tempDirs.push(tempDir);
  40. const outputPath = join(tempDir, "electron-repro.webm");
  41. const frameDir = join(tempDir, "frames");
  42. const encodeVideo = vi.fn();
  43. const session = await startElectronWindowRecording({
  44. captureFrame: async () => Buffer.from("fake-png-frame"),
  45. encodeVideo,
  46. frameDir,
  47. frameIntervalMs: 60_000,
  48. outputPath,
  49. });
  50. await session.discard();
  51. expect(encodeVideo).not.toHaveBeenCalled();
  52. await expect(access(frameDir)).rejects.toMatchObject({ code: "ENOENT" });
  53. await expect(access(outputPath)).rejects.toMatchObject({ code: "ENOENT" });
  54. });
  55. it("collects a pre-roll before allowing the case to start", async () => {
  56. const tempDir = await mkdtemp(join(tmpdir(), "zcode-electron-recorder-"));
  57. tempDirs.push(tempDir);
  58. const startedAt = Date.now();
  59. const session = await startElectronWindowRecording({
  60. captureFrame: async () => Buffer.from("fake-png-frame"),
  61. frameIntervalMs: 100,
  62. outputPath: join(tempDir, "electron-repro.webm"),
  63. preRollDurationMs: 120,
  64. });
  65. expect(Date.now() - startedAt).toBeGreaterThanOrEqual(100);
  66. await session.discard();
  67. });
  68. it("keeps the video timeline aligned with the case when later captures are missed", async () => {
  69. const tempDir = await mkdtemp(join(tmpdir(), "zcode-electron-recorder-"));
  70. tempDirs.push(tempDir);
  71. const outputPath = join(tempDir, "electron-repro.webm");
  72. const encodedFrameCounts: number[] = [];
  73. let captureAttempts = 0;
  74. const session = await startElectronWindowRecording({
  75. captureFrame: async () => {
  76. captureAttempts += 1;
  77. if (captureAttempts === 1) {
  78. return Buffer.from("first-frame");
  79. }
  80. throw new Error("capturePage became unavailable");
  81. },
  82. encodeVideo: async ({ framePaths, outputPath: encodedOutputPath }) => {
  83. encodedFrameCounts.push(framePaths.length);
  84. await writeFile(encodedOutputPath, "fake-webm");
  85. },
  86. frameIntervalMs: 100,
  87. outputPath,
  88. });
  89. await new Promise((resolveWait) => setTimeout(resolveWait, 240));
  90. const result = await session.stop();
  91. expect(result.frameCount).toBeGreaterThanOrEqual(3);
  92. expect(encodedFrameCounts).toEqual([result.frameCount]);
  93. expect(result.captureError).toContain("capturePage became unavailable");
  94. });
  95. it("bounds a capturePage request that never settles", async () => {
  96. const tempDir = await mkdtemp(join(tmpdir(), "zcode-electron-recorder-"));
  97. tempDirs.push(tempDir);
  98. const startedAt = Date.now();
  99. await expect(
  100. startElectronWindowRecording({
  101. captureFrame: () => new Promise<Buffer>(() => undefined),
  102. captureTimeoutMs: 30,
  103. outputPath: join(tempDir, "electron-repro.webm"),
  104. }),
  105. ).rejects.toThrow("frame capture timed out after 30ms");
  106. expect(Date.now() - startedAt).toBeLessThan(1_000);
  107. });
  108. it("bounds a custom encoder that never settles", async () => {
  109. const tempDir = await mkdtemp(join(tmpdir(), "zcode-electron-recorder-"));
  110. tempDirs.push(tempDir);
  111. const session = await startElectronWindowRecording({
  112. captureFrame: async () => Buffer.from("fake-png-frame"),
  113. encodeTimeoutMs: 30,
  114. encodeVideo: () => new Promise<void>(() => undefined),
  115. frameIntervalMs: 60_000,
  116. outputPath: join(tempDir, "electron-repro.webm"),
  117. stopTimeoutMs: 100,
  118. });
  119. await expect(session.stop()).rejects.toThrow("video encoding timed out after 30ms");
  120. });
  121. it("bounds the complete stop path even while tail recording", async () => {
  122. const tempDir = await mkdtemp(join(tmpdir(), "zcode-electron-recorder-"));
  123. tempDirs.push(tempDir);
  124. const session = await startElectronWindowRecording({
  125. captureFrame: async () => Buffer.from("fake-png-frame"),
  126. frameIntervalMs: 60_000,
  127. outputPath: join(tempDir, "electron-repro.webm"),
  128. stopTimeoutMs: 30,
  129. });
  130. await expect(session.stop({ tailDurationMs: 1_000 })).rejects.toThrow(
  131. "recording stop timed out after 30ms",
  132. );
  133. });
  134. it("reports a missing encoder instead of failing the case at stop time", async () => {
  135. // CI runner 没有 ffmpeg 时,录屏只是证据通道,调用方据此跳过录屏而不是让行为用例失败。
  136. await expect(
  137. isElectronRecordingEncoderAvailable(join(tmpdir(), "zcode-missing-ffmpeg-binary")),
  138. ).resolves.toBe(false);
  139. });
  140. it("treats an encoder whose -version exits non-zero as unavailable", async () => {
  141. // 回归:探测曾在 spawn 成功后立即返回 true;node 能启动但 `node -version` 以非零码退出,
  142. // 这类损坏或错配的编码器会在 stop() 编码时才失败,把证据通道问题记成行为失败。
  143. await expect(isElectronRecordingEncoderAvailable(process.execPath)).resolves.toBe(false);
  144. });
  145. it("kills a recording command that exceeds its timeout", async () => {
  146. await expect(
  147. runElectronRecordingCommand(process.execPath, ["-e", "setInterval(() => undefined, 1000)"], {
  148. timeoutMs: 100,
  149. }),
  150. ).rejects.toThrow("timed out after 100ms");
  151. });
  152. });