// @vitest-environment jsdom import { act } from 'react' import { createRoot, type Root } from 'react-dom/client' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { QAModelSettings } from './QAModelSettings' import type { AISettings } from './qaAITypes' let root: Root, host: HTMLDivElement type Call = { path: string; method: string; body: Record | null } let calls: Call[] let responder: (call: Call) => { status: number; data?: AISettings; error?: string } const saved: AISettings = { revision: 1, base_url: 'https://model.example.invalid/v1', model: 'example-model', key_configured: true, verification: 'verified', checked_at: '2026-11-18T10:00:00Z', automatic_evaluation: true, automatic_translation: false, storage_ready: true, } const applied = (call: Call, extra: Partial = {}): AISettings => ({ ...saved, revision: saved.revision + 1, base_url: String(call.body?.base_url), model: String(call.body?.model), key_configured: call.body?.key_action !== 'clear', verification: call.body?.key_action === 'clear' ? 'not_configured' : 'verified', ...extra, }) const ok = (data: AISettings) => ({ status: 200, data }) beforeEach(() => { Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true }) host = document.createElement('div') document.body.append(host) root = createRoot(host) calls = [] responder = (call) => ok(call.method === 'GET' ? saved : applied(call)) vi.stubGlobal( 'fetch', vi.fn(async (path: string, init?: RequestInit) => { const call: Call = { path, method: init?.method ?? 'GET', body: init?.body ? JSON.parse(String(init.body)) : null, } calls.push(call) const result = responder(call) return new Response(JSON.stringify(result.error ? { error: result.error } : { data: result.data }), { status: result.status, }) }) ) }) afterEach(async () => { await act(async () => root.unmount()) host.remove() vi.unstubAllGlobals() }) const button = (label: string) => { const found = [...host.querySelectorAll('button')].find((item) => item.textContent?.trim() === label) expect(found, label).toBeTruthy() return found! } const click = async (label: string) => { const target = button(label) await act(async () => target.click()) await act(async () => {}) } const type = async (label: string, value: string) => { const input = [...host.querySelectorAll('label')] .find((item) => item.textContent?.startsWith(label)) ?.querySelector('input') expect(input, label).toBeTruthy() await act(async () => { Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')!.set!.call(input, value) input!.dispatchEvent(new Event('input', { bubbles: true })) }) } const current = () => host.querySelector('.qa-ai-model-current')?.textContent ?? '' const writes = () => calls.filter((call) => call.method === 'POST') const disable = async () => { await click('清除密钥并停用…') await click('确认清除并停用') } describe('AI 模型连接草稿编辑器', () => { it('tests the draft without touching the saved connection and echoes the server after save', async () => { await act(async () => root.render()) expect(current()).toContain('example-model · 已通过真实连接测试') await type('模型名称', 'draft-model') await click('测试连接') const test = writes().at(-1)! expect(test.path).toBe('/api/v1/qa-ops/ai/settings/test') expect(test.body).toEqual({ base_revision: 1, base_url: saved.base_url, model: 'draft-model', key_action: 'keep', }) expect(host.textContent).toContain('草稿测试:已通过真实连接测试') expect(host.textContent).toContain('草稿未保存,当前生效配置未改变') // 测试不更新「当前生效」,也不产生保存。 expect(current()).toContain('example-model · 已通过真实连接测试') expect(writes().filter((call) => call.path.endsWith('/settings'))).toHaveLength(0) // 再改草稿 ⇒ 上一次测试结论失效。 await type('模型名称', 'saved-model') expect(host.textContent).not.toContain('草稿测试:已通过真实连接测试') await click('保存配置') const save = writes().at(-1)! expect(save.path).toBe('/api/v1/qa-ops/ai/settings') expect(save.body).toEqual({ base_revision: 1, base_url: saved.base_url, model: 'saved-model', key_action: 'keep' }) // 保存后用服务端响应回显当前信息,而不是本地草稿转写。 expect(current()).toContain('saved-model · 已通过真实连接测试') expect(host.textContent).toContain('已保存并更新当前连接:saved-model · 已通过真实连接测试') expect(host.textContent).toContain('草稿与已保存配置一致') }) it('keeps a configured key collapsed and sends a replacement only when asked', async () => { await act(async () => root.render()) expect(host.querySelector('input[type=password]')).toBeNull() await click('更换密钥') await type('新 API 密钥', 'test-only-replacement-key') await click('保存配置') expect(writes().at(-1)!.body).toEqual({ base_revision: 1, base_url: saved.base_url, model: saved.model, key_action: 'replace', api_key: 'test-only-replacement-key', }) expect(host.querySelector('input[type=password]')).toBeNull() expect(host.textContent).not.toContain('test-only-replacement-key') await click('更换密钥') await click('取消更换') expect(host.querySelector('input[type=password]')).toBeNull() expect(host.textContent).toContain('草稿与已保存配置一致') }) it('requires a fresh key when the address changes and hides cancelling', async () => { await act(async () => root.render()) await type('API 基础地址', 'https://other.example.invalid/v1') expect(host.querySelector('input[type=password]')).toBeTruthy() expect(host.textContent).toContain('已更换基础地址:须重新录入密钥') expect([...host.querySelectorAll('button')].some((item) => item.textContent?.trim() === '取消更换')).toBe(false) expect(button('保存配置').disabled).toBe(true) await type('新 API 密钥', 'second-secret') await click('保存配置') expect(writes().at(-1)!.body).toEqual({ base_revision: 1, base_url: 'https://other.example.invalid/v1', model: saved.model, key_action: 'replace', api_key: 'second-secret', }) }) it('disables only the saved connection, and cancelling the confirmation has no effect', async () => { await act(async () => root.render()) await type('模型名称', 'unsaved-draft') await click('清除密钥并停用…') await click('取消') expect(writes()).toHaveLength(0) expect(current()).toContain('example-model · 已通过真实连接测试') expect([...host.querySelectorAll('input')].find((item) => item.value === 'unsaved-draft')).toBeTruthy() await disable() const clear = writes().at(-1)! // 草稿里的 unsaved-draft 不会被夹带成停用提交。 expect(clear.body).toEqual({ base_revision: 1, base_url: saved.base_url, model: saved.model, key_action: 'clear', }) expect(current()).toContain('未配置可用密钥') expect(host.textContent).toContain('已清除密钥并停用连接') expect(host.querySelector('input[type=password]')).toBeTruthy() }) it('drops a stale draft test when the saved revision changed elsewhere', async () => { let gets = 0 responder = (call) => { if (call.method === 'GET') { gets += 1 return ok(gets === 1 ? saved : { ...saved, revision: 2, model: 'other-model' }) } return call.path.endsWith('/settings/test') ? { status: 409, error: '配置、输入、人工复核或预览已变化,请刷新;现有结论未被覆盖' } : ok(applied(call)) } await act(async () => root.render()) await type('模型名称', 'stale-draft') await click('测试连接') expect(host.textContent).toContain('已载入最新已保存配置(版本 2),草稿未保存,请重新测试') expect(current()).toContain('other-model') expect(host.textContent).not.toContain('草稿测试:') // 之后的保存基于刷新后的版本号,不会拿旧 revision 覆盖别人的修改。 await type('模型名称', 'after-conflict') await click('保存配置') expect(writes().at(-1)!.body).toMatchObject({ base_revision: 2, model: 'after-conflict' }) }) })