fix(library): 收口 B1-R1/B2-R1/B5-R1 并让恢复链经过正式队列终态
B1-R1: 演练写集改为受限 restore-drill(V2 信封/contract 1/schema 摘要,零对象、零索引、零本地映射,来源身份必填)。B2-R1: verify 作业先建不可变 operation 并携带 operation_id,同 job recover 的网关 claim 同时携带原 operation 与 recovery 身份。B5-R1: 前端按 scope 世代与动作序号丢弃迟到响应,不污染新 scope 或父级旧回调。harness 的 recovery 改由生产 jobs.Worker.RunOnce 消费并读回 jobs 终态;原失败窗口作业保持 failed,不改写历史。
This commit is contained in:
@@ -158,6 +158,47 @@ describe('RestoreVerificationSection mounted (offline fixtures)', () => {
|
||||
expect(onComplete).toHaveBeenCalledWith('libverify-internal-1')
|
||||
})
|
||||
|
||||
it('切 scope 后迟到的旧 POST 不能污染新目标或触发父级旧作业回调', async () => {
|
||||
const onAccepted = vi.fn()
|
||||
const onComplete = vi.fn()
|
||||
let resolvePost: ((value: unknown) => void) | null = null
|
||||
vi.stubGlobal('fetch', async (url: string, init?: { method?: string }) => {
|
||||
if (init?.method === 'POST') {
|
||||
return new Promise((resolve) => {
|
||||
resolvePost = resolve
|
||||
})
|
||||
}
|
||||
return jsonResponse(String(url).includes('scope=external') ? externalView : internalView)
|
||||
})
|
||||
await act(async () => {
|
||||
root.render(
|
||||
<RestoreVerificationSection scope="internal" isAdmin onAccepted={onAccepted} onComplete={onComplete} />
|
||||
)
|
||||
})
|
||||
// 旧 internal POST 挂起时切到 external:external 的真实观测已经完成。
|
||||
await act(async () => {
|
||||
;[...container.querySelectorAll('button')].find((item) => item.textContent?.includes('跑一次恢复验证'))!.click()
|
||||
})
|
||||
await act(async () => {
|
||||
root.render(
|
||||
<RestoreVerificationSection scope="external" isAdmin onAccepted={onAccepted} onComplete={onComplete} />
|
||||
)
|
||||
})
|
||||
expect(container.textContent).toContain('fixture-external')
|
||||
expect(container.textContent).not.toContain('已验证(当前目标)')
|
||||
expect(container.textContent).not.toContain('old-internal-job')
|
||||
// 放行旧 internal POST:它的 then/onAccepted/reload 必须全部失效。
|
||||
await act(async () => {
|
||||
resolvePost?.(jsonResponse({ job: { id: 'verify-internal-new', scope: 'internal' }, reused: false }, 202))
|
||||
})
|
||||
expect(container.textContent).toContain('fixture-external')
|
||||
expect(container.textContent).not.toContain('已验证(当前目标)')
|
||||
expect(container.textContent).not.toContain('old-internal-job')
|
||||
expect(container.textContent).not.toContain('verify-internal-new')
|
||||
expect(onAccepted).not.toHaveBeenCalled()
|
||||
expect(onComplete).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('HTTP 失败按失败显示,不把响应当通过证据', async () => {
|
||||
vi.stubGlobal('fetch', async () =>
|
||||
jsonResponse({ scope: 'internal', verified: true, reason: '证据读取失败', target, history: [] }, 503)
|
||||
|
||||
@@ -112,8 +112,15 @@ export function RestoreVerificationSection({
|
||||
const mounted = useRef(true)
|
||||
const generation = useRef(0)
|
||||
const viewScopeRef = useRef('')
|
||||
// scopeRef 是"当前渲染的 scope":异步回调必须同时核对 scope 与世代,
|
||||
// 只比 GET generation 会被迟到的旧 POST 重新递增而失效(审核 B5-R1)。
|
||||
const scopeRef = useRef(scope)
|
||||
const scopeEpoch = useRef(0)
|
||||
const actionSeq = useRef(0)
|
||||
const acceptedJob = useRef('')
|
||||
const sawRunning = useRef(false)
|
||||
const onAcceptedRef = useRef(onAccepted)
|
||||
onAcceptedRef.current = onAccepted
|
||||
const onCompleteRef = useRef(onComplete)
|
||||
onCompleteRef.current = onComplete
|
||||
|
||||
@@ -122,7 +129,7 @@ export function RestoreVerificationSection({
|
||||
return fetchLibraryVerification(targetScope, signal)
|
||||
.then((next) => {
|
||||
// 晚到的旧 scope/旧代次响应一律丢弃:它不能改写当前 scope 的观测。
|
||||
if (!mounted.current || request !== generation.current) return
|
||||
if (!mounted.current || request !== generation.current || targetScope !== scopeRef.current) return
|
||||
setView(next)
|
||||
viewScopeRef.current = next.scope
|
||||
setStale(false)
|
||||
@@ -141,7 +148,7 @@ export function RestoreVerificationSection({
|
||||
}
|
||||
})
|
||||
.catch((err: unknown) => {
|
||||
if (!mounted.current || request !== generation.current) return
|
||||
if (!mounted.current || request !== generation.current || targetScope !== scopeRef.current) return
|
||||
if ((err as Error)?.name === 'AbortError') return
|
||||
// 刷新失败:同 scope 的旧结果保留但明确标成过期;别的 scope 的旧结果直接丢掉。
|
||||
setStale(true)
|
||||
@@ -152,6 +159,11 @@ export function RestoreVerificationSection({
|
||||
|
||||
useEffect(() => {
|
||||
mounted.current = true
|
||||
scopeRef.current = scope
|
||||
// 切 scope 使所有在飞动作失效:旧 scope 的 POST/回调不能写回新目标。
|
||||
scopeEpoch.current += 1
|
||||
actionSeq.current = 0
|
||||
setPending(false)
|
||||
// 切 scope:旧 scope 的证据不能继续显示为"当前目标已验证"。
|
||||
setView(null)
|
||||
viewScopeRef.current = ''
|
||||
@@ -173,21 +185,37 @@ export function RestoreVerificationSection({
|
||||
}, [scope])
|
||||
|
||||
const onRun = useCallback(() => {
|
||||
const actionScope = scope
|
||||
const actionEpoch = scopeEpoch.current
|
||||
const action = ++actionSeq.current
|
||||
// 动作仍然有效 = 组件挂着 + 还是同一个 scope 世代 + 仍是最新一次点击。
|
||||
const current = () =>
|
||||
mounted.current &&
|
||||
scopeEpoch.current === actionEpoch &&
|
||||
scopeRef.current === actionScope &&
|
||||
actionSeq.current === action
|
||||
setPending(true)
|
||||
setError(null)
|
||||
setNotice('')
|
||||
enqueueLibraryVerification(scope)
|
||||
enqueueLibraryVerification(actionScope)
|
||||
.then((accepted) => {
|
||||
if (!current() || accepted.scope !== actionScope) return
|
||||
acceptedJob.current = accepted.job_id
|
||||
sawRunning.current = false
|
||||
setNotice(`已投递恢复验证作业 ${accepted.job_id};页面会自动刷新进度与终态,父级作业/维护/闸口同步更新。`)
|
||||
// 父页面必须立刻知道"有一个验证在跑":否则它不会轮询 overview/维护。
|
||||
onAccepted?.(accepted.job_id)
|
||||
void reload(scope)
|
||||
onAcceptedRef.current?.(accepted.job_id)
|
||||
void reload(actionScope)
|
||||
})
|
||||
.catch((err: unknown) => setError(err instanceof Error ? err.message : '投递恢复验证失败'))
|
||||
.finally(() => setPending(false))
|
||||
}, [scope, reload, onAccepted])
|
||||
.catch((err: unknown) => {
|
||||
if (!current()) return
|
||||
setError(err instanceof Error ? err.message : '投递恢复验证失败')
|
||||
})
|
||||
.finally(() => {
|
||||
// 旧动作的 finally 不能清掉新 scope 新动作的 pending。
|
||||
if (current()) setPending(false)
|
||||
})
|
||||
}, [scope, reload])
|
||||
|
||||
return (
|
||||
<RestoreVerificationView
|
||||
|
||||
Reference in New Issue
Block a user