fix(web): 空/非空/不可读目标分开呈现并收窄建基线入口
build / build (push) Successful in 1m42s

后端 overview 早已区分 known / unknown+empty_baseline / unknown 非空 / unreachable,
页面只判断 state === 'known',把"已证空"和"读不到"都写成"未知",并在两种情况下
都给出"建立目标权威基线"入口——而该接口对空目标返回 409
library_baseline_target_empty、对读不到的目标返回 503 library_baseline_unprobeable。
按后端真实契约做最小分类:

- LibraryKnown 补 empty_baseline 与 unreachable 两个已存在的字段/状态;
- 页面按四类分别呈现:known 显示生效版本+快照摘要;空目标写明"已读且为空/首次发布/
  写前仍生成完整预备份";非空缺权威版本才指向基线入口;不可读照实说读取失败;
- baselineNeeded 从"非 known"收窄为"非空且无权威版本",不动授权、写闸、预备份、
  容量与 worker 执行准入等任何后端检查,也不放宽后端的 409。

行为测试新增四类状态、空→不可读→空刷新不残留旧状态、空目标无候选时按钮禁用;
原"非 known 一律未知"的断言随文案更正。本地门禁 16 项 success
(tests-web 498 passed,tsc -b 无错)。
This commit is contained in:
姚章浩
2026-11-02 10:56:29 +08:00
parent f1cddfb601
commit c256f2985c
3 changed files with 175 additions and 11 deletions
+121 -2
View File
@@ -184,8 +184,9 @@ describe('ReleaseTargetsView(确认稿布局)', () => {
it('未接入项显示“未接入/未知”,不用 0 / 无变化冒充', () => {
const html = render()
expect(html).toContain('生产当前')
// 生产没有权威版本时必须写未知,不能拿测试快照顶替
expect(html).toContain('未知(尚未建立权威版本)')
// 生产没有权威版本时必须写"非空且尚无权威版本",不能拿测试快照顶替,
// 也不能把所有非 known 一律写成"未知"(空目标与读不到是另外两类)。
expect(html).toContain('目标非空,尚无权威版本')
expect(html).toContain('尚无记录 · 生产与测试的文档级差异读取未接线')
expect(html).toContain('未接入')
})
@@ -708,6 +709,124 @@ describe('ReleaseTargetsView(确认稿布局)', () => {
})
})
// 后端 /api/v1/library/overview 已经区分四类生产目标状态(library_flow.go unknownLibraryTarget、
// library_baseline.go:81 的空目标 409)。页面此前只判断 state === 'known',于是"已证空"和
// "读不到"都被写成"未知"、并给出必然 409 的"建立目标权威基线"入口。
describe('生产目标四类状态:known / 空 / 非空 / 不可读', () => {
const baseline = { canStart: true, pending: false, onStart: vi.fn() }
const emptyTarget: LibraryOverview['production'] = {
state: 'unknown',
empty_baseline: true,
reason: '生产目标可读且为空:本次是首次发布,写入前仍会生成完整预备份',
}
const nonEmptyTarget: LibraryOverview['production'] = {
state: 'unknown',
reason: '目标非空且尚无权威版本',
}
const unreadableTarget: LibraryOverview['production'] = {
state: 'unreachable',
reason: '无法读取目标整库状态:请检查目标连接后重试,不能据此判定空库或建立基线',
}
const knownTarget: LibraryOverview['production'] = {
state: 'known',
version: {
id: 'v-known',
kind: 'production_effective',
snapshot_id: 'c'.repeat(64),
version_label: '生效 20261030-01',
source_job_id: 'libsync-known',
evidence: {},
created_by: 'admin',
created_at: '2026-10-30T09:00:00Z',
},
}
it('空目标:写明"已读且为空/首次发布/写前仍预备份",且不给会 409 的建基线入口', () => {
const html = render({
baseline,
overview: {
...overview,
production: emptyTarget,
// 接口在空基线上不再给"建立基线"缺项(librarySyncGateWithGrant:657-661)
gate: { sync_allowed: false, reasons: ['没有与目标快照匹配的有效发布授权'] },
},
})
expect(html).toContain('目标已读且为空')
expect(html).toContain('首次发布')
expect(html).toContain('写前仍会生成完整预备份')
// 页面不能把"已证空"写成未知,也不能引导去建一个必然被 409 拒绝的基线。
expect(html).not.toContain('未知(尚未建立权威版本)')
expect(html).not.toMatch(/建立目标权威基线(维护隔离)<\/button>/)
})
it('非空且无生效版本:仍指向既有基线入口,不谎称空库', () => {
const html = render({
baseline,
overview: {
...overview,
production: nonEmptyTarget,
gate: {
sync_allowed: false,
reasons: [
'目标非空且没有生效版本记录:先建立目标权威基线(维护隔离下捕获整库并逐类校验),或确认目标确实是空库',
],
},
},
})
expect(html).toContain('目标非空')
expect(html).toMatch(/建立目标权威基线(维护隔离)<\/button>/)
expect(html).not.toContain('目标已读且为空')
})
it('不可读:照实说读取失败,既不认定空也不认定非空,更不引导建基线', () => {
const html = render({
baseline,
overview: {
...overview,
production: unreadableTarget,
gate: { sync_allowed: false, reasons: [unreadableTarget.reason!] },
},
})
expect(html).toContain('无法读取目标整库状态')
expect(html).not.toContain('目标已读且为空')
expect(html).not.toContain('目标非空')
expect(html).not.toMatch(/建立目标权威基线(维护隔离)<\/button>/)
})
it('known:显示权威生效版本与快照摘要,不再写未知', () => {
const html = render({ baseline, overview: { ...overview, production: knownTarget } })
expect(html).toContain('生效 20261030-01')
expect(html).toContain(knownTarget.version!.snapshot_id.slice(0, 12))
expect(html).not.toContain('未知(尚未建立权威版本)')
expect(html).not.toMatch(/建立目标权威基线(维护隔离)<\/button>/)
})
it('刷新后分类只由当前 overview 决定:空 → 不可读 → 空 不残留旧状态', () => {
const seenEmpty = render({ baseline, overview: { ...overview, production: emptyTarget } })
const seenUnreadable = render({ baseline, overview: { ...overview, production: unreadableTarget } })
const recovered = render({ baseline, overview: { ...overview, production: emptyTarget } })
expect(seenEmpty).toContain('目标已读且为空')
expect(seenUnreadable).not.toContain('目标已读且为空')
expect(seenUnreadable).toContain('无法读取目标整库状态')
expect(recovered).toContain('目标已读且为空')
expect(recovered).not.toContain('无法读取目标整库状态')
})
it('空目标但没有测试封存:启动同步保持禁用并说明缺候选', () => {
const html = render({
baseline,
overview: {
...overview,
production: emptyTarget,
test_sealed: undefined,
test_sealed_reason: '还没有该目标的测试封存',
},
})
expect(html).toMatch(/<button[^>]*class="rel-primary"[^>]*disabled/)
expect(html).toContain('尚无测试封存')
})
})
// E5:轮询必须覆盖"投递 → 终态读回"。
// 旧实现只按封存队列的作业判断,于是点了同步/恢复/建立基线之后页面根本不刷新,
// 看上去像"还没启动";要么反过来,一个失败作业被永远轮询下去。
+43 -8
View File
@@ -159,6 +159,18 @@ function scopeView(
const scopeName = (scope: Scope) => (scope === 'internal' ? '对内知识库' : '对外知识库')
// 生产目标必须按接口的四类状态原样呈现:把"已读且为空"和"读不到"都写成"未知",
// 会让人去点一个必然被拒绝的"建立基线"(后端对空目标返回 409 library_baseline_target_empty,
// 对读不到的目标返回 503 library_baseline_unprobeable)。
type ProductionTargetClass = 'known' | 'empty' | 'nonempty' | 'unreachable'
function productionTargetClass(production: LibraryOverview['production']): ProductionTargetClass {
if (production.state === 'known' && production.version) return 'known'
if (production.state === 'unreachable') return 'unreachable'
// unknown 有两种:可读且空(空基线,首次发布合法起点)与可读但非空(缺权威版本)。
return production.empty_baseline ? 'empty' : 'nonempty'
}
// 不可发起时必须说清是哪一条不满足:读的是接口给出的真实原因(执行器不可达、
// 指向别的实例、降级、观测过期),不是一句写死的"通道未接线"。
function captureHint(view: ScopeView, canWrite: boolean): string {
@@ -702,8 +714,10 @@ function ProtectionCard({
onOpen: () => void
}) {
const backupKnown = prebackup.state === 'known' && prebackup.version
// 基线入口只在"目标非空、却没有生效版本"时才是答案;空库走首次同步(预备份+登记)。
const baselineNeeded = production.state !== 'known' && targetSealed
// 基线入口只在"目标非空、却没有生效版本"时才是答案;空库走首次同步(预备份+登记),
// 读不到时必须先修连接,两者都不能被引导到会 409/503 的基线入口。
const productionTarget = productionTargetClass(production)
const baselineNeeded = productionTarget === 'nonempty' && targetSealed
return (
<aside className="rel-panel rel-protection">
<header className="rel-panel-head">
@@ -735,9 +749,21 @@ function ProtectionCard({
</dd>
<dt>生产当前版本</dt>
<dd>
{production.state === 'known' && production.version
? production.version.version_label || production.version.snapshot_id.slice(0, 12)
: '未知(生产还没有权威版本记录)'}
{productionTarget === 'known' && production.version ? (
<>
{production.version.version_label || production.version.snapshot_id.slice(0, 12)}
<br />
<small>
快照 {production.version.snapshot_id.slice(0, 12)} · 登记于 {stamp(production.version.created_at)}
</small>
</>
) : productionTarget === 'empty' ? (
'目标已读且为空:首次发布还没有生效版本(写前仍会生成完整预备份)'
) : productionTarget === 'unreachable' ? (
production.reason || '读取失败:无法确认目标是否为空'
) : (
'未知(目标非空且还没有权威版本)'
)}
</dd>
<dt>备份范围</dt>
<dd>数据库 / 资源 / 索引 / Wiki</dd>
@@ -1369,6 +1395,7 @@ export function ReleaseTargetsView(props: ViewProps) {
// 三类版本严格分开:target 是测试封存,production 是生产权威版本,prebackup 是本次预备份。
const target = view.overview?.test_sealed
const production = view.overview?.production ?? { state: 'unknown' as const }
const productionClass = productionTargetClass(production)
const prebackup = view.overview?.prebackup ?? { state: 'unknown' as const }
const diff = view.overview?.diff ?? {
state: 'unknown' as const,
@@ -1462,10 +1489,18 @@ export function ReleaseTargetsView(props: ViewProps) {
</div>
<p className="rel-versionline">
生产当前{' '}
{production.state === 'known' && production.version ? (
<b>{production.version.version_label || `生效 ${production.version.snapshot_id.slice(0, 12)}`}</b>
{productionClass === 'known' && production.version ? (
<b>
{production.version.version_label || `生效 ${production.version.snapshot_id.slice(0, 12)}`}
{' · '}
快照 {production.version.snapshot_id.slice(0, 12)}
</b>
) : productionClass === 'empty' ? (
<b>目标已读且为空(首次发布)</b>
) : productionClass === 'unreachable' ? (
<b className="rel-unknown">读取失败:{production.reason || '无法读取目标整库状态'}</b>
) : (
<b className="rel-unknown">未知(尚未建立权威版本)</b>
<b className="rel-unknown">目标非空,尚无权威版本</b>
)}{' '}
→ 拟发布{' '}
<b>{target ? `封存 ${target.snapshot_id.slice(0, 12)} · ${stamp(target.created_at)}` : '还没有测试封存'}</b> ·{' '}
+11 -1
View File
@@ -416,9 +416,19 @@ export interface LibraryVersionRecord {
}
export interface LibraryKnown {
state: 'known' | 'unknown'
/**
* 后端 /api/v1/library/overview 的真实状态:
* - known:有权威生效版本;
* - unknown + empty_baseline:目标可读、确实为空,是首次发布的合法起点;
* - unknown(无 empty_baseline):目标非空却没有权威版本;
* - unreachable:读不到目标,既不能判定空也不能判定非空。
*/
state: 'known' | 'unknown' | 'unreachable'
version?: LibraryVersionRecord
reason?: string
/** 目标可读且内容为空:首次发布。只证明生产范围内容表计数为 0,
* ES/对象身份与残留仍由执行侧预备份/准入核验;页面不得据此宣称"三存储已验证"。 */
empty_baseline?: boolean
/** 只在预备份上有意义:这份备份的内容是否就是当前拟发布目标。 */
bound_to_target?: boolean
}