feat: 知识治理平台(含已验证发布/回滚契约)

全栈知识治理平台首次入库(P1-P10 + 发布链路模拟验证闭环):

- 治理核心:源目录/变更集/人工审核/发布批次/发布执行(fail-closed 闸口)
- 测试通道:WeKnora 测试 KB 上传/轮询/验收;poll 完成时写入
  parser_generation/index_generation,修复 manual/seed 路径发布闸口永久拦截
- 发布适配器 v071(本地 source=测试角色 / 云端 target=生产角色 模拟验证通过):
  * ES 导出/导入 mapping 自适应(.keyword 子字段、可选 tenant_id)
  * OSS locator 支持嵌套 scheme;chunks.seq_id 由目标序列分配
  * bundle 归档同身份重试原子替换;verifier 按 bundle chunk_type 计数
  * 回滚 job payload 支持 prior_execution_id(strict 解码)
- 三通道发布+回滚演练实测:publish 4/4 verified,检索命中,
  rollback 4/4 rolled_back,目标端 PG is_enabled=false、ES is_enabled=false
- 前端 A·Data-console 风格全站统一,120 用例通过
- 门禁:go vet / go test 全包通过,gofmt 干净
This commit is contained in:
2026-09-13 12:02:58 +08:00
committed by 姚章浩
parent ed48c4ceda
commit dcdc8d1ddd
410 changed files with 56661 additions and 1 deletions
+143
View File
@@ -0,0 +1,143 @@
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'], 'engineer-1', '已核对差异')).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'], operator_ref: 'engineer-1', 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'], operator_ref: '姚工', 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')
})
})