Files
knowledge-governance/web/src/testPublications.test.ts
dev 0fb78cfe26
build / build (push) Successful in 1m51s
feat(web): 发布管理界面精简 + 系统设置审查修复
发布管理:状态卡 + 一个主按钮,准备动作与检查项分层次收纳,去掉重复入口。
系统设置:F02 停滞阈值接入活动配置;F03 分区保存边界;F05/F06 生产绑定分层与凭据说明;
F07 备份未接主机/未知证据如实显示;F09 分区独立加载与保存后重探;F10 文案精简;F11 重置密码对话框。
2026-11-17 15:23:29 +08:00

253 lines
8.1 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest'
import {
fetchAcceptanceResults,
fetchTestPublications,
parseTestPublicationsResponse,
retryPublication,
retryPublicationsBatch,
startTestPublications,
fetchJobFailures,
fetchRemovalRequiredPublications,
startPublicationRemoval,
} 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('需下架条目的移除入口', () => {
it('只取被禁止但仍存活在测试库的条目', async () => {
const fetchMock = vi
.fn()
.mockResolvedValue({ ok: true, json: async () => ({ data: [], total: 0, limit: 50, offset: 0 }) })
vi.stubGlobal('fetch', fetchMock)
await fetchRemovalRequiredPublications()
const url = String(fetchMock.mock.calls[0][0])
expect(url).toContain('/api/v1/test-publications?')
expect(url).toContain('removal=required')
})
it('带幂等键触发下架并回传任务号', async () => {
const fetchMock = vi.fn().mockResolvedValue(
new Response(JSON.stringify({ publication_id: 'tp-1', job_id: 'job-remove-1', state: 'disabling' }), {
status: 202,
})
)
vi.stubGlobal('fetch', fetchMock)
await expect(startPublicationRemoval('tp-1')).resolves.toEqual({ job_id: 'job-remove-1' })
const [url, init] = fetchMock.mock.calls[0]
expect(url).toBe('/api/v1/test-publications/tp-1/removal')
expect(init.method).toBe('POST')
expect(String(init.headers['Idempotency-Key'])).not.toHaveLength(0)
})
it('把服务端拒绝原因原样抛给界面', async () => {
const fetchMock = vi
.fn()
.mockResolvedValue({ status: 409, json: async () => ({ error: '该知识的检索已经移除,无需重复下架' }) })
vi.stubGlobal('fetch', fetchMock)
await expect(startPublicationRemoval('tp-1')).rejects.toThrow('该知识的检索已经移除,无需重复下架')
})
})
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')
})
})