Files
knowledge-governance/web/src/testPublications.test.ts
T
zheromiku f6cb90cd70 feat(web): login gate, session identity UI and member management
- main.tsx 挂 AuthProvider 门控:未登录→登录页;must_change_password→强制改密页
- 登录页/改密页/顶栏身份(头像+姓名+角色徽章+退出)按 A·Data-console token 样式
- 全局 fetch 拦截器:任意业务请求 401 自动回登录页(会话过期/被撤销不留死表单)
- 系统设置新增「成员与角色」分区(仅 admin 可见):名单、角色调整、启停、重置临时密码、新增成员
- 消灭全部手填操作人:审核/封批/确认计划集/范围规则/来源调度/UI 设置/开始处理/批量重试/
  目录差异确认共 9 处输入框删除,请求体不再携带 actor 字段(服务端从会话注入)
- 测试断言方向翻转:显式验证请求体不含 operator_ref/created_by 等字段
- 附带修复:登录失败计数 SQL 参数类型无法推断导致 500(pgx 扩展协议),显式 cast 解决

前端门禁:tsc 0 错、125 用例全绿、build 通过
2026-09-30 10:54:07 +08:00

144 lines
6.0 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest'
import { fetchAcceptanceResults, fetchTestPublications, parseTestPublicationsResponse, retryPublication, retryPublicationsBatch, startTestPublications, fetchJobFailures } from './testPublications'
afterEach(() => vi.unstubAllGlobals())
describe('parseTestPublicationsResponse', () => {
it('parses the safe publication projection envelope', () => {
const result = parseTestPublicationsResponse({
data: [{
id: 'pub-safe', title: 'Safe title', ingest_state: 'failed',
processing_state: 'failed', retrieval_state: 'unpublished',
target_scope: 'external', change_set_id: 'cs-1', change_type: 'updated', can_start: false,
retry_count: 2, last_error: 'remote_parse_failed', can_retry: true,
updated_at: '2026-09-04T05:00:00Z', test_kb_id: 'must-drop',
}],
total: 1,
limit: 25,
offset: 0,
})
expect(result.total).toBe(1)
expect(result.items[0].can_retry).toBe(true)
expect(result.items[0].target_scope).toBe('external')
expect(result.items[0].change_type).toBe('updated')
expect(result.items[0].last_error).toBe('remote_parse_failed')
expect(result.items[0]).not.toHaveProperty('test_kb_id')
})
})
describe('fetchTestPublications', () => {
it('requests the selected processing state and page', async () => {
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({ data: [], total: 0, limit: 25, offset: 0 }),
})
vi.stubGlobal('fetch', fetchMock)
await fetchTestPublications({ processing_state: 'failed', limit: 25, offset: 0 })
expect(fetchMock).toHaveBeenCalledWith(
'/api/v1/test-publications?processing_state=failed&limit=25&offset=0',
expect.objectContaining({ headers: { Accept: 'application/json' } }),
)
})
})
describe('fetchAcceptanceResults', () => {
it('reads the acceptance record list', async () => {
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({ data: [{ check_type: 'chunks_present', state: 'passed', observed_at: '2026-09-04T05:05:00Z' }] }),
})
vi.stubGlobal('fetch', fetchMock)
const records = await fetchAcceptanceResults('pub-safe')
expect(records).toHaveLength(1)
expect(records[0].check_type).toBe('chunks_present')
expect(fetchMock).toHaveBeenCalledWith(
'/api/v1/test-publications/pub-safe/acceptance',
expect.objectContaining({ headers: { Accept: 'application/json' } }),
)
})
})
describe('retryPublication', () => {
it('posts the retry request', async () => {
const fetchMock = vi.fn().mockResolvedValue({ ok: true })
vi.stubGlobal('fetch', fetchMock)
await retryPublication('pub-safe')
expect(fetchMock).toHaveBeenCalledWith(
'/api/v1/test-publications/pub-safe/retry',
expect.objectContaining({ method: 'POST' }),
)
})
it('surfaces the server error message', async () => {
const fetchMock = vi.fn().mockResolvedValue({
ok: false,
status: 409,
json: async () => ({ error: 'publication is not in a retryable state' }),
})
vi.stubGlobal('fetch', fetchMock)
await expect(retryPublication('pub-safe')).rejects.toThrow('publication is not in a retryable state')
})
})
describe('startTestPublications', () => {
it('starts only the explicitly confirmed processing plans', async () => {
const fetchMock = vi.fn().mockResolvedValue(new Response(JSON.stringify({ data: { selected: 2, started: 1, already_started: 1, unavailable: 0 } }), { status: 202 }))
vi.stubGlobal('fetch', fetchMock)
await expect(startTestPublications(['pub-a', 'pub-b'], '已核对差异')).resolves.toEqual({ selected: 2, started: 1, already_started: 1, unavailable: 0 })
expect(fetchMock).toHaveBeenCalledWith('/api/v1/test-publications/start', expect.objectContaining({
method: 'POST',
headers: expect.objectContaining({ 'Idempotency-Key': expect.any(String) }),
body: JSON.stringify({ publication_ids: ['pub-a', 'pub-b'], reason: '已核对差异' }),
}))
})
})
describe('retryPublicationsBatch', () => {
it('sends one server-side batch request and parses per-item outcomes', async () => {
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({ data: { batch_key: 'op-1', total: 2, started: 1, already_running: 1, not_retryable: 0,
outcomes: [{ publication_id: 'tp-1', result: 'started' }, { publication_id: 'tp-2', result: 'already_running' }] }, operation: 'created' }),
})
vi.stubGlobal('fetch', fetchMock)
const result = await retryPublicationsBatch(['tp-1', 'tp-2'], '解析服务恢复')
expect(fetchMock).toHaveBeenCalledWith('/api/v1/test-publications/retry-batch',
expect.objectContaining({ method: 'POST' }))
const call = fetchMock.mock.calls[0] as [string, RequestInit]
expect(JSON.parse(String(call[1].body))).toEqual({ publication_ids: ['tp-1', 'tp-2'], reason: '解析服务恢复' })
expect(result.started).toBe(1)
expect(result.outcomes[1].result).toBe('already_running')
})
it('rejects a malformed batch envelope', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: true, json: async () => ({ data: {} }) }))
await expect(retryPublicationsBatch(['tp-1'], 'r')).rejects.toThrow()
})
})
describe('fetchJobFailures', () => {
it('reads the open dead-letter projection', async () => {
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({ data: { items: [{ id: 'jf-1', job_id: 'job-1', job_type: 'test.upload', error_code: 'remote_permanent', attempt: 5, max_attempts_at_failure: 5, state: 'open', created_at: '2026-09-11T20:00:00Z' }], total: 1, limit: 50, offset: 0 } }),
})
vi.stubGlobal('fetch', fetchMock)
const items = await fetchJobFailures()
expect(fetchMock).toHaveBeenCalledWith('/api/v1/job-failures?state=open&limit=50', expect.objectContaining({ headers: { Accept: 'application/json' } }))
expect(items[0].error_code).toBe('remote_permanent')
})
})