e2eSourceEvidence.test.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. import { mkdtemp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
  3. import { tmpdir } from "node:os";
  4. import { join } from "node:path";
  5. import { changedSourceFiles, withE2ESourceEvidence } from "./e2e/reporting/e2e-source-evidence.js";
  6. let directory: string | undefined;
  7. afterEach(async () => {
  8. if (directory) await rm(directory, { recursive: true, force: true });
  9. directory = undefined;
  10. });
  11. async function setup() {
  12. directory = await mkdtemp(join(tmpdir(), "e2e-source-evidence-"));
  13. const appOutputDir = join(directory, "app");
  14. await mkdir(appOutputDir);
  15. await writeFile(join(appOutputDir, "index.js"), "const fixture = 1;");
  16. return { appOutputDir, artifactDir: join(directory, "report") };
  17. }
  18. describe("E2E source evidence", () => {
  19. it("detects changed, added and deleted source files", () => {
  20. expect(changedSourceFiles({ a: "1", b: "2" }, { a: "3", c: "4" })).toEqual(["a", "b", "c"]);
  21. });
  22. it("records stable fresh source and staged artifacts", async () => {
  23. const paths = await setup();
  24. const build = vi.fn(async () => {});
  25. await withE2ESourceEvidence({
  26. ...paths,
  27. freshBuild: true,
  28. captureSources: async () => ({ "packages/ui/src/a.ts": "hash" }),
  29. build,
  30. });
  31. const evidence = JSON.parse(
  32. await readFile(join(paths.artifactDir, "build-source-evidence.json"), "utf8"),
  33. );
  34. expect(evidence.status).toBe("fresh-stable");
  35. expect(evidence.sources["packages/ui/src/a.ts"]).toBe("hash");
  36. expect(evidence.outputs["index.js"]).toMatch(/^[a-f0-9]{64}$/);
  37. expect(evidence.domains).toEqual(["renderer", "host", "main"]);
  38. expect(build).toHaveBeenCalledTimes(1);
  39. });
  40. it("does not validate a source edit during build", async () => {
  41. const paths = await setup();
  42. let version = "before";
  43. const evidence = await withE2ESourceEvidence({
  44. ...paths,
  45. freshBuild: true,
  46. captureSources: async () => ({ a: version }),
  47. build: async () => {
  48. version = "after";
  49. },
  50. });
  51. expect(evidence.status).toBe("source-changed-during-build");
  52. expect(evidence.changedFiles).toEqual(["a"]);
  53. });
  54. it("never assigns current sources to a reused build", async () => {
  55. const paths = await setup();
  56. const captureSources = vi.fn(async () => ({ a: "current" }));
  57. const evidence = await withE2ESourceEvidence({
  58. ...paths,
  59. freshBuild: false,
  60. captureSources,
  61. build: async () => {},
  62. });
  63. expect(evidence.status).toBe("unverified-reused-build");
  64. expect(evidence.sources).toEqual({});
  65. expect(captureSources).not.toHaveBeenCalled();
  66. });
  67. it("does not write a successful manifest when build fails", async () => {
  68. const paths = await setup();
  69. await expect(
  70. withE2ESourceEvidence({
  71. ...paths,
  72. freshBuild: true,
  73. captureSources: async () => ({ a: "1" }),
  74. build: async () => {
  75. throw new Error("build failed");
  76. },
  77. }),
  78. ).rejects.toThrow("build failed");
  79. await expect(
  80. readFile(join(paths.artifactDir, "build-source-evidence.json")),
  81. ).rejects.toMatchObject({ code: "ENOENT" });
  82. });
  83. });