test(core): fix mcp-client test failure and add compliance transport tests

This commit is contained in:
Coco Sheng
2026-05-20 13:09:35 -04:00
parent 7183a57dc2
commit 2d10ac9e86
2 changed files with 67 additions and 1 deletions
+1 -1
View File
@@ -2213,7 +2213,7 @@ describe('mcp-client', () => {
// For SSEClientTransport, the fetch is private or passed to the SDK.
// We can check if it creates the transport successfully.
expect(transport).toBeInstanceOf(SSEClientTransport);
expect(unwrap(transport)).toBeInstanceOf(SSEClientTransport);
} finally {
vi.unstubAllEnvs();
vi.unstubAllGlobals();
@@ -128,4 +128,70 @@ describe('McpComplianceTransport', () => {
mockTransport.onmessage!(emptyResponse);
expect(onMessage.mock.calls[0][0].result.structuredContent).toBeUndefined();
});
it('should only parse text content blocks', async () => {
const mockTransport = createMockTransport();
const complianceTransport = new McpComplianceTransport(mockTransport);
const onMessage = vi.fn();
complianceTransport.onmessage = onMessage;
const mediaResponse: JSONRPCMessage = {
jsonrpc: '2.0',
id: 1,
result: {
content: [
{
type: 'image',
data: 'base64data',
mimeType: 'image/png',
},
],
},
};
mockTransport.onmessage!(mediaResponse);
expect(onMessage.mock.calls[0][0].result.structuredContent).toBeUndefined();
});
it('should handle responses with multiple content blocks (only fixes the first one if it is text)', async () => {
const mockTransport = createMockTransport();
const complianceTransport = new McpComplianceTransport(mockTransport);
const onMessage = vi.fn();
complianceTransport.onmessage = onMessage;
const multiResponse: JSONRPCMessage = {
jsonrpc: '2.0',
id: 1,
result: {
content: [
{ type: 'text', text: JSON.stringify({ first: true }) },
{ type: 'text', text: 'second item' },
],
},
};
mockTransport.onmessage!(multiResponse);
expect(onMessage.mock.calls[0][0].result.structuredContent).toEqual({
first: true,
});
});
it('should handle error responses gracefully', async () => {
const mockTransport = createMockTransport();
const complianceTransport = new McpComplianceTransport(mockTransport);
const onMessage = vi.fn();
complianceTransport.onmessage = onMessage;
const errorResponse: JSONRPCMessage = {
jsonrpc: '2.0',
id: 1,
error: {
code: -32000,
message: 'Internal error',
},
};
mockTransport.onmessage!(errorResponse);
expect(onMessage).toHaveBeenCalledWith(errorResponse);
});
});