Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { emitDescriptors } from '../src/cli/emit'
3
4describe('emitDescriptors', () => {
5 it('emits a ServerDescriptor type per server with typed tool inputs', async () => {
6 const out = await emitDescriptors({
7 weather: {
8 prefix: undefined,
9 surface: {
10 tools: [
11 {
12 name: 'get_weather',
13 inputSchema: {
14 type: 'object',
15 properties: { city: { type: 'string' } },
16 required: ['city'],
17 },
18 },
19 ],
20 resources: [],
21 prompts: [],
22 capabilities: { tools: {} },
23 },
24 },
25 })
26 expect(out).toContain('export interface WeatherServer')
27 expect(out).toContain('get_weather')
28 expect(out).toContain('city')
29 // Combined pool map, keyed by config key, referencing the per-server interface.
30 expect(out).toContain('export interface MCPServers')
31 expect(out).toMatch(/"weather":\s*WeatherServer/)
32 })
33
34 it('escapes hostile names and rejects pascal-case interface collisions', async () => {
35 const surface = {
36 tools: [
37 {
38 name: `quote'"\nbreak`,
39 inputSchema: { type: 'object', properties: {} },
40 },
41 ],
42 resources: [],
43 prompts: [],
44 capabilities: { tools: {} },
45 }
46 // Tool names with quotes/newlines must be escaped in the emitted TS.
47 const out = await emitDescriptors({
48 '1pm-server': { prefix: undefined, surface },
49 })
50 expect(out).toContain(JSON.stringify(`quote'"\nbreak`))
51 // Leading digit gets an underscore prefix to stay a valid identifier.
52 expect(out).toContain('export interface _1pmServerServer')
53
54 // `foo-bar` and `foo_bar` pascal-case identically — must throw, not emit
55 // duplicate interfaces.
56 await expect(
57 emitDescriptors({
58 'foo-bar': { prefix: undefined, surface },
59 foo_bar: { prefix: undefined, surface },
60 }),
61 ).rejects.toThrow(/collision/)
62 })
63})