import { afterEach, describe, expect, it, vi } from 'vitest' import { fetchMonitorTasks, isCurrentTarget, targetLabel } from './monitorTasks' import { healthLabel } from './WeKnoraMonitor' import type { ServiceHealth } from './monitor' afterEach(() => vi.unstubAllGlobals()) describe('monitor table contract', () => { it('rejects malformed facets instead of rendering a broken selector', async () => { vi.stubGlobal( 'fetch', vi.fn().mockResolvedValue({ ok: true, json: async () => ({ data: { items: [], total: 0, limit: 20, offset: 0, retryable: 0, sources: [{ id: 7, name: 'bad' }], targets: [], }, }), }) ) await expect(fetchMonitorTasks({ status: 'active' })).rejects.toThrow('筛选项') }) it('passes the filter and page to the server without client-side totals', async () => { const data = { items: [], total: 145, limit: 20, offset: 40, retryable: 12, sources: [], targets: [] } const request = vi.fn().mockResolvedValue({ ok: true, json: async () => ({ data }) }) vi.stubGlobal('fetch', request) const result = await fetchMonitorTasks({ status: 'attention', action: 'remove', q: '手册' }, 40) expect(result.total).toBe(145) expect(result.retryable).toBe(12) const url = new URL(request.mock.calls[0][0], 'http://localhost') expect(url.searchParams.get('offset')).toBe('40') expect(url.searchParams.get('action')).toBe('remove') expect(url.searchParams.get('q')).toBe('手册') }) it('distinguishes unknown, unconfigured, blocked and inapplicable services', () => { const s = { applicable: true, configured: true, readiness: 'unknown' } as ServiceHealth expect(healthLabel(s)).toBe('未确认') expect(healthLabel({ ...s, configured: false })).toBe('未配置') expect(healthLabel({ ...s, applicable: false })).toBe('不适用') expect(healthLabel({ ...s, readiness: 'blocked' })).toBe('阻断') }) }) describe('target identity labels (F15)', () => { const bindings = { internal: '0c84a098-d3ed-4b82-a165-e6de9691d6a8', external: 'ext-11112222-3333' } it('distinguishes same-named knowledge bases by short id and binding state', () => { const current = targetLabel('对内知识库', bindings.internal, 'internal', bindings) const historical = targetLabel('对内知识库', 'sc-ed1f812c4e5fabe817fc6351104938c4', 'internal', bindings) expect(current).toContain('0c84a098') expect(current).toContain('当前绑定') expect(historical).toContain('sc-ed1f8') expect(historical).toContain('历史') expect(current).not.toBe(historical) }) it('never treats an env/unknown target as the current binding', () => { expect(isCurrentTarget('1b52c140-2e18-4676-afc5-4c7d8d190a8c', 'internal', bindings)).toBe(false) }) })