// @vitest-environment node import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { afterEach, describe, expect, test } from "vitest"; import { collectInstalledRuntimePackages } from "./generate-sbom.mjs"; const temporaryDirectories = []; afterEach(() => { for (const directory of temporaryDirectories.splice(0)) { fs.rmSync(directory, { recursive: true, force: true }); } }); describe("runtime SBOM inventory", () => { test("reports exact package versions present in the pruned pnpm virtual store", () => { const root = fs.mkdtempSync(path.join(os.tmpdir(), "hivegrid-sbom-")); temporaryDirectories.push(root); const nodeModules = path.join(root, "node_modules"); const alpha = path.join(nodeModules, ".pnpm", "alpha@1.2.3", "node_modules", "alpha"); const beta = path.join(nodeModules, ".pnpm", "@scope+beta@4.5.6", "node_modules", "@scope", "beta"); fs.mkdirSync(alpha, { recursive: true }); fs.mkdirSync(beta, { recursive: true }); fs.writeFileSync(path.join(alpha, "package.json"), JSON.stringify({ name: "alpha", version: "1.2.3", license: "MIT" })); fs.writeFileSync(path.join(beta, "package.json"), JSON.stringify({ name: "@scope/beta", version: "4.5.6", license: "Apache-2.0" })); expect(collectInstalledRuntimePackages(nodeModules)).toEqual([ { name: "@scope/beta", version: "4.5.6", license: "Apache-2.0" }, { name: "alpha", version: "1.2.3", license: "MIT" }, ]); }); test("fails when the supplied runtime tree is not a pnpm deployment", () => { const root = fs.mkdtempSync(path.join(os.tmpdir(), "hivegrid-sbom-missing-")); temporaryDirectories.push(root); expect(() => collectInstalledRuntimePackages(path.join(root, "node_modules"))).toThrow("virtual store not found"); }); });