fork of hey-api/openapi-ts because I need some additional things
1import { getRegistryUrl, inputToScalarPath, type Parsed, parseShorthand } from '../scalar';
2
3describe('readme utils', () => {
4 describe('parseShorthand', () => {
5 it('should parse full format with organization and project', () => {
6 const result = parseShorthand('@myorg/myproject');
7 expect(result).toEqual({
8 organization: '@myorg',
9 project: 'myproject',
10 });
11 });
12
13 it('should parse organization and project with hyphens', () => {
14 const result = parseShorthand('@my-org/my-project');
15 expect(result).toEqual({
16 organization: '@my-org',
17 project: 'my-project',
18 });
19 });
20
21 it('should throw error for invalid formats', () => {
22 expect(() => parseShorthand('')).toThrow('Invalid Scalar shorthand format');
23 expect(() => parseShorthand('@org')).toThrow('Invalid Scalar shorthand format');
24 expect(() => parseShorthand('@org/project#')).toThrow('Invalid Scalar shorthand format');
25 expect(() => parseShorthand('https://example.com')).toThrow(
26 'Invalid Scalar shorthand format',
27 );
28 });
29
30 it('should throw error for invalid UUID characters', () => {
31 expect(() => parseShorthand('abc@123')).toThrow('Invalid Scalar shorthand format');
32 expect(() => parseShorthand('abc/123')).toThrow('Invalid Scalar shorthand format');
33 expect(() => parseShorthand('abc#123')).toThrow('Invalid Scalar shorthand format');
34 expect(() => parseShorthand('abc 123')).toThrow('Invalid Scalar shorthand format');
35 });
36
37 it('should handle empty UUID', () => {
38 expect(() => parseShorthand('@org/project#')).toThrow('Invalid Scalar shorthand format');
39 });
40 });
41
42 describe('getRegistryUrl', () => {
43 it('should generate correct URL', () => {
44 expect(getRegistryUrl('@foo', 'bar')).toBe(
45 'https://registry.scalar.com/@foo/apis/bar/latest?format=json',
46 );
47 expect(getRegistryUrl('@foo-with-hyphens', 'bar')).toBe(
48 'https://registry.scalar.com/@foo-with-hyphens/apis/bar/latest?format=json',
49 );
50 });
51 });
52
53 describe('inputToScalarPath', () => {
54 it('should transform full format to API URL', () => {
55 const result = inputToScalarPath('scalar:@foo/bar');
56 expect(result).toEqual({
57 organization: '@foo',
58 path: 'https://registry.scalar.com/@foo/apis/bar/latest?format=json',
59 project: 'bar',
60 registry: 'scalar',
61 });
62 });
63
64 it('should throw error for invalid inputs', () => {
65 expect(() => inputToScalarPath('invalid')).toThrow('Invalid Scalar shorthand format');
66 expect(() => inputToScalarPath('')).toThrow('Invalid Scalar shorthand format');
67 });
68 });
69
70 describe('integration scenarios', () => {
71 const validInputs: ReadonlyArray<{ expected: Parsed; input: string }> = [
72 {
73 expected: { organization: '@org', project: 'proj' },
74 input: '@org/proj',
75 },
76 {
77 expected: {
78 organization: '@my-org',
79 project: 'my-project',
80 },
81 input: '@my-org/my-project',
82 },
83 ];
84
85 it.each(validInputs)('should handle $input correctly', ({ expected, input }) => {
86 expect(parseShorthand(input)).toEqual(expected);
87 expect(inputToScalarPath(`scalar:${input}`)).toEqual({
88 organization: expected.organization,
89 path: `https://registry.scalar.com/${expected.organization}/apis/${expected.project}/latest?format=json`,
90 project: expected.project,
91 registry: 'scalar',
92 });
93 });
94
95 const invalidInputs = [
96 '',
97 '@',
98 '@org',
99 '@org/',
100 'uuid with spaces',
101 'uuid@invalid',
102 'uuid/invalid',
103 'uuid#invalid',
104 'https://example.com',
105 './local-file.yaml',
106 ];
107
108 it.each(invalidInputs)('should reject invalid input: %s', (input) => {
109 expect(() => parseShorthand(input)).toThrow();
110 });
111 });
112});