|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import type { UserConfig } from '../../types/config'; |
| 4 | +import { getInput } from '../input'; |
| 5 | + |
| 6 | +describe('input config', () => { |
| 7 | + describe('getInput', () => { |
| 8 | + it('should handle string input', () => { |
| 9 | + const userConfig: UserConfig = { |
| 10 | + input: 'https://example.com/openapi.yaml', |
| 11 | + output: 'src/client', |
| 12 | + }; |
| 13 | + |
| 14 | + const result = getInput(userConfig); |
| 15 | + expect(result.path).toBe('https://example.com/openapi.yaml'); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should transform ReadMe simple format input', () => { |
| 19 | + const userConfig: UserConfig = { |
| 20 | + input: 'readme:abc123', |
| 21 | + output: 'src/client', |
| 22 | + }; |
| 23 | + |
| 24 | + const result = getInput(userConfig); |
| 25 | + expect(result.path).toBe( |
| 26 | + 'https://dash.readme.com/api/v1/api-registry/abc123', |
| 27 | + ); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should transform ReadMe full format input', () => { |
| 31 | + const userConfig: UserConfig = { |
| 32 | + input: 'readme:@myorg/myproject#uuid123', |
| 33 | + output: 'src/client', |
| 34 | + }; |
| 35 | + |
| 36 | + const result = getInput(userConfig); |
| 37 | + expect(result.path).toBe( |
| 38 | + 'https://dash.readme.com/api/v1/api-registry/uuid123', |
| 39 | + ); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should handle ReadMe input with hyphens', () => { |
| 43 | + const userConfig: UserConfig = { |
| 44 | + input: 'readme:@my-org/my-project#test-uuid-123', |
| 45 | + output: 'src/client', |
| 46 | + }; |
| 47 | + |
| 48 | + const result = getInput(userConfig); |
| 49 | + expect(result.path).toBe( |
| 50 | + 'https://dash.readme.com/api/v1/api-registry/test-uuid-123', |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should handle object input with ReadMe path', () => { |
| 55 | + const userConfig: UserConfig = { |
| 56 | + input: { |
| 57 | + fetch: { |
| 58 | + headers: { |
| 59 | + Authorization: 'Bearer token', |
| 60 | + }, |
| 61 | + }, |
| 62 | + path: 'readme:abc123', |
| 63 | + }, |
| 64 | + output: 'src/client', |
| 65 | + }; |
| 66 | + |
| 67 | + const result = getInput(userConfig); |
| 68 | + expect(result.path).toBe( |
| 69 | + 'https://dash.readme.com/api/v1/api-registry/abc123', |
| 70 | + ); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should handle object input with ReadMe full format path', () => { |
| 74 | + const userConfig: UserConfig = { |
| 75 | + input: { |
| 76 | + path: 'readme:@org/project#uuid', |
| 77 | + watch: true, |
| 78 | + }, |
| 79 | + output: 'src/client', |
| 80 | + }; |
| 81 | + |
| 82 | + const result = getInput(userConfig); |
| 83 | + expect(result.path).toBe( |
| 84 | + 'https://dash.readme.com/api/v1/api-registry/uuid', |
| 85 | + ); |
| 86 | + expect(result.watch.enabled).toBe(true); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should handle HeyAPI input format (existing functionality)', () => { |
| 90 | + const userConfig: UserConfig = { |
| 91 | + input: { |
| 92 | + organization: 'myorg', |
| 93 | + project: 'myproject', |
| 94 | + }, |
| 95 | + output: 'src/client', |
| 96 | + }; |
| 97 | + |
| 98 | + const result = getInput(userConfig); |
| 99 | + expect(result.path).toBe('https://get.heyapi.dev'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should handle object input (existing functionality)', () => { |
| 103 | + const userConfig: UserConfig = { |
| 104 | + input: { |
| 105 | + info: { title: 'Test API', version: '1.0.0' }, |
| 106 | + openapi: '3.0.0', |
| 107 | + }, |
| 108 | + output: 'src/client', |
| 109 | + }; |
| 110 | + |
| 111 | + const result = getInput(userConfig); |
| 112 | + expect(result.path).toEqual({ |
| 113 | + info: { title: 'Test API', version: '1.0.0' }, |
| 114 | + openapi: '3.0.0', |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should not transform non-ReadMe string inputs', () => { |
| 119 | + const inputs = [ |
| 120 | + 'https://example.com/openapi.yaml', |
| 121 | + './local-file.yaml', |
| 122 | + '/absolute/path/to/file.json', |
| 123 | + 'file.yaml', |
| 124 | + ]; |
| 125 | + |
| 126 | + inputs.forEach((input) => { |
| 127 | + const userConfig: UserConfig = { input, output: 'src/client' }; |
| 128 | + const result = getInput(userConfig); |
| 129 | + expect(result.path).toBe(input); |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should handle watch options with ReadMe inputs', () => { |
| 134 | + const userConfig: UserConfig = { |
| 135 | + input: 'readme:abc123', |
| 136 | + output: 'src/client', |
| 137 | + watch: { |
| 138 | + enabled: true, |
| 139 | + interval: 2000, |
| 140 | + }, |
| 141 | + }; |
| 142 | + |
| 143 | + const result = getInput(userConfig); |
| 144 | + expect(result.path).toBe( |
| 145 | + 'https://dash.readme.com/api/v1/api-registry/abc123', |
| 146 | + ); |
| 147 | + expect(result.watch.enabled).toBe(true); |
| 148 | + expect(result.watch.interval).toBe(2000); |
| 149 | + }); |
| 150 | + |
| 151 | + it('should preserve other input object properties when transforming ReadMe path', () => { |
| 152 | + const userConfig: UserConfig = { |
| 153 | + input: { |
| 154 | + fetch: { |
| 155 | + headers: { 'X-Custom': 'value' }, |
| 156 | + }, |
| 157 | + path: 'readme:test123', |
| 158 | + watch: { enabled: true, interval: 1500 }, |
| 159 | + }, |
| 160 | + output: 'src/client', |
| 161 | + }; |
| 162 | + |
| 163 | + const result = getInput(userConfig); |
| 164 | + expect(result.path).toBe( |
| 165 | + 'https://dash.readme.com/api/v1/api-registry/test123', |
| 166 | + ); |
| 167 | + // Note: fetch options are preserved in the input object, not in the result |
| 168 | + // The watch options should be processed separately |
| 169 | + expect(result.watch.enabled).toBe(true); |
| 170 | + expect(result.watch.interval).toBe(1500); |
| 171 | + }); |
| 172 | + }); |
| 173 | + |
| 174 | + describe('error handling', () => { |
| 175 | + it('should throw error for invalid ReadMe format', () => { |
| 176 | + const userConfig: UserConfig = { |
| 177 | + input: 'readme:', |
| 178 | + output: 'src/client', |
| 179 | + }; |
| 180 | + |
| 181 | + expect(() => getInput(userConfig)).toThrow('Invalid ReadMe input format'); |
| 182 | + }); |
| 183 | + |
| 184 | + it('should throw error for invalid ReadMe UUID', () => { |
| 185 | + const userConfig: UserConfig = { |
| 186 | + input: 'readme:invalid uuid with spaces', |
| 187 | + output: 'src/client', |
| 188 | + }; |
| 189 | + |
| 190 | + expect(() => getInput(userConfig)).toThrow('Invalid ReadMe input format'); |
| 191 | + }); |
| 192 | + |
| 193 | + it('should throw error for invalid ReadMe format in object input', () => { |
| 194 | + const userConfig: UserConfig = { |
| 195 | + input: { |
| 196 | + path: 'readme:@org/project', |
| 197 | + }, |
| 198 | + output: 'src/client', |
| 199 | + }; |
| 200 | + |
| 201 | + expect(() => getInput(userConfig)).toThrow('Invalid ReadMe input format'); |
| 202 | + }); |
| 203 | + }); |
| 204 | +}); |
0 commit comments