Files
short-drama-agent-front/tests/features/storyboard/derived-review.test.ts
T

212 lines
9.7 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import { derivedReviewApi } from '@/features/storyboard/derived-review-api'
import { draftFromFormal } from '@/features/storyboard/derived-review'
import { subjectImagesApi } from '@/features/subject-images/api'
import type { DesignedShot } from '@/features/storyboard/types'
import type { KeyframeSpec } from '@/features/production/types'
/** 已保存的正式镜头样本,保留空间、动作和形态值。 */
const shot: DesignedShot = {
shotId: 'shot/1',
beatNo: 1,
shotNo: 1,
title: '开门',
description: '角色站在门边',
visualFocus: '手',
direction: {
shotSize: 'wide',
cameraAngle: 'eye_level',
cameraMovement: 'static',
composition: '门在右侧',
cameraPosition: '室内',
lensIntent: 'wide',
depthOfField: 'deep',
visualIntent: '空间关系'
},
visualState: {
timeOfDay: '午后',
weather: '',
atmosphere: '',
environmentTransientState: '日光',
continuityNote: '保持视角',
subjects: [
{
subjectId: 'scene',
subjectRef: '@SC0001',
subjectFormId: 'scene-form',
formName: '客厅',
expression: '',
pose: '',
gaze: '',
position: '画面右侧',
transientState: ''
}
]
}
}
/** 正式来源及已确认的场景几何。 */
const keyframe: KeyframeSpec = {
formalSourceFingerprint: 'a'.repeat(64),
projectId: 'p/1',
shotId: shot.shotId,
episodeNo: 1,
beatNo: 1,
shotNo: 1,
prompt: '正式描述',
references: [
{
subjectId: 'scene',
subjectRef: '@SC0001',
module: 'scene',
role: 'layout-master',
imageId: 'master-1',
imageUrl: '/storage/master.png',
spatialLayoutVersion: 'geometry-1'
}
]
}
describe('摄影草案与规格复用的正式契约', () => {
it('受控修订只变更摄影字段,采用原预览和正式来源', async () => {
const fingerprint = 'b'.repeat(64)
const draft = draftFromFormal(shot, keyframe, fingerprint)
const original = structuredClone(draft)
draft.direction.composition = '保留原场景并移动构图'
draft.direction.cameraPosition = '室内右侧'
expect(draft.visualState).toEqual(original.visualState)
expect(draft.sceneSources).toEqual([
{ subjectRef: '@SC0001', imageId: 'master-1', spatialLayoutVersion: 'geometry-1' }
])
const mock = vi
.fn<typeof fetch>()
.mockResolvedValueOnce(
new Response(
JSON.stringify({ data: { draftFingerprint: 'draft-fingerprint', productionReady: false } })
)
)
.mockResolvedValueOnce(new Response(JSON.stringify({ data: { shots: [{ shotId: shot.shotId }] } })))
vi.stubGlobal('fetch', mock)
const params = { provider: 'seedream', generationOptions: { size: '2048x2048' } }
const preview = await derivedReviewApi.preview(shot.shotId, draft, params)
expect(preview.productionReady).toBe(false)
expect(mock).toHaveBeenCalledTimes(1)
await derivedReviewApi.confirm('p/1', '人工', [
{
...params,
shotId: shot.shotId,
draft,
expectedDraftFingerprint: preview.draftFingerprint,
expectedCurrentFormalSourceFingerprint: keyframe.formalSourceFingerprint
}
])
expect(mock.mock.calls.map(([url]) => url)).toEqual([
'/api/storyboard-shots/shot%2F1/keyframe/preview',
'/api/projects/p%2F1/storyboard-derived-reviews/confirm'
])
expect(JSON.parse(String(mock.mock.calls[1]![1]?.body)).shots[0]).toMatchObject({
...params,
draft,
expectedCurrentFormalSourceFingerprint: keyframe.formalSourceFingerprint,
expectedDraftFingerprint: 'draft-fingerprint'
})
})
it('场景沿用保留其它状态,只将选定的正式前镜证据交给免费预览', async () => {
const draft = draftFromFormal(shot, keyframe, 'b'.repeat(64))
draft.sceneStateCarryForward = { previousShotId: 'previous', subjectRef: '@SC0001' }
draft.visualState.subjects[0]!.transientState = '门半开'
const mock = vi
.fn<typeof fetch>()
.mockResolvedValue(new Response(JSON.stringify({ data: { productionReady: false } })))
vi.stubGlobal('fetch', mock)
await derivedReviewApi.preview(shot.shotId, draft, {})
expect(JSON.parse(String(mock.mock.calls[0]![1]?.body)).draft).toMatchObject({
sceneStateCarryForward: draft.sceneStateCarryForward,
visualState: {
subjects: [{ formId: 'scene-form', transientState: '门半开' }],
environment: { transientState: '日光' }
}
})
expect(shot.visualState?.subjects[0]?.transientState).toBe('')
expect(mock).toHaveBeenCalledTimes(1)
})
it('道具复用冲突只发送一次 reuseOnly,不回退文字模型', async () => {
const mock = vi
.fn<typeof fetch>()
.mockResolvedValue(new Response(JSON.stringify({ message: '没有等价规格' }), { status: 409 }))
vi.stubGlobal('fetch', mock)
await expect(subjectImagesApi.prepareStructure('form/1', true)).rejects.toMatchObject({ status: 409 })
expect(mock).toHaveBeenCalledTimes(1)
expect(mock.mock.calls[0]![0]).toBe('/api/subject-forms/form%2F1/prop-structure-spec/generate')
expect(JSON.parse(String(mock.mock.calls[0]![1]?.body))).toEqual({ reuseOnly: true })
})
})
/** 草案 API 的隔离保存记录,只模拟正式修订规则。 */
describe('待审摄影草案的保存与回读', () => {
it('按项目和镜头区分草案;首次保存、回读和 409 不触发预览', async () => {
const rows = new Map<string, { revision: number; draft: unknown }>()
const fetcher = vi.fn<typeof fetch>().mockImplementation(async (path, options) => {
const url = new URL(String(path), 'http://localhost')
const key = url.pathname
const previous = rows.get(key)
if (options?.method !== 'PUT') return new Response(JSON.stringify({ data: previous ?? null }))
const body = JSON.parse(String(options.body)) as { expectedRevision: number | null; draft: unknown }
if (body.expectedRevision !== (previous?.revision ?? null))
return new Response(
JSON.stringify({
message: '草案修订已变化',
details: {
code: 'KEYFRAME_REVIEW_DRAFT_REVISION_CONFLICT',
currentRevision: previous?.revision ?? null
}
}),
{ status: 409 }
)
const data = { revision: (previous?.revision ?? 0) + 1, draft: body.draft }
rows.set(key, data)
return new Response(JSON.stringify({ data }))
})
vi.stubGlobal('fetch', fetcher)
const first = draftFromFormal(shot, keyframe, 'b'.repeat(64))
const second = { ...first, direction: { ...first.direction, composition: '另一镜构图' } }
expect(await derivedReviewApi.draft('p/1', 'one')).toBeNull()
await derivedReviewApi.saveDraft('p/1', 'one', { expectedRevision: null, draft: first })
await derivedReviewApi.saveDraft('p/1', 'two', { expectedRevision: null, draft: second })
expect((await derivedReviewApi.draft('p/1', 'one'))?.draft.direction.composition).toBe(
first.direction.composition
)
expect((await derivedReviewApi.draft('p/1', 'two'))?.draft.direction.composition).toBe('另一镜构图')
await expect(
derivedReviewApi.saveDraft('p/1', 'one', { expectedRevision: null, draft: second })
).rejects.toMatchObject({
status: 409,
details: { currentRevision: 1 }
})
expect(fetcher.mock.calls.map(([url]) => String(url))).toEqual([
'/api/storyboard-shots/one/keyframe-review-draft?projectId=p%2F1',
'/api/storyboard-shots/one/keyframe-review-draft?projectId=p%2F1',
'/api/storyboard-shots/two/keyframe-review-draft?projectId=p%2F1',
'/api/storyboard-shots/one/keyframe-review-draft?projectId=p%2F1',
'/api/storyboard-shots/two/keyframe-review-draft?projectId=p%2F1',
'/api/storyboard-shots/one/keyframe-review-draft?projectId=p%2F1'
])
})
})
it('视线修订只更新选中 gaze,原正式事实与机位不变', async () => {
const { reviseSubjectGaze } = await import('@/features/storyboard/derived-review')
const formal = draftFromFormal(shot, keyframe, 'a'.repeat(64))
const revised = reviseSubjectGaze(formal, '@SC0001', '画面左侧', '人工确认视线意图')
expect(revised.subjectStateRevision?.changes).toEqual([
{ subjectRef: '@SC0001', field: 'gaze', before: '', after: '画面左侧' }
])
expect(formal.visualState.subjects[0]!.gaze).toBe('')
expect(revised.direction).toEqual(formal.direction)
expect({ ...revised.visualState.subjects[0], gaze: '' }).toEqual(formal.visualState.subjects[0])
expect(() => reviseSubjectGaze(formal, '@CH9999', '左侧', '理由')).toThrow('请选择正式主体')
expect(() => reviseSubjectGaze(formal, '@SC0001', '', '理由')).toThrow('请选择正式主体')
expect(() => reviseSubjectGaze(revised, '@SC0001', '右侧', '理由')).toThrow('请从当前正式状态')
})