test: added tests for checking correct error codes are thrown on request timeout and connection closed scenarios
This commit is contained in:
parent
d77cd0deb3
commit
8fa0a5c88b
1 changed files with 63 additions and 0 deletions
63
src/shared/protocol.test.ts
Normal file
63
src/shared/protocol.test.ts
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
import { Protocol } from "./protocol.js";
|
||||
import { Transport } from "./transport.js";
|
||||
import {
|
||||
McpError,
|
||||
ErrorCode,
|
||||
Request,
|
||||
Result,
|
||||
Notification,
|
||||
} from "../types.js";
|
||||
import { ZodType, z } from "zod";
|
||||
|
||||
// Mock Transport class
|
||||
class MockTransport implements Transport {
|
||||
onclose?: () => void;
|
||||
onerror?: (error: Error) => void;
|
||||
onmessage?: (message: unknown) => void;
|
||||
|
||||
async start(): Promise<void> {}
|
||||
async close(): Promise<void> {
|
||||
this.onclose?.();
|
||||
}
|
||||
async send(_message: unknown): Promise<void> {}
|
||||
}
|
||||
|
||||
describe("protocol tests", () => {
|
||||
let protocol: Protocol<Request, Notification, Result>;
|
||||
let transport: MockTransport;
|
||||
|
||||
beforeEach(() => {
|
||||
transport = new MockTransport();
|
||||
protocol = new (class extends Protocol<Request, Notification, Result> {
|
||||
protected assertCapabilityForMethod(): void {}
|
||||
protected assertNotificationCapability(): void {}
|
||||
protected assertRequestHandlerCapability(): void {}
|
||||
})();
|
||||
});
|
||||
|
||||
test("should throw a timeout error if the request exceeds the timeout", async () => {
|
||||
await protocol.connect(transport);
|
||||
const request = { method: "example", params: {} };
|
||||
try {
|
||||
const mockSchema: ZodType<{ result: string }> = z.object({
|
||||
result: z.string(),
|
||||
});
|
||||
await protocol.request(request, mockSchema, {
|
||||
timeout: 100,
|
||||
}); // Short timeout for test
|
||||
} catch (error) {
|
||||
expect(error).toBeInstanceOf(McpError);
|
||||
if (error instanceof McpError) {
|
||||
expect(error.code).toBe(ErrorCode.RequestTimeout);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test("should invoke onclose when the connection is closed", async () => {
|
||||
const oncloseMock = jest.fn();
|
||||
protocol.onclose = oncloseMock;
|
||||
await protocol.connect(transport);
|
||||
await transport.close();
|
||||
expect(oncloseMock).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Loading…
Reference in a new issue