
























@@ -0,0 +1,109 @@
1+import fs from "node:fs/promises";
2+import path from "node:path";
3+import { afterEach, describe, expect, it } from "vitest";
4+import { cleanupTempDirs, makeTempDir } from "../test/helpers/temp-dir.js";
5+import {
6+buildOpenClawCompileCacheRespawnPlan,
7+isSourceCheckoutInstallRoot,
8+resolveEntryInstallRoot,
9+shouldEnableOpenClawCompileCache,
10+} from "./entry.compile-cache.js";
11+12+describe("entry compile cache", () => {
13+const tempDirs: string[] = [];
14+15+afterEach(() => {
16+cleanupTempDirs(tempDirs);
17+});
18+19+it("resolves install roots from source and dist entry paths", () => {
20+expect(resolveEntryInstallRoot("/repo/openclaw/src/entry.ts")).toBe("/repo/openclaw");
21+expect(resolveEntryInstallRoot("/repo/openclaw/dist/entry.js")).toBe("/repo/openclaw");
22+expect(resolveEntryInstallRoot("/pkg/openclaw/entry.js")).toBe("/pkg/openclaw");
23+});
24+25+it("treats git and source entry markers as source checkouts", async () => {
26+const root = makeTempDir(tempDirs, "openclaw-compile-cache-source-");
27+await fs.writeFile(path.join(root, ".git"), "gitdir: .git/worktrees/openclaw\n", "utf8");
28+29+expect(isSourceCheckoutInstallRoot(root)).toBe(true);
30+});
31+32+it("disables compile cache for source-checkout installs", async () => {
33+const root = makeTempDir(tempDirs, "openclaw-compile-cache-src-entry-");
34+await fs.mkdir(path.join(root, "src"), { recursive: true });
35+await fs.writeFile(path.join(root, "src", "entry.ts"), "export {};\n", "utf8");
36+37+expect(
38+shouldEnableOpenClawCompileCache({
39+env: {},
40+installRoot: root,
41+}),
42+).toBe(false);
43+});
44+45+it("keeps compile cache enabled for packaged installs unless disabled by env", () => {
46+const root = makeTempDir(tempDirs, "openclaw-compile-cache-package-");
47+48+expect(shouldEnableOpenClawCompileCache({ env: {}, installRoot: root })).toBe(true);
49+expect(
50+shouldEnableOpenClawCompileCache({
51+env: { NODE_DISABLE_COMPILE_CACHE: "1" },
52+installRoot: root,
53+}),
54+).toBe(false);
55+});
56+57+it("builds a one-shot no-cache respawn plan when source checkout inherits NODE_COMPILE_CACHE", async () => {
58+const root = makeTempDir(tempDirs, "openclaw-compile-cache-respawn-");
59+await fs.mkdir(path.join(root, "src"), { recursive: true });
60+await fs.writeFile(path.join(root, "src", "entry.ts"), "export {};\n", "utf8");
61+62+const plan = buildOpenClawCompileCacheRespawnPlan({
63+currentFile: path.join(root, "dist", "entry.js"),
64+env: { NODE_COMPILE_CACHE: "/tmp/openclaw-cache" },
65+execArgv: ["--no-warnings"],
66+execPath: "/usr/bin/node",
67+installRoot: root,
68+argv: ["/usr/bin/node", path.join(root, "dist", "entry.js"), "status", "--json"],
69+});
70+71+expect(plan).toEqual({
72+command: "/usr/bin/node",
73+args: ["--no-warnings", path.join(root, "dist", "entry.js"), "status", "--json"],
74+env: {
75+NODE_DISABLE_COMPILE_CACHE: "1",
76+OPENCLAW_SOURCE_COMPILE_CACHE_RESPAWNED: "1",
77+},
78+});
79+});
80+81+it("does not respawn packaged installs when NODE_COMPILE_CACHE is configured", () => {
82+const root = makeTempDir(tempDirs, "openclaw-compile-cache-package-respawn-");
83+84+expect(
85+buildOpenClawCompileCacheRespawnPlan({
86+currentFile: path.join(root, "dist", "entry.js"),
87+env: { NODE_COMPILE_CACHE: "/tmp/openclaw-cache" },
88+installRoot: root,
89+}),
90+).toBeUndefined();
91+});
92+93+it("does not respawn source checkouts twice", async () => {
94+const root = makeTempDir(tempDirs, "openclaw-compile-cache-respawn-once-");
95+await fs.mkdir(path.join(root, "src"), { recursive: true });
96+await fs.writeFile(path.join(root, "src", "entry.ts"), "export {};\n", "utf8");
97+98+expect(
99+buildOpenClawCompileCacheRespawnPlan({
100+currentFile: path.join(root, "dist", "entry.js"),
101+env: {
102+NODE_COMPILE_CACHE: "/tmp/openclaw-cache",
103+OPENCLAW_SOURCE_COMPILE_CACHE_RESPAWNED: "1",
104+},
105+installRoot: root,
106+}),
107+).toBeUndefined();
108+});
109+});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。