forked from openclaw/openclaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentry.compile-cache.test.ts
More file actions
248 lines (211 loc) · 8.3 KB
/
Copy pathentry.compile-cache.test.ts
File metadata and controls
248 lines (211 loc) · 8.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import type { ChildProcess } from "node:child_process";
import { EventEmitter } from "node:events";
import fs from "node:fs/promises";
import path from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import { cleanupTempDirs, makeTempDir } from "../test/helpers/temp-dir.js";
import {
buildOpenClawCompileCacheRespawnPlan,
isSourceCheckoutInstallRoot,
resolveOpenClawCompileCacheDirectory,
resolveEntryInstallRoot,
runOpenClawCompileCacheRespawnPlan,
shouldEnableOpenClawCompileCache,
} from "./entry.compile-cache.js";
function requireFirstMockCall(mock: { mock: { calls: unknown[][] } }, label: string): unknown[] {
const [call] = mock.mock.calls;
if (!call) {
throw new Error(`expected ${label} call`);
}
return call;
}
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();
});
it("runs compile-cache respawn plans with the child-process bridge", () => {
const child = new EventEmitter() as ChildProcess;
const spawn = vi.fn(() => child);
const attachChildProcessBridge = vi.fn();
const exit = vi.fn();
const writeError = vi.fn();
runOpenClawCompileCacheRespawnPlan(
{
command: "/usr/bin/node",
args: ["/repo/openclaw/dist/entry.js", "status"],
env: { NODE_DISABLE_COMPILE_CACHE: "1" },
},
{
spawn: spawn as unknown as typeof import("node:child_process").spawn,
attachChildProcessBridge,
exit: exit as unknown as (code?: number) => never,
writeError,
},
);
expect(spawn).toHaveBeenCalledWith(
"/usr/bin/node",
["/repo/openclaw/dist/entry.js", "status"],
{
stdio: "inherit",
env: { NODE_DISABLE_COMPILE_CACHE: "1" },
},
);
const [bridgeChild, bridgeOptions] = requireFirstMockCall(
attachChildProcessBridge,
"child process bridge attach",
);
expect(bridgeChild).toBe(child);
expect(bridgeOptions).toEqual({ onSignal: expect.any(Function) });
child.emit("exit", 0, null);
expect(exit).toHaveBeenCalledWith(0);
expect(writeError).not.toHaveBeenCalled();
});
it("marks signal-terminated compile-cache respawn children as failed without forcing another exit", () => {
const child = new EventEmitter() as ChildProcess;
const spawn = vi.fn(() => child);
const exit = vi.fn();
runOpenClawCompileCacheRespawnPlan(
{
command: "/usr/bin/node",
args: ["/repo/openclaw/dist/entry.js"],
env: {},
},
{
spawn: spawn as unknown as typeof import("node:child_process").spawn,
attachChildProcessBridge: vi.fn(),
exit: exit as unknown as (code?: number) => never,
writeError: vi.fn(),
},
);
child.emit("exit", null, "SIGTERM");
expect(exit).toHaveBeenCalledWith(1);
});
it("waits for a signaled compile-cache respawn child after force-killing it", () => {
vi.useFakeTimers();
const child = new EventEmitter() as ChildProcess;
const kill = vi.fn<(signal?: NodeJS.Signals) => boolean>(() => true);
child.kill = kill as ChildProcess["kill"];
const spawn = vi.fn(() => child);
const exit = vi.fn();
let onSignal: ((signal: NodeJS.Signals) => void) | undefined;
try {
runOpenClawCompileCacheRespawnPlan(
{
command: "/usr/bin/node",
args: ["/repo/openclaw/dist/entry.js"],
env: {},
},
{
spawn: spawn as unknown as typeof import("node:child_process").spawn,
attachChildProcessBridge: vi.fn((_child, options) => {
onSignal = options?.onSignal;
return { detach: vi.fn() };
}),
exit: exit as unknown as (code?: number) => never,
writeError: vi.fn(),
},
);
onSignal?.("SIGTERM");
vi.advanceTimersByTime(1_000);
expect(kill).toHaveBeenCalledWith("SIGTERM");
expect(exit).not.toHaveBeenCalled();
vi.advanceTimersByTime(1_000);
expect(kill).toHaveBeenCalledWith(process.platform === "win32" ? "SIGTERM" : "SIGKILL");
expect(exit).not.toHaveBeenCalled();
child.emit("exit", null, "SIGKILL");
expect(exit).toHaveBeenCalledWith(1);
} finally {
vi.useRealTimers();
}
});
});