feat(web): 发布页接上签发同步授权与建立基线入口
build / build (push) Successful in 1m31s

发布页原来只有一个永远 disabled 的"启动同步",title 还写着"生产同步执行链
尚未接线"——执行链其实已经存在。现在:

- 管理员看到"签发同步授权(30 分钟)"(action=sync,绑定范围+目标快照),
  签发与执行仍是两步,不会自动签发;
- 闸口全满足时"启动同步"真的按 POST /library/syncs 投递,被拒时逐条显示
  接口返回的缺项;
- 目标非空又无生效版本时,管理员看到"建立目标权威基线(维护隔离)";
- 测试封存候选改用接口给的 test_sealed(来自版本登记),不再拿归档里最新
  一份顶替;
- 维护卡片区分"未运行维护 / 维护能力未接线 / 能力未知",逐项列出缺的变量。

验证:vitest 99 个文件 469 个用例全绿,tsc --noEmit 无错;反向自检(把按钮
改回恒 disabled、把维护文案改回旧句)两处按预期变红后复原。
This commit is contained in:
姚章浩
2026-10-29 18:16:25 +08:00
parent 517d5f862d
commit 3284658eb1
5 changed files with 460 additions and 13 deletions
+82
View File
@@ -0,0 +1,82 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
import { createLibrarySyncAuthorization, enqueueLibraryBaseline, enqueueLibrarySync } from './librarySnapshots'
// 这些用例钉住"两步走"的接线:授权与执行是两个请求,参数必须与接口契约一致。
// 任何一步把 action 写错、或把 snapshot 换掉,都会在真实调用里悄悄失效(授权永远匹配不上)。
const jsonResponse = (status: number, body: unknown) =>
new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json' } })
afterEach(() => vi.restoreAllMocks())
describe('发布页的同步授权与执行接线', () => {
it('签同步授权必须声明 action=sync 与有限有效期', async () => {
const fetchMock = vi.fn().mockResolvedValue(
jsonResponse(201, {
authorization: { id: 'libauth-sync-1', authorized_by: 'admin', expires_at: '2026-10-29T10:00:00Z' },
})
)
vi.stubGlobal('fetch', fetchMock)
const grant = await createLibrarySyncAuthorization('internal', 'snap-1', 30)
expect(grant.id).toBe('libauth-sync-1')
const [url, init] = fetchMock.mock.calls[0]
expect(url).toBe('/api/v1/library/authorizations')
expect(JSON.parse(init.body)).toEqual({
scope: 'internal',
snapshot_id: 'snap-1',
action: 'sync',
expires_in_minutes: 30,
})
})
it('启动同步把准确的 range 与目标快照传给 /library/syncs', async () => {
const fetchMock = vi
.fn()
.mockResolvedValue(jsonResponse(202, { job: { id: 'libsync-internal-1', scope: 'internal' }, reused: false }))
vi.stubGlobal('fetch', fetchMock)
const accepted = await enqueueLibrarySync('internal', 'snap-1')
expect(accepted.job_id).toBe('libsync-internal-1')
const [url, init] = fetchMock.mock.calls[0]
expect(url).toBe('/api/v1/library/syncs')
expect(JSON.parse(init.body)).toEqual({ scope: 'internal', target_snapshot_id: 'snap-1' })
})
it('闸口未齐时把接口给的缺项原样抛出,而不是假装已启动', async () => {
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue(
jsonResponse(503, {
code: 'library_sync_blocked',
reasons: ['目标非空且没有生效版本记录', '没有与目标快照匹配的有效发布授权'],
})
)
)
await expect(enqueueLibrarySync('internal', 'snap-1')).rejects.toThrow(
/目标非空且没有生效版本记录;没有与目标快照匹配的有效发布授权/
)
})
it('建立基线只传 range,并返回作业号', async () => {
const fetchMock = vi
.fn()
.mockResolvedValue(jsonResponse(202, { job: { id: 'libbase-internal-1', scope: 'internal' }, reused: false }))
vi.stubGlobal('fetch', fetchMock)
const accepted = await enqueueLibraryBaseline('internal')
expect(accepted.job_id).toBe('libbase-internal-1')
const [url, init] = fetchMock.mock.calls[0]
expect(url).toBe('/api/v1/library/baselines')
expect(JSON.parse(init.body)).toEqual({ scope: 'internal' })
})
it('建立基线被拒时照实说明(例如目标为空或已有生效版本)', async () => {
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue(
jsonResponse(409, {
code: 'library_baseline_exists',
error: '该目标已有生效版本,不需要再建基线;换版本请走同步或恢复',
})
)
)
await expect(enqueueLibraryBaseline('internal')).rejects.toThrow(/该目标已有生效版本/)
})
})