|
| 1 | +// tests/40-emit-utility/09-server-url-generator.spec.ts |
| 2 | + |
| 3 | +import { describe, expect, it } from 'vitest'; |
| 4 | +import { Project } from 'ts-morph'; |
| 5 | +import { SwaggerParser } from '@src/core/parser.js'; |
| 6 | +import { ServerUrlGenerator } from '@src/service/emit/utility/server-url.generator.js'; |
| 7 | +import { createTestProject } from '../shared/helpers.js'; |
| 8 | +import ts from 'typescript'; |
| 9 | + |
| 10 | +describe('Emitter: ServerUrlGenerator', () => { |
| 11 | + |
| 12 | + const runGenerator = (servers: any[]) => { |
| 13 | + const project = createTestProject(); |
| 14 | + const parser = new SwaggerParser({ |
| 15 | + openapi: '3.0.0', |
| 16 | + info: { title: 'Test', version: '1.0' }, |
| 17 | + paths: {}, |
| 18 | + servers |
| 19 | + } as any, { options: {} } as any); |
| 20 | + |
| 21 | + new ServerUrlGenerator(parser, project).generate('/out'); |
| 22 | + return project; |
| 23 | + }; |
| 24 | + |
| 25 | + const compileHelper = (project: Project) => { |
| 26 | + const sourceFile = project.getSourceFileOrThrow('/out/utils/server-url.ts'); |
| 27 | + const startText = sourceFile.getText(); |
| 28 | + // Removing export keywords to evaluate in this context as strict CommonJS/Script |
| 29 | + const jsCode = ts.transpile(startText.replace(/export /g, ''), { |
| 30 | + target: ts.ScriptTarget.ESNext, |
| 31 | + module: ts.ModuleKind.CommonJS |
| 32 | + }); |
| 33 | + const moduleScope = { API_SERVERS: [], getServerUrl: null as any }; |
| 34 | + // Evaluate in simple scope |
| 35 | + new Function('scope', ` |
| 36 | + ${jsCode} |
| 37 | + scope.API_SERVERS = API_SERVERS; |
| 38 | + scope.getServerUrl = getServerUrl; |
| 39 | + `)(moduleScope); |
| 40 | + return moduleScope; |
| 41 | + }; |
| 42 | + |
| 43 | + it('should not generate file if no servers are defined', () => { |
| 44 | + const project = runGenerator([]); |
| 45 | + expect(project.getSourceFile('/out/utils/server-url.ts')).toBeUndefined(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should generate API_SERVERS constant', () => { |
| 49 | + const project = runGenerator([ |
| 50 | + { url: 'https://api.example.com', description: 'Production' } |
| 51 | + ]); |
| 52 | + const text = project.getSourceFileOrThrow('/out/utils/server-url.ts').getText(); |
| 53 | + expect(text).toContain('export const API_SERVERS: ServerConfiguration[] = ['); |
| 54 | + expect(text).toContain('"url": "https://api.example.com"'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should generate logic to substitute simple variables', () => { |
| 58 | + const project = runGenerator([ |
| 59 | + { |
| 60 | + url: 'https://{env}.example.com/v1', |
| 61 | + variables: { env: { default: 'dev' } } |
| 62 | + } |
| 63 | + ]); |
| 64 | + |
| 65 | + const { getServerUrl } = compileHelper(project); |
| 66 | + |
| 67 | + // Use default |
| 68 | + expect(getServerUrl(0)).toBe('https://dev.example.com/v1'); |
| 69 | + // Override |
| 70 | + expect(getServerUrl(0, { env: 'prod' })).toBe('https://prod.example.com/v1'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should generate logic to look up server by description', () => { |
| 74 | + const project = runGenerator([ |
| 75 | + { url: 'https://dev.api.com', description: 'Development' }, |
| 76 | + { url: 'https://prod.api.com', description: 'Production' } |
| 77 | + ]); |
| 78 | + |
| 79 | + const { getServerUrl } = compileHelper(project); |
| 80 | + |
| 81 | + expect(getServerUrl('Production')).toBe('https://prod.api.com'); |
| 82 | + expect(getServerUrl('Development')).toBe('https://dev.api.com'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should throw error if server is not found', () => { |
| 86 | + const project = runGenerator([{ url: '/' }]); |
| 87 | + const { getServerUrl } = compileHelper(project); |
| 88 | + expect(() => getServerUrl(99)).toThrow('Server not found: 99'); |
| 89 | + expect(() => getServerUrl('Unknown')).toThrow('Server not found: Unknown'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should handle multiple variables', () => { |
| 93 | + const project = runGenerator([ |
| 94 | + { |
| 95 | + url: '{protocol}://{host}:{port}/{base}', |
| 96 | + variables: { |
| 97 | + protocol: { default: 'https' }, |
| 98 | + host: { default: 'localhost' }, |
| 99 | + port: { default: '8080' }, |
| 100 | + base: { default: 'api' } |
| 101 | + } |
| 102 | + } |
| 103 | + ]); |
| 104 | + const { getServerUrl } = compileHelper(project); |
| 105 | + |
| 106 | + // Defaults |
| 107 | + expect(getServerUrl(0)).toBe('https://localhost:8080/api'); |
| 108 | + // Partial override |
| 109 | + expect(getServerUrl(0, { port: '3000', protocol: 'http' })).toBe('http://localhost:3000/api'); |
| 110 | + }); |
| 111 | +}); |
0 commit comments