Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import {
3 generateTypeStubs,
4 jsonSchemaToTypeScript,
5} from '../src/type-generator/json-schema-to-ts'
6
7describe('jsonSchemaToTypeScript', () => {
8 it('handles string type', () => {
9 const result = jsonSchemaToTypeScript({ type: 'string' }, 'MyType')
10 expect(result.name).toBe('string')
11 expect(result.declaration).toBe('')
12 })
13
14 it('handles number type', () => {
15 const result = jsonSchemaToTypeScript({ type: 'number' }, 'MyType')
16 expect(result.name).toBe('number')
17 })
18
19 it('handles integer type as number', () => {
20 const result = jsonSchemaToTypeScript({ type: 'integer' }, 'MyType')
21 expect(result.name).toBe('number')
22 })
23
24 it('handles boolean type', () => {
25 const result = jsonSchemaToTypeScript({ type: 'boolean' }, 'MyType')
26 expect(result.name).toBe('boolean')
27 })
28
29 it('handles null type', () => {
30 const result = jsonSchemaToTypeScript({ type: 'null' }, 'MyType')
31 expect(result.name).toBe('null')
32 })
33
34 it('generates interface for objects with properties', () => {
35 const schema = {
36 type: 'object',
37 properties: {
38 name: { type: 'string' },
39 age: { type: 'number' },
40 },
41 required: ['name'],
42 }
43 const result = jsonSchemaToTypeScript(schema, 'Person')
44 expect(result.name).toBe('Person')
45 expect(result.declaration).toContain('interface Person')
46 expect(result.declaration).toContain('name: string;')
47 expect(result.declaration).toContain('age?: number;')
48 })
49
50 it('marks optional vs required properties', () => {
51 const schema = {
52 type: 'object',
53 properties: {
54 required: { type: 'string' },
55 optional: { type: 'string' },
56 },
57 required: ['required'],
58 }
59 const result = jsonSchemaToTypeScript(schema, 'Test')
60 expect(result.declaration).toContain('required: string;')
61 expect(result.declaration).toContain('optional?: string;')
62 })
63
64 it('handles nested objects as inline types', () => {
65 const schema = {
66 type: 'object',
67 properties: {
68 address: {
69 type: 'object',
70 properties: {
71 street: { type: 'string' },
72 },
73 },
74 },
75 }
76 const result = jsonSchemaToTypeScript(schema, 'User')
77 expect(result.declaration).toContain('address?:')
78 expect(result.declaration).toContain('street')
79 })
80
81 it('handles arrays with items', () => {
82 const schema = {
83 type: 'array',
84 items: { type: 'string' },
85 }
86 const result = jsonSchemaToTypeScript(schema, 'List')
87 expect(result.name).toBe('Array<string>')
88 })
89
90 it('handles arrays without items', () => {
91 const schema = { type: 'array' }
92 const result = jsonSchemaToTypeScript(schema, 'List')
93 expect(result.name).toBe('Array<unknown>')
94 })
95
96 it('handles enums as union types', () => {
97 const schema = { enum: ['red', 'green', 'blue'] }
98 const result = jsonSchemaToTypeScript(schema, 'Color')
99 expect(result.name).toBe('"red" | "green" | "blue"')
100 })
101
102 it('keeps enums and consts on typed schemas', () => {
103 const schema = {
104 type: 'object',
105 properties: {
106 aggregation: { type: 'string', enum: ['sum', 'none'] },
107 mode: { type: 'string', const: 'auto' },
108 scope: { type: 'string', enum: ['all', 'one'], const: 'one' },
109 },
110 }
111 const result = jsonSchemaToTypeScript(schema, 'Measure')
112 expect(result.declaration).toContain('aggregation?: "sum" | "none";')
113 expect(result.declaration).toContain('mode?: "auto";')
114 expect(result.declaration).toContain('scope?: "one";')
115 })
116
117 it('keeps an enum or const on an object schema as a literal type', () => {
118 const properties = { id: { type: 'string' } }
119 const withEnum = jsonSchemaToTypeScript(
120 { type: 'object', properties, enum: [{ id: 'a' }] },
121 'Choice',
122 )
123 expect(withEnum).toEqual({ name: '{"id":"a"}', declaration: '' })
124 const withConst = jsonSchemaToTypeScript(
125 { type: 'object', properties, const: { id: 'b' } },
126 'Choice',
127 )
128 expect(withConst).toEqual({ name: '{"id":"b"}', declaration: '' })
129 })
130
131 it('emits property descriptions as JSDoc unless disabled', () => {
132 const schema = {
133 type: 'object',
134 properties: {
135 unit: { type: 'string', description: 'ISO currency or %, e.g. */EUR' },
136 },
137 }
138 expect(jsonSchemaToTypeScript(schema, 'Measure').declaration).toBe(
139 'interface Measure {\n /** ISO currency or %, e.g. *\\/EUR */\n unit?: string;\n}',
140 )
141 expect(
142 jsonSchemaToTypeScript(schema, 'Measure', false).declaration,
143 ).not.toContain('/**')
144 })
145
146 it('handles anyOf as union', () => {
147 const schema = {
148 anyOf: [{ type: 'string' }, { type: 'number' }],
149 }
150 const result = jsonSchemaToTypeScript(schema, 'Union')
151 expect(result.name).toBe('string | number')
152 })
153
154 it('handles oneOf as union', () => {
155 const schema = {
156 oneOf: [{ type: 'boolean' }, { type: 'null' }],
157 }
158 const result = jsonSchemaToTypeScript(schema, 'Union')
159 expect(result.name).toBe('boolean | null')
160 })
161
162 it('handles type arrays as union', () => {
163 const schema = { type: ['string', 'null'] }
164 const result = jsonSchemaToTypeScript(schema, 'Nullable')
165 expect(result.name).toBe('string | null')
166 })
167
168 it('returns unknown for empty/unknown schemas', () => {
169 const result = jsonSchemaToTypeScript({}, 'Mystery')
170 expect(result.name).toBe('unknown')
171 })
172})
173
174describe('generateTypeStubs', () => {
175 it('generates full declarations with function signatures and JSDoc', () => {
176 const bindings = {
177 external_fetch: {
178 name: 'external_fetch',
179 description: 'Fetch data from API',
180 inputSchema: {
181 type: 'object',
182 properties: { url: { type: 'string' } },
183 required: ['url'],
184 },
185 execute: async () => ({}),
186 },
187 }
188
189 const stubs = generateTypeStubs(bindings)
190 expect(stubs).toContain('interface External_fetchInput')
191 expect(stubs).toContain('url: string')
192 expect(stubs).toContain(
193 'declare function external_fetch(input: External_fetchInput)',
194 )
195 expect(stubs).toContain('/** Fetch data from API */')
196 })
197
198 it('omits JSDoc when includeDescriptions is false', () => {
199 const bindings = {
200 external_fetch: {
201 name: 'external_fetch',
202 description: 'Fetch data from API',
203 inputSchema: {
204 type: 'object',
205 properties: { url: { type: 'string', description: 'Target URL' } },
206 },
207 outputSchema: {
208 type: 'object',
209 properties: { body: { type: 'string', description: 'Raw body' } },
210 },
211 execute: async () => ({}),
212 },
213 }
214
215 const stubs = generateTypeStubs(bindings, { includeDescriptions: false })
216 expect(stubs).not.toContain('/**')
217 expect(stubs).toContain('declare function external_fetch')
218 })
219
220 it('includes output type when outputSchema is present', () => {
221 const bindings = {
222 external_fetch: {
223 name: 'external_fetch',
224 description: 'Fetch data',
225 inputSchema: {
226 type: 'object',
227 properties: { q: { type: 'string' } },
228 },
229 outputSchema: {
230 type: 'object',
231 properties: { data: { type: 'string' } },
232 },
233 execute: async () => ({}),
234 },
235 }
236
237 const stubs = generateTypeStubs(bindings)
238 expect(stubs).toContain('External_fetchOutput')
239 expect(stubs).toContain('Promise<External_fetchOutput>')
240 })
241})