|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest' |
| 2 | +import { CalmSchemaRegistry } from './calm-schema-registry' |
| 3 | +import type { Logger } from '../ports/logger' |
| 4 | +import type { Config } from '../ports/config' |
| 5 | + |
| 6 | +// Mock vscode module |
| 7 | +vi.mock('vscode', () => ({ |
| 8 | + workspace: { |
| 9 | + workspaceFolders: [{ uri: { fsPath: '/workspace' } }], |
| 10 | + fs: { |
| 11 | + readDirectory: vi.fn(), |
| 12 | + readFile: vi.fn() |
| 13 | + } |
| 14 | + }, |
| 15 | + Uri: { |
| 16 | + file: vi.fn((path: string) => ({ fsPath: path })), |
| 17 | + joinPath: vi.fn((_base: any, ...parts: string[]) => ({ fsPath: `${_base.fsPath}/${parts.join('/')}` })) |
| 18 | + }, |
| 19 | + FileType: { |
| 20 | + File: 1, |
| 21 | + Directory: 2 |
| 22 | + } |
| 23 | +})) |
| 24 | + |
| 25 | +describe('CalmSchemaRegistry', () => { |
| 26 | + let registry: CalmSchemaRegistry |
| 27 | + let mockLogger: Logger |
| 28 | + let mockConfig: Config |
| 29 | + let mockExtensionUri: any |
| 30 | + |
| 31 | + beforeEach(() => { |
| 32 | + vi.clearAllMocks() |
| 33 | + |
| 34 | + mockLogger = { |
| 35 | + info: vi.fn(), |
| 36 | + warn: vi.fn(), |
| 37 | + error: vi.fn(), |
| 38 | + debug: vi.fn() |
| 39 | + } |
| 40 | + |
| 41 | + mockConfig = { |
| 42 | + filesGlobs: vi.fn(() => []), |
| 43 | + templateGlobs: vi.fn(() => []), |
| 44 | + previewLayout: vi.fn(() => 'dagre'), |
| 45 | + showLabels: vi.fn(() => true), |
| 46 | + urlMapping: vi.fn(() => undefined), |
| 47 | + schemaAdditionalFolders: vi.fn(() => []) |
| 48 | + } |
| 49 | + |
| 50 | + mockExtensionUri = { fsPath: '/extension' } |
| 51 | + |
| 52 | + registry = new CalmSchemaRegistry(mockExtensionUri, mockLogger, mockConfig) |
| 53 | + }) |
| 54 | + |
| 55 | + describe('isKnownCalmSchema', () => { |
| 56 | + it('should recognize CALM release schema URLs', () => { |
| 57 | + expect(registry.isKnownCalmSchema('https://calm.finos.org/release/1.1/meta/calm.json')).toBe(true) |
| 58 | + expect(registry.isKnownCalmSchema('https://calm.finos.org/release/1.0/meta/calm.json')).toBe(true) |
| 59 | + }) |
| 60 | + |
| 61 | + it('should recognize CALM draft schema URLs', () => { |
| 62 | + expect(registry.isKnownCalmSchema('https://calm.finos.org/draft/2025-03/meta/calm.json')).toBe(true) |
| 63 | + }) |
| 64 | + |
| 65 | + it('should recognize other CALM meta schema files', () => { |
| 66 | + expect(registry.isKnownCalmSchema('https://calm.finos.org/release/1.1/meta/core.json')).toBe(true) |
| 67 | + expect(registry.isKnownCalmSchema('https://calm.finos.org/release/1.1/meta/flow.json')).toBe(true) |
| 68 | + }) |
| 69 | + |
| 70 | + it('should not recognize non-CALM URLs', () => { |
| 71 | + expect(registry.isKnownCalmSchema('https://json-schema.org/draft/2020-12/schema')).toBe(false) |
| 72 | + expect(registry.isKnownCalmSchema('https://example.com/schema.json')).toBe(false) |
| 73 | + }) |
| 74 | + |
| 75 | + it('should not recognize URLs with wrong path structure', () => { |
| 76 | + expect(registry.isKnownCalmSchema('https://calm.finos.org/other/path/schema.json')).toBe(false) |
| 77 | + }) |
| 78 | + }) |
| 79 | + |
| 80 | + describe('reset', () => { |
| 81 | + it('should clear schemas and mark as not initialized', async () => { |
| 82 | + // First initialize |
| 83 | + const vscode = await import('vscode') |
| 84 | + vi.mocked(vscode.workspace.fs.readDirectory).mockResolvedValue([]) |
| 85 | + |
| 86 | + await registry.initialize() |
| 87 | + |
| 88 | + // Then reset |
| 89 | + registry.reset() |
| 90 | + |
| 91 | + // getRegisteredSchemaUrls should be empty after reset |
| 92 | + expect(registry.getRegisteredSchemaUrls()).toHaveLength(0) |
| 93 | + }) |
| 94 | + }) |
| 95 | + |
| 96 | + describe('getSchemaPath', () => { |
| 97 | + it('should return undefined for unregistered schemas', () => { |
| 98 | + expect(registry.getSchemaPath('https://calm.finos.org/release/1.1/meta/calm.json')).toBeUndefined() |
| 99 | + }) |
| 100 | + }) |
| 101 | + |
| 102 | + describe('getRegisteredSchemaUrls', () => { |
| 103 | + it('should return empty array initially', () => { |
| 104 | + expect(registry.getRegisteredSchemaUrls()).toEqual([]) |
| 105 | + }) |
| 106 | + }) |
| 107 | + |
| 108 | + describe('initialize', () => { |
| 109 | + it('should load schemas from bundled directory', async () => { |
| 110 | + const vscode = await import('vscode') |
| 111 | + |
| 112 | + // Mock directory structure: dist/calm/release/1.1/meta/calm.json |
| 113 | + vi.mocked(vscode.workspace.fs.readDirectory) |
| 114 | + .mockResolvedValueOnce([['release', 2]]) // dist/calm |
| 115 | + .mockResolvedValueOnce([['1.1', 2]]) // dist/calm/release |
| 116 | + .mockResolvedValueOnce([['meta', 2]]) // dist/calm/release/1.1 |
| 117 | + .mockResolvedValueOnce([['calm.json', 1]]) // dist/calm/release/1.1/meta |
| 118 | + |
| 119 | + vi.mocked(vscode.workspace.fs.readFile).mockResolvedValue( |
| 120 | + Buffer.from(JSON.stringify({ |
| 121 | + $id: 'https://calm.finos.org/release/1.1/meta/calm.json', |
| 122 | + title: 'CALM Schema' |
| 123 | + })) |
| 124 | + ) |
| 125 | + |
| 126 | + await registry.initialize() |
| 127 | + |
| 128 | + expect(registry.getSchemaPath('https://calm.finos.org/release/1.1/meta/calm.json')).toBeDefined() |
| 129 | + expect(mockLogger.info).toHaveBeenCalledWith(expect.stringContaining('initialized with')) |
| 130 | + }) |
| 131 | + |
| 132 | + it('should handle missing directories gracefully', async () => { |
| 133 | + const vscode = await import('vscode') |
| 134 | + |
| 135 | + vi.mocked(vscode.workspace.fs.readDirectory).mockRejectedValue(new Error('Directory not found')) |
| 136 | + |
| 137 | + await registry.initialize() |
| 138 | + |
| 139 | + // Should not throw - silently handles missing directory |
| 140 | + expect(registry.getRegisteredSchemaUrls()).toHaveLength(0) |
| 141 | + }) |
| 142 | + |
| 143 | + it('should only initialize once', async () => { |
| 144 | + const vscode = await import('vscode') |
| 145 | + vi.mocked(vscode.workspace.fs.readDirectory).mockResolvedValue([]) |
| 146 | + |
| 147 | + await registry.initialize() |
| 148 | + await registry.initialize() |
| 149 | + |
| 150 | + // readDirectory should only be called for the first initialization |
| 151 | + expect(vscode.workspace.fs.readDirectory).toHaveBeenCalledTimes(1) |
| 152 | + }) |
| 153 | + |
| 154 | + it('should load schemas from additional folders', async () => { |
| 155 | + const vscode = await import('vscode') |
| 156 | + |
| 157 | + // Configure additional folders |
| 158 | + vi.mocked(mockConfig.schemaAdditionalFolders).mockReturnValue(['my-schemas']) |
| 159 | + |
| 160 | + // Mock bundled schemas directory (empty) |
| 161 | + vi.mocked(vscode.workspace.fs.readDirectory) |
| 162 | + .mockResolvedValueOnce([]) // dist/calm (empty) |
| 163 | + .mockResolvedValueOnce([['custom.json', 1]]) // my-schemas |
| 164 | + |
| 165 | + vi.mocked(vscode.workspace.fs.readFile).mockResolvedValue( |
| 166 | + Buffer.from(JSON.stringify({ |
| 167 | + $id: 'https://my-org.com/schemas/custom.json', |
| 168 | + title: 'Custom Schema' |
| 169 | + })) |
| 170 | + ) |
| 171 | + |
| 172 | + await registry.initialize() |
| 173 | + |
| 174 | + expect(registry.getSchemaPath('https://my-org.com/schemas/custom.json')).toBeDefined() |
| 175 | + }) |
| 176 | + }) |
| 177 | +}) |
0 commit comments