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