fork of hey-api/openapi-ts because I need some additional things
at feat/skip-token 143 lines 5.0 kB view raw
1import { getRegistryUrl, inputToReadmePath, type Parsed, parseShorthand } from '../readme'; 2 3describe('readme utils', () => { 4 describe('parseShorthand', () => { 5 it('should parse simple UUID format', () => { 6 const result = parseShorthand('abc123'); 7 expect(result).toEqual({ uuid: 'abc123' }); 8 }); 9 10 it('should parse UUID with hyphens', () => { 11 const result = parseShorthand('test-uuid-123'); 12 expect(result).toEqual({ uuid: 'test-uuid-123' }); 13 }); 14 15 it('should parse full format with organization and project', () => { 16 const result = parseShorthand('@myorg/myproject#uuid123'); 17 expect(result).toEqual({ 18 organization: 'myorg', 19 project: 'myproject', 20 uuid: 'uuid123', 21 }); 22 }); 23 24 it('should parse organization and project with hyphens', () => { 25 const result = parseShorthand('@my-org/my-project#test-uuid'); 26 expect(result).toEqual({ 27 organization: 'my-org', 28 project: 'my-project', 29 uuid: 'test-uuid', 30 }); 31 }); 32 33 it('should throw error for invalid formats', () => { 34 expect(() => parseShorthand('')).toThrow('Invalid ReadMe shorthand format'); 35 expect(() => parseShorthand('@org')).toThrow('Invalid ReadMe shorthand format'); 36 expect(() => parseShorthand('@org/project')).toThrow('Invalid ReadMe shorthand format'); 37 expect(() => parseShorthand('@org/project#')).toThrow('Invalid ReadMe shorthand format'); 38 expect(() => parseShorthand('https://example.com')).toThrow( 39 'Invalid ReadMe shorthand format', 40 ); 41 }); 42 43 it('should throw error for invalid UUID characters', () => { 44 expect(() => parseShorthand('abc@123')).toThrow('Invalid ReadMe shorthand format'); 45 expect(() => parseShorthand('abc/123')).toThrow('Invalid ReadMe shorthand format'); 46 expect(() => parseShorthand('abc#123')).toThrow('Invalid ReadMe shorthand format'); 47 expect(() => parseShorthand('abc 123')).toThrow('Invalid ReadMe shorthand format'); 48 }); 49 50 it('should handle empty UUID', () => { 51 expect(() => parseShorthand('@org/project#')).toThrow('Invalid ReadMe shorthand format'); 52 }); 53 }); 54 55 describe('getRegistryUrl', () => { 56 it('should generate correct URL', () => { 57 expect(getRegistryUrl('abc123')).toBe('https://dash.readme.com/api/v1/api-registry/abc123'); 58 expect(getRegistryUrl('test-uuid-with-hyphens')).toBe( 59 'https://dash.readme.com/api/v1/api-registry/test-uuid-with-hyphens', 60 ); 61 }); 62 }); 63 64 describe('inputToReadmePath', () => { 65 it('should transform simple UUID format to API URL', () => { 66 const result = inputToReadmePath('readme:abc123'); 67 expect(result).toEqual({ 68 path: 'https://dash.readme.com/api/v1/api-registry/abc123', 69 registry: 'readme', 70 uuid: 'abc123', 71 }); 72 }); 73 74 it('should transform full format to API URL', () => { 75 const result = inputToReadmePath('readme:@myorg/myproject#uuid123'); 76 expect(result).toEqual({ 77 organization: 'myorg', 78 path: 'https://dash.readme.com/api/v1/api-registry/uuid123', 79 project: 'myproject', 80 registry: 'readme', 81 uuid: 'uuid123', 82 }); 83 }); 84 85 it('should throw error for invalid inputs', () => { 86 expect(() => inputToReadmePath('invalid')).toThrow('Invalid ReadMe shorthand format'); 87 expect(() => inputToReadmePath('')).toThrow('Invalid ReadMe shorthand format'); 88 }); 89 }); 90 91 describe('integration scenarios', () => { 92 const validInputs: ReadonlyArray<{ expected: Parsed; input: string }> = [ 93 { expected: { uuid: 'simple123' }, input: 'simple123' }, 94 { 95 expected: { uuid: 'uuid-with-hyphens' }, 96 input: 'uuid-with-hyphens', 97 }, 98 { expected: { uuid: 'UUID123' }, input: 'UUID123' }, 99 { 100 expected: { organization: 'org', project: 'proj', uuid: 'uuid' }, 101 input: '@org/proj#uuid', 102 }, 103 { 104 expected: { 105 organization: 'my-org', 106 project: 'my-project', 107 uuid: 'my-uuid', 108 }, 109 input: '@my-org/my-project#my-uuid', 110 }, 111 ]; 112 113 it.each(validInputs)('should handle $input correctly', ({ expected, input }) => { 114 expect(parseShorthand(input)).toEqual(expected); 115 expect(inputToReadmePath(`readme:${input}`)).toEqual({ 116 organization: expected.organization, 117 path: `https://dash.readme.com/api/v1/api-registry/${expected.uuid}`, 118 project: expected.project, 119 registry: 'readme', 120 uuid: expected.uuid, 121 }); 122 }); 123 124 const invalidInputs = [ 125 '', 126 '@', 127 '@org', 128 '@org/', 129 '@org/proj', 130 '@org/proj#', 131 'uuid with spaces', 132 'uuid@invalid', 133 'uuid/invalid', 134 'uuid#invalid', 135 'https://example.com', 136 './local-file.yaml', 137 ]; 138 139 it.each(invalidInputs)('should reject invalid input: %s', (input) => { 140 expect(() => parseShorthand(input)).toThrow(); 141 }); 142 }); 143});