e2eAtomicJsonFile.test.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { open, readFile, rm, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { describe, expect, it, vi } from "vitest";
  5. import {
  6. replaceE2EFileWithRetry,
  7. writeE2EJsonFileAtomically,
  8. } from "./e2e/helpers/e2e-atomic-json-file.js";
  9. describe("Desktop E2E atomic JSON file", () => {
  10. it("writes a complete temporary JSON file before replacing the capture artifact", async () => {
  11. const mkdir = vi.fn();
  12. const rename = vi.fn();
  13. const remove = vi.fn();
  14. const write = vi.fn();
  15. await writeE2EJsonFileAtomically(
  16. "C:/e2e/capture.json",
  17. { records: [{ id: "record-1" }], version: 1 },
  18. { mkdir, remove, rename, wait: vi.fn(), write },
  19. );
  20. const temporaryPath = write.mock.calls[0]?.[0] as string;
  21. expect(temporaryPath).not.toBe("C:/e2e/capture.json");
  22. expect(temporaryPath).toContain("capture.json.");
  23. expect(JSON.parse(write.mock.calls[0]?.[1] as string)).toEqual({
  24. records: [{ id: "record-1" }],
  25. version: 1,
  26. });
  27. expect(rename).toHaveBeenCalledWith(temporaryPath, "C:/e2e/capture.json");
  28. expect(remove).not.toHaveBeenCalled();
  29. });
  30. it("retries a transient Windows replacement conflict before succeeding", async () => {
  31. const conflict = Object.assign(new Error("file is busy"), { code: "EPERM" });
  32. const rename = vi.fn().mockRejectedValueOnce(conflict).mockResolvedValueOnce(undefined);
  33. const wait = vi.fn().mockResolvedValue(undefined);
  34. await writeE2EJsonFileAtomically(
  35. "C:/e2e/capture.json",
  36. { records: [] },
  37. {
  38. mkdir: vi.fn(),
  39. remove: vi.fn(),
  40. rename,
  41. wait,
  42. write: vi.fn(),
  43. },
  44. );
  45. expect(rename).toHaveBeenCalledTimes(2);
  46. expect(wait).toHaveBeenCalledTimes(1);
  47. });
  48. it("reuses the bounded replacement retry for non-JSON E2E fixtures", async () => {
  49. const conflict = Object.assign(new Error("file is busy"), { code: "EACCES" });
  50. const rename = vi.fn().mockRejectedValueOnce(conflict).mockResolvedValueOnce(undefined);
  51. const wait = vi.fn().mockResolvedValue(undefined);
  52. await replaceE2EFileWithRetry("memory.md.replacement", "memory.md", {
  53. rename,
  54. wait,
  55. });
  56. expect(rename).toHaveBeenNthCalledWith(1, "memory.md.replacement", "memory.md");
  57. expect(rename).toHaveBeenCalledTimes(2);
  58. expect(wait).toHaveBeenCalledTimes(1);
  59. });
  60. it("removes the temporary file when replacement fails", async () => {
  61. const remove = vi.fn();
  62. const rename = vi.fn(() => {
  63. throw new Error("rename failed");
  64. });
  65. const write = vi.fn();
  66. await expect(
  67. writeE2EJsonFileAtomically(
  68. "/e2e/capture.json",
  69. { records: [] },
  70. { mkdir: vi.fn(), remove, rename, wait: vi.fn(), write },
  71. ),
  72. ).rejects.toThrow("rename failed");
  73. expect(remove).toHaveBeenCalledWith(write.mock.calls[0]?.[0], {
  74. force: true,
  75. });
  76. });
  77. it.runIf(process.platform === "win32")(
  78. "waits until a Windows reader releases the previous capture artifact",
  79. async () => {
  80. const artifactPath = join(tmpdir(), `zcode-e2e-atomic-${process.pid}.json`);
  81. await writeFile(artifactPath, JSON.stringify({ version: "old" }), "utf-8");
  82. const reader = await open(artifactPath, "r");
  83. const releaseReader = setTimeout(() => {
  84. void reader.close().catch(() => undefined);
  85. }, 75);
  86. try {
  87. await writeE2EJsonFileAtomically(artifactPath, { version: "new" });
  88. await expect(readFile(artifactPath, "utf-8")).resolves.toContain('"new"');
  89. } finally {
  90. clearTimeout(releaseReader);
  91. await reader.close().catch(() => undefined);
  92. await rm(artifactPath, { force: true });
  93. }
  94. },
  95. );
  96. });