mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 16:20:43 +00:00
test: share debug proxy reset helper
This commit is contained in:
47
extensions/test-support/debug-proxy-env-test-helpers.ts
Normal file
47
extensions/test-support/debug-proxy-env-test-helpers.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { afterEach, vi } from "vitest";
|
||||
|
||||
export const DEBUG_PROXY_ENV_KEYS = [
|
||||
"OPENCLAW_DEBUG_PROXY_ENABLED",
|
||||
"OPENCLAW_DEBUG_PROXY_DB_PATH",
|
||||
"OPENCLAW_DEBUG_PROXY_BLOB_DIR",
|
||||
"OPENCLAW_DEBUG_PROXY_SESSION_ID",
|
||||
] as const;
|
||||
|
||||
type DebugProxyEnvKey = (typeof DEBUG_PROXY_ENV_KEYS)[number];
|
||||
type DebugProxyEnvSnapshot = Partial<Record<DebugProxyEnvKey, string | undefined>>;
|
||||
|
||||
function snapshotDebugProxyEnv(): DebugProxyEnvSnapshot {
|
||||
return Object.fromEntries(
|
||||
DEBUG_PROXY_ENV_KEYS.map((key) => [key, process.env[key]]),
|
||||
) as DebugProxyEnvSnapshot;
|
||||
}
|
||||
|
||||
function restoreDebugProxyEnv(snapshot: DebugProxyEnvSnapshot): void {
|
||||
for (const key of DEBUG_PROXY_ENV_KEYS) {
|
||||
const value = snapshot[key];
|
||||
if (value === undefined) {
|
||||
delete process.env[key];
|
||||
} else {
|
||||
process.env[key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function installDebugProxyTestResetHooks() {
|
||||
const originalFetch = globalThis.fetch;
|
||||
let priorProxyEnv: DebugProxyEnvSnapshot = {};
|
||||
|
||||
afterEach(() => {
|
||||
globalThis.fetch = originalFetch;
|
||||
vi.restoreAllMocks();
|
||||
restoreDebugProxyEnv(priorProxyEnv);
|
||||
priorProxyEnv = {};
|
||||
});
|
||||
|
||||
return {
|
||||
captureProxyEnv() {
|
||||
priorProxyEnv = snapshotDebugProxyEnv();
|
||||
},
|
||||
originalFetch,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user