Files
knowledge-governance/web/src/App.test.tsx
T
姚章浩 ef3c1fbf84 fix(web): 侧栏环境标注改为云测试环境
为什么:平台已迁到云测试主机,侧栏底部仍硬编码「本地治理环境」,会让人误以为
操作的是本机旧栈。

改了什么:文案改为「云测试环境」,用例同时断言不再出现旧文案。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 17:09:43 +08:00

1114 lines
35 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { renderToStaticMarkup } from 'react-dom/server'
import { describe, expect, it, vi } from 'vitest'
import App, {
JobDashboard,
KnowledgeInventory,
ModulePage,
ReleaseManagement,
SourceConnections,
WorkspaceShell,
reviewGateLabel,
} from './App'
import { AuthContext } from './auth'
import type { SourceAssetPage } from './assets'
import type { Job } from './jobs'
import type {
ReleaseBatchDetail,
ReleaseBatchPage,
ReleaseExecutionDetail,
ReleaseExecutionPage,
ReleasePlan,
ReviewCandidatePage,
} from './reviewRelease'
import type { SourceConnectionPage, SourceScanDetail, SourceScanPage } from './sourceConnections'
const baseJob: Job = {
id: 'job-1',
type: 'demo',
payload: {},
state: 'pending',
priority: 0,
available_at: '2026-09-03T13:00:00Z',
attempt: 0,
max_attempts: 5,
idempotency_key: 'demo:1',
created_at: '2026-09-03T13:00:00Z',
updated_at: '2026-09-03T13:00:00Z',
}
describe('WorkspaceShell', () => {
it('renders the approved modules in the left navigation', () => {
const html = renderToStaticMarkup(
<WorkspaceShell workspace={{ openTabs: ['dashboard'], activeTab: 'dashboard' }} onOpen={vi.fn()}>
<div>工作区内容</div>
</WorkspaceShell>
)
expect(html).toContain('aria-label="功能导航"')
for (const label of ['来源与目录', '解析监控', '发布管理', '系统设置']) {
expect(html).toContain(label)
}
expect(html).not.toContain('来源链接')
expect(html).not.toContain('待处理文件')
expect(html).toContain('云测试环境')
expect(html).not.toContain('本地治理环境')
})
it('omits the system open-page tab strip while retaining navigation and the current location', () => {
const html = renderToStaticMarkup(
<WorkspaceShell workspace={{ openTabs: ['dashboard', 'tasks', 'releases'], activeTab: 'tasks' }} onOpen={vi.fn()}>
<div>工作区内容</div>
</WorkspaceShell>
)
expect(html).not.toContain('aria-label="已打开页面"')
expect(html).not.toContain('class="tab-list"')
expect(html).not.toContain('class="workspace-tab')
expect(html).toContain('来源与目录')
expect(html).toContain('解析监控')
expect(html).toContain('发布管理')
expect(html).toContain('aria-label="当前位置"')
expect(html).toContain('<b>解析监控</b>')
expect(html).toContain('class="nav-item active"')
})
it('does not render obsolete module-tab close controls', () => {
const html = renderToStaticMarkup(
<WorkspaceShell workspace={{ openTabs: ['dashboard', 'tasks'], activeTab: 'tasks' }} onOpen={vi.fn()}>
<div>工作区内容</div>
</WorkspaceShell>
)
expect(html).not.toContain('aria-label="关闭解析监控标签"')
expect(html).not.toContain('aria-label="关闭来源与目录标签"')
})
it('renders a collapsed mobile navigation control', () => {
const html = renderToStaticMarkup(
<WorkspaceShell workspace={{ openTabs: ['dashboard'], activeTab: 'dashboard' }} onOpen={vi.fn()}>
<div>工作区内容</div>
</WorkspaceShell>
)
expect(html).toContain('aria-label="切换功能导航"')
expect(html).toContain('aria-expanded="false"')
})
})
describe('App', () => {
it('renders the dashboard inside the workspace without the system page tabs', () => {
const html = renderToStaticMarkup(<App />)
expect(html).toContain('aria-label="功能导航"')
expect(html).not.toContain('aria-label="已打开页面"')
expect(html).not.toContain('class="tab-list"')
expect(html).not.toContain('class="workspace-tab')
expect(html).toContain('<h1 class="page-title">来源与目录</h1>')
expect(html).not.toContain('最近任务')
})
})
describe('ModulePage', () => {
it('renders the governance overview for the dashboard module', () => {
const html = renderToStaticMarkup(
<ModulePage
moduleId="dashboard"
jobs={[baseJob, { ...baseJob, id: 'job-2', state: 'completed' }]}
loading={false}
error={null}
onRefresh={vi.fn()}
/>
)
expect(html).toContain('治理工作台')
expect(html).toContain('P2 · 知识治理总览')
expect(html).toContain('PP历史来源清单已接管')
expect(html).toContain('生产写入:关闭')
expect(html).toContain('以「系统设置 → 服务配置」的实时探测为准')
expect(html).not.toContain('本地测试WeKnora已连接')
expect(html).not.toContain('生产WeKnora仍未连接')
expect(html).not.toContain('尚未连接测试或生产WeKnora')
expect(html).toContain('任务概览')
expect(html).toContain('Release Worker 默认关闭')
expect(html).toContain('最近任务')
expect(html).toContain('job-2')
})
it('renders the test publication table for the tasks module', () => {
const html = renderToStaticMarkup(
<ModulePage
moduleId="tasks"
jobs={[]}
loading={false}
error={null}
onRefresh={vi.fn()}
publicationPage={{
items: [
{
id: 'pub-1',
title: 'Safe title',
ingest_state: 'failed',
processing_state: 'failed',
retrieval_state: 'unpublished',
target_scope: 'internal',
change_set_id: 'cs-1',
change_type: 'added',
can_start: false,
retry_count: 2,
last_error: 'remote_parse_failed',
can_retry: true,
updated_at: '2026-09-04T05:00:00Z',
},
],
total: 1,
limit: 50,
offset: 0,
}}
publicationLoading={false}
publicationError={null}
publicationState=""
onPublicationStateChange={vi.fn()}
onPublicationPageChange={vi.fn()}
onPublicationRefresh={vi.fn()}
onRetry={vi.fn()}
/>
)
expect(html).toContain('<h1>解析监控</h1>')
expect(html).toContain('总体概览')
expect(html).toContain('失败或停滞')
expect(html).toContain('pub-1')
expect(html).toContain('remote_parse_failed')
expect(html).toContain('重试')
})
it('renders the real inventory for the knowledge module', () => {
const html = renderToStaticMarkup(
<ModulePage
moduleId="knowledge"
jobs={[]}
loading={false}
error={null}
onRefresh={vi.fn()}
assetPage={{
items: [],
summary: { total: 613, completed: 530, content_alias: 29, over_limit: 54 },
limit: 50,
offset: 0,
}}
assetLoading={false}
assetError={null}
assetState=""
onAssetStateChange={vi.fn()}
onAssetPageChange={vi.fn()}
onAssetRefresh={vi.fn()}
/>
)
expect(html).toContain('统一治理清单')
expect(html).toContain('613')
expect(html).not.toContain('功能将在后续阶段接入')
})
it('renders the real source connection workbench', () => {
const html = renderToStaticMarkup(
<ModulePage
moduleId="sources"
jobs={[]}
loading={false}
error={null}
onRefresh={vi.fn()}
connectionPage={{
items: [
{
id: 'conn-pp',
source_type: 'pp',
name: 'PP历史公开知识',
enabled: false,
schedule_configured: false,
health_status: 'healthy',
item_count: 613,
updated_at: '2026-09-06T01:00:00Z',
last_scan: {
id: 'scan-1',
state: 'completed',
observed_items: 613,
unchanged_items: 559,
added_items: 0,
updated_items: 0,
over_limit_items: 54,
missing_items: 0,
planned_uploads: 0,
error_code: '',
created_at: '2026-09-06T00:59:00Z',
completed_at: '2026-09-06T01:00:00Z',
},
},
{
id: 'conn-alist',
source_type: 'alist',
name: 'AList只读源',
enabled: true,
schedule_configured: true,
health_status: 'healthy',
item_count: 42,
updated_at: '2026-09-06T01:00:00Z',
},
],
summary: { total: 2, enabled: 1, disabled: 1, healthy: 1, degraded: 0, unhealthy: 0, unknown: 1 },
}}
connectionLoading={false}
connectionError={null}
onConnectionRefresh={vi.fn()}
scanningConnectionId={null}
onTriggerSourceScan={vi.fn()}
/>
)
expect(html).toContain('<h1>来源连接</h1>')
expect(html).not.toContain('<code>conn-pp</code>')
for (const text of [
'来源连接',
'PP历史公开知识',
'AList只读源',
'613',
'已配置',
'健康',
'最近扫描',
'未变化 559',
'超过上限 54',
'检查快照变化',
]) {
expect(html).toContain(text)
}
expect(html).toContain('class="source-connections-table"')
expect(html).not.toContain('将在后续阶段接入')
})
})
describe('KnowledgeInventory', () => {
it('renders real source summary and safe paginated asset fields', () => {
const page: SourceAssetPage = {
items: [
{
id: 'si-safe',
source_type: 'pp',
connection_name: 'PP历史公开知识',
size_bytes: 52428801,
visibility: 'external',
source_change: 'unchanged',
content_state: 'over_limit',
legacy_status: 'over_limit',
has_publication: false,
processing_state: '',
retrieval_state: '',
updated_at: '2026-09-04T00:00:00Z',
},
{
id: 'si-manual',
source_type: 'manual' as never,
connection_name: '本地验收',
size_bytes: 128,
visibility: 'internal',
source_change: 'added',
content_state: 'verified',
legacy_status: '',
has_publication: true,
processing_state: 'pending',
retrieval_state: 'unpublished',
updated_at: '2026-09-05T00:00:00Z',
},
],
summary: { total: 613, completed: 530, content_alias: 29, over_limit: 54 },
limit: 50,
offset: 0,
}
const html = renderToStaticMarkup(
<KnowledgeInventory
page={page}
loading={false}
error={null}
state=""
onStateChange={vi.fn()}
onPageChange={vi.fn()}
onRefresh={vi.fn()}
/>
)
for (const text of ['知识资产', '613', '530', '29', '54', 'PP', '手工验收', '50.0 MiB', '超过旧同步上限']) {
expect(html).toContain(text)
}
expect(html).toContain('统一来源清单')
expect(html).not.toContain('当前展示PP历史公开来源')
})
})
describe('SourceConnections scan workbench', () => {
it('renders scan history and selected safe change records', () => {
const now = '2026-09-06T06:00:00Z'
const page: SourceConnectionPage = {
items: [
{
id: 'conn-pp',
source_type: 'pp',
name: 'PP历史公开知识',
enabled: false,
schedule_configured: false,
health_status: 'healthy',
item_count: 613,
updated_at: now,
},
],
summary: { total: 1, enabled: 0, disabled: 1, healthy: 1, degraded: 0, unhealthy: 0, unknown: 0 },
}
const scanPage: SourceScanPage = {
items: [
{
id: 'scan-1',
state: 'completed',
observed_items: 613,
unchanged_items: 558,
added_items: 0,
updated_items: 1,
over_limit_items: 54,
missing_items: 0,
planned_uploads: 1,
error_code: '',
created_at: now,
completed_at: now,
},
],
total: 1,
limit: 20,
offset: 0,
}
const detail: SourceScanDetail = {
...scanPage.items[0],
changes: [
{ id: 'chg-1', source_item_id: 'si-safe', change_type: 'updated', observed_size_bytes: 2048, created_at: now },
],
}
const html = renderToStaticMarkup(
<SourceConnections
page={page}
loading={false}
error={null}
scanningConnectionId={null}
scanPage={scanPage}
scanDetail={detail}
scanLoading={false}
scanError={null}
selectedScanId="scan-1"
onRefresh={vi.fn()}
onTriggerScan={vi.fn()}
onSelectScan={vi.fn()}
/>
)
for (const text of ['扫描历史与变化记录', 'scan-1', '已选择', '变化记录', '内容更新', 'si-safe', '2.0 KiB']) {
expect(html).toContain(text)
}
expect(html).not.toContain('stable_external_id')
expect(html).not.toContain('observed_revision')
})
it('states explicitly when a selected scan has no changes', () => {
const now = '2026-09-06T06:00:00Z'
const scan = {
id: 'scan-1',
state: 'completed' as const,
observed_items: 613,
unchanged_items: 559,
added_items: 0,
updated_items: 0,
over_limit_items: 54,
missing_items: 0,
planned_uploads: 0,
error_code: '',
created_at: now,
completed_at: now,
}
const html = renderToStaticMarkup(
<SourceConnections
page={{
items: [],
summary: { total: 0, enabled: 0, disabled: 0, healthy: 0, degraded: 0, unhealthy: 0, unknown: 0 },
}}
loading={false}
error={null}
scanningConnectionId={null}
scanPage={{ items: [scan], total: 1, limit: 20, offset: 0 }}
scanDetail={{ ...scan, changes: [] }}
scanLoading={false}
scanError={null}
selectedScanId="scan-1"
onRefresh={vi.fn()}
onTriggerScan={vi.fn()}
onSelectScan={vi.fn()}
/>
)
expect(html).toContain('本次扫描未发现新增、更新或不可用变化')
})
})
describe('JobDashboard', () => {
it('renders summary counts and task states', () => {
const html = renderToStaticMarkup(
<JobDashboard
jobs={[
baseJob,
{ ...baseJob, id: 'job-2', state: 'running', attempt: 1 },
{ ...baseJob, id: 'job-3', state: 'completed', attempt: 1 },
]}
loading={false}
error={null}
onRefresh={vi.fn()}
/>
)
expect(html).toContain('同步任务')
expect(html).toContain('任务总数')
expect(html).toContain('>3<')
expect(html).toContain('运行中')
expect(html).toContain('已完成')
expect(html).toContain('job-2')
})
})
describe('ReleaseManagement', () => {
it('renders human review, draft and seal gates without implying production release', () => {
const now = '2026-09-06T10:00:00Z'
const priorNow = '2026-09-06T09:00:00Z'
const reviewPage: ReviewCandidatePage = {
items: [
{
publication_id: 'tp-safe',
title: 'Synthetic',
source_type: 'manual',
current_visibility: 'internal',
knowledge_exists: 'passed',
chunks_present: 'passed',
retrieval_hit: 'manual_required',
gate_status: 'ready',
gate_code: 'ready',
review_state: 'pending',
decided_visibility: '',
decision_code: '',
reviewer_ref: '',
reviewed_at: null,
updated_at: now,
},
],
total: 1,
limit: 20,
offset: 0,
}
const batchPage: ReleaseBatchPage = {
items: [
{
id: 'rb-safe',
name: 'Synthetic batch',
state: 'draft',
policy_version: 'release-gate-v1',
created_by: 'engineer-1',
sealed_by: '',
item_count: 1,
created_at: now,
sealed_at: null,
},
],
total: 1,
limit: 20,
offset: 0,
}
const batchDetail: ReleaseBatchDetail = {
...batchPage.items[0],
items: [{ publication_id: 'tp-safe', title: 'Synthetic', decided_visibility: 'internal', action: 'delete' }],
}
const executionPage: ReleaseExecutionPage = {
items: [
{
id: 'rx-auto',
release_batch_id: 'rb-safe',
operation: 'rollback',
prior_execution_id: 'rx-safe',
state: 'running',
target_environment: 'production',
authorized_by: 'system:auto-rollback',
job_id: 'job-auto',
item_count: 1,
batch_size: 50,
current_batch: 1,
total_batches: 1,
completed_item_count: 0,
failed_item_count: 0,
error_code: '',
created_at: now,
started_at: now,
completed_at: null,
},
{
id: 'rx-safe',
release_batch_id: 'rb-safe',
operation: 'publish',
prior_execution_id: '',
state: 'failed',
target_environment: 'production',
authorized_by: 'owner',
job_id: 'job-safe',
item_count: 2,
batch_size: 50,
current_batch: 1,
total_batches: 1,
completed_item_count: 1,
failed_item_count: 1,
error_code: 'release_item_failed',
created_at: priorNow,
started_at: priorNow,
completed_at: priorNow,
},
],
total: 2,
limit: 20,
offset: 0,
}
const executionDetail: ReleaseExecutionDetail = {
...executionPage.items[0],
items: [
{
publication_id: 'tp-safe',
title: 'Synthetic',
action: 'delete',
state: 'verified',
previous_production_revision: 'prod-v0',
production_revision: 'absent',
error_code: '',
created_at: now,
completed_at: now,
target_knowledge_base_id: '0b217e8a-2a65-40a3-ab8f-34ac5c7a6f05',
},
{
publication_id: 'tp-restored',
title: 'Restored',
action: 'delete',
state: 'rolled_back',
previous_production_revision: 'prod-v0',
production_revision: 'prod-v0',
error_code: '',
created_at: now,
completed_at: now,
target_knowledge_base_id: '0b217e8a-2a65-40a3-ab8f-34ac5c7a6f05',
},
],
}
const releasePlan: ReleasePlan = {
items: [
{
publication_id: 'tp-safe',
title: 'B.pdf',
target_scope: 'external',
action: 'add',
test_state: 'ready',
production_state: 'absent',
can_release: true,
blocker: '',
},
{
publication_id: 'tp-delete',
title: 'C.pdf',
target_scope: 'internal',
action: 'delete',
test_state: 'not_required',
production_state: 'present',
can_release: true,
blocker: '',
},
{
publication_id: '',
title: 'D.pdf',
target_scope: 'internal',
action: 'blocked',
test_state: 'ready',
production_state: 'present',
can_release: false,
blocker: 'source_unavailable',
},
],
summary: { add: 1, update: 0, delete: 1, blocked: 1, unchanged: 11284 },
}
const html = renderToStaticMarkup(
<ReleaseManagement
reviewPage={reviewPage}
reviewLoading={false}
reviewError={null}
reviewingId={null}
batchPage={batchPage}
batchDetail={batchDetail}
batchLoading={false}
batchError={null}
mutatingBatchId={null}
onReview={vi.fn()}
onReviewRefresh={vi.fn()}
onCreateBatch={vi.fn()}
onSealBatch={vi.fn()}
onSelectBatch={vi.fn()}
onBatchRefresh={vi.fn()}
releaseCapabilities={{
production_release_requests_enabled: true,
production_backup_restore_verified: false,
test_auto_review_policy: '',
}}
executionPage={executionPage}
executionDetail={executionDetail}
executionLoading={false}
executionError={null}
onSelectExecution={vi.fn()}
onExecutionRefresh={vi.fn()}
releasePlan={releasePlan}
selectedDeleteIDs={['tp-delete']}
onToggleDelete={vi.fn()}
/>
)
for (const text of [
'来源与目录 → 测试知识库 → 生产知识库',
'发布计划',
'新增 1',
'更新 0',
'删除 1',
'未变化 11284',
'B.pdf',
'C.pdf',
'D.pdf',
'将从生产检索中移除',
'我确认所选删除项会停止生产检索',
'待发布(0 + 下架 1)',
'已选 1 项(删除待确认)',
'为什么按钮不能点:请确认所选删除项会停止生产检索;至少 1 项动作通过确认;请填写批次名称',
'至少 1 项动作通过确认',
'来源当前不可用,保留生产知识',
'正在执行自动回滚',
'批次 1 / 1',
'每批最多 50 项',
'Snapshot #20260906-090000Z',
'待你审核',
'需人工确认',
'批准',
'驳回',
'创建并锁定清单',
'锁定清单(不会上线)',
'Synthetic batch',
'删除生产知识',
'已从生产检索中移除',
'已恢复生产知识',
'仍可检查调整',
'Release Worker 默认关闭',
'锁定清单 ≠ 上线',
'生产确认、发布验证与回滚',
'生产操作权限:已阻断',
'生产请求门禁已开启',
'快照恢复验证未通过',
'生产发布已阻断',
]) {
expect(html).toContain(text)
}
// 需求(2026-10-10):发布界面要简单——不再显示步骤脚手架/流程编号/引导卡。
for (const text of [
'从检查到发布,只走这 7 步',
'1. 查看来源变化',
'2. 确认测试结果',
'3. 审核发布内容',
'4. 创建并锁定发布清单',
'5. 生产负责人确认',
'6. 发布并验证',
'7. 必要时回滚',
'当前在第',
'共 7 步',
'现在要做:',
'完成本步的',
'完成后进入:第',
'如何决定批准或驳回',
'查看审核与发布说明',
'普通业务操作,不会影响生产',
'生产危险操作',
'module-tabs',
'release-journey',
'release-guidance',
'guidance-step',
]) {
expect(html).not.toContain(text)
}
expect(html).not.toContain('>授权生产发布</button>')
expect(html).not.toContain('knowledge_id')
expect(html).not.toContain('acceptance_run_id')
expect(html).toContain('aria-label="选择删除C.pdf"')
expect(html).toContain('筛选 3 / 全部 3')
expect(html).toContain('aria-label="确认删除生产知识"')
expect(html).toContain('class="plan-delete"')
expect(html).toContain('<span class="unmet">至少 1 项动作通过确认</span>')
expect(reviewGateLabel({ review_state: 'approved', gate_status: 'ready' })).toBe('测试已通过')
expect(reviewGateLabel({ review_state: 'pending', gate_status: 'ready' })).toBe('可以审核')
for (const technicalValue of [
'tp-safe',
'rb-safe',
'rx-safe',
'job-safe',
'release-gate-v1',
'prod-v0',
'prod-v1',
'>verified<',
]) {
expect(html).not.toContain(technicalValue)
}
expect(html).not.toContain('工程师标识')
})
})
describe('release plan pagination (R3)', () => {
const baseProps = {
reviewPage: { items: [], total: 0, limit: 20, offset: 0 } as any,
reviewLoading: false,
reviewError: null,
reviewingId: null,
batchPage: { items: [], total: 0, limit: 20, offset: 0 } as any,
batchDetail: null,
batchLoading: false,
batchError: null,
mutatingBatchId: null,
onReview: vi.fn(),
onReviewRefresh: vi.fn(),
onCreateBatch: vi.fn(),
onSealBatch: vi.fn(),
onSelectBatch: vi.fn(),
onBatchRefresh: vi.fn(),
releaseCapabilities: {
production_release_requests_enabled: false,
production_backup_restore_verified: false,
test_auto_review_policy: '',
},
executionPage: { items: [], total: 0, limit: 20, offset: 0 } as any,
executionDetail: null,
executionLoading: false,
executionError: null,
onSelectExecution: vi.fn(),
onExecutionRefresh: vi.fn(),
selectedDeleteIDs: [] as string[],
onToggleDelete: vi.fn(),
}
const planWith = (count: number): ReleasePlan => ({
items: Array.from({ length: count }, (_, i) => ({
publication_id: `tp-${i}`,
title: `文档${i}.pdf`,
target_scope: 'internal' as const,
action: 'add' as const,
test_state: 'ready',
production_state: 'absent',
can_release: true,
blocker: '',
})),
summary: { add: count, update: 0, delete: 0, blocked: 0, unchanged: 0 },
})
it('renders at most one page (25 rows) instead of the full 618-row plan', () => {
const html = renderToStaticMarkup(<ReleaseManagement {...baseProps} releasePlan={planWith(618)} />)
expect(html).toContain('筛选 618 / 全部 618')
expect(html).toContain('第 1 / 25 页 · 每页 25 条')
expect(html.match(/class="release-plan-table"/g)?.length ?? 0).toBeLessThanOrEqual(1)
// Only page-1 titles render; a page-2 row and a late row are absent from the DOM.
expect(html).toContain('文档0.pdf')
expect(html).not.toContain('文档25.pdf')
expect(html).not.toContain('文档617.pdf')
expect(html).toContain('仅可发布')
expect(html).toContain('下一页')
})
it('keeps small plans pagination-free and pages exactly at the 25-item boundary', () => {
const small = renderToStaticMarkup(<ReleaseManagement {...baseProps} releasePlan={planWith(3)} />)
expect(small).toContain('筛选 3 / 全部 3')
expect(small).not.toContain('上一页')
const exact = renderToStaticMarkup(<ReleaseManagement {...baseProps} releasePlan={planWith(25)} />)
expect(exact).not.toContain('上一页')
expect(exact).toContain('文档24.pdf')
const over = renderToStaticMarkup(<ReleaseManagement {...baseProps} releasePlan={planWith(26)} />)
expect(over).toContain('第 1 / 2 页 · 每页 25 条')
expect(over).not.toContain('文档25.pdf')
const huge = renderToStaticMarkup(<ReleaseManagement {...baseProps} releasePlan={planWith(5000)} />)
expect(huge.match(/<tr /g)?.length ?? 0).toBeLessThanOrEqual(60)
expect(huge).toContain('第 1 / 200 页')
})
})
describe('R4 rollback levels in the execution timeline', () => {
const baseProps = {
reviewPage: { items: [], total: 0, limit: 20, offset: 0 } as any,
reviewLoading: false,
reviewError: null,
reviewingId: null,
batchPage: { items: [], total: 0, limit: 20, offset: 0 } as any,
batchDetail: null,
batchLoading: false,
batchError: null,
mutatingBatchId: null,
onReview: vi.fn(),
onReviewRefresh: vi.fn(),
onCreateBatch: vi.fn(),
onSealBatch: vi.fn(),
onSelectBatch: vi.fn(),
onBatchRefresh: vi.fn(),
releaseCapabilities: {
production_release_requests_enabled: true,
production_backup_restore_verified: false,
test_auto_review_policy: '',
},
releasePlan: { items: [], summary: { add: 0, update: 0, delete: 0, blocked: 0, unchanged: 0 } } as ReleasePlan,
executionDetail: null,
executionLoading: false,
executionError: null,
onSelectExecution: vi.fn(),
onExecutionRefresh: vi.fn(),
selectedDeleteIDs: [] as string[],
onToggleDelete: vi.fn(),
}
const exec = (over: Record<string, unknown>) =>
({
id: 'rx-1',
release_batch_id: 'rb-1',
operation: 'rollback',
prior_execution_id: 'rx-0',
state: 'rolling_back',
target_environment: 'production',
authorized_by: 'system:auto-rollback',
job_id: 'job-1',
item_count: 2,
batch_size: 50,
current_batch: 1,
total_batches: 1,
completed_item_count: 1,
failed_item_count: 0,
error_code: '',
failure_level: '',
restore_trigger: 'auto_rollback_after_failure',
created_at: '2026-09-12T09:00:00Z',
started_at: '2026-09-12T09:01:00Z',
completed_at: null,
...over,
}) as any
it('labels the compensating stage and the escalation after a failed rollback', () => {
const html = renderToStaticMarkup(
<ReleaseManagement {...baseProps} executionPage={{ items: [exec({})], total: 1, limit: 20, offset: 0 }} />
)
expect(html).toContain('正在回滚')
expect(html).toContain('正在执行自动回滚')
const failed = renderToStaticMarkup(
<ReleaseManagement
{...baseProps}
executionPage={{
items: [exec({ state: 'rollback_failed', failed_item_count: 1 })],
total: 1,
limit: 20,
offset: 0,
}}
/>
)
expect(failed).toContain('回滚失败')
expect(failed).toContain('整体恢复')
const done = renderToStaticMarkup(
<ReleaseManagement
{...baseProps}
executionPage={{
items: [exec({ state: 'rolled_back', completed_item_count: 2 })],
total: 1,
limit: 20,
offset: 0,
}}
/>
)
expect(done).toContain('已回滚')
expect(done).toContain('已恢复')
const graded = renderToStaticMarkup(
<ReleaseManagement
{...baseProps}
executionPage={{
items: [exec({ operation: 'publish', state: 'failed', failure_level: 'batch' })],
total: 1,
limit: 20,
offset: 0,
}}
/>
)
expect(graded).toContain('本批后续已停止')
})
})
// 生产动作(授权发布 / 重排 / 回滚):页面按钮只对管理员出现,且必须逐字输入固定确认语句。
describe('ReleaseManagement production actions', () => {
const authValue = (role: 'admin' | 'engineer') => ({
identity: { id: `${role}-1`, username: role, display_name: role, role, must_change_password: false },
ready: true,
login: async () => null,
logout: async () => {},
changePassword: async () => null,
signedOut: 0,
notifySignedOut: () => {},
})
const now = '2026-10-08T08:00:00Z'
const batchPage: ReleaseBatchPage = {
items: [
{
id: 'rb-sealed',
name: '单文档首发',
state: 'sealed',
policy_version: 'release-gate-v1',
created_by: 'admin',
sealed_by: 'admin',
item_count: 1,
created_at: now,
sealed_at: now,
},
],
total: 1,
limit: 20,
offset: 0,
}
const executionPage: ReleaseExecutionPage = {
items: [
{
id: 'rx-done',
release_batch_id: 'rb-sealed',
operation: 'publish',
prior_execution_id: '',
state: 'completed',
target_environment: 'production',
authorized_by: 'admin',
job_id: 'job-1',
item_count: 1,
batch_size: 50,
current_batch: 1,
total_batches: 1,
completed_item_count: 1,
failed_item_count: 0,
error_code: '',
created_at: now,
started_at: now,
completed_at: now,
},
{
id: 'rx-bad',
release_batch_id: 'rb-sealed',
operation: 'publish',
prior_execution_id: '',
state: 'failed',
target_environment: 'production',
authorized_by: 'admin',
job_id: 'job-2',
item_count: 1,
batch_size: 50,
current_batch: 1,
total_batches: 1,
completed_item_count: 0,
failed_item_count: 1,
error_code: 'release_item_failed',
created_at: now,
started_at: now,
completed_at: now,
},
],
total: 2,
limit: 20,
offset: 0,
}
const base = (extra: Record<string, unknown> = {}) => (
<ReleaseManagement
reviewPage={{ items: [], total: 0, limit: 20, offset: 0 }}
reviewLoading={false}
reviewError={null}
reviewingId={null}
batchPage={batchPage}
batchDetail={null}
batchLoading={false}
batchError={null}
mutatingBatchId={null}
onReview={vi.fn()}
onReviewRefresh={vi.fn()}
onCreateBatch={vi.fn()}
onSealBatch={vi.fn()}
onSelectBatch={vi.fn()}
onBatchRefresh={vi.fn()}
releaseCapabilities={{
production_release_requests_enabled: true,
production_backup_restore_verified: true,
test_auto_review_policy: '',
}}
executionPage={executionPage}
executionDetail={null}
executionLoading={false}
executionError={null}
onSelectExecution={vi.fn()}
onExecutionRefresh={vi.fn()}
releasePlan={{ items: [], summary: { add: 0, update: 0, delete: 0, blocked: 0, unchanged: 0 } }}
selectedDeleteIDs={[]}
onToggleDelete={vi.fn()}
{...extra}
/>
)
it('offers 授权发布 / 重排 / 回滚 to admins only', () => {
const admin = renderToStaticMarkup(<AuthContext.Provider value={authValue('admin')}>{base()}</AuthContext.Provider>)
expect(admin).toContain('授权发布')
expect(admin).toContain('重排')
expect(admin).toContain('回滚')
expect(admin).toContain('生产操作可执行(待确认)')
const engineer = renderToStaticMarkup(
<AuthContext.Provider value={authValue('engineer')}>{base()}</AuthContext.Provider>
)
expect(engineer).not.toContain('授权发布')
expect(engineer).not.toContain('<button type="button">回滚</button>')
expect(engineer).not.toContain('<button type="button">重排</button>')
})
it('requires the exact confirmation phrase before the action can be submitted', () => {
const pending = { kind: 'authorize' as const, id: 'rb-sealed', label: '单文档首发' }
const wrong = renderToStaticMarkup(
<AuthContext.Provider value={authValue('admin')}>
{base({
pendingReleaseAction: pending,
releaseActionPhrase: 'publish',
onStartReleaseAction: vi.fn(),
onReleaseActionPhrase: vi.fn(),
onSubmitReleaseAction: vi.fn(),
onCancelReleaseAction: vi.fn(),
})}
</AuthContext.Provider>
)
expect(wrong).toContain('publish-to-production')
expect(wrong).toMatch(/<button type="button" disabled=""[^>]*>确认执行<\/button>/)
const right = renderToStaticMarkup(
<AuthContext.Provider value={authValue('admin')}>
{base({
pendingReleaseAction: pending,
releaseActionPhrase: 'publish-to-production',
onStartReleaseAction: vi.fn(),
onReleaseActionPhrase: vi.fn(),
onSubmitReleaseAction: vi.fn(),
onCancelReleaseAction: vi.fn(),
})}
</AuthContext.Provider>
)
expect(right).toMatch(/<button type="button">确认执行<\/button>/)
})
it('surfaces the server reason when the action is refused', () => {
const html = renderToStaticMarkup(
<AuthContext.Provider value={authValue('admin')}>
{base({
pendingReleaseAction: null,
releaseActionError: 'production release authorization required',
releaseActionMessage: null,
onStartReleaseAction: vi.fn(),
onReleaseActionPhrase: vi.fn(),
onSubmitReleaseAction: vi.fn(),
onCancelReleaseAction: vi.fn(),
})}
</AuthContext.Provider>
)
expect(html).toContain('production release authorization required')
})
})