e2eNodeCoverageNormalization.test.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
  2. import { tmpdir } from "node:os";
  3. import { resolve } from "node:path";
  4. import { pathToFileURL } from "node:url";
  5. import { mkdtemp } from "node:fs/promises";
  6. import { describe, expect, it } from "vitest";
  7. import {
  8. compactV8CoverageDirectory,
  9. createC8ConversionConfig,
  10. normalizeV8CoveragePayload,
  11. } from "./e2e/reporting/e2e-node-coverage.js";
  12. describe("E2E Node coverage normalization", () => {
  13. it("keeps only generated bundles that can map back to the selected first-party domain", async () => {
  14. const fixtureRoot = await mkdtemp(resolve(tmpdir(), "zcode-e2e-node-coverage-"));
  15. const sourceMapRoot = resolve(fixtureRoot, "out/main");
  16. const canonicalBundle = resolve(sourceMapRoot, "index.js");
  17. mkdirSync(sourceMapRoot, { recursive: true });
  18. writeFileSync(canonicalBundle, "export const covered = true;\n", "utf-8");
  19. writeFileSync(
  20. `${canonicalBundle}.map`,
  21. JSON.stringify({
  22. mappings: "AAAA",
  23. names: [],
  24. sources: ["../../../src/main/index.ts"],
  25. version: 3,
  26. }),
  27. "utf-8",
  28. );
  29. const runtimeBundleUrl = pathToFileURL(
  30. resolve(fixtureRoot, "staged/app/main/index.js"),
  31. ).href;
  32. const dependencyUrl = pathToFileURL(
  33. resolve(fixtureRoot, "node_modules/dependency/index.js"),
  34. ).href;
  35. const payload = {
  36. result: [
  37. { functions: [], scriptId: "1", url: runtimeBundleUrl },
  38. { functions: [], scriptId: "2", url: dependencyUrl },
  39. { functions: [], scriptId: "3", url: "node:internal/process/task_queues" },
  40. ],
  41. "source-map-cache": {
  42. [runtimeBundleUrl]: { data: null, lineLengths: [1], url: null },
  43. [dependencyUrl]: { data: null, lineLengths: [1], url: null },
  44. },
  45. };
  46. normalizeV8CoveragePayload(payload, sourceMapRoot);
  47. expect(payload.result).toEqual([
  48. expect.objectContaining({ scriptId: "1", url: pathToFileURL(canonicalBundle).href }),
  49. ]);
  50. expect(Object.keys(payload["source-map-cache"])).toEqual([
  51. pathToFileURL(canonicalBundle).href,
  52. ]);
  53. expect(
  54. payload["source-map-cache"][pathToFileURL(canonicalBundle).href]?.data?.sources,
  55. ).toEqual([pathToFileURL(resolve(sourceMapRoot, "../../../src/main/index.ts")).href]);
  56. expect(readFileSync(`${canonicalBundle}.map`, "utf-8")).toContain("../../../src/main/index.ts");
  57. normalizeV8CoveragePayload(payload, sourceMapRoot, { includeSourceMapCache: false });
  58. expect(payload.result).toHaveLength(1);
  59. expect(payload["source-map-cache"]).toBeUndefined();
  60. });
  61. it("asks c8 to merge large multi-process runs incrementally", () => {
  62. expect(createC8ConversionConfig("/tmp/raw", "/tmp/reports")).toMatchObject({
  63. "merge-async": true,
  64. "reports-dir": "/tmp/reports",
  65. "temp-directory": "/tmp/raw",
  66. });
  67. });
  68. it("compacts each finished worker once and retains one source map per unique bundle", async () => {
  69. const fixtureRoot = await mkdtemp(resolve(tmpdir(), "zcode-e2e-node-compact-"));
  70. const rawDir = resolve(fixtureRoot, "raw");
  71. const sourceMapRoot = resolve(fixtureRoot, "out/main");
  72. const canonicalBundle = resolve(sourceMapRoot, "index.js");
  73. const canonicalChunk = resolve(sourceMapRoot, "lazy-chunk.js");
  74. mkdirSync(rawDir, { recursive: true });
  75. mkdirSync(sourceMapRoot, { recursive: true });
  76. writeFileSync(canonicalBundle, "export const covered = true;\n", "utf-8");
  77. writeFileSync(canonicalChunk, "export const lazy = true;\n", "utf-8");
  78. for (const [bundle, source] of [
  79. [canonicalBundle, "../../../src/main/index.ts"],
  80. [canonicalChunk, "../../../src/main/lazy.ts"],
  81. ]) {
  82. writeFileSync(
  83. `${bundle}.map`,
  84. JSON.stringify({ mappings: "AAAA", names: [], sources: [source], version: 3 }),
  85. "utf-8",
  86. );
  87. }
  88. const writeRaw = (pid: number, bundleName: "index" | "lazy-chunk") => {
  89. const runtimeBundleUrl = pathToFileURL(
  90. resolve(fixtureRoot, `staged/app/main/${bundleName}.js`),
  91. ).href;
  92. const dependencyUrl = pathToFileURL(
  93. resolve(fixtureRoot, `node_modules/dependency-${pid}/index.js`),
  94. ).href;
  95. writeFileSync(
  96. resolve(rawDir, `coverage-${pid}-1-0.json`),
  97. JSON.stringify({
  98. result: [
  99. { functions: [], scriptId: "1", url: runtimeBundleUrl },
  100. { functions: [], scriptId: "2", url: dependencyUrl },
  101. ],
  102. "source-map-cache": {
  103. [runtimeBundleUrl]: { data: null, lineLengths: [1], url: null },
  104. [dependencyUrl]: { data: null, lineLengths: [1], url: null },
  105. },
  106. }),
  107. "utf-8",
  108. );
  109. };
  110. writeRaw(101, "index");
  111. writeRaw(102, "lazy-chunk");
  112. expect(compactV8CoverageDirectory({ rawDir, sourceMapRoot })).toMatchObject({
  113. compactedFileCount: 2,
  114. sourceMapCacheRetained: true,
  115. });
  116. const first = JSON.parse(
  117. readFileSync(resolve(rawDir, "coverage-101-1-0.json"), "utf-8"),
  118. ) as { result: unknown[]; "source-map-cache"?: object };
  119. const second = JSON.parse(
  120. readFileSync(resolve(rawDir, "coverage-102-1-0.json"), "utf-8"),
  121. ) as { result: unknown[]; "source-map-cache"?: object };
  122. expect(first.result).toHaveLength(1);
  123. expect(second.result).toHaveLength(1);
  124. expect(
  125. [first["source-map-cache"], second["source-map-cache"]]
  126. .flatMap((cache) => Object.keys(cache ?? {})),
  127. ).toHaveLength(2);
  128. expect(
  129. readFileSync(resolve(rawDir, ".zcode-compacted-coverage-files-v1.marker"), "utf-8"),
  130. ).toContain("coverage-101-1-0.json");
  131. const alreadyCompactedPath = resolve(rawDir, "coverage-101-1-0.json");
  132. const alreadyCompactedSentinel = JSON.stringify({
  133. result: [{ functions: [], scriptId: "sentinel", url: "sentinel://do-not-read-again" }],
  134. });
  135. writeFileSync(alreadyCompactedPath, alreadyCompactedSentinel, "utf-8");
  136. writeRaw(103, "index");
  137. expect(compactV8CoverageDirectory({ rawDir, sourceMapRoot })).toMatchObject({
  138. compactedFileCount: 1,
  139. sourceMapCacheRetained: true,
  140. });
  141. expect(readFileSync(alreadyCompactedPath, "utf-8")).toBe(alreadyCompactedSentinel);
  142. const third = JSON.parse(
  143. readFileSync(resolve(rawDir, "coverage-103-1-0.json"), "utf-8"),
  144. ) as { "source-map-cache"?: object };
  145. expect(third["source-map-cache"]).toBeUndefined();
  146. expect(compactV8CoverageDirectory({ rawDir, sourceMapRoot }).compactedFileCount).toBe(0);
  147. });
  148. });