Files
knowledge-governance/web/src/librarySyncAuth.test.ts
T
姚章浩 8d838d9cb4
build / build (push) Successful in 1m45s
feat(library): 整库发布身份、写集合与收尾边界
执行时真实时钟与持久授权回读(撤销/过期即停并保留预备份)、写集合写前落盘、
租约与 attempt 身份、恢复队列与原发送身份分离、成功收尾必须释放维护后再登记终态。
2026-11-01 11:35:10 +08:00

87 lines
3.6 KiB
TypeScript

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', 'source-bound')
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',
source_id: 'source-bound',
})
})
it('闸口未齐时把接口给的缺项原样抛出,而不是假装已启动', async () => {
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue(
jsonResponse(503, {
code: 'library_sync_blocked',
reasons: ['目标非空且没有生效版本记录', '没有与目标快照匹配的有效发布授权'],
})
)
)
await expect(enqueueLibrarySync('internal', 'snap-1', 'source-bound')).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(/该目标已有生效版本/)
})
})