|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { JSONRPC_VERSION } from './constants.js'; |
| 4 | +import { isCallToolResult, isJSONRPCErrorResponse, isJSONRPCResponse, isJSONRPCResultResponse } from './guards.js'; |
| 5 | + |
| 6 | +describe('isJSONRPCResponse', () => { |
| 7 | + it('returns true for a valid result response', () => { |
| 8 | + expect( |
| 9 | + isJSONRPCResponse({ |
| 10 | + jsonrpc: JSONRPC_VERSION, |
| 11 | + id: 1, |
| 12 | + result: {} |
| 13 | + }) |
| 14 | + ).toBe(true); |
| 15 | + }); |
| 16 | + |
| 17 | + it('returns true for a valid error response', () => { |
| 18 | + expect( |
| 19 | + isJSONRPCResponse({ |
| 20 | + jsonrpc: JSONRPC_VERSION, |
| 21 | + id: 1, |
| 22 | + error: { code: -32_600, message: 'Invalid Request' } |
| 23 | + }) |
| 24 | + ).toBe(true); |
| 25 | + }); |
| 26 | + |
| 27 | + it('returns false for a request', () => { |
| 28 | + expect( |
| 29 | + isJSONRPCResponse({ |
| 30 | + jsonrpc: JSONRPC_VERSION, |
| 31 | + id: 1, |
| 32 | + method: 'test' |
| 33 | + }) |
| 34 | + ).toBe(false); |
| 35 | + }); |
| 36 | + |
| 37 | + it('returns false for a notification', () => { |
| 38 | + expect( |
| 39 | + isJSONRPCResponse({ |
| 40 | + jsonrpc: JSONRPC_VERSION, |
| 41 | + method: 'test' |
| 42 | + }) |
| 43 | + ).toBe(false); |
| 44 | + }); |
| 45 | + |
| 46 | + it('returns false for arbitrary objects', () => { |
| 47 | + expect(isJSONRPCResponse({ foo: 'bar' })).toBe(false); |
| 48 | + }); |
| 49 | + |
| 50 | + it('narrows the type correctly', () => { |
| 51 | + const value: unknown = { |
| 52 | + jsonrpc: JSONRPC_VERSION, |
| 53 | + id: 1, |
| 54 | + result: { content: [] } |
| 55 | + }; |
| 56 | + if (isJSONRPCResponse(value)) { |
| 57 | + // Type should be narrowed to JSONRPCResponse |
| 58 | + expect(value.jsonrpc).toBe(JSONRPC_VERSION); |
| 59 | + expect(value.id).toBe(1); |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + it('agrees with isJSONRPCResultResponse || isJSONRPCErrorResponse', () => { |
| 64 | + const values = [ |
| 65 | + { jsonrpc: JSONRPC_VERSION, id: 1, result: {} }, |
| 66 | + { jsonrpc: JSONRPC_VERSION, id: 2, error: { code: -1, message: 'err' } }, |
| 67 | + { jsonrpc: JSONRPC_VERSION, id: 3, method: 'test' }, |
| 68 | + { jsonrpc: JSONRPC_VERSION, method: 'notify' }, |
| 69 | + { foo: 'bar' }, |
| 70 | + null, |
| 71 | + 42 |
| 72 | + ]; |
| 73 | + for (const v of values) { |
| 74 | + expect(isJSONRPCResponse(v)).toBe(isJSONRPCResultResponse(v) || isJSONRPCErrorResponse(v)); |
| 75 | + } |
| 76 | + }); |
| 77 | +}); |
| 78 | + |
| 79 | +describe('isCallToolResult', () => { |
| 80 | + it('returns false for an empty object (content is required)', () => { |
| 81 | + expect(isCallToolResult({})).toBe(false); |
| 82 | + }); |
| 83 | + |
| 84 | + it('returns true for a result with content', () => { |
| 85 | + expect( |
| 86 | + isCallToolResult({ |
| 87 | + content: [{ type: 'text', text: 'hello' }] |
| 88 | + }) |
| 89 | + ).toBe(true); |
| 90 | + }); |
| 91 | + |
| 92 | + it('returns true for a result with isError', () => { |
| 93 | + expect( |
| 94 | + isCallToolResult({ |
| 95 | + content: [{ type: 'text', text: 'fail' }], |
| 96 | + isError: true |
| 97 | + }) |
| 98 | + ).toBe(true); |
| 99 | + }); |
| 100 | + |
| 101 | + it('returns true for a result with structuredContent', () => { |
| 102 | + expect( |
| 103 | + isCallToolResult({ |
| 104 | + content: [], |
| 105 | + structuredContent: { key: 'value' } |
| 106 | + }) |
| 107 | + ).toBe(true); |
| 108 | + }); |
| 109 | + |
| 110 | + it('returns false for non-objects', () => { |
| 111 | + expect(isCallToolResult(null)).toBe(false); |
| 112 | + expect(isCallToolResult(42)).toBe(false); |
| 113 | + expect(isCallToolResult('string')).toBe(false); |
| 114 | + }); |
| 115 | + |
| 116 | + it('returns false for invalid content items', () => { |
| 117 | + expect( |
| 118 | + isCallToolResult({ |
| 119 | + content: [{ type: 'invalid' }] |
| 120 | + }) |
| 121 | + ).toBe(false); |
| 122 | + }); |
| 123 | +}); |
0 commit comments