-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtypes.test.ts
More file actions
193 lines (176 loc) · 5.29 KB
/
types.test.ts
File metadata and controls
193 lines (176 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { describe, it, expect } from '@jest/globals'
import {
JSONRPCRequestSchema,
JSONRPCResponseSchema,
JSONRPCNotificationSchema,
ToolSchema,
ResourceSchema,
ErrorCode,
} from '../src/types.js'
describe('Types and Schemas', () => {
describe('JSONRPCRequestSchema', () => {
it('should validate a valid request', () => {
const request = {
jsonrpc: '2.0',
id: 'test-123',
method: 'tools/call',
params: { name: 'test-tool' },
}
const result = JSONRPCRequestSchema.safeParse(request)
expect(result.success).toBe(true)
})
it('should validate request with numeric id', () => {
const request = {
jsonrpc: '2.0',
id: 123,
method: 'test',
}
const result = JSONRPCRequestSchema.safeParse(request)
expect(result.success).toBe(true)
})
it('should reject invalid jsonrpc version', () => {
const request = {
jsonrpc: '1.0',
id: 'test',
method: 'test',
}
const result = JSONRPCRequestSchema.safeParse(request)
expect(result.success).toBe(false)
})
it('should reject request without method', () => {
const request = {
jsonrpc: '2.0',
id: 'test',
}
const result = JSONRPCRequestSchema.safeParse(request)
expect(result.success).toBe(false)
})
})
describe('JSONRPCResponseSchema', () => {
it('should validate a successful response', () => {
const response = {
jsonrpc: '2.0',
id: 'test-123',
result: { data: 'success' },
}
const result = JSONRPCResponseSchema.safeParse(response)
expect(result.success).toBe(true)
})
it('should validate an error response', () => {
const response = {
jsonrpc: '2.0',
id: 'test-123',
error: {
code: ErrorCode.INVALID_PARAMS,
message: 'Invalid parameters',
},
}
const result = JSONRPCResponseSchema.safeParse(response)
expect(result.success).toBe(true)
})
it('should validate error response with data', () => {
const response = {
jsonrpc: '2.0',
id: 'test-123',
error: {
code: ErrorCode.INTERNAL_ERROR,
message: 'Internal error',
data: { details: 'More info' },
},
}
const result = JSONRPCResponseSchema.safeParse(response)
expect(result.success).toBe(true)
})
})
describe('JSONRPCNotificationSchema', () => {
it('should validate a valid notification', () => {
const notification = {
jsonrpc: '2.0',
method: 'notifications/server/online',
params: { server_name: 'test' },
}
const result = JSONRPCNotificationSchema.safeParse(notification)
expect(result.success).toBe(true)
})
it('should validate notification without params', () => {
const notification = {
jsonrpc: '2.0',
method: 'notifications/disconnected',
}
const result = JSONRPCNotificationSchema.safeParse(notification)
expect(result.success).toBe(true)
})
})
describe('ToolSchema', () => {
it('should validate a valid tool', () => {
const tool = {
name: 'test-tool',
description: 'A test tool',
inputSchema: {
type: 'object',
properties: {
input: { type: 'string' },
},
},
}
const result = ToolSchema.safeParse(tool)
expect(result.success).toBe(true)
})
it('should validate tool without description', () => {
const tool = {
name: 'minimal-tool',
inputSchema: {},
}
const result = ToolSchema.safeParse(tool)
expect(result.success).toBe(true)
})
it('should reject tool without name', () => {
const tool = {
inputSchema: {},
}
const result = ToolSchema.safeParse(tool)
expect(result.success).toBe(false)
})
})
describe('ResourceSchema', () => {
it('should validate a valid resource', () => {
const resource = {
uri: 'file:///path/to/resource',
name: 'Test Resource',
description: 'A test resource',
mimeType: 'text/plain',
}
const result = ResourceSchema.safeParse(resource)
expect(result.success).toBe(true)
})
it('should validate resource with minimal fields', () => {
const resource = {
uri: 'file:///path',
name: 'Minimal',
}
const result = ResourceSchema.safeParse(resource)
expect(result.success).toBe(true)
})
it('should reject resource without uri', () => {
const resource = {
name: 'No URI',
}
const result = ResourceSchema.safeParse(resource)
expect(result.success).toBe(false)
})
})
describe('ErrorCode', () => {
it('should have correct JSON-RPC error codes', () => {
expect(ErrorCode.PARSE_ERROR).toBe(-32700)
expect(ErrorCode.INVALID_REQUEST).toBe(-32600)
expect(ErrorCode.METHOD_NOT_FOUND).toBe(-32601)
expect(ErrorCode.INVALID_PARAMS).toBe(-32602)
expect(ErrorCode.INTERNAL_ERROR).toBe(-32603)
})
it('should have correct MCP-specific error codes', () => {
expect(ErrorCode.INVALID_MESSAGE).toBe(-32000)
expect(ErrorCode.TOOL_NOT_FOUND).toBe(-32001)
expect(ErrorCode.RESOURCE_NOT_FOUND).toBe(-32002)
})
})
})