From 74fb04b2d1e50b1f227a19d0c0582394beff4f4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9A=E7=AB=A0=E6=B5=A9?= Date: Fri, 30 Oct 2026 01:33:51 +0800 Subject: [PATCH] =?UTF-8?q?fix(web):=20=E6=95=B4=E5=BA=93=E9=A1=B5?= =?UTF-8?q?=E6=8C=89=E4=BD=9C=E4=B8=9A=E7=B1=BB=E5=9E=8B=E4=B8=8E=E7=BB=88?= =?UTF-8?q?=E6=80=81=E6=98=BE=E7=A4=BA=E5=B9=B6=E8=A1=A5=E5=85=A8=E8=BD=AE?= =?UTF-8?q?=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 成功的作业不再写"尚未启动/此时尚未运行",阶段条对恢复/基线也读自己的阶段行; - 基线/恢复不再被写成"生产同步""封存进行中",改为逐类映射; - 抽屉按版本登记标注每份归档,未登记的不冒充候选; - 轮询覆盖投递→终态读回:投递中动作、封存队列、任一 scope 在飞作业都算在跑, 读到终态再读回一次就停,失败作业不会被无限轮询。 --- web/src/ReleaseTargets.test.tsx | 303 +++++++++++++++++++++++++++++++- web/src/ReleaseTargets.tsx | 225 +++++++++++++++++++----- web/src/librarySnapshots.ts | 21 ++- 3 files changed, 498 insertions(+), 51 deletions(-) diff --git a/web/src/ReleaseTargets.test.tsx b/web/src/ReleaseTargets.test.tsx index b5394eb..b3f1600 100644 --- a/web/src/ReleaseTargets.test.tsx +++ b/web/src/ReleaseTargets.test.tsx @@ -1,12 +1,13 @@ import { renderToStaticMarkup } from 'react-dom/server' import { describe, expect, it, vi } from 'vitest' -import { ReleaseTargetsView } from './ReleaseTargets' +import { ReleaseTargetsView, libraryPollingActive } from './ReleaseTargets' import type { LibraryCaptureJob, LibraryJobRecord, LibraryOverview, LibrarySnapshot, LibrarySnapshotList, + LibraryStageRow, } from './librarySnapshots' const internalSnapshot: LibrarySnapshot = { @@ -306,8 +307,267 @@ describe('ReleaseTargetsView(确认稿布局)', () => { it('抽屉只展示当前选中库的历史', () => { const html = render({ drawerOpen: true, scope: 'external' }) - expect(html).toContain('封存 01343b0b310b') - expect(html).not.toContain('封存 f992d64ea45b') + expect(html).toContain('快照 01343b0b310b') + expect(html).not.toContain('快照 f992d64ea45b') + }) + + // E3:归档里同时躺着测试封存、目标基线与每次预备份。旧页面把每一行都写"测试封存", + // 于是预备份/基线被当成了"可以发布的候选"。类型只能来自版本登记。 + it('抽屉按版本登记标注每份归档,不把预备份/基线写成测试封存', () => { + const html = render({ + drawerOpen: true, + overview: { + ...overview, + versions: [ + { + id: 'libver-sealed', + kind: 'test_sealed', + snapshot_id: internalSnapshot.snapshot_id, + version_label: '', + source_job_id: 'libseal-1', + created_at: internalSnapshot.created_at, + }, + { + id: 'libver-pre', + kind: 'production_prebackup', + snapshot_id: '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef', + version_label: '', + source_job_id: 'librestore-internal-1', + created_at: '2026-10-29T16:59:39Z', + }, + ], + }, + page: { + ...page, + snapshots: [ + internalSnapshot, + { + ...internalSnapshot, + snapshot_id: '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef', + created_at: '2026-10-29T16:59:39Z', + }, + ], + }, + }) + expect(html).toContain('测试封存') + expect(html).toContain('生产预备份') + // 没有被登记过的归档文件也不能冒充测试封存 + expect(html).not.toContain('测试封存 { + const html = render({ + drawerOpen: true, + overview: { + ...overview, + prebackup: { + state: 'known', + version: { + id: 'libver-pre', + kind: 'production_prebackup', + snapshot_id: '089676a09107ceab3bf5e88651f443da26e5d9b2d0de380840c5ec24941a07c2', + version_label: '', + source_job_id: 'librestore-internal-1793293164573204381', + evidence: {}, + created_by: 'worker', + created_at: '2026-10-29T16:59:39Z', + }, + bound_to_target: false, + }, + }, + }) + expect(html).toContain('已登记预备份') + expect(html).not.toContain('尚未创建') + expect(html).toContain('作业 librestore-internal-1793293164573204381') + expect(html).toContain('不是当前拟发布目标的快照') + expect(html).toContain('最近一次生产预备份') + expect(html).not.toContain('无记录(尚未为本次目标创建)') + }) + + // E1:作业已经成功,页面不能还说"尚未启动/此时尚未运行"。 + it('已完成的同步不再写“尚未启动”,阶段按本次作业打勾', () => { + const succeeded: LibraryJobRecord = { + ...persistedSeal, + id: 'libsync-internal-1793292008635856030', + kind: 'sync', + state: 'succeeded', + stage: 'finalize', + snapshot_id: '4b720eb42fca75de40aa862f2b53d0751e648d760dafaca309aaee08771f2c73', + target_snapshot_id: internalSnapshot.snapshot_id, + finished_at: '2026-10-29T16:40:36Z', + updated_at: '2026-10-29T16:40:36Z', + stages: [ + { + stage: 'prebackup', + state: 'succeeded', + total: null, + completed: null, + bytes_total: null, + bytes_done: null, + detail: '', + updated_at: '2026-10-29T16:40:21Z', + }, + { + stage: 'database', + state: 'succeeded', + total: 67, + completed: 67, + bytes_total: 88086, + bytes_done: 88086, + detail: '', + updated_at: '2026-10-29T16:40:22Z', + }, + { + stage: 'verify', + state: 'succeeded', + total: null, + completed: null, + bytes_total: null, + bytes_done: null, + detail: '', + updated_at: '2026-10-29T16:40:26Z', + }, + { + stage: 'finalize', + state: 'succeeded', + total: null, + completed: null, + bytes_total: null, + bytes_done: null, + detail: '已登记生效版本', + updated_at: '2026-10-29T16:40:26Z', + }, + ], + } + const html = render({ overview: { ...overview, job: succeeded, jobs: [succeeded] } }) + expect(html).not.toContain('尚未启动,不修改生产') + expect(html).not.toContain('此时尚未运行') + expect(html).toContain('生产同步已完成') + expect(html).toContain('生产同步 · 已完成') + const stageBar = html.slice(html.indexOf('aria-label="作业阶段"'), html.indexOf('rel-steps-note')) + expect(stageBar).toContain('✓') + }) + + // E4:基线/恢复不是"生产同步",也不是"封存进行中"。 + it('建立基线进行中不写成同步中,也不写成封存进行中', () => { + const baseline: LibraryJobRecord = { + ...persistedSeal, + id: 'libbase-internal-1', + kind: 'baseline', + state: 'running', + stage: 'database', + finished_at: null, + } + const html = render({ overview: { ...overview, job: baseline, jobs: [baseline] } }) + expect(html).toContain('建立目标基线进行中') + expect(html).toContain('建立目标基线作业') + expect(html).toContain('建基线中') + expect(html).not.toContain('封存进行中') + expect(html).not.toContain('生产同步作业') + expect(html).not.toContain('同步中') + }) + + it('恢复作业用“按版本恢复”而不是“生产同步”,并说明已回到该快照', () => { + const stages: LibraryStageRow[] = [ + { + stage: 'prebackup', + state: 'succeeded', + total: null, + completed: null, + bytes_total: null, + bytes_done: null, + detail: '', + updated_at: '2026-10-29T16:59:39Z', + }, + { + stage: 'database', + state: 'succeeded', + total: 5, + completed: 5, + bytes_total: 7138, + bytes_done: 7138, + detail: '', + updated_at: '2026-10-29T16:59:40Z', + }, + { + stage: 'resources', + state: 'succeeded', + total: 1, + completed: 1, + bytes_total: 507, + bytes_done: 507, + detail: '', + updated_at: '2026-10-29T16:59:40Z', + }, + { + stage: 'index', + state: 'succeeded', + total: 2, + completed: 2, + bytes_total: 28563, + bytes_done: 28563, + detail: '', + updated_at: '2026-10-29T16:59:40Z', + }, + { + stage: 'wiki', + state: 'succeeded', + total: 10, + completed: 10, + bytes_total: 23144, + bytes_done: 23144, + detail: '', + updated_at: '2026-10-29T16:59:40Z', + }, + { + stage: 'verify', + state: 'succeeded', + total: null, + completed: null, + bytes_total: null, + bytes_done: null, + detail: '', + updated_at: '2026-10-29T16:59:40Z', + }, + { + stage: 'finalize', + state: 'succeeded', + total: null, + completed: null, + bytes_total: null, + bytes_done: null, + detail: '已登记生效版本', + updated_at: '2026-10-29T16:59:40Z', + }, + ] + const restoreRunning: LibraryJobRecord = { + ...persistedSeal, + id: 'librestore-internal-1793293164573204381', + kind: 'restore', + state: 'running', + stage: 'wiki', + target_snapshot_id: '4b720eb42fca75de40aa862f2b53d0751e648d760dafaca309aaee08771f2c73', + finished_at: null, + stages, + } + const running = render({ overview: { ...overview, job: restoreRunning, jobs: [restoreRunning] } }) + expect(running).toContain('按版本恢复作业') + expect(running).toContain('恢复到 4b720eb42fca') + expect(running).toContain('恢复进行中(目标在维护中)') + expect(running).not.toContain('生产同步作业') + + const restoreDone: LibraryJobRecord = { + ...restoreRunning, + state: 'succeeded', + finished_at: '2026-10-29T16:59:51Z', + } + const html = render({ overview: { ...overview, job: restoreDone, jobs: [restoreDone] } }) + expect(html).toContain('按版本恢复') + expect(html).toContain('按版本恢复已完成') + const stageBar = html.slice(html.indexOf('aria-label="作业阶段"'), html.indexOf('rel-steps-note')) + expect(stageBar).toContain('生产已回到该快照') }) it('同步中布局显示四类数据卡与明细,且不编造百分比', () => { @@ -446,3 +706,40 @@ describe('ReleaseTargetsView(确认稿布局)', () => { expect(unknown).not.toContain('也没有维护能力配置') }) }) + +// E5:轮询必须覆盖"投递 → 终态读回"。 +// 旧实现只按封存队列的作业判断,于是点了同步/恢复/建立基线之后页面根本不刷新, +// 看上去像"还没启动";要么反过来,一个失败作业被永远轮询下去。 +describe('libraryPollingActive(轮询范围)', () => { + const idle = { internal: null, external: null } as Record<'internal' | 'external', LibraryOverview | null> + const withJob = (state: LibraryJobRecord['state'], kind: LibraryJobRecord['kind']) => + ({ + internal: { ...overview, job: { ...persistedSeal, state, kind }, jobs: [] }, + external: null, + }) as Record<'internal' | 'external', LibraryOverview | null> + + it('投递中就已经在轮询,不必等到作业出现在列表里', () => { + expect(libraryPollingActive({ captures: [], overviews: idle, pending: true })).toBe(true) + }) + + it('持久作业在飞(同步/恢复/基线)也轮询', () => { + expect(libraryPollingActive({ captures: [], overviews: withJob('running', 'sync'), pending: false })).toBe(true) + expect(libraryPollingActive({ captures: [], overviews: withJob('pending', 'restore'), pending: false })).toBe(true) + expect(libraryPollingActive({ captures: [], overviews: withJob('running', 'baseline'), pending: false })).toBe(true) + }) + + it('封存队列里的作业同样算在飞', () => { + expect(libraryPollingActive({ captures: [runningJob], overviews: idle, pending: false })).toBe(true) + }) + + it('终态必须停下来:失败作业不许被无限轮询', () => { + expect(libraryPollingActive({ captures: [], overviews: withJob('succeeded', 'sync'), pending: false })).toBe(false) + expect(libraryPollingActive({ captures: [], overviews: withJob('failed', 'sync'), pending: false })).toBe(false) + expect(libraryPollingActive({ captures: [], overviews: withJob('cancelled', 'restore'), pending: false })).toBe( + false + ) + expect( + libraryPollingActive({ captures: [{ ...runningJob, state: 'failed' }], overviews: idle, pending: false }) + ).toBe(false) + }) +}) diff --git a/web/src/ReleaseTargets.tsx b/web/src/ReleaseTargets.tsx index 0988204..4fddf50 100644 --- a/web/src/ReleaseTargets.tsx +++ b/web/src/ReleaseTargets.tsx @@ -1,4 +1,4 @@ -import { useCallback, useEffect, useState } from 'react' +import { useCallback, useEffect, useRef, useState } from 'react' import { createLibraryRestoreAuthorization, createLibrarySyncAuthorization, @@ -22,6 +22,7 @@ import { type LibrarySnapshot, type LibrarySnapshotList, type LibraryStageRow, + type LibraryVersionRef, } from './librarySnapshots' import { useOptionalAuth } from './auth' import './releaseTargets.css' @@ -174,7 +175,17 @@ function captureHint(view: ScopeView, canWrite: boolean): string { function statusOf(view: ScopeView) { const job = view.job if (job && (job.state === 'running' || job.state === 'pending')) { - return { text: job.kind === 'seal' ? '封存中' : '同步中', tone: 'blue' as const } + // 页签徽标也要说清是哪一类作业在跑:把"建立基线"写成"同步中"会让人 + // 以为生产正在被写入。 + const text = + job.kind === 'seal' + ? '封存中' + : job.kind === 'restore' + ? '恢复中' + : job.kind === 'baseline' + ? '建基线中' + : '同步中' + return { text, tone: 'blue' as const } } if (view.overview?.production.state === 'known') return { text: '生产已记录版本', tone: 'green' as const } if (view.overview?.test_sealed || view.snapshot) return { text: '已有封存', tone: 'green' as const } @@ -215,6 +226,57 @@ function jobStateText(job: LibraryJobRecord): string { return map[job.state] ?? job.state } +// 作业类型必须逐类写清:基线不是同步,恢复也不是同步。混写会让人以为 +// "建立基线"正在把测试内容写进生产,或者把恢复当成一次正式发布。 +export function jobKindText(kind?: string): string { + if (kind === 'seal') return '测试封存' + if (kind === 'sync') return '生产同步' + if (kind === 'restore') return '按版本恢复' + if (kind === 'baseline') return '建立目标基线' + return kind ? kind : '—' +} + +// 归档里的每一份快照属于哪一类版本:只能来自版本登记,不能一律写"测试封存"。 +export function snapshotVersionLabel(refs: LibraryVersionRef[] | undefined, snapshotID: string): string { + const kinds = new Set((refs ?? []).filter((item) => item.snapshot_id === snapshotID).map((item) => item.kind)) + const labels: string[] = [] + if (kinds.has('test_sealed')) labels.push('测试封存') + if (kinds.has('production_effective')) labels.push('生产生效') + if (kinds.has('production_prebackup')) labels.push('生产预备份') + return labels.length > 0 ? labels.join(' · ') : '未登记(归档文件,不作为发布候选)' +} + +// 生产写入的当前状态:不能用一句写死的"尚未启动"顶替已经完成的作业。 +export function publishStateText(job?: LibraryJobRecord): string { + if (!job) return '尚未启动,不修改生产' + if (job.state === 'pending' || job.state === 'running') { + return job.kind === 'restore' + ? '恢复进行中(目标在维护中)' + : job.kind === 'baseline' + ? '建立基线进行中(只读捕获目标,不写内容)' + : '同步进行中(目标在维护中)' + } + if (job.state === 'succeeded') return `${jobKindText(job.kind)}已完成 · ${stamp(job.finished_at ?? job.updated_at)}` + if (job.state === 'failed') return `${jobKindText(job.kind)}失败 · ${stamp(job.finished_at ?? job.updated_at)}` + return `${jobKindText(job.kind)}已取消 · ${stamp(job.finished_at ?? job.updated_at)}` +} + +// 轮询范围 = 投递中 + 有在飞作业(封存队列与持久作业都算)。 +// 终态(成功/失败/取消)必须停下来:无限轮询一个失败作业既没意义, +// 也会把"作业已经结束"这个事实淹没在不停刷新的页面里。 +export function libraryPollingActive(input: { + captures: LibraryCaptureJob[] + overviews: Record + pending: boolean +}): boolean { + if (input.pending) return true + if (input.captures.some((job) => ACTIVE_STATES.includes(job.state))) return true + return (['internal', 'external'] as Scope[]).some((scope) => { + const job = input.overviews[scope]?.job + return Boolean(job && ACTIVE_STATES.includes(job.state)) + }) +} + // ---- 目标维护窗口:级别 / 进度 / 恢复错误 ---- // // 维护窗口有两种级别:实例级(跨 scope 共用一把锁,真正挡住写入者)和库级。 @@ -403,7 +465,9 @@ export function wikiContributionLines(diff: LibraryOverview['diff'] | undefined) function StageBar({ view }: { view: ScopeView }) { const job = view.job const sealJob = job?.kind === 'seal' ? job : undefined - const syncJob = job?.kind === 'sync' ? job : undefined + // 同步、恢复、建立基线共用同一串阶段(预备份 → 数据四类 → 校验 → 登记版本)。 + // 只按"sync"找阶段会让刚跑完的恢复/基线在页面上显示成"本次作业尚未开始"。 + const writeJob = job && job.kind !== 'seal' ? job : undefined let first: { state: StepState; hint: string } if (sealJob) { @@ -419,19 +483,24 @@ function StageBar({ view }: { view: ScopeView }) { ? '已排队,等待 worker' : '本次封存未完成', } - } else if (syncJob) { - first = { state: 'done', hint: '已绑定的测试版本' } + } else if (writeJob) { + first = + writeJob.kind === 'restore' + ? { state: 'done', hint: '恢复到选定快照(不经过测试封存)' } + : writeJob.kind === 'baseline' + ? { state: 'done', hint: '目标自身内容,非测试封存' } + : { state: 'done', hint: '已绑定的测试版本' } } else { first = { state: 'idle', hint: '本次作业尚未开始' } } - const prebackup = syncJob ? stageOf(syncJob, 'prebackup') : undefined - const dataStages = syncJob + const prebackup = writeJob ? stageOf(writeJob, 'prebackup') : undefined + const dataStages = writeJob ? ['database', 'resources', 'index', 'wiki'] - .map((name) => stageOf(syncJob, name)) + .map((name) => stageOf(writeJob, name)) .filter((row): row is LibraryStageRow => Boolean(row)) : [] - const verifyStage = syncJob ? stageOf(syncJob, 'verify') : undefined + const verifyStage = writeJob ? stageOf(writeJob, 'verify') : undefined const stages: { label: string; state: StepState; hint: string }[] = [ { label: '固定测试版本', ...first }, @@ -460,8 +529,15 @@ function StageBar({ view }: { view: ScopeView }) { }, { label: '新版本生效', - state: syncJob && syncJob.state === 'succeeded' ? 'done' : 'idle', - hint: syncJob && syncJob.state === 'succeeded' ? '生产已生效' : '未接入', + state: writeJob && writeJob.state === 'succeeded' ? 'done' : 'idle', + hint: + writeJob && writeJob.state === 'succeeded' + ? writeJob.kind === 'restore' + ? '生产已回到该快照' + : writeJob.kind === 'baseline' + ? '目标基线已登记(目标内容未改)' + : '生产已生效' + : '未接入', }, ] return ( @@ -480,9 +556,7 @@ function StageBar({ view }: { view: ScopeView }) {

{job - ? `作业 ${job.id} · ${job.kind === 'seal' ? '测试封存' : '生产同步'} · ${jobStateText(job)}${ - job.error ? ` · ${job.error}` : '' - }` + ? `作业 ${job.id} · ${jobKindText(job.kind)} · ${jobStateText(job)}${job.error ? ` · ${job.error}` : ''}` : view.snapshot ? `已有测试封存 ${stamp(view.snapshot.created_at)}(历史记录),但本次作业尚未开始;没有当前作业证据时不会把历史封存当作阶段完成。` : '本次作业尚未开始。'} @@ -603,14 +677,30 @@ function ProtectionCard({