215 lines
8.7 KiB
TypeScript
215 lines
8.7 KiB
TypeScript
import type { ContinuityDiagnosis, ContinuityInput, ContinuityReview } from '@/features/production/continuity.types'
|
|
import type { DesignedShot } from '@/features/storyboard/types'
|
|
import type { LayoutConfirmation } from '@/features/production/layout-api'
|
|
|
|
/** 隔离测试使用的固定正式来源版本。 */
|
|
export const fingerprint = 'a'.repeat(64)
|
|
/** 隔离图组的摄影设计。 */
|
|
export const direction = {
|
|
shotSize: 'wide',
|
|
cameraAngle: 'eye-level',
|
|
cameraMovement: 'static',
|
|
composition: '窗在左、门在右,结构位置清晰。',
|
|
cameraPosition: '同侧观察面',
|
|
lensIntent: 'wide',
|
|
depthOfField: 'deep',
|
|
visualIntent: '固定结构对照'
|
|
}
|
|
/** 三镜正式顺序,专用于免费 Mock 验收。 */
|
|
export const continuityShots: DesignedShot[] = ['s1', 's2', 's3'].map((shotId, i) => ({
|
|
shotId,
|
|
shotNo: i + 1,
|
|
beatNo: 1,
|
|
title: `隔离测试镜头 ${i + 1}`,
|
|
description: '固定结构测试画面',
|
|
visualFocus: '固定结构',
|
|
direction,
|
|
visualState: null
|
|
}))
|
|
/** 与正式确认入口相同的完整参数。 */
|
|
export const confirmation: LayoutConfirmation = {
|
|
name: '测试空间',
|
|
sceneSubjectIds: ['scene'],
|
|
constraints: [{ kind: 'attachment', description: '固定连接位置', subjectIds: ['scene'], source: 'art-direction' }],
|
|
evidence: '隔离人工依据',
|
|
expectedVersion: fingerprint
|
|
}
|
|
/**
|
|
* 创建与本次输入绑定的正式诊断形状。
|
|
* @param input 免费诊断请求
|
|
* @param imageUrl 隔离图片地址
|
|
* @returns 当前图组与观察结论
|
|
*/
|
|
export function diagnosisFixture(input: ContinuityInput, imageUrl = '/storage/test-frame.png'): ContinuityDiagnosis {
|
|
return {
|
|
projectId: 'mock-project',
|
|
sceneFormId: input.sceneFormId,
|
|
fingerprint,
|
|
layout: {
|
|
id: 'layout',
|
|
name: '测试空间',
|
|
version: fingerprint,
|
|
evidence: '隔离人工依据',
|
|
constraints: confirmation.constraints
|
|
},
|
|
observation: input.observation ?? null,
|
|
status: input.observation ? 'passed' : 'insufficient_evidence',
|
|
issues: input.observation
|
|
? []
|
|
: [{ code: 'observations_missing', shotIds: input.shotIds, message: '待人工观察固定地标和局部结构' }],
|
|
frames: input.shotIds.map((shotId, i) => ({
|
|
shotId,
|
|
episodeNo: 1,
|
|
beatNo: 1,
|
|
shotNo: i + 1,
|
|
direction,
|
|
scene: {
|
|
subjectId: 'scene',
|
|
subjectRef: '@SC001',
|
|
formId: 'scene-form',
|
|
position: '同一空间',
|
|
transientState: i === 0 ? '门关闭' : '门打开'
|
|
},
|
|
primaryId: 'primary-' + shotId,
|
|
imageUrl,
|
|
status: 'passed',
|
|
formalSourceFingerprint: fingerprint,
|
|
providerInputFingerprint: fingerprint,
|
|
layoutReferences: []
|
|
}))
|
|
}
|
|
}
|
|
/**
|
|
* 创建可改变来源版本的纯 Mock 服务;未定义请求一律拒绝,不访问真实服务。
|
|
* @param imageUrl 隔离测试图片
|
|
* @returns Mock fetch、调用记录及可切换状态
|
|
*/
|
|
export function createContinuityMock(imageUrl = '/storage/test-frame.png') {
|
|
const calls: Array<{ url: string; method: string; body: Record<string, unknown> }> = []
|
|
const state = { stale: false, version: fingerprint, review: null as ContinuityReview | null, conflict: false }
|
|
/**
|
|
* 拦截全部请求,保证测试不产生真实 API 写入。
|
|
* @param input 请求地址
|
|
* @param init 请求参数
|
|
* @returns 隔离的正式响应
|
|
*/
|
|
const fetcher: typeof fetch = async (input, init) => {
|
|
const url = new URL(String(input), 'http://localhost')
|
|
const path = url.pathname
|
|
const method = init?.method ?? 'GET'
|
|
const body = JSON.parse(String(init?.body || '{}')) as Record<string, unknown>
|
|
calls.push({ url: String(input), method, body })
|
|
let data: unknown
|
|
let status = 200
|
|
if (path.endsWith('/generation-spec')) {
|
|
const shotId = path.split('/')[3]!
|
|
data = {
|
|
projectId: 'mock-project',
|
|
shotId,
|
|
episodeNo: 1,
|
|
beatNo: 1,
|
|
shotNo: Number(shotId.slice(1)),
|
|
title: '隔离测试镜头',
|
|
direction,
|
|
subjects: [
|
|
{
|
|
subjectId: 'scene',
|
|
subjectRef: '@SC001',
|
|
subjectName: '测试场景',
|
|
subjectFormId: 'scene-form',
|
|
subjectFormName: '日间',
|
|
module: 'scene'
|
|
}
|
|
]
|
|
}
|
|
} else if (path.endsWith('/keyframes')) {
|
|
const shotId = path.split('/')[3]!
|
|
data = [{ id: 'primary-' + shotId, shotId, isPrimary: true, status: 'completed', imageUrl }]
|
|
} else if (path.endsWith('/shared-spatial-layouts')) {
|
|
if (method === 'PUT') {
|
|
if (state.conflict)
|
|
return new Response(JSON.stringify({ message: '空间布局版本已变化,请重新审核' }), { status: 409 })
|
|
state.version = 'b'.repeat(64)
|
|
state.stale = true
|
|
data = { ...body, id: 'layout', version: state.version }
|
|
} else data = [{ id: 'layout', name: '测试空间', version: state.version, sceneSubjectIds: ['scene'] }]
|
|
} else if (path.endsWith('/masters'))
|
|
data = [
|
|
{
|
|
id: 'master',
|
|
subjectFormId: null,
|
|
viewDescription: '测试全局观察面',
|
|
imageUrl,
|
|
status: 'confirmed',
|
|
spatialLayoutVersion: fingerprint
|
|
}
|
|
]
|
|
else if (path.endsWith('/images'))
|
|
data = [{ id: 'scene-primary', subjectFormId: 'scene-form', isPrimary: true, imageUrl }]
|
|
else if (path.endsWith('/validate/preview'))
|
|
data = {
|
|
sourceCurrent: !state.stale,
|
|
sourceStaleReason: state.stale ? '布局来源已变化,需要重新核对' : null
|
|
}
|
|
else if (path.endsWith('/continuity/diagnosis')) {
|
|
data = diagnosisFixture(body as unknown as ContinuityInput, imageUrl)
|
|
if (state.stale)
|
|
data = {
|
|
...(data as ContinuityDiagnosis),
|
|
status: 'stale',
|
|
fingerprint: null,
|
|
layout: null,
|
|
observation: null,
|
|
issues: (body.shotIds as string[]).map(shotId => ({
|
|
code: 'stale',
|
|
shotIds: [shotId],
|
|
message: '布局来源已变化'
|
|
})),
|
|
frames: (data as ContinuityDiagnosis).frames.map(frame => ({
|
|
...frame,
|
|
status: 'stale',
|
|
reason: '布局来源已变化'
|
|
}))
|
|
}
|
|
} else if (path.endsWith('/continuity/reviews') && method === 'POST') {
|
|
if (state.conflict)
|
|
return new Response(JSON.stringify({ message: '来源已变化', code: 'CONTINUITY_REVIEW_STALE' }), {
|
|
status: 409
|
|
})
|
|
state.review ??= {
|
|
reviewId: 'mock-review',
|
|
reviewer: String(body.reviewer),
|
|
reviewedAt: '2026-11-07T00:00:00Z',
|
|
status: 'passed',
|
|
invalidation: null,
|
|
diagnosis: diagnosisFixture(body as unknown as ContinuityInput, imageUrl)
|
|
}
|
|
data = state.review
|
|
} else if (path.includes('/continuity/reviews/')) {
|
|
data = state.review
|
|
? {
|
|
...state.review,
|
|
...(state.stale
|
|
? { status: 'stale', invalidation: { code: 'evidence_changed', issues: [] } }
|
|
: {})
|
|
}
|
|
: null
|
|
} else if (path.endsWith('/keyframe/preview'))
|
|
data = {
|
|
shotId: path.split('/')[3],
|
|
providerPrompt: '固定结构约束:门轴在远窗侧。',
|
|
draftFingerprint: 'd'.repeat(64),
|
|
layoutConfirmation: confirmation,
|
|
productionReady: false,
|
|
requiresLayoutConfirmation: true,
|
|
requiresMasterReconfirmation: true
|
|
}
|
|
else {
|
|
data = null
|
|
status = 404
|
|
}
|
|
return new Response(JSON.stringify({ data }), { status })
|
|
}
|
|
return { fetcher, calls, state }
|
|
}
|