mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-12 14:02:57 +00:00
test(e2e): assert mcp reconnect temp state
This commit is contained in:
@@ -11,7 +11,10 @@ import {
|
||||
maybeApprovePendingBridgePairing,
|
||||
waitFor,
|
||||
} from "./mcp-channels-harness.ts";
|
||||
import { createMcpClientTempState } from "./mcp-client-temp-state.ts";
|
||||
import {
|
||||
connectMcpClientWithPairingReconnect,
|
||||
createMcpClientTempState,
|
||||
} from "./mcp-client-temp-state.ts";
|
||||
|
||||
function summarizeSessionRows(rows: Array<Record<string, unknown>> | undefined) {
|
||||
return (rows ?? []).map((entry) => ({
|
||||
@@ -107,23 +110,17 @@ async function main() {
|
||||
"expected seeded gateway deliveryContext target",
|
||||
);
|
||||
|
||||
mcpHandle = await connectMcpClient({
|
||||
gatewayUrl,
|
||||
gatewayToken,
|
||||
mcpHandle = await connectMcpClientWithPairingReconnect({
|
||||
tempState: mcpTempState,
|
||||
connect: (tempState) =>
|
||||
connectMcpClient({
|
||||
gatewayUrl,
|
||||
gatewayToken,
|
||||
tempState,
|
||||
}),
|
||||
maybeApprovePairing: () => maybeApprovePendingBridgePairing(gateway),
|
||||
});
|
||||
let mcp = mcpHandle.client;
|
||||
|
||||
if (await maybeApprovePendingBridgePairing(gateway)) {
|
||||
await Promise.allSettled([mcp.close(), mcpHandle.transport.close()]);
|
||||
mcpHandle.cleanup();
|
||||
mcpHandle = await connectMcpClient({
|
||||
gatewayUrl,
|
||||
gatewayToken,
|
||||
tempState: mcpTempState,
|
||||
});
|
||||
mcp = mcpHandle.client;
|
||||
}
|
||||
const mcp = mcpHandle.client;
|
||||
const callTool = <T>(params: Parameters<typeof mcp.callTool>[0]) =>
|
||||
mcp.callTool(params, undefined, { timeout: 240_000 }) as Promise<T>;
|
||||
|
||||
|
||||
@@ -9,6 +9,12 @@ export type McpClientTempState = {
|
||||
tokenFile: string;
|
||||
};
|
||||
|
||||
export type ReconnectableMcpClientHandle = {
|
||||
cleanup: () => void;
|
||||
client: { close: () => Promise<unknown> };
|
||||
transport: { close: () => Promise<unknown> };
|
||||
};
|
||||
|
||||
export function createMcpClientTempState(params: {
|
||||
gatewayToken: string;
|
||||
tempRoot?: string;
|
||||
@@ -27,3 +33,28 @@ export function createMcpClientTempState(params: {
|
||||
tokenFile,
|
||||
};
|
||||
}
|
||||
|
||||
export async function connectMcpClientWithPairingReconnect<
|
||||
T extends ReconnectableMcpClientHandle,
|
||||
>(params: {
|
||||
connect: (tempState: McpClientTempState) => Promise<T>;
|
||||
maybeApprovePairing: () => Promise<boolean>;
|
||||
tempState: McpClientTempState;
|
||||
}): Promise<T> {
|
||||
let handle = await params.connect(params.tempState);
|
||||
let shouldReconnect: boolean;
|
||||
try {
|
||||
shouldReconnect = await params.maybeApprovePairing();
|
||||
} catch (error) {
|
||||
await Promise.allSettled([handle.client.close(), handle.transport.close()]);
|
||||
handle.cleanup();
|
||||
throw error;
|
||||
}
|
||||
if (!shouldReconnect) {
|
||||
return handle;
|
||||
}
|
||||
await Promise.allSettled([handle.client.close(), handle.transport.close()]);
|
||||
handle.cleanup();
|
||||
handle = await params.connect(params.tempState);
|
||||
return handle;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user