|
| 1 | +// @vitest-environment jsdom |
| 2 | + |
| 3 | +import React, { act } from 'react'; |
| 4 | +import { createRoot, type Root } from 'react-dom/client'; |
| 5 | +import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 6 | +import { aiApi } from '@/infrastructure/api'; |
| 7 | +import { configManager } from '@/infrastructure/config/services/ConfigManager'; |
| 8 | +import type { AIModelConfig } from '@/infrastructure/config/types'; |
| 9 | +import { i18nService } from '@/infrastructure/i18n'; |
| 10 | +import ModelSettingsPage from './ModelSettingsPage'; |
| 11 | + |
| 12 | +globalThis.IS_REACT_ACT_ENVIRONMENT = true; |
| 13 | + |
| 14 | +function deferred() { |
| 15 | + let resolve!: (value: unknown) => void; |
| 16 | + let reject!: (reason: Error) => void; |
| 17 | + const promise = new Promise<unknown>((resolvePromise, rejectPromise) => { |
| 18 | + resolve = resolvePromise; |
| 19 | + reject = rejectPromise; |
| 20 | + }); |
| 21 | + return { promise, resolve, reject }; |
| 22 | +} |
| 23 | + |
| 24 | +const model: AIModelConfig = { |
| 25 | + id: 'loading-test-model', |
| 26 | + name: 'Test provider', |
| 27 | + model_name: 'loading-test-model', |
| 28 | + provider: 'openai', |
| 29 | + base_url: 'https://example.test/v1', |
| 30 | + api_key: '', |
| 31 | + enabled: true, |
| 32 | + category: 'general_chat', |
| 33 | + capabilities: ['text_chat'], |
| 34 | +}; |
| 35 | + |
| 36 | +// Service reads are controlled to exercise pending, failed, and recovered states. |
| 37 | +// The actual settings and design-system components render; this is not visual QA. |
| 38 | +describe('ModelSettingsPage independent loading', () => { |
| 39 | + let container: HTMLDivElement; |
| 40 | + let root: Root; |
| 41 | + let reads: Map<string, ReturnType<typeof deferred>>; |
| 42 | + const copy = (key: string) => String(i18nService.getI18nInstance().t(key, { ns: 'settings/models' })); |
| 43 | + const button = (key: string) => [...container.querySelectorAll<HTMLButtonElement>('button')] |
| 44 | + .find(element => element.textContent === copy(key))!; |
| 45 | + const selectors = () => [...container.querySelectorAll<HTMLButtonElement>( |
| 46 | + '.default-model-config [role="combobox"]', |
| 47 | + )]; |
| 48 | + |
| 49 | + beforeAll(async () => { |
| 50 | + await Promise.all((['settings', 'settings/models', 'settings/default-model', 'components'] as const).map( |
| 51 | + namespace => i18nService.loadNamespace(namespace), |
| 52 | + )); |
| 53 | + }); |
| 54 | + |
| 55 | + beforeEach(() => { |
| 56 | + reads = new Map([ |
| 57 | + 'ai.models', 'ai.default_models', 'ai.proxy', |
| 58 | + 'ai.stream_idle_timeout_secs', 'ai.stream_ttft_timeout_secs', |
| 59 | + ].map(path => [path, deferred()])); |
| 60 | + vi.spyOn(configManager, 'getOptionalConfig').mockImplementation( |
| 61 | + <T,>(path: string) => reads.get(path)!.promise as Promise<T | undefined>, |
| 62 | + ); |
| 63 | + vi.spyOn(configManager, 'getConfig').mockImplementation( |
| 64 | + <T,>(path?: string) => reads.get(path!)!.promise as Promise<T>, |
| 65 | + ); |
| 66 | + vi.spyOn(aiApi, 'getModelCatalog').mockImplementation(() => new Promise(() => {})); |
| 67 | + vi.spyOn(aiApi, 'getLocalModelsDevCatalogs').mockImplementation(() => new Promise(() => {})); |
| 68 | + vi.spyOn(aiApi, 'getModelsDevCatalogStatus').mockImplementation(() => new Promise(() => {})); |
| 69 | + vi.spyOn(aiApi, 'listSubscriptionAccounts').mockImplementation(() => new Promise(() => {})); |
| 70 | + vi.spyOn(aiApi, 'onModelCatalogUpdated').mockReturnValue(() => {}); |
| 71 | + container = document.createElement('div'); |
| 72 | + document.body.append(container); |
| 73 | + root = createRoot(container); |
| 74 | + }); |
| 75 | + |
| 76 | + afterEach(() => { |
| 77 | + act(() => root.unmount()); |
| 78 | + container.remove(); |
| 79 | + vi.restoreAllMocks(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('shows stable content immediately and releases models without waiting for catalogs or network settings', async () => { |
| 83 | + await act(async () => root.render(<ModelSettingsPage />)); |
| 84 | + |
| 85 | + for (const key of ['sections.acquisition', 'sections.selectionModes', 'sections.pool', 'sections.more']) { |
| 86 | + expect(container.textContent).toContain(copy(key)); |
| 87 | + } |
| 88 | + for (const key of ['acquisition.noneConfigured', 'acquisition.noneImported', 'pool.empty']) { |
| 89 | + expect(container.textContent).not.toContain(copy(key)); |
| 90 | + } |
| 91 | + expect(selectors()).toHaveLength(4); |
| 92 | + expect(selectors().every(element => element.disabled)).toBe(true); |
| 93 | + expect(button('actions.addModel').disabled).toBe(true); |
| 94 | + expect(button('acquisition.import').disabled).toBe(false); |
| 95 | + |
| 96 | + await act(async () => { |
| 97 | + reads.get('ai.models')!.resolve([model]); |
| 98 | + reads.get('ai.default_models')!.resolve({ primary: model.id }); |
| 99 | + }); |
| 100 | + |
| 101 | + expect(container.querySelector('[data-testid="settings-model-pool"]')?.textContent).toContain(model.model_name); |
| 102 | + expect(selectors()[0].disabled).toBe(false); |
| 103 | + expect(selectors()[0].textContent).toContain(model.model_name); |
| 104 | + expect(button('actions.addModel').disabled).toBe(false); |
| 105 | + }); |
| 106 | + |
| 107 | + it('isolates a failed model read, keeps network settings usable, and recovers through the section retry', async () => { |
| 108 | + await act(async () => root.render(<ModelSettingsPage />)); |
| 109 | + await act(async () => { |
| 110 | + reads.get('ai.models')!.reject(new Error('Model host unavailable')); |
| 111 | + reads.get('ai.default_models')!.resolve({}); |
| 112 | + reads.get('ai.proxy')!.resolve({ enabled: true, url: 'http://localhost:7890' }); |
| 113 | + reads.get('ai.stream_idle_timeout_secs')!.resolve(120); |
| 114 | + reads.get('ai.stream_ttft_timeout_secs')!.resolve(60); |
| 115 | + }); |
| 116 | + |
| 117 | + expect(container.textContent).toContain(copy('messages.loadFailedLocked')); |
| 118 | + expect(container.textContent).toContain(copy('sections.selectionModes')); |
| 119 | + expect(container.textContent).not.toContain(copy('pool.empty')); |
| 120 | + expect(selectors().every(element => element.disabled)).toBe(true); |
| 121 | + await act(async () => button('sections.more').click()); |
| 122 | + const proxy = [...container.querySelectorAll<HTMLInputElement>('input')] |
| 123 | + .find(element => element.value === 'http://localhost:7890'); |
| 124 | + expect(proxy?.disabled).toBe(false); |
| 125 | + expect([...container.querySelectorAll<HTMLInputElement>('input')].some( |
| 126 | + element => element.value === '120' && !element.disabled, |
| 127 | + )).toBe(true); |
| 128 | + |
| 129 | + reads.set('ai.models', deferred()); |
| 130 | + await act(async () => button('messages.retry').click()); |
| 131 | + await act(async () => reads.get('ai.models')!.resolve([model])); |
| 132 | + expect(container.textContent).not.toContain(copy('messages.loadFailedLocked')); |
| 133 | + expect(container.querySelector('[data-testid="settings-model-pool"]')?.textContent).toContain(model.model_name); |
| 134 | + expect(button('actions.addModel').disabled).toBe(false); |
| 135 | + }); |
| 136 | +}); |
0 commit comments