mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 06:20:43 +00:00
test: mock proxy capture store
This commit is contained in:
@@ -1,8 +1,34 @@
|
||||
import { mkdtempSync, rmSync } from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const storeState = vi.hoisted(() => {
|
||||
const events: Record<string, unknown>[] = [];
|
||||
const store = {
|
||||
upsertSession: vi.fn(),
|
||||
endSession: vi.fn(),
|
||||
recordEvent: vi.fn((event: Record<string, unknown>) => {
|
||||
events.push(event);
|
||||
}),
|
||||
};
|
||||
return {
|
||||
events,
|
||||
store,
|
||||
closeDebugProxyCaptureStore: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("./store.sqlite.js", () => ({
|
||||
closeDebugProxyCaptureStore: storeState.closeDebugProxyCaptureStore,
|
||||
getDebugProxyCaptureStore: () => storeState.store,
|
||||
persistEventPayload: (
|
||||
_store: unknown,
|
||||
payload: { data?: Buffer | string | null; contentType?: string },
|
||||
) => ({
|
||||
contentType: payload.contentType,
|
||||
...(typeof payload.data === "string" ? { dataText: payload.data } : {}),
|
||||
}),
|
||||
safeJsonString: (value: unknown) => (value == null ? undefined : JSON.stringify(value)),
|
||||
}));
|
||||
|
||||
describe("debug proxy runtime", () => {
|
||||
const envKeys = [
|
||||
"OPENCLAW_DEBUG_PROXY_ENABLED",
|
||||
@@ -13,13 +39,16 @@ describe("debug proxy runtime", () => {
|
||||
] as const;
|
||||
const savedEnv = Object.fromEntries(envKeys.map((key) => [key, process.env[key]]));
|
||||
const originalFetch = globalThis.fetch;
|
||||
let tempDir = "";
|
||||
|
||||
beforeEach(() => {
|
||||
tempDir = mkdtempSync(path.join(os.tmpdir(), "openclaw-proxy-runtime-"));
|
||||
storeState.events.length = 0;
|
||||
storeState.store.upsertSession.mockClear();
|
||||
storeState.store.endSession.mockClear();
|
||||
storeState.store.recordEvent.mockClear();
|
||||
storeState.closeDebugProxyCaptureStore.mockClear();
|
||||
process.env.OPENCLAW_DEBUG_PROXY_ENABLED = "1";
|
||||
process.env.OPENCLAW_DEBUG_PROXY_DB_PATH = path.join(tempDir, "capture.sqlite");
|
||||
process.env.OPENCLAW_DEBUG_PROXY_BLOB_DIR = path.join(tempDir, "blobs");
|
||||
process.env.OPENCLAW_DEBUG_PROXY_DB_PATH = "/tmp/openclaw-proxy-runtime-test.sqlite";
|
||||
process.env.OPENCLAW_DEBUG_PROXY_BLOB_DIR = "/tmp/openclaw-proxy-runtime-test-blobs";
|
||||
process.env.OPENCLAW_DEBUG_PROXY_SESSION_ID = "runtime-test-session";
|
||||
process.env.OPENCLAW_DEBUG_PROXY_SOURCE_PROCESS = "runtime-test";
|
||||
});
|
||||
@@ -34,8 +63,6 @@ describe("debug proxy runtime", () => {
|
||||
process.env[key] = value;
|
||||
}
|
||||
}
|
||||
rmSync(tempDir, { recursive: true, force: true });
|
||||
vi.resetModules();
|
||||
});
|
||||
|
||||
it("captures ambient global fetch calls when debug proxy mode is enabled", async () => {
|
||||
@@ -44,7 +71,6 @@ describe("debug proxy runtime", () => {
|
||||
) as typeof fetch;
|
||||
|
||||
const runtime = await import("./runtime.js");
|
||||
const storeModule = await import("./store.sqlite.js");
|
||||
runtime.initializeDebugProxyCapture("test");
|
||||
await globalThis.fetch("https://api.minimax.io/anthropic/messages", {
|
||||
method: "POST",
|
||||
@@ -54,14 +80,9 @@ describe("debug proxy runtime", () => {
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
runtime.finalizeDebugProxyCapture();
|
||||
|
||||
const store = storeModule.getDebugProxyCaptureStore(
|
||||
process.env.OPENCLAW_DEBUG_PROXY_DB_PATH!,
|
||||
process.env.OPENCLAW_DEBUG_PROXY_BLOB_DIR!,
|
||||
);
|
||||
const events = store.getSessionEvents("runtime-test-session", 20);
|
||||
const events = storeState.events.filter((event) => event.sessionId === "runtime-test-session");
|
||||
expect(events.some((event) => event.host === "api.minimax.io")).toBe(true);
|
||||
expect(events.some((event) => event.kind === "request")).toBe(true);
|
||||
expect(events.some((event) => event.kind === "response")).toBe(true);
|
||||
store.close();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user