Files
openclaw/test/scripts/write-package-dist-inventory.test.ts
Peter Steinberger 710a38588f fix: write package inventory through symlinked source paths (#132252)
Package preparation through a symlinked source directory must generate the
postinstall inventory and pending-install guard instead of silently doing
nothing. Match the writer entrypoint through the existing real-path helper
while retaining argv-only activation so imports remain inert despite PM2 hints.

Add real-subprocess regression coverage for canonical and aliased entrypoints
and for imports with absent or same-basename argv and absent or seeded files.

AI-assisted with Codex.
2026-08-28 18:56:03 -07:00

102 lines
4.1 KiB
TypeScript

import { spawnSync } from "node:child_process";
import fs from "node:fs";
import { createRequire } from "node:module";
import path from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import { describe, expect, it } from "vitest";
import {
PACKAGE_DIST_INVENTORY_RELATIVE_PATH,
PACKAGE_INSTALL_GUARD_RELATIVE_PATH,
} from "../../scripts/lib/package-dist-inventory-contract.mts";
import { withTempDirSync } from "../../src/test-helpers/temp-dir.js";
const repoRoot = fs.realpathSync(fileURLToPath(new URL("../..", import.meta.url)));
const writerPath = path.join(repoRoot, "scripts/write-package-dist-inventory.ts");
const loaderUrl = pathToFileURL(createRequire(import.meta.url).resolve("tsx")).href;
function withPackageFixture(run: (packageRoot: string) => void) {
withTempDirSync({ prefix: "openclaw-path-alias-inventory-" }, (packageRoot) => {
fs.mkdirSync(path.join(packageRoot, "dist"));
fs.writeFileSync(path.join(packageRoot, "package.json"), '{"type":"module"}\n');
fs.writeFileSync(path.join(packageRoot, "dist/entry.js"), "export {};\n");
run(packageRoot);
});
}
function runNode(packageRoot: string, args: string[]) {
const result = spawnSync(process.execPath, ["--import", loaderUrl, ...args], {
cwd: packageRoot,
encoding: "utf8",
timeout: 30_000,
env: {
...process.env,
// The tiny cwd fixture still uses the real source closure's workspace aliases.
TSX_TSCONFIG_PATH: path.join(repoRoot, "tsconfig.json"),
TSX_DISABLE_CACHE: "1",
pm_exec_path: writerPath,
},
});
expect(result.error).toBeUndefined();
expect(result.status, result.stderr).toBe(0);
}
describe("write-package-dist-inventory direct entry", () => {
it.each(["canonical", "directory alias"])("writes both artifacts via %s", (entry) => {
withPackageFixture((packageRoot) => {
let scriptPath = writerPath;
if (entry === "directory alias") {
const sourceAlias = path.join(packageRoot, "source-alias");
fs.symlinkSync(repoRoot, sourceAlias, process.platform === "win32" ? "junction" : "dir");
scriptPath = path.join(sourceAlias, "scripts/write-package-dist-inventory.ts");
}
runNode(packageRoot, [scriptPath]);
expect(
JSON.parse(
fs.readFileSync(path.join(packageRoot, PACKAGE_DIST_INVENTORY_RELATIVE_PATH), "utf8"),
),
).toEqual(["dist/entry.js"]);
expect(
fs.readFileSync(path.join(packageRoot, PACKAGE_INSTALL_GUARD_RELATIVE_PATH), "utf8"),
).toBe("OpenClaw package preinstall has not completed.\n");
expect(fs.readdirSync(path.join(packageRoot, "dist")).sort()).toEqual(
[
"entry.js",
path.basename(PACKAGE_DIST_INVENTORY_RELATIVE_PATH),
path.basename(PACKAGE_INSTALL_GUARD_RELATIVE_PATH),
].sort(),
);
});
});
describe.each([false, true])("import with existing artifacts=%s", (seeded) => {
it.each(["no argv", "same basename"])("stays inert with %s and PM2 hints", (entry) => {
withPackageFixture((packageRoot) => {
const expected: Record<string, string> = { "entry.js": "export {};\n" };
if (seeded) {
expected[path.basename(PACKAGE_DIST_INVENTORY_RELATIVE_PATH)] = '["dist/previous.js"]\n';
expected[path.basename(PACKAGE_INSTALL_GUARD_RELATIVE_PATH)] = "existing guard\n";
for (const [name, content] of Object.entries(expected)) {
fs.writeFileSync(path.join(packageRoot, "dist", name), content);
}
}
const importSource = `await import(${JSON.stringify(pathToFileURL(writerPath).href)});\n`;
const importerPath = path.join(packageRoot, path.basename(writerPath));
fs.writeFileSync(importerPath, importSource);
runNode(
packageRoot,
entry === "no argv" ? ["--input-type=module", "--eval", importSource] : [importerPath],
);
const actual = Object.fromEntries(
fs
.readdirSync(path.join(packageRoot, "dist"))
.map((name) => [name, fs.readFileSync(path.join(packageRoot, "dist", name), "utf8")]),
);
expect(actual).toEqual(expected);
});
});
});
});