|
| 1 | +import * as fs from 'fs-extra'; |
| 2 | +import * as os from 'os'; |
| 3 | +import * as path from 'path'; |
| 4 | +import { GlobalConfigManager } from '../../lib/GlobalConfig'; |
| 5 | + |
| 6 | +jest.mock('fs-extra'); |
| 7 | +jest.mock('os'); |
| 8 | +jest.mock('path'); |
| 9 | + |
| 10 | +describe('GlobalConfigManager', () => { |
| 11 | + let configManager: GlobalConfigManager; |
| 12 | + let mockFs: jest.Mocked<typeof fs>; |
| 13 | + let mockOs: jest.Mocked<typeof os>; |
| 14 | + let mockPath: jest.Mocked<typeof path>; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + configManager = new GlobalConfigManager(); |
| 18 | + mockFs = fs as jest.Mocked<typeof fs>; |
| 19 | + mockOs = os as jest.Mocked<typeof os>; |
| 20 | + mockPath = path as jest.Mocked<typeof path>; |
| 21 | + |
| 22 | + mockOs.homedir.mockReturnValue('/home/test'); |
| 23 | + mockPath.join.mockImplementation((...args) => args.join('/')); |
| 24 | + jest.spyOn(console, 'warn').mockImplementation(() => {}); |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(() => { |
| 28 | + jest.clearAllMocks(); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('read', () => { |
| 32 | + it('should return null when global config does not exist', async () => { |
| 33 | + (mockFs.pathExists as any).mockResolvedValue(false); |
| 34 | + |
| 35 | + const result = await configManager.read(); |
| 36 | + |
| 37 | + expect(result).toBeNull(); |
| 38 | + expect(mockFs.readJson).not.toHaveBeenCalled(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should return parsed config when file exists', async () => { |
| 42 | + const config = { |
| 43 | + skills: { |
| 44 | + registries: { |
| 45 | + 'my-org/skills': 'https://github.com/my-org/skills.git' |
| 46 | + } |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + (mockFs.pathExists as any).mockResolvedValue(true); |
| 51 | + (mockFs.readJson as any).mockResolvedValue(config); |
| 52 | + |
| 53 | + const result = await configManager.read(); |
| 54 | + |
| 55 | + expect(result).toEqual(config); |
| 56 | + expect(mockFs.readJson).toHaveBeenCalledWith('/home/test/.ai-devkit/.ai-devkit.json'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should warn and return null when JSON is invalid', async () => { |
| 60 | + (mockFs.pathExists as any).mockResolvedValue(true); |
| 61 | + (mockFs.readJson as any).mockRejectedValue(new Error('Invalid JSON')); |
| 62 | + |
| 63 | + const result = await configManager.read(); |
| 64 | + |
| 65 | + expect(result).toBeNull(); |
| 66 | + expect(console.warn).toHaveBeenCalled(); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe('getSkillRegistries', () => { |
| 71 | + it('should return empty map when no config', async () => { |
| 72 | + (mockFs.pathExists as any).mockResolvedValue(false); |
| 73 | + |
| 74 | + const result = await configManager.getSkillRegistries(); |
| 75 | + |
| 76 | + expect(result).toEqual({}); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should return only string registry entries', async () => { |
| 80 | + const config = { |
| 81 | + skills: { |
| 82 | + registries: { |
| 83 | + 'my-org/skills': 'https://github.com/my-org/skills.git', |
| 84 | + 'bad/entry': 123 |
| 85 | + } |
| 86 | + } |
| 87 | + }; |
| 88 | + |
| 89 | + (mockFs.pathExists as any).mockResolvedValue(true); |
| 90 | + (mockFs.readJson as any).mockResolvedValue(config); |
| 91 | + |
| 92 | + const result = await configManager.getSkillRegistries(); |
| 93 | + |
| 94 | + expect(result).toEqual({ |
| 95 | + 'my-org/skills': 'https://github.com/my-org/skills.git' |
| 96 | + }); |
| 97 | + }); |
| 98 | + }); |
| 99 | +}); |
0 commit comments