e2eFailureArtifacts.test.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { mkdir, mkdtemp, readdir, 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 } from "vitest";
  5. import {
  6. E2EFailureArtifactCollector,
  7. materializeLogDeltas,
  8. snapshotLogOffsets,
  9. } from "./e2e/reporting/e2e-failure-artifacts.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("E2E failure artifacts", () => {
  15. it("copies only log bytes emitted by the current case", async () => {
  16. const tempDir = await mkdtemp(join(tmpdir(), "zcode-e2e-failure-artifacts-"));
  17. tempDirs.push(tempDir);
  18. const runtimeLogs = join(tempDir, "runtime-logs");
  19. const outputDir = join(tempDir, "failure", "logs");
  20. const desktopLog = join(runtimeLogs, "desktop", "2026-07-13.log");
  21. const agentLog = join(runtimeLogs, "agent", "zcode-2026-07-13.jsonl");
  22. await mkdir(join(runtimeLogs, "desktop"), { recursive: true });
  23. await mkdir(join(runtimeLogs, "agent"), { recursive: true });
  24. await writeFile(desktopLog, "before-desktop\n", { encoding: "utf-8", flag: "w" });
  25. const offsets = await snapshotLogOffsets(runtimeLogs);
  26. await writeFile(desktopLog, "case-desktop\n", { encoding: "utf-8", flag: "a" });
  27. await writeFile(agentLog, "case-agent\n", { encoding: "utf-8", flag: "w" });
  28. const copied = await materializeLogDeltas({
  29. afterRoot: runtimeLogs,
  30. beforeOffsets: offsets,
  31. outputDir,
  32. });
  33. expect(copied).toEqual([
  34. { path: join("agent", "zcode-2026-07-13.jsonl"), truncated: false },
  35. { path: join("desktop", "2026-07-13.log"), truncated: false },
  36. ]);
  37. await expect(readFile(join(outputDir, "desktop", "2026-07-13.log"), "utf-8")).resolves.toBe(
  38. "case-desktop\n",
  39. );
  40. await expect(
  41. readFile(join(outputDir, "agent", "zcode-2026-07-13.jsonl"), "utf-8"),
  42. ).resolves.toBe("case-agent\n");
  43. });
  44. it("discards the capture of a skipped case instead of filing it as a failure", async () => {
  45. const tempDir = await mkdtemp(join(tmpdir(), "zcode-e2e-failure-artifacts-"));
  46. tempDirs.push(tempDir);
  47. // 录像依赖真实 Electron;假 browser 让录像失败并被记为采集错误,不影响跳过判定。
  48. const collector = new E2EFailureArtifactCollector({
  49. artifactDir: tempDir,
  50. browser: {} as WebdriverIO.Browser,
  51. enabled: true,
  52. workerId: "0-0",
  53. });
  54. const test = { title: "windows only case" };
  55. await collector.start(test);
  56. const refs = await collector.complete(test, {
  57. duration: 1,
  58. passed: false,
  59. status: "skipped",
  60. });
  61. expect(refs).toBeUndefined();
  62. const leftovers = await readdir(join(tempDir, "failures"), { recursive: true });
  63. expect(leftovers.filter((entry) => entry.includes("attempt-0"))).toEqual([]);
  64. });
  65. });