|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | + |
| 3 | +const { |
| 4 | + mockDiscoverConfigurationSources, |
| 5 | + mockReconcileEnvValues, |
| 6 | + mockPromptCopilotKey, |
| 7 | + mockMothershipOverride, |
| 8 | + mockChatFlagValues, |
| 9 | + mockOutro, |
| 10 | +} = vi.hoisted(() => ({ |
| 11 | + mockDiscoverConfigurationSources: vi.fn(), |
| 12 | + mockReconcileEnvValues: vi.fn(), |
| 13 | + mockPromptCopilotKey: vi.fn(), |
| 14 | + mockMothershipOverride: vi.fn(), |
| 15 | + mockChatFlagValues: vi.fn(), |
| 16 | + mockOutro: vi.fn(), |
| 17 | +})) |
| 18 | + |
| 19 | +vi.mock('./configuration-sources', () => ({ |
| 20 | + discoverConfigurationSources: mockDiscoverConfigurationSources, |
| 21 | +})) |
| 22 | + |
| 23 | +vi.mock('./env-files', () => ({ |
| 24 | + reconcileEnvValues: mockReconcileEnvValues, |
| 25 | +})) |
| 26 | + |
| 27 | +vi.mock('./prompter', () => ({ |
| 28 | + outro: mockOutro, |
| 29 | +})) |
| 30 | + |
| 31 | +vi.mock('./steps', () => ({ |
| 32 | + chatFlagValues: mockChatFlagValues, |
| 33 | + mothershipOverride: mockMothershipOverride, |
| 34 | + promptCopilotKey: mockPromptCopilotKey, |
| 35 | +})) |
| 36 | + |
| 37 | +vi.mock('./theme', () => ({ |
| 38 | + theme: { accent: (value: string) => value }, |
| 39 | +})) |
| 40 | + |
| 41 | +import { runFeatureSetup } from './feature-setup' |
| 42 | + |
| 43 | +describe('Chat feature setup', () => { |
| 44 | + beforeEach(() => { |
| 45 | + vi.clearAllMocks() |
| 46 | + mockDiscoverConfigurationSources.mockReturnValue([ |
| 47 | + { |
| 48 | + kind: 'compose', |
| 49 | + label: 'Docker Compose', |
| 50 | + location: '.env', |
| 51 | + values: new Map([['COPILOT_API_KEY', 'existing-key']]), |
| 52 | + managedByCurrentCheckout: true, |
| 53 | + }, |
| 54 | + ]) |
| 55 | + mockMothershipOverride.mockReturnValue({ |
| 56 | + SIM_AGENT_API_URL: 'https://copilot.example.com', |
| 57 | + }) |
| 58 | + mockChatFlagValues.mockReturnValue({ NEXT_PUBLIC_CHAT_DISABLED: 'false' }) |
| 59 | + }) |
| 60 | + |
| 61 | + it('writes only the Chat configuration to the detected install', async () => { |
| 62 | + mockPromptCopilotKey.mockResolvedValue('new-key') |
| 63 | + |
| 64 | + await runFeatureSetup('chat', []) |
| 65 | + |
| 66 | + expect(mockPromptCopilotKey).toHaveBeenCalledWith('existing-key') |
| 67 | + expect(mockReconcileEnvValues).toHaveBeenCalledWith('root', [], { |
| 68 | + COPILOT_API_KEY: 'new-key', |
| 69 | + NEXT_PUBLIC_CHAT_DISABLED: 'false', |
| 70 | + SIM_AGENT_API_URL: 'https://copilot.example.com', |
| 71 | + }) |
| 72 | + expect(mockOutro).toHaveBeenCalledWith( |
| 73 | + 'Chat written to .env. Recreate the app container for it to take effect.' |
| 74 | + ) |
| 75 | + }) |
| 76 | + |
| 77 | + it('fails without changing configuration when no key is received', async () => { |
| 78 | + mockPromptCopilotKey.mockResolvedValue(null) |
| 79 | + |
| 80 | + await expect(runFeatureSetup('chat', [])).rejects.toThrow( |
| 81 | + 'Chat setup did not receive an API key. No configuration was changed.' |
| 82 | + ) |
| 83 | + expect(mockReconcileEnvValues).not.toHaveBeenCalled() |
| 84 | + }) |
| 85 | +}) |
0 commit comments