Files
openclaw/src/entry.compile-cache.test.ts
2026-04-30 01:16:38 +01:00

126 lines
4.6 KiB
TypeScript

import fs from "node:fs/promises";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { cleanupTempDirs, makeTempDir } from "../test/helpers/temp-dir.js";
import {
buildOpenClawCompileCacheRespawnPlan,
isSourceCheckoutInstallRoot,
resolveOpenClawCompileCacheDirectory,
resolveEntryInstallRoot,
shouldEnableOpenClawCompileCache,
} from "./entry.compile-cache.js";
describe("entry compile cache", () => {
const tempDirs: string[] = [];
afterEach(() => {
cleanupTempDirs(tempDirs);
});
it("resolves install roots from source and dist entry paths", () => {
expect(resolveEntryInstallRoot("/repo/openclaw/src/entry.ts")).toBe("/repo/openclaw");
expect(resolveEntryInstallRoot("/repo/openclaw/dist/entry.js")).toBe("/repo/openclaw");
expect(resolveEntryInstallRoot("/pkg/openclaw/entry.js")).toBe("/pkg/openclaw");
});
it("treats git and source entry markers as source checkouts", async () => {
const root = makeTempDir(tempDirs, "openclaw-compile-cache-source-");
await fs.writeFile(path.join(root, ".git"), "gitdir: .git/worktrees/openclaw\n", "utf8");
expect(isSourceCheckoutInstallRoot(root)).toBe(true);
});
it("disables compile cache for source-checkout installs", async () => {
const root = makeTempDir(tempDirs, "openclaw-compile-cache-src-entry-");
await fs.mkdir(path.join(root, "src"), { recursive: true });
await fs.writeFile(path.join(root, "src", "entry.ts"), "export {};\n", "utf8");
expect(
shouldEnableOpenClawCompileCache({
env: {},
installRoot: root,
}),
).toBe(false);
});
it("keeps compile cache enabled for packaged installs unless disabled by env", () => {
const root = makeTempDir(tempDirs, "openclaw-compile-cache-package-");
expect(shouldEnableOpenClawCompileCache({ env: {}, installRoot: root })).toBe(true);
expect(
shouldEnableOpenClawCompileCache({
env: { NODE_DISABLE_COMPILE_CACHE: "1" },
installRoot: root,
}),
).toBe(false);
});
it("scopes packaged compile cache by package install metadata", async () => {
const root = makeTempDir(tempDirs, "openclaw-compile-cache-package-key-");
const packageJsonPath = path.join(root, "package.json");
await fs.writeFile(packageJsonPath, '{"version":"2026.4.29"}\n', "utf8");
const directory = resolveOpenClawCompileCacheDirectory({
env: { NODE_COMPILE_CACHE: path.join(root, ".node-cache") },
installRoot: root,
});
expect(directory).toContain(path.join(".node-cache", "openclaw"));
expect(directory).toContain("2026.4.29");
expect(path.basename(directory)).toMatch(/^\d+-\d+$/);
});
it("builds a one-shot no-cache respawn plan when source checkout inherits NODE_COMPILE_CACHE", async () => {
const root = makeTempDir(tempDirs, "openclaw-compile-cache-respawn-");
await fs.mkdir(path.join(root, "src"), { recursive: true });
await fs.writeFile(path.join(root, "src", "entry.ts"), "export {};\n", "utf8");
const plan = buildOpenClawCompileCacheRespawnPlan({
currentFile: path.join(root, "dist", "entry.js"),
env: { NODE_COMPILE_CACHE: "/tmp/openclaw-cache" },
execArgv: ["--no-warnings"],
execPath: "/usr/bin/node",
installRoot: root,
argv: ["/usr/bin/node", path.join(root, "dist", "entry.js"), "status", "--json"],
});
expect(plan).toEqual({
command: "/usr/bin/node",
args: ["--no-warnings", path.join(root, "dist", "entry.js"), "status", "--json"],
env: {
NODE_DISABLE_COMPILE_CACHE: "1",
OPENCLAW_SOURCE_COMPILE_CACHE_RESPAWNED: "1",
},
});
});
it("does not respawn packaged installs when NODE_COMPILE_CACHE is configured", () => {
const root = makeTempDir(tempDirs, "openclaw-compile-cache-package-respawn-");
expect(
buildOpenClawCompileCacheRespawnPlan({
currentFile: path.join(root, "dist", "entry.js"),
env: { NODE_COMPILE_CACHE: "/tmp/openclaw-cache" },
installRoot: root,
}),
).toBeUndefined();
});
it("does not respawn source checkouts twice", async () => {
const root = makeTempDir(tempDirs, "openclaw-compile-cache-respawn-once-");
await fs.mkdir(path.join(root, "src"), { recursive: true });
await fs.writeFile(path.join(root, "src", "entry.ts"), "export {};\n", "utf8");
expect(
buildOpenClawCompileCacheRespawnPlan({
currentFile: path.join(root, "dist", "entry.js"),
env: {
NODE_COMPILE_CACHE: "/tmp/openclaw-cache",
OPENCLAW_SOURCE_COMPILE_CACHE_RESPAWNED: "1",
},
installRoot: root,
}),
).toBeUndefined();
});
});