|
| 1 | +import { jest } from '@jest/globals'; |
| 2 | + |
| 3 | +const mockPrompt: any = jest.fn(); |
| 4 | + |
| 5 | +const mockConfigManager: any = { |
| 6 | + read: jest.fn(), |
| 7 | + create: jest.fn(), |
| 8 | + update: jest.fn(), |
| 9 | + addPhase: jest.fn() |
| 10 | +}; |
| 11 | + |
| 12 | +const mockTemplateManager: any = { |
| 13 | + checkEnvironmentExists: jest.fn(), |
| 14 | + fileExists: jest.fn(), |
| 15 | + setupMultipleEnvironments: jest.fn(), |
| 16 | + copyPhaseTemplate: jest.fn() |
| 17 | +}; |
| 18 | + |
| 19 | +const mockSkillManager: any = { |
| 20 | + addSkill: jest.fn() |
| 21 | +}; |
| 22 | + |
| 23 | +jest.mock('inquirer', () => ({ |
| 24 | + __esModule: true, |
| 25 | + default: { |
| 26 | + prompt: (...args: unknown[]) => mockPrompt(...args) |
| 27 | + } |
| 28 | +})); |
| 29 | + |
| 30 | +jest.mock('../../../lib/Config', () => ({ |
| 31 | + ConfigManager: jest.fn(() => mockConfigManager) |
| 32 | +})); |
| 33 | + |
| 34 | +jest.mock('../../../lib/TemplateManager', () => ({ |
| 35 | + TemplateManager: jest.fn(() => mockTemplateManager) |
| 36 | +})); |
| 37 | + |
| 38 | +jest.mock('../../../lib/EnvironmentSelector', () => ({ |
| 39 | + EnvironmentSelector: jest.fn() |
| 40 | +})); |
| 41 | + |
| 42 | +jest.mock('../../../lib/SkillManager', () => ({ |
| 43 | + SkillManager: jest.fn(() => mockSkillManager) |
| 44 | +})); |
| 45 | + |
| 46 | +import { getInstallExitCode, reconcileAndInstall } from '../../../services/install/install.service'; |
| 47 | + |
| 48 | +describe('install service', () => { |
| 49 | + const installConfig = { |
| 50 | + environments: ['codex' as const], |
| 51 | + phases: ['requirements' as const], |
| 52 | + skills: [{ registry: 'codeaholicguy/ai-devkit', name: 'debug' }] |
| 53 | + }; |
| 54 | + |
| 55 | + beforeEach(() => { |
| 56 | + jest.clearAllMocks(); |
| 57 | + |
| 58 | + mockConfigManager.read.mockResolvedValue({ |
| 59 | + environments: [], |
| 60 | + phases: [] |
| 61 | + }); |
| 62 | + mockConfigManager.create.mockResolvedValue({ |
| 63 | + environments: [], |
| 64 | + phases: [] |
| 65 | + }); |
| 66 | + mockConfigManager.update.mockResolvedValue({}); |
| 67 | + mockConfigManager.addPhase.mockResolvedValue({}); |
| 68 | + |
| 69 | + mockTemplateManager.checkEnvironmentExists.mockResolvedValue(false); |
| 70 | + mockTemplateManager.fileExists.mockResolvedValue(false); |
| 71 | + mockTemplateManager.setupMultipleEnvironments.mockResolvedValue([]); |
| 72 | + mockTemplateManager.copyPhaseTemplate.mockResolvedValue('docs/ai/requirements/README.md'); |
| 73 | + |
| 74 | + mockSkillManager.addSkill.mockResolvedValue(undefined); |
| 75 | + mockPrompt.mockResolvedValue({ overwrite: false }); |
| 76 | + }); |
| 77 | + |
| 78 | + it('installs all sections on happy path', async () => { |
| 79 | + const report = await reconcileAndInstall(installConfig, {}); |
| 80 | + |
| 81 | + expect(mockConfigManager.update).toHaveBeenCalledWith({ |
| 82 | + environments: ['codex'], |
| 83 | + phases: ['requirements'], |
| 84 | + skills: [{ registry: 'codeaholicguy/ai-devkit', name: 'debug' }] |
| 85 | + }); |
| 86 | + expect(report.environments.installed).toBe(1); |
| 87 | + expect(report.phases.installed).toBe(1); |
| 88 | + expect(report.skills.installed).toBe(1); |
| 89 | + expect(report.warnings).toEqual([]); |
| 90 | + }); |
| 91 | + |
| 92 | + it('prompts and skips conflicting artifacts when overwrite is not confirmed', async () => { |
| 93 | + mockTemplateManager.checkEnvironmentExists |
| 94 | + .mockResolvedValueOnce(true) |
| 95 | + .mockResolvedValueOnce(true); |
| 96 | + mockTemplateManager.fileExists |
| 97 | + .mockResolvedValueOnce(true) |
| 98 | + .mockResolvedValueOnce(true); |
| 99 | + mockPrompt.mockResolvedValue({ overwrite: false }); |
| 100 | + |
| 101 | + const report = await reconcileAndInstall(installConfig, {}); |
| 102 | + |
| 103 | + expect(mockPrompt).toHaveBeenCalledTimes(1); |
| 104 | + expect(report.environments.skipped).toBe(1); |
| 105 | + expect(report.phases.skipped).toBe(1); |
| 106 | + expect(report.skills.installed).toBe(1); |
| 107 | + expect(mockConfigManager.update).toHaveBeenCalledWith({ |
| 108 | + environments: [], |
| 109 | + phases: [], |
| 110 | + skills: [{ registry: 'codeaholicguy/ai-devkit', name: 'debug' }] |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + it('overwrites conflicting artifacts when overwrite is confirmed via prompt', async () => { |
| 115 | + mockTemplateManager.checkEnvironmentExists |
| 116 | + .mockResolvedValueOnce(true) |
| 117 | + .mockResolvedValueOnce(true); |
| 118 | + mockTemplateManager.fileExists |
| 119 | + .mockResolvedValueOnce(true) |
| 120 | + .mockResolvedValueOnce(true); |
| 121 | + mockPrompt.mockResolvedValue({ overwrite: true }); |
| 122 | + |
| 123 | + const report = await reconcileAndInstall(installConfig, {}); |
| 124 | + |
| 125 | + expect(mockPrompt).toHaveBeenCalledTimes(1); |
| 126 | + expect(report.environments.installed).toBe(1); |
| 127 | + expect(report.phases.installed).toBe(1); |
| 128 | + }); |
| 129 | + |
| 130 | + it('auto-overwrites and does not prompt when --overwrite is set', async () => { |
| 131 | + mockTemplateManager.checkEnvironmentExists |
| 132 | + .mockResolvedValueOnce(true) |
| 133 | + .mockResolvedValueOnce(true); |
| 134 | + mockTemplateManager.fileExists |
| 135 | + .mockResolvedValueOnce(true) |
| 136 | + .mockResolvedValueOnce(true); |
| 137 | + |
| 138 | + const report = await reconcileAndInstall(installConfig, { overwrite: true }); |
| 139 | + |
| 140 | + expect(mockPrompt).not.toHaveBeenCalled(); |
| 141 | + expect(report.environments.installed).toBe(1); |
| 142 | + expect(report.phases.installed).toBe(1); |
| 143 | + }); |
| 144 | + |
| 145 | + it('reports skill failures as warnings and continues', async () => { |
| 146 | + mockSkillManager.addSkill.mockRejectedValue(new Error('network down')); |
| 147 | + |
| 148 | + const report = await reconcileAndInstall(installConfig, {}); |
| 149 | + |
| 150 | + expect(report.skills.failed).toBe(1); |
| 151 | + expect(report.warnings).toEqual([ |
| 152 | + 'Skill codeaholicguy/ai-devkit/debug failed: network down' |
| 153 | + ]); |
| 154 | + expect(getInstallExitCode(report)).toBe(0); |
| 155 | + }); |
| 156 | + |
| 157 | + it('returns non-zero exit code when environment or phase failures occur', () => { |
| 158 | + const report = { |
| 159 | + environments: { installed: 0, skipped: 0, failed: 1 }, |
| 160 | + phases: { installed: 0, skipped: 0, failed: 0 }, |
| 161 | + skills: { installed: 0, skipped: 0, failed: 0 }, |
| 162 | + warnings: [] |
| 163 | + }; |
| 164 | + |
| 165 | + expect(getInstallExitCode(report)).toBe(1); |
| 166 | + }); |
| 167 | +}); |
0 commit comments