browserRecordingArtifactMaterializer.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { 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 { materializeBrowserRecordingArtifact } from "../src/host/browserRecordingArtifactMaterializer.js";
  6. const tempRoots: string[] = [];
  7. const artifact = {
  8. path: "/main/tmp/recording.webm",
  9. mimeType: "video/webm" as const,
  10. width: 1280,
  11. height: 720,
  12. fps: 25,
  13. durationMs: 1_000,
  14. frameCount: 25,
  15. };
  16. afterEach(async () => {
  17. await Promise.all(tempRoots.splice(0).map((path) => rm(path, { recursive: true, force: true })));
  18. });
  19. describe("browserRecordingArtifactMaterializer", () => {
  20. it("原子覆盖工作区内已有 WebM,并返回实际绝对路径", async () => {
  21. const root = await mkdtemp(join(tmpdir(), "zcode-recording-materialize-"));
  22. tempRoots.push(root);
  23. const source = join(root, "source.webm");
  24. await writeFile(source, "new-video");
  25. await writeFile(join(root, "demo.webm"), "old-video");
  26. const result = await materializeBrowserRecordingArtifact({
  27. artifact,
  28. localPath: source,
  29. outputPath: "demo.webm",
  30. workspacePath: root,
  31. });
  32. expect(result.path).toBe(join(root, "demo.webm"));
  33. await expect(readFile(result.path, "utf8")).resolves.toBe("new-video");
  34. });
  35. it("拒绝本地和远端 outputPath 越出 workspace", async () => {
  36. const root = await mkdtemp(join(tmpdir(), "zcode-recording-materialize-"));
  37. tempRoots.push(root);
  38. await expect(
  39. materializeBrowserRecordingArtifact({
  40. artifact,
  41. localPath: join(root, "source.webm"),
  42. outputPath: "../escape.webm",
  43. workspacePath: root,
  44. }),
  45. ).rejects.toThrow("inside the workspace");
  46. await expect(
  47. materializeBrowserRecordingArtifact({
  48. artifact,
  49. localPath: join(root, "source.webm"),
  50. outputPath: "../escape.webm",
  51. workspacePath: "/remote/repo",
  52. remoteSessionId: "ssh:one",
  53. remoteBackend: { upload: vi.fn() },
  54. }),
  55. ).rejects.toThrow("inside the remote workspace");
  56. });
  57. it("本地和远端 materializer 都拒绝旧 MP4 扩展名", async () => {
  58. const root = await mkdtemp(join(tmpdir(), "zcode-recording-materialize-"));
  59. tempRoots.push(root);
  60. await expect(
  61. materializeBrowserRecordingArtifact({
  62. artifact,
  63. localPath: join(root, "source.webm"),
  64. outputPath: "recordings/legacy.mp4",
  65. workspacePath: root,
  66. }),
  67. ).rejects.toThrow("must end with .webm");
  68. await expect(
  69. materializeBrowserRecordingArtifact({
  70. artifact,
  71. localPath: join(root, "source.webm"),
  72. outputPath: "recordings/legacy.mp4",
  73. workspacePath: "/remote/repo",
  74. remoteSessionId: "ssh:one",
  75. remoteBackend: { upload: vi.fn() },
  76. }),
  77. ).rejects.toThrow("must end with .webm");
  78. });
  79. it("远端 workspace 复用 backend.upload,不把 main 临时路径暴露给 Agent", async () => {
  80. const upload = vi.fn(async () => undefined);
  81. const result = await materializeBrowserRecordingArtifact({
  82. artifact,
  83. localPath: "/main/tmp/recording.webm",
  84. outputPath: "recordings/demo.webm",
  85. workspacePath: "/remote/repo",
  86. remoteSessionId: "ssh:one",
  87. remoteBackend: { upload },
  88. });
  89. expect(upload).toHaveBeenCalledWith(
  90. "/main/tmp/recording.webm",
  91. "/remote/repo/recordings/demo.webm",
  92. );
  93. expect(result.path).toBe("/remote/repo/recordings/demo.webm");
  94. });
  95. });