test(e2e): assert mcp reconnect temp state

This commit is contained in:
Vincent Koc
2026-06-03 13:59:25 +02:00
parent 6d7b80fa1c
commit 03ccdb9fbc
4 changed files with 114 additions and 23 deletions

View File

@@ -1,8 +1,12 @@
import { existsSync, mkdtempSync, readFileSync, rmSync, statSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";
import { createMcpClientTempState } from "../../scripts/e2e/mcp-client-temp-state.js";
import { describe, expect, it, vi } from "vitest";
import {
connectMcpClientWithPairingReconnect,
createMcpClientTempState,
type McpClientTempState,
} from "../../scripts/e2e/mcp-client-temp-state.js";
describe("mcp-channels harness", () => {
it("creates unique client temp state and removes token files on cleanup", () => {
@@ -27,11 +31,69 @@ describe("mcp-channels harness", () => {
}
});
it("reuses one MCP temp state across the pairing reconnect path", () => {
const source = readFileSync("scripts/e2e/mcp-channels-docker-client.ts", "utf8");
it("reuses one MCP temp state across the pairing reconnect path", async () => {
const tempState = createMcpClientTempState({ gatewayToken: "pairing-token" });
const firstHandle = {
cleanup: vi.fn(),
client: { close: vi.fn(async () => undefined) },
transport: { close: vi.fn(async () => undefined) },
};
const secondHandle = {
cleanup: vi.fn(),
client: { close: vi.fn(async () => undefined) },
transport: { close: vi.fn(async () => undefined) },
};
const connectCalls: McpClientTempState[] = [];
const connect = vi.fn(async (state: McpClientTempState) => {
connectCalls.push(state);
return connectCalls.length === 1 ? firstHandle : secondHandle;
});
expect(source).toContain("const mcpTempState = createMcpClientTempState({ gatewayToken });");
expect(source.match(/tempState: mcpTempState/gu)).toHaveLength(2);
expect(source).toContain("mcpTempState.cleanup();");
try {
await expect(
connectMcpClientWithPairingReconnect({
connect,
maybeApprovePairing: async () => true,
tempState,
}),
).resolves.toBe(secondHandle);
expect(connect).toHaveBeenCalledTimes(2);
expect(connectCalls).toEqual([tempState, tempState]);
expect(firstHandle.client.close).toHaveBeenCalledOnce();
expect(firstHandle.transport.close).toHaveBeenCalledOnce();
expect(firstHandle.cleanup).toHaveBeenCalledOnce();
expect(secondHandle.cleanup).not.toHaveBeenCalled();
} finally {
tempState.cleanup();
}
});
it("cleans up the first MCP client when pairing approval fails", async () => {
const tempState = createMcpClientTempState({ gatewayToken: "pairing-token" });
const handle = {
cleanup: vi.fn(),
client: { close: vi.fn(async () => undefined) },
transport: { close: vi.fn(async () => undefined) },
};
const failure = new Error("pairing approval failed");
try {
await expect(
connectMcpClientWithPairingReconnect({
connect: async () => handle,
maybeApprovePairing: async () => {
throw failure;
},
tempState,
}),
).rejects.toBe(failure);
expect(handle.client.close).toHaveBeenCalledOnce();
expect(handle.transport.close).toHaveBeenCalledOnce();
expect(handle.cleanup).toHaveBeenCalledOnce();
} finally {
tempState.cleanup();
}
});
});