Files
openclaw/src/plugins/runtime/gateway-request-scope.test.ts
2026-03-28 04:28:54 +00:00

59 lines
1.9 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import type { PluginRuntimeGatewayRequestScope } from "./gateway-request-scope.js";
const TEST_SCOPE: PluginRuntimeGatewayRequestScope = {
context: {} as PluginRuntimeGatewayRequestScope["context"],
isWebchatConnect: (() => false) as PluginRuntimeGatewayRequestScope["isWebchatConnect"],
};
afterEach(() => {
vi.resetModules();
});
describe("gateway request scope", () => {
async function importGatewayRequestScopeModule() {
return await import("./gateway-request-scope.js");
}
async function withTestGatewayScope<T>(
run: (runtimeScope: Awaited<ReturnType<typeof importGatewayRequestScopeModule>>) => Promise<T>,
) {
const runtimeScope = await importGatewayRequestScopeModule();
return await runtimeScope.withPluginRuntimeGatewayRequestScope(TEST_SCOPE, async () => {
return await run(runtimeScope);
});
}
function expectGatewayScope(
runtimeScope: Awaited<ReturnType<typeof importGatewayRequestScopeModule>>,
expected: PluginRuntimeGatewayRequestScope,
) {
expect(runtimeScope.getPluginRuntimeGatewayRequestScope()).toEqual(expected);
}
async function expectGatewayScopeWithPluginId(pluginId: string) {
await withTestGatewayScope(async (runtimeScope) => {
await runtimeScope.withPluginRuntimePluginIdScope(pluginId, async () => {
expectGatewayScope(runtimeScope, {
...TEST_SCOPE,
pluginId,
});
});
});
}
it("reuses AsyncLocalStorage across reloaded module instances", async () => {
const first = await importGatewayRequestScopeModule();
await first.withPluginRuntimeGatewayRequestScope(TEST_SCOPE, async () => {
vi.resetModules();
const second = await importGatewayRequestScopeModule();
expectGatewayScope(second, TEST_SCOPE);
});
});
it("attaches plugin id to the active scope", async () => {
await expectGatewayScopeWithPluginId("voice-call");
});
});