test: scope send state env helper

This commit is contained in:
Shakker
2026-06-23 14:30:54 +01:00
parent 52251261ca
commit 6834a2d47b

View File

@@ -8,6 +8,7 @@ import { jsonResult } from "../../agents/tools/common.js";
import type { ChannelPlugin } from "../../channels/plugins/types.js";
import { setActivePluginRegistry } from "../../plugins/runtime.js";
import { createTestRegistry } from "../../test-utils/channel-plugins.js";
import { captureEnv, setTestEnvValue } from "../../test-utils/env.js";
import type { GatewayRequestContext } from "./types.js";
type ResolveOutboundTarget = typeof import("../../infra/outbound/targets.js").resolveOutboundTarget;
@@ -213,17 +214,13 @@ async function runMessageActionRequest(
}
async function withTempOpenClawStateDir<T>(test: (stateDir: string) => Promise<T>): Promise<T> {
const previous = process.env.OPENCLAW_STATE_DIR;
const envSnapshot = captureEnv(["OPENCLAW_STATE_DIR"]);
const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "gateway-send-state-"));
process.env.OPENCLAW_STATE_DIR = stateDir;
setTestEnvValue("OPENCLAW_STATE_DIR", stateDir);
try {
return await test(stateDir);
} finally {
if (previous === undefined) {
delete process.env.OPENCLAW_STATE_DIR;
} else {
process.env.OPENCLAW_STATE_DIR = previous;
}
envSnapshot.restore();
await fs.rm(stateDir, { recursive: true, force: true });
}
}