e2eReportingLifecycle.test.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
  2. import os from "node:os";
  3. import path from "node:path";
  4. import { afterEach, describe, expect, it } from "vitest";
  5. import { createE2EReporter } from "./e2e/reporting/e2e-reporting";
  6. import type {
  7. E2ELifecycleEvent,
  8. E2ELifecycleSummary,
  9. } from "./e2e/reporting/e2e-reporting-types.js";
  10. const createdDirs: string[] = [];
  11. async function createArtifactsDir(): Promise<string> {
  12. const dir = await mkdtemp(path.join(os.tmpdir(), "zcode-e2e-reporting-lifecycle-"));
  13. createdDirs.push(dir);
  14. return dir;
  15. }
  16. describe("desktop e2e reporting lifecycle", () => {
  17. afterEach(async () => {
  18. await Promise.all(
  19. createdDirs.splice(0).map((dir) => rm(dir, { force: true, recursive: true })),
  20. );
  21. });
  22. it("compacts completed process coverage after each case instead of waiting for worker exit", async () => {
  23. const artifactsDir = await createArtifactsDir();
  24. const previous = {
  25. coverage: process.env.ZCODE_E2E_COVERAGE,
  26. artifacts: process.env.ZCODE_E2E_ARTIFACT_DIR,
  27. };
  28. process.env.ZCODE_E2E_COVERAGE = "1";
  29. process.env.ZCODE_E2E_ARTIFACT_DIR = artifactsDir;
  30. try {
  31. const reporter = createE2EReporter({
  32. containerE2E: false,
  33. desktopDir: artifactsDir,
  34. repoRoot: process.cwd(),
  35. requestedSpecs: [],
  36. });
  37. await reporter.prepare([]);
  38. const rawFile = path.join(artifactsDir, "coverage", "raw", "main", "coverage-123.json");
  39. await writeFile(rawFile, JSON.stringify({ result: [] }));
  40. reporter.recordTestResult({ title: "case after reload" }, { duration: 1, passed: true });
  41. const raw = JSON.parse(await readFile(rawFile, "utf8"));
  42. expect(raw["zcode-e2e-normalized-source-map-root"]).toBe(
  43. path.join(artifactsDir, "out", "main").replaceAll("\\", "/"),
  44. );
  45. expect(raw.result).toEqual([]);
  46. const result = JSON.parse(
  47. (await readFile(path.join(artifactsDir, "test-results.ndjson"), "utf8")).trim(),
  48. );
  49. expect(result.passed).toBe(true);
  50. } finally {
  51. for (const [name, value] of [
  52. ["ZCODE_E2E_COVERAGE", previous.coverage],
  53. ["ZCODE_E2E_ARTIFACT_DIR", previous.artifacts],
  54. ]) {
  55. if (value === undefined) delete process.env[name!];
  56. else process.env[name!] = value;
  57. }
  58. }
  59. });
  60. it("appends lifecycle ndjson and aggregates by phase", async () => {
  61. const artifactsDir = await createArtifactsDir();
  62. process.env.ZCODE_E2E_ARTIFACT_DIR = artifactsDir;
  63. const reporter = createE2EReporter({
  64. containerE2E: false,
  65. desktopDir: artifactsDir,
  66. repoRoot: process.cwd(),
  67. requestedSpecs: [],
  68. }) as {
  69. prepare: (specs: string[]) => Promise<void> | void;
  70. recordLifecycleEvent?: (event: E2ELifecycleEvent) => void;
  71. writeSummary: (exitCode: number) => Promise<void> | void;
  72. };
  73. await reporter.prepare([
  74. "./test/e2e/infrastructure/smoke/container-boot.test.ts",
  75. "./test/e2e/conversation-session/composer/conversation-session-first-send.test.ts",
  76. ]);
  77. expect(reporter.recordLifecycleEvent).toBeTypeOf("function");
  78. reporter.recordLifecycleEvent?.({
  79. completedAt: "2026-07-10T00:00:01.000Z",
  80. durationMs: 10,
  81. phase: "prepare",
  82. specs: ["./test/e2e/infrastructure/smoke/container-boot.test.ts"],
  83. startedAt: "2026-07-10T00:00:00.990Z",
  84. });
  85. reporter.recordLifecycleEvent?.({
  86. cid: "worker-1",
  87. completedAt: "2026-07-10T00:00:03.000Z",
  88. durationMs: 20,
  89. phase: "before-session",
  90. specs: ["./test/e2e/infrastructure/smoke/container-boot.test.ts"],
  91. startedAt: "2026-07-10T00:00:02.980Z",
  92. });
  93. reporter.recordLifecycleEvent?.({
  94. cid: "worker-1",
  95. completedAt: "2026-07-10T00:00:04.000Z",
  96. durationMs: 30,
  97. phase: "before-session",
  98. specs: ["./test/e2e/infrastructure/smoke/container-boot.test.ts"],
  99. startedAt: "2026-07-10T00:00:03.970Z",
  100. });
  101. reporter.recordLifecycleEvent?.({
  102. cid: "worker-1",
  103. completedAt: "2026-07-10T00:00:05.000Z",
  104. durationMs: 40,
  105. phase: "worker",
  106. specs: ["./test/e2e/infrastructure/smoke/container-boot.test.ts"],
  107. startedAt: "2026-07-10T00:00:04.960Z",
  108. });
  109. await reporter.writeSummary(0);
  110. const lifecycleEventsPath = path.join(artifactsDir, "lifecycle-events.ndjson");
  111. const summaryPath = path.join(artifactsDir, "summary.json");
  112. const lifecycleEvents = await readFile(lifecycleEventsPath, "utf8");
  113. expect(lifecycleEvents.trim().split("\n")).toHaveLength(4);
  114. const summary = JSON.parse(await readFile(summaryPath, "utf8")) as {
  115. lifecycle?: E2ELifecycleSummary;
  116. };
  117. expect(summary.lifecycle?.byPhase?.prepare).toEqual({
  118. count: 1,
  119. p50Ms: 10,
  120. p95Ms: 10,
  121. totalMs: 10,
  122. });
  123. expect(summary.lifecycle?.byPhase?.["before-session"]).toEqual({
  124. count: 2,
  125. p50Ms: 20,
  126. p95Ms: 30,
  127. totalMs: 50,
  128. });
  129. expect(summary.lifecycle?.byPhase?.worker).toEqual({
  130. count: 1,
  131. p50Ms: 40,
  132. p95Ms: 40,
  133. totalMs: 40,
  134. });
  135. });
  136. it("keeps lifecycle events even when summary is never written", async () => {
  137. const artifactsDir = await createArtifactsDir();
  138. process.env.ZCODE_E2E_ARTIFACT_DIR = artifactsDir;
  139. const reporter = createE2EReporter({
  140. containerE2E: false,
  141. desktopDir: artifactsDir,
  142. repoRoot: process.cwd(),
  143. requestedSpecs: [],
  144. }) as {
  145. prepare: (specs: string[]) => Promise<void> | void;
  146. recordLifecycleEvent?: (event: {
  147. completedAt: string;
  148. durationMs: number;
  149. phase: "after";
  150. specs: string[];
  151. startedAt: string;
  152. }) => void;
  153. };
  154. await reporter.prepare(["./test/e2e/infrastructure/smoke/container-boot.test.ts"]);
  155. expect(reporter.recordLifecycleEvent).toBeTypeOf("function");
  156. reporter.recordLifecycleEvent?.({
  157. completedAt: "2026-07-10T00:00:05.000Z",
  158. durationMs: 40,
  159. phase: "after",
  160. specs: ["./test/e2e/infrastructure/smoke/container-boot.test.ts"],
  161. startedAt: "2026-07-10T00:00:04.960Z",
  162. });
  163. const lifecycleEvents = await readFile(
  164. path.join(artifactsDir, "lifecycle-events.ndjson"),
  165. "utf8",
  166. );
  167. expect(lifecycleEvents.trim().split("\n")).toHaveLength(1);
  168. });
  169. it("records bounded failure-artifact collection errors without replacing the test result", async () => {
  170. const artifactsDir = await createArtifactsDir();
  171. process.env.ZCODE_E2E_ARTIFACT_DIR = artifactsDir;
  172. const reporter = createE2EReporter({
  173. containerE2E: false,
  174. desktopDir: artifactsDir,
  175. repoRoot: process.cwd(),
  176. requestedSpecs: [],
  177. });
  178. await reporter.prepare(["./test/e2e/infrastructure/smoke/container-boot.test.ts"]);
  179. reporter.recordTestResult(
  180. { title: "keeps the original assertion" },
  181. {
  182. duration: 12,
  183. error: new Error("original assertion failed"),
  184. passed: false,
  185. status: "failed",
  186. },
  187. undefined,
  188. ["failure artifacts: collection timed out after 60000ms"],
  189. );
  190. const result = JSON.parse(
  191. (await readFile(path.join(artifactsDir, "test-results.ndjson"), "utf8")).trim(),
  192. ) as {
  193. collectionErrors?: string[];
  194. error?: string;
  195. passed: boolean;
  196. };
  197. expect(result).toMatchObject({
  198. collectionErrors: ["failure artifacts: collection timed out after 60000ms"],
  199. error: "original assertion failed",
  200. passed: false,
  201. });
  202. });
  203. });