e2eCoverageHeatmap.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { mkdtemp, mkdir, 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 {
  6. buildCoverageHeatmapData,
  7. writeE2ECoverageHeatmap,
  8. } from "./e2e/reporting/e2e-coverage-heatmap";
  9. const createdDirs: string[] = [];
  10. async function createFixture() {
  11. const root = await mkdtemp(path.join(os.tmpdir(), "zcode-e2e-heatmap-"));
  12. createdDirs.push(root);
  13. const repoRoot = path.join(root, "repo");
  14. const coverageDir = path.join(root, "coverage");
  15. await mkdir(path.join(coverageDir, "main"), { recursive: true });
  16. await writeFile(
  17. path.join(coverageDir, "coverage-final.json"),
  18. JSON.stringify({
  19. [path.join(repoRoot, "packages/ui/src/hot.ts")]: {
  20. f: { 0: 12, 1: 0 },
  21. fnMap: {
  22. 0: { loc: { start: { line: 4 } }, name: "hot" },
  23. 1: { loc: { start: { line: 8 } }, name: "cold" },
  24. },
  25. },
  26. [path.join(repoRoot, "packages/ui/src/warm.ts")]: {
  27. f: { 0: 3 },
  28. fnMap: { 0: { loc: { start: { line: 2 } }, name: "warm" } },
  29. },
  30. }),
  31. "utf-8",
  32. );
  33. await writeFile(
  34. path.join(coverageDir, "main/coverage-final.json"),
  35. JSON.stringify({
  36. [path.join(repoRoot, "packages/desktop/src/main/index.ts")]: {
  37. f: { 0: 5 },
  38. fnMap: { 0: { loc: { start: { line: 10 } }, name: "start" } },
  39. },
  40. }),
  41. "utf-8",
  42. );
  43. return { coverageDir, repoRoot };
  44. }
  45. describe("desktop E2E coverage heatmap", () => {
  46. afterEach(async () => {
  47. await Promise.all(
  48. createdDirs.splice(0).map((dir) => rm(dir, { force: true, recursive: true })),
  49. );
  50. });
  51. it("aggregates function hit counters per runtime domain and file", async () => {
  52. const fixture = await createFixture();
  53. const data = buildCoverageHeatmapData(fixture);
  54. expect(data.domains.map((domain) => domain.domain)).toEqual(["renderer", "main"]);
  55. expect(data.domains[0]).toMatchObject({
  56. coveredFunctions: 2,
  57. functionHits: 15,
  58. functions: 3,
  59. });
  60. expect(data.domains[0]?.files[0]).toMatchObject({
  61. coveredFunctions: 1,
  62. functionHits: 12,
  63. functions: 2,
  64. hottestFunction: {
  65. hits: 12,
  66. line: 4,
  67. name: "hot",
  68. path: "packages/ui/src/hot.ts",
  69. },
  70. path: "packages/ui/src/hot.ts",
  71. });
  72. expect(data.domains[1]).toMatchObject({
  73. coveredFunctions: 1,
  74. functionHits: 5,
  75. functions: 1,
  76. });
  77. });
  78. it("writes a self-contained drill-down report and machine-readable data", async () => {
  79. const fixture = await createFixture();
  80. const summary = writeE2ECoverageHeatmap(fixture);
  81. expect(summary).toMatchObject({
  82. domainCount: 2,
  83. fileCount: 3,
  84. });
  85. const html = await readFile(summary?.htmlIndex ?? "", "utf-8");
  86. const data = JSON.parse(await readFile(summary?.dataJson ?? "", "utf-8")) as { scale?: string };
  87. expect(html).toContain("coverage-heatmap-data");
  88. expect(html).toContain("每个等大 tile 代表一个源码文件");
  89. expect(html).toContain('id="heat-grid"');
  90. expect(html).toContain("gridTemplateColumns");
  91. expect(html).toContain("每函数平均命中");
  92. expect(html).not.toContain("面积表示函数数量");
  93. expect(html).not.toContain('id="granularity-select"');
  94. expect(html).not.toContain("父目录");
  95. expect(html).not.toContain('id="breadcrumbs"');
  96. expect(html).not.toContain("继续下钻");
  97. expect(html).not.toContain("fetch(");
  98. expect(data.scale).toBe("log1p");
  99. });
  100. });