// @vitest-environment jsdom import { act } from 'react' import { createRoot } from 'react-dom/client' import type { Root } from 'react-dom/client' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { WeKnoraMonitor } from './WeKnoraMonitor' import { AuthContext } from './auth' import { reconciliationMessages } from './reconciliationMessages' const binding = (environment: 'test' | 'production', configured: boolean) => ({ environment, revision: 1, base_url: configured ? `https://${environment}.weknora.example` : '', key_configured: configured, key_source: configured ? 'sealed' : '', key_tail: '', internal_kb_id: '', internal_kb_name: '', external_kb_id: '', external_kb_name: '', verification: configured ? 'verified' : 'not_configured', checked_at: null, updated_by: 'admin', updated_at: null, source: 'web', storage_ready: true, replanned: 0, retired_duplicates: 0, internal_fallback: '', external_fallback: '', frozen: 0, retargetable: 0, }) const auth = { identity: { id: 'admin-1', username: 'admin', display_name: 'admin', role: 'admin' as const, must_change_password: false, }, ready: true, login: async () => null, logout: async () => {}, changePassword: async () => null, signedOut: 0, notifySignedOut: () => {}, } let container: HTMLDivElement let root: Root const flush = async () => { await act(async () => { await new Promise((resolve) => setTimeout(resolve, 0)) }) } const json = (value: unknown, status = 200) => new Response(JSON.stringify(value), { status, headers: { 'Content-Type': 'application/json' } }) beforeEach(() => { ;(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true container = document.createElement('div') document.body.appendChild(container) root = createRoot(container) }) afterEach(() => { act(() => root.unmount()) container.remove() vi.unstubAllGlobals() }) const renderMonitor = async () => { await act(async () => root.render( ) ) await flush() await flush() } describe('monitor binding preflight', () => { it('shows the test-binding reason before the KB is selectable and never posts a run', async () => { const fetchMock = vi.fn(async (input: RequestInfo | URL, _init?: RequestInit) => { const url = String(input) if (url === '/api/v1/kb-bindings') { return json({ data: { environments: [binding('test', false), binding('production', true)], storage_ready: true, }, }) } if (url.startsWith('/api/v1/reconciliation/runs')) { return json({ data: { items: [], total: 0, limit: 20, offset: 0 } }) } return json({ error: 'not available in this fixture' }, 503) }) vi.stubGlobal('fetch', fetchMock) await renderMonitor() expect(container.textContent).toContain(reconciliationMessages.bindingMissing) const create = [...container.querySelectorAll('button')].find((button) => button.textContent?.trim() === '立即对账') expect(create).toBeTruthy() expect(create?.hasAttribute('disabled')).toBe(false) await act(async () => create!.click()) await flush() expect(container.textContent).toContain(reconciliationMessages.bindingMissing) const posts = fetchMock.mock.calls.filter( ([url, init]) => String(url).includes('/api/v1/reconciliation/runs') && init?.method === 'POST' ) expect(posts).toHaveLength(0) }) it('keeps the not-enabled reason when the service is unwired and the binding is also missing', async () => { const fetchMock = vi.fn(async (input: RequestInfo | URL, _init?: RequestInit) => { const url = String(input) if (url === '/api/v1/kb-bindings') { return json({ data: { environments: [binding('test', false), binding('production', false)], storage_ready: true, }, }) } if (url.startsWith('/api/v1/reconciliation/runs')) { return json({ error: 'reconciliation_not_configured', code: 'reconciliation_not_configured' }, 503) } return json({ error: 'not available in this fixture' }, 503) }) vi.stubGlobal('fetch', fetchMock) await renderMonitor() expect(container.textContent).toContain(reconciliationMessages.notEnabled) expect(container.textContent).not.toContain(reconciliationMessages.bindingMissing) const create = [...container.querySelectorAll('button')].find((button) => button.textContent?.trim() === '立即对账') expect(create?.hasAttribute('disabled')).toBe(false) await act(async () => create!.click()) await flush() expect(container.textContent).toContain(reconciliationMessages.notEnabled) expect(container.textContent).not.toContain(reconciliationMessages.bindingMissing) }) })