|
| 1 | +/** |
| 2 | + * Unit tests for configPersistence.ts |
| 3 | + * |
| 4 | + * Covers: loadPersistedConfig, savePersistedConfig, legacy format migration |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 8 | +import { |
| 9 | + loadPersistedConfig, |
| 10 | + savePersistedConfig, |
| 11 | + type PersistedConfig, |
| 12 | +} from '../configPersistence'; |
| 13 | +import type { LLMConfig } from '../llmClient'; |
| 14 | +import type { ImageGenConfig } from '../imageGenClient'; |
| 15 | + |
| 16 | +// ─── Constants ────────────────────────────────────────────────────────────────── |
| 17 | + |
| 18 | +const MOCK_LLM_CONFIG: LLMConfig = { |
| 19 | + provider: 'openai', |
| 20 | + apiKey: 'sk-test', |
| 21 | + baseUrl: 'https://api.openai.com', |
| 22 | + model: 'gpt-4', |
| 23 | +}; |
| 24 | + |
| 25 | +const MOCK_IMAGEGEN_CONFIG: ImageGenConfig = { |
| 26 | + provider: 'openai', |
| 27 | + apiKey: 'sk-img-test', |
| 28 | + baseUrl: 'https://api.openai.com', |
| 29 | + model: 'gpt-image-1.5', |
| 30 | +}; |
| 31 | + |
| 32 | +const MOCK_PERSISTED: PersistedConfig = { |
| 33 | + llm: MOCK_LLM_CONFIG, |
| 34 | + imageGen: MOCK_IMAGEGEN_CONFIG, |
| 35 | +}; |
| 36 | + |
| 37 | +// ─── Setup / Teardown ─────────────────────────────────────────────────────────── |
| 38 | + |
| 39 | +beforeEach(() => { |
| 40 | + vi.restoreAllMocks(); |
| 41 | +}); |
| 42 | + |
| 43 | +afterEach(() => { |
| 44 | + vi.restoreAllMocks(); |
| 45 | +}); |
| 46 | + |
| 47 | +// ─── loadPersistedConfig() ────────────────────────────────────────────────────── |
| 48 | + |
| 49 | +describe('loadPersistedConfig()', () => { |
| 50 | + it('returns full config when file has new { llm, imageGen } format', async () => { |
| 51 | + globalThis.fetch = vi.fn().mockResolvedValueOnce({ |
| 52 | + ok: true, |
| 53 | + json: () => Promise.resolve(MOCK_PERSISTED), |
| 54 | + } as unknown as Response); |
| 55 | + |
| 56 | + const result = await loadPersistedConfig(); |
| 57 | + |
| 58 | + expect(result).toEqual(MOCK_PERSISTED); |
| 59 | + expect(result?.llm).toEqual(MOCK_LLM_CONFIG); |
| 60 | + expect(result?.imageGen).toEqual(MOCK_IMAGEGEN_CONFIG); |
| 61 | + }); |
| 62 | + |
| 63 | + it('returns { llm } only when imageGen is absent', async () => { |
| 64 | + const withoutImageGen = { llm: MOCK_LLM_CONFIG }; |
| 65 | + globalThis.fetch = vi.fn().mockResolvedValueOnce({ |
| 66 | + ok: true, |
| 67 | + json: () => Promise.resolve(withoutImageGen), |
| 68 | + } as unknown as Response); |
| 69 | + |
| 70 | + const result = await loadPersistedConfig(); |
| 71 | + |
| 72 | + expect(result?.llm).toEqual(MOCK_LLM_CONFIG); |
| 73 | + expect(result?.imageGen).toBeUndefined(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('migrates legacy flat LLMConfig format to { llm } wrapper', async () => { |
| 77 | + // Legacy format: flat LLMConfig at top level (has "provider", no "llm" key) |
| 78 | + globalThis.fetch = vi.fn().mockResolvedValueOnce({ |
| 79 | + ok: true, |
| 80 | + json: () => Promise.resolve(MOCK_LLM_CONFIG), |
| 81 | + } as unknown as Response); |
| 82 | + |
| 83 | + const result = await loadPersistedConfig(); |
| 84 | + |
| 85 | + expect(result).toEqual({ llm: MOCK_LLM_CONFIG }); |
| 86 | + expect(result?.llm.provider).toBe('openai'); |
| 87 | + expect(result?.imageGen).toBeUndefined(); |
| 88 | + }); |
| 89 | + |
| 90 | + it('returns null when API returns 404', async () => { |
| 91 | + globalThis.fetch = vi.fn().mockResolvedValueOnce({ |
| 92 | + ok: false, |
| 93 | + status: 404, |
| 94 | + } as Response); |
| 95 | + |
| 96 | + expect(await loadPersistedConfig()).toBeNull(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('returns null when fetch throws (network error)', async () => { |
| 100 | + globalThis.fetch = vi.fn().mockRejectedValueOnce(new Error('Network error')); |
| 101 | + |
| 102 | + expect(await loadPersistedConfig()).toBeNull(); |
| 103 | + }); |
| 104 | + |
| 105 | + it('returns null when response is not a recognized format', async () => { |
| 106 | + globalThis.fetch = vi.fn().mockResolvedValueOnce({ |
| 107 | + ok: true, |
| 108 | + json: () => Promise.resolve({ unrelated: 'data' }), |
| 109 | + } as unknown as Response); |
| 110 | + |
| 111 | + expect(await loadPersistedConfig()).toBeNull(); |
| 112 | + }); |
| 113 | +}); |
| 114 | + |
| 115 | +// ─── savePersistedConfig() ────────────────────────────────────────────────────── |
| 116 | + |
| 117 | +describe('savePersistedConfig()', () => { |
| 118 | + it('POSTs the full config to /api/llm-config', async () => { |
| 119 | + const mockFetch = vi.fn().mockResolvedValueOnce({ ok: true } as Response); |
| 120 | + globalThis.fetch = mockFetch; |
| 121 | + |
| 122 | + await savePersistedConfig(MOCK_PERSISTED); |
| 123 | + |
| 124 | + expect(mockFetch).toHaveBeenCalledWith('/api/llm-config', { |
| 125 | + method: 'POST', |
| 126 | + headers: { 'Content-Type': 'application/json' }, |
| 127 | + body: JSON.stringify(MOCK_PERSISTED), |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + it('includes imageGen in the persisted JSON', async () => { |
| 132 | + const mockFetch = vi.fn().mockResolvedValueOnce({ ok: true } as Response); |
| 133 | + globalThis.fetch = mockFetch; |
| 134 | + |
| 135 | + await savePersistedConfig(MOCK_PERSISTED); |
| 136 | + |
| 137 | + const body = JSON.parse(mockFetch.mock.calls[0][1].body as string); |
| 138 | + expect(body.llm).toEqual(MOCK_LLM_CONFIG); |
| 139 | + expect(body.imageGen).toEqual(MOCK_IMAGEGEN_CONFIG); |
| 140 | + }); |
| 141 | + |
| 142 | + it('omits imageGen when not provided', async () => { |
| 143 | + const mockFetch = vi.fn().mockResolvedValueOnce({ ok: true } as Response); |
| 144 | + globalThis.fetch = mockFetch; |
| 145 | + |
| 146 | + await savePersistedConfig({ llm: MOCK_LLM_CONFIG }); |
| 147 | + |
| 148 | + const body = JSON.parse(mockFetch.mock.calls[0][1].body as string); |
| 149 | + expect(body.llm).toEqual(MOCK_LLM_CONFIG); |
| 150 | + expect(body.imageGen).toBeUndefined(); |
| 151 | + }); |
| 152 | + |
| 153 | + it('does not throw when fetch fails', async () => { |
| 154 | + globalThis.fetch = vi.fn().mockRejectedValueOnce(new Error('Network error')); |
| 155 | + |
| 156 | + await expect(savePersistedConfig(MOCK_PERSISTED)).resolves.toBeUndefined(); |
| 157 | + }); |
| 158 | +}); |
0 commit comments