|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { resolveSchemaRef, loadArchitectureAndPattern, loadArchitecture, loadPattern, loadPatternFromArchitectureIfPresent } from './loading-helpers'; |
| 3 | +import { CALM_HUB_PROTO, DocumentLoader } from './document-loader'; |
| 4 | +import { SchemaDirectory } from '../schema-directory'; |
| 5 | +import { Logger } from '../logger'; |
| 6 | + |
| 7 | +describe('resolveSchemaRef', () => { |
| 8 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 9 | + const mockLogger: any = { debug: vi.fn(), error: vi.fn(), info: vi.fn(), warn: vi.fn() }; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + vi.resetAllMocks(); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should return http URLs unchanged', () => { |
| 16 | + const result = resolveSchemaRef('http://example.com/schema.json', '/path/to/arch.json', mockLogger); |
| 17 | + expect(result).toBe('http://example.com/schema.json'); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should return https URLs unchanged', () => { |
| 21 | + const result = resolveSchemaRef('https://calm.finos.org/schema.json', '/path/to/arch.json', mockLogger); |
| 22 | + expect(result).toBe('https://calm.finos.org/schema.json'); |
| 23 | + }); |
| 24 | + |
| 25 | + it(`should return ${CALM_HUB_PROTO} protocol URLs unchanged`, () => { |
| 26 | + const result = resolveSchemaRef(`${CALM_HUB_PROTO}//namespace/schema`, '/path/to/arch.json', mockLogger); |
| 27 | + expect(result).toBe(`${CALM_HUB_PROTO}//namespace/schema`); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should return file:// URLs unchanged', () => { |
| 31 | + const result = resolveSchemaRef('file:///absolute/path/schema.json', '/path/to/arch.json', mockLogger); |
| 32 | + expect(result).toBe('file:///absolute/path/schema.json'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should return absolute file paths unchanged', () => { |
| 36 | + const result = resolveSchemaRef('/absolute/path/schema.json', '/path/to/arch.json', mockLogger); |
| 37 | + expect(result).toBe('/absolute/path/schema.json'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should resolve relative paths against architecture file directory', () => { |
| 41 | + const result = resolveSchemaRef('../schemas/custom.json', '/project/architectures/arch.json', mockLogger); |
| 42 | + expect(result).toBe('/project/schemas/custom.json'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should resolve sibling relative paths against architecture file directory', () => { |
| 46 | + const result = resolveSchemaRef('./schema.json', '/project/architectures/arch.json', mockLogger); |
| 47 | + expect(result).toBe('/project/architectures/schema.json'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should resolve simple filename against architecture file directory', () => { |
| 51 | + const result = resolveSchemaRef('schema.json', '/project/architectures/arch.json', mockLogger); |
| 52 | + expect(result).toBe('/project/architectures/schema.json'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should return schemaRef unchanged when architecturePath is empty', () => { |
| 56 | + const result = resolveSchemaRef('../schemas/custom.json', '', mockLogger); |
| 57 | + expect(result).toBe('../schemas/custom.json'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should log debug message when resolving relative path', () => { |
| 61 | + resolveSchemaRef('../schemas/custom.json', '/project/architectures/arch.json', mockLogger); |
| 62 | + expect(mockLogger.debug).toHaveBeenCalledWith( |
| 63 | + expect.stringContaining('Resolved relative $schema path') |
| 64 | + ); |
| 65 | + }); |
| 66 | +}); |
| 67 | + |
| 68 | +describe('loading helpers', () => { |
| 69 | + const mockLogger = { debug: vi.fn(), warn: vi.fn(), error: vi.fn(), log: vi.fn(), info: vi.fn() } as unknown as Logger; |
| 70 | + const mockDocLoader = { |
| 71 | + loadMissingDocument: vi.fn(), |
| 72 | + initialise: vi.fn(), |
| 73 | + resolvePath: vi.fn() |
| 74 | + } as unknown as DocumentLoader; |
| 75 | + const mockSchemaDirectory = { |
| 76 | + getSchema: vi.fn() |
| 77 | + } as unknown as SchemaDirectory; |
| 78 | + |
| 79 | + beforeEach(() => { |
| 80 | + vi.resetAllMocks(); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('loadArchitecture', () => { |
| 84 | + it('should return undefined if architecturePath is not provided', async () => { |
| 85 | + const result = await loadArchitecture(undefined, mockDocLoader, mockLogger); |
| 86 | + expect(result).toBeUndefined(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should load architecture from path', async () => { |
| 90 | + const arch = { kind: 'architecture' }; |
| 91 | + vi.mocked(mockDocLoader.loadMissingDocument).mockResolvedValue(arch); |
| 92 | + const result = await loadArchitecture('path/to/arch.json', mockDocLoader, mockLogger); |
| 93 | + expect(mockDocLoader.loadMissingDocument).toHaveBeenCalledWith('path/to/arch.json', 'architecture'); |
| 94 | + expect(result).toBe(arch); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('loadPattern', () => { |
| 99 | + it('should return undefined if patternPath is not provided', async () => { |
| 100 | + const result = await loadPattern(undefined, mockDocLoader, mockLogger); |
| 101 | + expect(result).toBeUndefined(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should load pattern from path', async () => { |
| 105 | + const pattern = { kind: 'pattern' }; |
| 106 | + vi.mocked(mockDocLoader.loadMissingDocument).mockResolvedValue(pattern); |
| 107 | + const result = await loadPattern('path/to/pattern.json', mockDocLoader, mockLogger); |
| 108 | + expect(mockDocLoader.loadMissingDocument).toHaveBeenCalledWith('path/to/pattern.json', 'pattern'); |
| 109 | + expect(result).toBe(pattern); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe('loadPatternFromArchitectureIfPresent', () => { |
| 114 | + it('should return undefined if architecture is missing', async () => { |
| 115 | + const result = await loadPatternFromArchitectureIfPresent(undefined, 'arch.json', mockDocLoader, mockSchemaDirectory, mockLogger); |
| 116 | + expect(result).toBeUndefined(); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should return undefined if architecture has no $schema', async () => { |
| 120 | + const result = await loadPatternFromArchitectureIfPresent({}, 'arch.json', mockDocLoader, mockSchemaDirectory, mockLogger); |
| 121 | + expect(result).toBeUndefined(); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should load schema from SchemaDirectory if available', async () => { |
| 125 | + const schema = { kind: 'pattern' }; |
| 126 | + const arch = { '$schema': 'pattern.json' }; |
| 127 | + vi.mocked(mockSchemaDirectory.getSchema).mockResolvedValue(schema); |
| 128 | + |
| 129 | + const result = await loadPatternFromArchitectureIfPresent(arch, '/path/arch.json', mockDocLoader, mockSchemaDirectory, mockLogger); |
| 130 | + |
| 131 | + expect(mockSchemaDirectory.getSchema).toHaveBeenCalledWith('/path/pattern.json'); |
| 132 | + expect(result).toBe(schema); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should fall back to docLoader if SchemaDirectory throws', async () => { |
| 136 | + const pattern = { kind: 'pattern' }; |
| 137 | + const arch = { '$schema': 'pattern.json' }; |
| 138 | + vi.mocked(mockSchemaDirectory.getSchema).mockRejectedValue(new Error('not found')); |
| 139 | + vi.mocked(mockDocLoader.loadMissingDocument).mockResolvedValue(pattern); |
| 140 | + |
| 141 | + const result = await loadPatternFromArchitectureIfPresent(arch, '/path/arch.json', mockDocLoader, mockSchemaDirectory, mockLogger); |
| 142 | + |
| 143 | + expect(mockDocLoader.loadMissingDocument).toHaveBeenCalledWith('/path/pattern.json', 'pattern'); |
| 144 | + expect(result).toBe(pattern); |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + describe('loadArchitectureAndPattern', () => { |
| 149 | + it('should load architecture and pattern when both paths provided', async () => { |
| 150 | + const arch = { kind: 'architecture' }; |
| 151 | + const pattern = { kind: 'pattern' }; |
| 152 | + vi.mocked(mockDocLoader.loadMissingDocument) |
| 153 | + .mockResolvedValueOnce(arch) |
| 154 | + .mockResolvedValueOnce(pattern); |
| 155 | + |
| 156 | + const result = await loadArchitectureAndPattern('arch.json', 'pattern.json', mockDocLoader, mockSchemaDirectory, mockLogger); |
| 157 | + |
| 158 | + expect(result).toEqual({ architecture: arch, pattern }); |
| 159 | + }); |
| 160 | + |
| 161 | + it('should load pattern only if architecture fails to load', async () => { |
| 162 | + const pattern = { kind: 'pattern' }; |
| 163 | + vi.mocked(mockDocLoader.loadMissingDocument) |
| 164 | + .mockResolvedValueOnce(undefined) // architecture |
| 165 | + .mockResolvedValueOnce(pattern); // pattern |
| 166 | + |
| 167 | + const result = await loadArchitectureAndPattern('arch.json', 'pattern.json', mockDocLoader, mockSchemaDirectory, mockLogger); |
| 168 | + |
| 169 | + expect(result).toEqual({ architecture: undefined, pattern }); |
| 170 | + }); |
| 171 | + |
| 172 | + it('should load pattern from architecture if patternPath missing', async () => { |
| 173 | + const arch = { kind: 'architecture', '$schema': 'pattern.json' }; |
| 174 | + const pattern = { kind: 'pattern' }; |
| 175 | + vi.mocked(mockDocLoader.loadMissingDocument).mockResolvedValueOnce(arch); |
| 176 | + vi.mocked(mockSchemaDirectory.getSchema).mockResolvedValue(pattern); |
| 177 | + |
| 178 | + const result = await loadArchitectureAndPattern('arch.json', undefined, mockDocLoader, mockSchemaDirectory, mockLogger); |
| 179 | + |
| 180 | + expect(result).toEqual({ architecture: arch, pattern }); |
| 181 | + }); |
| 182 | + }); |
| 183 | +}); |
0 commit comments