|
17 | 17 | import * as assert from 'assert'; |
18 | 18 | import { z } from 'genkit'; |
19 | 19 | import { describe, it } from 'node:test'; |
20 | | -import { AnthropicConfigSchema, resolveBetaEnabled } from '../src/types.js'; |
| 20 | +import { |
| 21 | + AnthropicConfigSchema, |
| 22 | + McpServerConfigSchema, |
| 23 | + resolveBetaEnabled, |
| 24 | +} from '../src/types.js'; |
21 | 25 |
|
22 | 26 | describe('resolveBetaEnabled', () => { |
23 | 27 | it('should return true when config.apiVersion is beta', () => { |
@@ -87,3 +91,170 @@ describe('resolveBetaEnabled', () => { |
87 | 91 | assert.strictEqual(resolveBetaEnabled(config, 'beta'), true); |
88 | 92 | }); |
89 | 93 | }); |
| 94 | + |
| 95 | +describe('McpServerConfigSchema', () => { |
| 96 | + it('should require HTTPS URL', () => { |
| 97 | + const httpConfig = { |
| 98 | + type: 'url' as const, |
| 99 | + url: 'http://example.com/mcp', |
| 100 | + name: 'test-server', |
| 101 | + }; |
| 102 | + const result = McpServerConfigSchema.safeParse(httpConfig); |
| 103 | + assert.strictEqual(result.success, false); |
| 104 | + if (!result.success) { |
| 105 | + assert.ok( |
| 106 | + result.error.issues.some((i) => |
| 107 | + i.message.includes('HTTPS') |
| 108 | + ) |
| 109 | + ); |
| 110 | + } |
| 111 | + }); |
| 112 | + |
| 113 | + it('should accept HTTPS URL', () => { |
| 114 | + const httpsConfig = { |
| 115 | + type: 'url' as const, |
| 116 | + url: 'https://example.com/mcp', |
| 117 | + name: 'test-server', |
| 118 | + }; |
| 119 | + const result = McpServerConfigSchema.safeParse(httpsConfig); |
| 120 | + assert.strictEqual(result.success, true); |
| 121 | + }); |
| 122 | +}); |
| 123 | + |
| 124 | +describe('AnthropicConfigSchema MCP validation', () => { |
| 125 | + it('should fail when server is not referenced by any toolset', () => { |
| 126 | + const config = { |
| 127 | + mcp_servers: [ |
| 128 | + { |
| 129 | + type: 'url' as const, |
| 130 | + url: 'https://example.com/mcp', |
| 131 | + name: 'orphan-server', |
| 132 | + }, |
| 133 | + ], |
| 134 | + // No mcp_toolsets |
| 135 | + }; |
| 136 | + const result = AnthropicConfigSchema.safeParse(config); |
| 137 | + assert.strictEqual(result.success, false); |
| 138 | + if (!result.success) { |
| 139 | + assert.ok( |
| 140 | + result.error.issues.some((i) => |
| 141 | + i.message.includes('not referenced by any toolset') |
| 142 | + ) |
| 143 | + ); |
| 144 | + } |
| 145 | + }); |
| 146 | + |
| 147 | + it('should fail when server is referenced by multiple toolsets', () => { |
| 148 | + const config = { |
| 149 | + mcp_servers: [ |
| 150 | + { |
| 151 | + type: 'url' as const, |
| 152 | + url: 'https://example.com/mcp', |
| 153 | + name: 'multi-ref-server', |
| 154 | + }, |
| 155 | + ], |
| 156 | + mcp_toolsets: [ |
| 157 | + { |
| 158 | + type: 'mcp_toolset' as const, |
| 159 | + mcp_server_name: 'multi-ref-server', |
| 160 | + }, |
| 161 | + { |
| 162 | + type: 'mcp_toolset' as const, |
| 163 | + mcp_server_name: 'multi-ref-server', |
| 164 | + }, |
| 165 | + ], |
| 166 | + }; |
| 167 | + const result = AnthropicConfigSchema.safeParse(config); |
| 168 | + assert.strictEqual(result.success, false); |
| 169 | + if (!result.success) { |
| 170 | + assert.ok( |
| 171 | + result.error.issues.some((i) => |
| 172 | + i.message.includes('referenced by 2 toolsets') |
| 173 | + ) |
| 174 | + ); |
| 175 | + } |
| 176 | + }); |
| 177 | + |
| 178 | + it('should pass when server is referenced by exactly one toolset', () => { |
| 179 | + const config = { |
| 180 | + mcp_servers: [ |
| 181 | + { |
| 182 | + type: 'url' as const, |
| 183 | + url: 'https://example.com/mcp', |
| 184 | + name: 'valid-server', |
| 185 | + }, |
| 186 | + ], |
| 187 | + mcp_toolsets: [ |
| 188 | + { |
| 189 | + type: 'mcp_toolset' as const, |
| 190 | + mcp_server_name: 'valid-server', |
| 191 | + }, |
| 192 | + ], |
| 193 | + }; |
| 194 | + const result = AnthropicConfigSchema.safeParse(config); |
| 195 | + assert.strictEqual(result.success, true); |
| 196 | + }); |
| 197 | + |
| 198 | + it('should fail when mcp_server names are not unique', () => { |
| 199 | + const config = { |
| 200 | + mcp_servers: [ |
| 201 | + { |
| 202 | + type: 'url' as const, |
| 203 | + url: 'https://example.com/mcp1', |
| 204 | + name: 'duplicate-name', |
| 205 | + }, |
| 206 | + { |
| 207 | + type: 'url' as const, |
| 208 | + url: 'https://example.com/mcp2', |
| 209 | + name: 'duplicate-name', |
| 210 | + }, |
| 211 | + ], |
| 212 | + mcp_toolsets: [ |
| 213 | + { |
| 214 | + type: 'mcp_toolset' as const, |
| 215 | + mcp_server_name: 'duplicate-name', |
| 216 | + }, |
| 217 | + ], |
| 218 | + }; |
| 219 | + const result = AnthropicConfigSchema.safeParse(config); |
| 220 | + assert.strictEqual(result.success, false); |
| 221 | + if (!result.success) { |
| 222 | + assert.ok( |
| 223 | + result.error.issues.some((i) => |
| 224 | + i.message.includes('must be unique') |
| 225 | + ) |
| 226 | + ); |
| 227 | + } |
| 228 | + }); |
| 229 | + |
| 230 | + it('should fail when toolset references unknown server', () => { |
| 231 | + const config = { |
| 232 | + mcp_servers: [ |
| 233 | + { |
| 234 | + type: 'url' as const, |
| 235 | + url: 'https://example.com/mcp', |
| 236 | + name: 'real-server', |
| 237 | + }, |
| 238 | + ], |
| 239 | + mcp_toolsets: [ |
| 240 | + { |
| 241 | + type: 'mcp_toolset' as const, |
| 242 | + mcp_server_name: 'real-server', |
| 243 | + }, |
| 244 | + { |
| 245 | + type: 'mcp_toolset' as const, |
| 246 | + mcp_server_name: 'unknown-server', |
| 247 | + }, |
| 248 | + ], |
| 249 | + }; |
| 250 | + const result = AnthropicConfigSchema.safeParse(config); |
| 251 | + assert.strictEqual(result.success, false); |
| 252 | + if (!result.success) { |
| 253 | + assert.ok( |
| 254 | + result.error.issues.some((i) => |
| 255 | + i.message.includes("unknown server 'unknown-server'") |
| 256 | + ) |
| 257 | + ); |
| 258 | + } |
| 259 | + }); |
| 260 | +}); |
0 commit comments