import assert from "node:assert/strict"; import crypto from "node:crypto"; import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { spawnSync } from "node:child_process"; import { fileURLToPath } from "node:url"; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../.."); const generator = path.join(root, "tools/web/generate-dependency-inventory.mjs"); const inventoryPath = path.join(root, "tests/golden/M13-05C/dependency-inventory.json"); const manifestPath = path.join(root, "tests/golden/M13-05C/manifest.json"); const sha256 = (bytes) => crypto.createHash("sha256").update(bytes).digest("hex"); const fileSha256 = (file) => sha256(fs.readFileSync(file)); const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "m13-05c-inventory-")); const regenerated = path.join(temporary, "dependency-inventory.json"); try { const run = spawnSync(process.execPath, [generator, regenerated], { cwd: root, encoding: "utf8", maxBuffer: 4 * 1024 * 1024 }); assert.equal(run.status, 0, `${run.stdout}\n${run.stderr}`); assert.deepEqual(fs.readFileSync(regenerated), fs.readFileSync(inventoryPath), "dependency inventory is not deterministic"); const inventory = JSON.parse(fs.readFileSync(inventoryPath, "utf8")); assert.equal(inventory.schemaVersion, 1); assert.equal(inventory.task, "M13-05C"); assert.equal(inventory.nextTask, "M13-05D"); assert.deepEqual(inventory.roots.production, ["react", "react-dom"]); assert.ok(inventory.roots.build.includes("vite")); assert.deepEqual(inventory.roots.test, ["@axe-core/playwright", "@playwright/test"]); assert.ok(inventory.packages.length > 0); assert.equal(inventory.packages.length, new Set(inventory.packages.map((item) => item.path)).size); assert.equal(inventory.packages.some((item) => item.categories.length === 0), false); for (const item of inventory.packages) { assert.match(item.path, /^node_modules\//u); assert.match(item.name, /\S/u); assert.match(item.version, /^\d+\.\d+\.\d+/u); assert.ok(item.categories.every((category) => ["production", "build", "test"].includes(category))); assert.ok(item.categories.length >= 1); if (item.resolved !== null) assert.match(item.resolved, /^https:\/\//u); if (item.integrity !== null) assert.match(item.integrity, /^sha512-/u); } assert.equal(inventory.categoryCounts.production, inventory.packages.filter((item) => item.categories.includes("production")).length); assert.equal(inventory.categoryCounts.build, inventory.packages.filter((item) => item.categories.includes("build")).length); assert.equal(inventory.categoryCounts.test, inventory.packages.filter((item) => item.categories.includes("test")).length); const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8")); assert.deepEqual({ schemaVersion: manifest.schemaVersion, task: manifest.task, parentTask: manifest.parentTask, nextTask: manifest.nextTask }, { schemaVersion: 1, task: "M13-05C", parentTask: "M13-05B", nextTask: "M13-05D" }); for (const artifact of Object.values(manifest.artifacts)) assert.equal(fileSha256(path.join(root, artifact.path)), artifact.sha256, artifact.path); process.stdout.write(`dependency-inventory-ok packages=${inventory.packages.length} production=${inventory.categoryCounts.production} build=${inventory.categoryCounts.build} test=${inventory.categoryCounts.test} shared=${inventory.sharedCount} deterministic=true next=M13-05D\n`); } finally { fs.rmSync(temporary, { recursive: true, force: true }); }