import { mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { afterEach, describe, expect, it } from "vitest"; import { NodeProjectProbe } from "./node-project-probe.ts"; const tempDirs: string[] = []; async function createTempDir(): Promise { const dir = await mkdtemp(join(tmpdir(), "pi-test-node-project-probe-")); tempDirs.push(dir); return dir; } afterEach(async () => { await Promise.all( tempDirs.splice(0).map((dir) => rm(dir, { recursive: true, force: true })), ); }); describe("NodeProjectProbe", () => { it("returns true when a path exists", async () => { const dir = await createTempDir(); const filePath = join(dir, "go.mod"); await writeFile(filePath, "module example.com/service\n", "utf8"); const probe = new NodeProjectProbe(); await expect(probe.exists(filePath)).resolves.toBe(true); }); it("returns false when a path does not exist", async () => { const dir = await createTempDir(); const missingPath = join(dir, "go.work"); const probe = new NodeProjectProbe(); await expect(probe.exists(missingPath)).resolves.toBe(false); }); it("returns false instead of throwing when a parent directory does not exist", async () => { const dir = await createTempDir(); const missingPath = join(dir, "missing", "go.mod"); const probe = new NodeProjectProbe(); await expect(probe.exists(missingPath)).resolves.toBe(false); }); });