forked from openclaw/openclaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnon-isolated-runner.ts
More file actions
253 lines (223 loc) · 7.69 KB
/
Copy pathnon-isolated-runner.ts
File metadata and controls
253 lines (223 loc) · 7.69 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
249
250
251
252
253
// Non-isolated runner helps execute tests without Vitest isolation.
import fs from "node:fs";
import path from "node:path";
import { TestRunner, type RunnerTask, type RunnerTestSuite, vi } from "vitest";
type EvaluatedModuleNode = {
promise?: unknown;
exports?: unknown;
evaluated?: boolean;
importers: Set<string>;
};
type EvaluatedModules = {
idToModuleMap: Map<string, EvaluatedModuleNode>;
};
const SHARED_TEST_SETUP = Symbol.for("openclaw.sharedTestSetup");
const EMBEDDED_RUN_STATE = Symbol.for("openclaw.embeddedRunState");
const REPLY_RUN_REGISTRY = Symbol.for("openclaw.replyRunRegistry");
const nativeTimerGlobals = {
setTimeout: globalThis.setTimeout,
clearTimeout: globalThis.clearTimeout,
setInterval: globalThis.setInterval,
clearInterval: globalThis.clearInterval,
setImmediate: globalThis.setImmediate,
clearImmediate: globalThis.clearImmediate,
Date: globalThis.Date,
};
function getSharedTestHome(): string | undefined {
const globalState = globalThis as typeof globalThis & {
[SHARED_TEST_SETUP]?: { tempHome?: string };
};
return globalState[SHARED_TEST_SETUP]?.tempHome ?? process.env.OPENCLAW_TEST_HOME;
}
function resetEvaluatedModules(modules: EvaluatedModules, resetMocks: boolean) {
const skipPaths = [
/\/vitest\/dist\//,
/vitest-virtual-\w+\/dist/u,
/@vitest\/dist/u,
...(resetMocks ? [] : [/^mock:/u]),
];
modules.idToModuleMap.forEach((node, modulePath) => {
if (skipPaths.some((pattern) => pattern.test(modulePath))) {
return;
}
node.promise = undefined;
node.exports = undefined;
node.evaluated = false;
node.importers.clear();
});
}
function restoreSharedTestHomeAfterEnvUnstub(testHomeRaw: string | undefined): void {
const testHome = testHomeRaw?.trim();
if (!testHome) {
return;
}
process.env.HOME = testHome;
process.env.USERPROFILE = testHome;
process.env.OPENCLAW_TEST_HOME = testHome;
delete process.env.OPENCLAW_CONFIG_PATH;
delete process.env.OPENCLAW_STATE_DIR;
delete process.env.OPENCLAW_AGENT_DIR;
process.env.XDG_CONFIG_HOME = path.join(testHome, ".config");
process.env.XDG_DATA_HOME = path.join(testHome, ".local", "share");
process.env.XDG_STATE_HOME = path.join(testHome, ".local", "state");
process.env.XDG_CACHE_HOME = path.join(testHome, ".cache");
}
function restoreRealTimers(): void {
if (vi.isFakeTimers()) {
vi.useRealTimers();
}
}
function restoreNativeTimerGlobals(): void {
Object.assign(globalThis, nativeTimerGlobals);
}
function restoreMocksThenRealTimers(): void {
// A spy created while fake timers are active captures the fake timer as its
// "original" implementation. Restore spies first, then swap timers back.
vi.restoreAllMocks();
restoreRealTimers();
restoreNativeTimerGlobals();
}
type CleanupAction = () => void;
type EmbeddedRunHandle = {
abort?: () => void;
cancel?: (reason?: "user_abort" | "restart" | "superseded") => void;
};
type EmbeddedRunWaiter = {
timer?: NodeJS.Timeout;
resolve?: (ended: boolean) => void;
};
type EmbeddedRunStateForTest = {
activeRuns?: Map<unknown, EmbeddedRunHandle>;
snapshots?: Map<unknown, unknown>;
sessionIdsByKey?: Map<unknown, unknown>;
sessionIdsByFile?: Map<unknown, unknown>;
abandonedRunsBySessionId?: Map<unknown, unknown>;
abandonedRunSessionIdsByKey?: Map<unknown, unknown>;
abandonedRunSessionIdsByFile?: Map<unknown, unknown>;
waiters?: Map<unknown, Set<EmbeddedRunWaiter>>;
modelSwitchRequests?: Map<unknown, unknown>;
};
type ReplyRunWaiter = {
finish?: (ended: boolean) => void;
};
type ReplyRunOperation = {
abortForRestart?: () => void;
};
type ReplyRunStateForTest = {
activeRunsByKey?: Map<unknown, ReplyRunOperation>;
activeSessionIdsByKey?: Map<unknown, unknown>;
activeKeysBySessionId?: Map<unknown, unknown>;
waitKeysBySessionId?: Map<unknown, unknown>;
waitersByKey?: Map<unknown, Set<ReplyRunWaiter>>;
};
function runCleanupActions(actions: CleanupAction[]): unknown {
let firstError: unknown;
for (const action of actions) {
try {
action();
} catch (error) {
firstError ??= error;
}
}
return firstError;
}
function resetOpenClawGlobalRunState(): void {
const cleanupActions: CleanupAction[] = [];
const globalStore = globalThis as Record<PropertyKey, unknown>;
const embeddedRunState = globalStore[EMBEDDED_RUN_STATE] as EmbeddedRunStateForTest | undefined;
for (const handle of embeddedRunState?.activeRuns?.values() ?? []) {
cleanupActions.push(() => {
if (handle.cancel) {
handle.cancel("restart");
return;
}
handle.abort?.();
});
}
for (const waiters of embeddedRunState?.waiters?.values() ?? []) {
for (const waiter of waiters) {
cleanupActions.push(() => {
if (waiter.timer) {
clearTimeout(waiter.timer);
}
waiter.resolve?.(true);
});
}
}
const replyRunState = globalStore[REPLY_RUN_REGISTRY] as ReplyRunStateForTest | undefined;
for (const operation of replyRunState?.activeRunsByKey?.values() ?? []) {
cleanupActions.push(() => {
operation.abortForRestart?.();
});
}
for (const waiters of replyRunState?.waitersByKey?.values() ?? []) {
for (const waiter of waiters) {
cleanupActions.push(() => {
waiter.finish?.(false);
});
}
}
const cleanupError = runCleanupActions(cleanupActions);
if (cleanupError) {
throw cleanupError;
}
embeddedRunState?.activeRuns?.clear();
embeddedRunState?.snapshots?.clear();
embeddedRunState?.sessionIdsByKey?.clear();
embeddedRunState?.sessionIdsByFile?.clear();
embeddedRunState?.abandonedRunsBySessionId?.clear();
embeddedRunState?.abandonedRunSessionIdsByKey?.clear();
embeddedRunState?.abandonedRunSessionIdsByFile?.clear();
embeddedRunState?.waiters?.clear();
embeddedRunState?.modelSwitchRequests?.clear();
replyRunState?.activeRunsByKey?.clear();
replyRunState?.activeSessionIdsByKey?.clear();
replyRunState?.activeKeysBySessionId?.clear();
replyRunState?.waitKeysBySessionId?.clear();
replyRunState?.waitersByKey?.clear();
}
export default class OpenClawNonIsolatedRunner extends TestRunner {
override onCollectStart(file: { filepath: string }) {
super.onCollectStart(file);
restoreRealTimers();
restoreNativeTimerGlobals();
restoreSharedTestHomeAfterEnvUnstub(getSharedTestHome());
const orderLogPath = process.env.OPENCLAW_VITEST_FILE_ORDER_LOG?.trim();
if (orderLogPath) {
fs.appendFileSync(orderLogPath, `START ${file.filepath}\n`);
}
}
override async onBeforeRunTask(test: RunnerTask) {
restoreRealTimers();
restoreNativeTimerGlobals();
await super.onBeforeRunTask(test);
}
override onBeforeTryTask(test: RunnerTask) {
restoreRealTimers();
restoreNativeTimerGlobals();
super.onBeforeTryTask(test);
}
override async onAfterRunSuite(suite: RunnerTestSuite) {
await super.onAfterRunSuite(suite);
if (this.config.isolate || !("filepath" in suite) || typeof suite.filepath !== "string") {
return;
}
const orderLogPath = process.env.OPENCLAW_VITEST_FILE_ORDER_LOG?.trim();
if (orderLogPath) {
fs.appendFileSync(orderLogPath, `END ${suite.filepath}\n`);
}
// Mirror the missing cleanup from Vitest isolate mode so shared workers do
// not carry file-scoped timers, stubs, spies, or stale module state
// forward into the next file.
restoreMocksThenRealTimers();
vi.unstubAllGlobals();
const testHome = getSharedTestHome();
vi.unstubAllEnvs();
restoreSharedTestHomeAfterEnvUnstub(testHome);
vi.clearAllMocks();
resetOpenClawGlobalRunState();
vi.resetModules();
this.moduleRunner?.mocker?.reset?.();
resetEvaluatedModules(this.workerState.evaluatedModules as EvaluatedModules, true);
}
}