import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { isAbsolute, join, relative, resolve } from "node:path"; import { test } from "node:test"; const workspace = process.cwd(); const script = resolve("scripts/postbuild-cleanup.mjs"); test("postbuild removes nested public maps and preserves runtime assets", async () => { const qa = resolve(".qa"); await mkdir(qa, { recursive: true }); const directory = await mkdtemp(join(qa, "postbuild-cleanup-")); const nested = join(directory, "static", "chunks"); try { await mkdir(nested, { recursive: true }); await writeFile(join(nested, "runtime.js"), "runtime"); await writeFile(join(nested, "runtime.js.map"), "source map"); await writeFile(join(nested, "notes.txt"), "keep"); const result = spawnSync(process.execPath, [script], { cwd: workspace, env: { ...process.env, NEXT_DIST_DIR: relative(workspace, directory) }, encoding: "utf8", }); assert.equal(result.status, 0, result.stderr); assert.match(result.stdout, /Removed 1 public source map files/); assert.equal(await readFile(join(nested, "runtime.js"), "utf8"), "runtime"); assert.equal(await readFile(join(nested, "notes.txt"), "utf8"), "keep"); await assert.rejects(readFile(join(nested, "runtime.js.map")), { code: "ENOENT" }); } finally { const location = relative(qa, directory); assert.ok(location.startsWith("postbuild-cleanup-") && !isAbsolute(location) && !location.includes("..")); await rm(directory, { recursive: true, force: true }); } }); test("postbuild refuses a build directory outside the workspace", () => { const result = spawnSync(process.execPath, [script], { cwd: workspace, env: { ...process.env, NEXT_DIST_DIR: ".." }, encoding: "utf8", }); assert.notEqual(result.status, 0); assert.match(result.stderr, /inside the workspace/); });