Files

152 lines
6.8 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest'
import { spatialMasterApi } from '@/features/spatial-layouts/api'
import {
canUseMasterReference,
masterReviewLabels,
toMasterPreview,
impactVisionLabel
} from '@/features/spatial-layouts/model'
import { runGuardedImageRequest, imageRequestNotice } from '@/features/subject-images/image-request-guard'
/** 所有请求均由隔离 Mock 接收,禁止连接真实 Provider。 */
afterEach(() => {
sessionStorage.clear()
vi.unstubAllGlobals()
})
describe('F04 母版正式契约与独立证据', () => {
it('历史及草稿修订预检、生成复用同一参数与指纹', async () => {
const fetcher = vi.fn<typeof fetch>(async () => new Response(JSON.stringify({ data: {} })))
vi.stubGlobal('fetch', fetcher)
const input = {
expectedVersion: 'a'.repeat(64),
referenceMasterId: 'draft',
revisionInstruction: '仅调整活动方向',
providerName: 'provider',
generationOptions: { size: '2048x2048' }
}
await spatialMasterApi.preflight('p/a', 'l/b', input)
await spatialMasterApi.generate('p/a', 'l/b', {
...input,
expectedPreviewFingerprint: 'f'.repeat(64),
confirmPaidGeneration: true
})
expect(String(fetcher.mock.calls[0]![0])).toBe(
'/api/projects/p%2Fa/shared-spatial-layouts/l%2Fb/masters/generation-preflight'
)
expect(JSON.parse(String(fetcher.mock.calls[0]![1]?.body))).toEqual(input)
expect(JSON.parse(String(fetcher.mock.calls[1]![1]?.body))).toEqual({
...input,
expectedPreviewFingerprint: 'f'.repeat(64),
confirmPaidGeneration: true
})
})
it('来源影响、人工清单和单独 Vision 使用正式路径及独立费用确认', async () => {
const fetcher = vi.fn<typeof fetch>(async () => new Response(JSON.stringify({ data: {} })))
vi.stubGlobal('fetch', fetcher)
await spatialMasterApi.impact('p', 'l', new AbortController().signal)
await spatialMasterApi.review('p', 'l', 'm')
await spatialMasterApi.validate('p', 'l', 'm')
await spatialMasterApi.confirm('p', 'l', 'm', {
expectedVersion: 'a'.repeat(64),
evidence: '人工依据',
confirmedBy: '审核人',
reviewResults: [{ index: 1, result: 'passed', evidence: '连接位置可见' }]
})
expect(fetcher.mock.calls.map(([url]) => String(url))).toEqual([
'/api/projects/p/shared-spatial-layouts/l/masters/recovery-impact',
'/api/projects/p/shared-spatial-layouts/l/masters/m/review',
'/api/projects/p/shared-spatial-layouts/l/masters/m/validate',
'/api/projects/p/shared-spatial-layouts/l/masters/m/confirm'
])
expect(JSON.parse(String(fetcher.mock.calls[2]![1]?.body))).toEqual({ confirmPaidValidation: true })
})
it('草稿只有明确修订且属于当前布局版本才能作参考,人工通过不冒充 Vision 通过', () => {
expect(canUseMasterReference({ status: 'draft', spatialLayoutVersion: 'old' }, 'now', true)).toBe(false)
expect(canUseMasterReference({ status: 'draft', spatialLayoutVersion: 'now' }, 'now', false)).toBe(false)
expect(canUseMasterReference({ status: 'draft', spatialLayoutVersion: 'now' }, 'now', true)).toBe(true)
expect(canUseMasterReference({ status: 'confirmed', spatialLayoutVersion: 'old' }, 'now', false)).toBe(true)
expect(
masterReviewLabels({
status: 'confirmed',
currentVersion: true,
reviewStatus: 'human_passed',
automatedVisionPassed: false
})
).toMatchObject({ human: '人工审核通过', vision: 'AI 视觉审核未通过或未运行', asset: '已确认' })
})
it('完成但质量待复核不能展示通过,历史分步证据缺失明确保留未知', () => {
expect(
impactVisionLabel({
id: 'v',
status: 'completed',
passed: true,
qualityDecision: 'needs_review',
evidenceComplete: true,
historical: true
})
).toBe('原结论待人工复核')
expect(
impactVisionLabel({
id: 'v',
status: 'running',
passed: true,
qualityDecision: 'passed',
evidenceComplete: true,
historical: false
})
).toBe('原审核未完成')
})
it('适配预检时保留原始参数和参考摘要,不虚构预计费用或正式确认', () => {
const result = toMasterPreview({
layoutId: 'l',
layoutVersion: 'a'.repeat(64),
provider: 'provider',
model: 'model',
generationOptions: { size: '2048x2048' },
prompt: '最终画面描述',
references: [
{
imageId: 'm',
imageUrl: '/storage/m.png',
providerImageUrl: 'data:image/png;base64,AA==',
providerInputResolved: true,
role: 'layout-master',
sourceMasterStatus: 'draft'
}
],
previewFingerprint: 'f'.repeat(64),
referenceMasterId: 'm',
revisionInstruction: '修订',
referenceCount: 1,
verifiedSceneCount: 2,
maxReferenceImages: 10,
generationAllowed: false,
reason: '免费预检'
})
expect(result.providerPrompt).toBe('最终画面描述')
expect(result.referenceImages).toEqual(['/storage/m.png'])
expect(result.previewFingerprint).toBe('f'.repeat(64))
})
it('仅在请求确定被拒绝且只读证据证明没有创建任务时解除费用保护', async () => {
const target = 'master-reject'
const reject = new Error('来源过期')
const proof = vi.fn<() => Promise<boolean>>().mockResolvedValue(true)
await expect(
runGuardedImageRequest(
target,
async () => {
throw reject
},
{ confirmRejection: proof }
)
).rejects.toBe(reject)
expect(imageRequestNotice(target)).toBe('')
const calls = vi.fn<() => Promise<void>>().mockRejectedValue(new Error('连接中断'))
await expect(runGuardedImageRequest(target, calls, { confirmRejection: async () => false })).rejects.toThrow(
'连接中断'
)
await expect(runGuardedImageRequest(target, calls)).rejects.toThrow('费用待核对')
expect(calls).toHaveBeenCalledTimes(1)
})
})