| 1 | // @effect-diagnostics nodeBuiltinImport:off - tests fake a failed write at the native v8 boundary. |
| 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 3 | import * as NodeFS from "node:fs"; |
| 4 | import * as NodePath from "node:path"; |
| 5 | import * as NodeV8 from "node:v8"; |
| 6 | import { assert, it } from "@effect/vitest"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as FileSystem from "effect/FileSystem"; |
| 9 | import { vi } from "vite-plus/test"; |
| 10 | |
| 11 | import { writeHeapSnapshot } from "./HeapSnapshot.ts"; |
| 12 | |
| 13 | vi.mock("node:v8", async (importOriginal) => { |
| 14 | const actual = await importOriginal<typeof NodeV8>(); |
| 15 | return { ...actual, writeHeapSnapshot: vi.fn(actual.writeHeapSnapshot) }; |
| 16 | }); |
| 17 | |
| 18 | it.layer(NodeServices.layer)("writeHeapSnapshot", (it) => { |
| 19 | it.effect("removes the partial file when the write fails", () => |
| 20 | Effect.gen(function* () { |
| 21 | const fs = yield* FileSystem.FileSystem; |
| 22 | const logsDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-heap-snapshot-test-" }); |
| 23 | let partialPath: string | undefined; |
| 24 | vi.mocked(NodeV8.writeHeapSnapshot).mockImplementationOnce((path) => { |
| 25 | partialPath = path; |
| 26 | if (path) NodeFS.writeFileSync(path, "partial"); |
| 27 | throw new Error("ENOSPC: no space left on device"); |
| 28 | }); |
| 29 | |
| 30 | yield* writeHeapSnapshot(logsDir); |
| 31 | |
| 32 | assert.strictEqual(NodePath.dirname(partialPath ?? ""), logsDir); |
| 33 | assert.deepEqual(yield* fs.readDirectory(logsDir), []); |
| 34 | }), |
| 35 | ); |
| 36 | }); |