Files
knowledge-governance/web/src/WeKnoraMonitor.binding.test.tsx
T
Zhero & Rin 6a12196bf3
build / build (push) Successful in 1m51s
fix(reconciliation): 失败提示改中文并去掉绑定页 090
- 页面 9 个出口统一走 reconciliationMessages:四个内部码不再进入 DOM,未知码回退中文;批次行/阶段码同样翻译。
- 绑定页加载与点击前读 /api/v1/kb-bindings 预检:测试环境未配置当场提示且不发 POST;未启用优先于绑定缺失。
- 运行时读按上游 401/403 与连接失败拆成 runtime_key_forbidden / runtime_unreachable,保留 404 与未配置语义。
- EnvironmentLabel 与前端环境标签改「测试/生产知识库」,不再出现 090。
- 验:make verify 17 项全绿;go test 三包、web 656 用例、typecheck 通过。
2026-12-02 10:36:15 +08:00

152 lines
4.9 KiB
TypeScript

// @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(
<AuthContext.Provider value={auth}>
<WeKnoraMonitor />
</AuthContext.Provider>
)
)
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)
})
})