import { climbsOut, inspectManifests } from "../../../tools/core/inspectors/manifest.inspector.ts"; import { describe, it } from "node:test"; import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { SURFACE_ROOT } from "../../../tools/core/constants/path.constants.ts"; import assert from "node:assert/strict"; import { resolve } from "node:path"; import { tmpdir } from "node:os"; const ROOT_MANIFEST = SURFACE_ROOT.length === 0 ? "package.json" : `${SURFACE_ROOT}/package.json`; const MANIFEST_TEXT = JSON.stringify({ dependencies: { leftpad: "1.0.0" }, exports: { ".": "./present.ts", "./gone": "./missing.ts" }, scripts: { build: "node ./present.ts", host: "node ../outside/run.ts", stale: "node ./renamed.ts --flag" }, workspaces: ["tools", "empty/*"], }); const withPackage = function withPackage(text: string, probe: (root: string) => void): void { const root = mkdtempSync(resolve(tmpdir(), "manifest-inspector-")); const surface = resolve(root, SURFACE_ROOT); mkdirSync(resolve(surface, "tools"), { recursive: true }); mkdirSync(resolve(surface, "empty", "child"), { recursive: true }); writeFileSync(resolve(surface, "present.ts"), 'import leftpad from "leftpad";\n'); writeFileSync(resolve(surface, "tools", "package.json"), "{}"); writeFileSync(resolve(surface, "package.json"), text); try { probe(root); } finally { rmSync(root, { force: true, recursive: true }); } }; describe("climbsOut", () => { it("is true only for a path segment that climbs to a parent", () => { assert.equal(climbsOut("../out/probe.ts"), true); assert.equal(climbsOut("tools/core/a..b/probe.ts"), false); }); }); describe("inspectManifests", () => { it("reports runtime dependencies, empty workspace globs, broken subpaths and script targets", () => { withPackage(MANIFEST_TEXT, (root) => { const findings = inspectManifests(root, []).filter((finding) => finding.path === ROOT_MANIFEST); assert.deepEqual( findings.map((finding) => [finding.rule, finding.locus]), [ ["declaration/declaredRuntimeDependency", 'dependencies "leftpad"'], ["declaration/emptyWorkspaceGlob", 'workspaces "empty/*"'], ["declaration/unresolvedSubpath", 'exports "./gone"'], ["declaration/hostClaimingScriptTarget", "scripts.host"], ["declaration/unresolvedScriptTarget", "scripts.stale"], ], ); }); }); it("reports a manifest that does not parse", () => { withPackage("{ not json", (root) => { const rules = inspectManifests(root, []).map((finding) => finding.rule); assert.ok(rules.includes("declaration/unparsableManifest")); }); }); });