1// @effect-diagnostics nodeBuiltinImport:off - tests fake a failed write at the native v8 boundary.
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import * as NodeFS from "node:fs";
4import * as NodePath from "node:path";
5import * as NodeV8 from "node:v8";
6import { assert, it } from "@effect/vitest";
7import * as Effect from "effect/Effect";
8import * as FileSystem from "effect/FileSystem";
9import { vi } from "vite-plus/test";
10
11import { writeHeapSnapshot } from "./HeapSnapshot.ts";
12
13vi.mock("node:v8", async (importOriginal) => {
14 const actual = await importOriginal<typeof NodeV8>();
15 return { ...actual, writeHeapSnapshot: vi.fn(actual.writeHeapSnapshot) };
16});
17
18it.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});