Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Adolf is a fork/vendored clone of github.com/openclaw/openclaw (v2026.6.11), free to diverge. Tree copied sans upstream .git; upstream remote added for future syncs. Node pinned to 24 (.nvmrc); engines already require >=22.19. Preserves docs/ARCHITECTURE.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LeqyaxJF2nbRXJtae2kNB2
254 lines
7.7 KiB
TypeScript
254 lines
7.7 KiB
TypeScript
// 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);
|
|
}
|
|
}
|