import { createApp, defineComponent, h, ref } from 'vue' import { NButton, NConfigProvider, NMessageProvider, NGlobalStyle, zhCN, dateZhCN } from 'naive-ui' import { useTheme } from '../../src/composables/useTheme' import AppDrawer from '../../src/components/ui/AppDrawer.vue' import ActionMessageProvider from '../../src/components/ui/ActionMessageProvider.vue' import ProjectSettingsDialog from '../../src/features/generation-config/components/ProjectSettingsDialog.vue' import KeyframeDialog from '../../src/features/production/components/KeyframeDialog.vue' import VideoAuditPanel from '../../src/features/production/components/VideoAuditPanel.vue' import { billingCatalog, billingProfile, imageAuthorizationsFixture, videoAuditFixture } from '../fixtures/billing' import type { ImageAuthorizationInput, ImageChargeRequest } from '../../src/features/image-authorization/types' import type { RegisterVideoAudit, ReviewVideoAudit } from '../../src/features/production/video-audit.types' import '../../src/styles/styles.css' import '../../src/styles/admin.css' /** 浏览器隔离页面的唯一请求记录,不连接真实服务。 */ const calls: Array<{ path: string; method: string; body: unknown }> = [] /** 每次场景切换都使用新的模拟项目投影。 */ let images = imageAuthorizationsFixture() /** 模拟原历史任务,费用处置从不改变原质量。 */ let video = videoAuditFixture() /** 仅用于明确制造一次审计并发冲突。 */ let conflictNext = false /** 场景重挂,刷新行为只执行读取。 */ const revision = ref(0) /** 可从 Chrome 只读核对真实组件发出的请求。 */ Object.assign(window, { billingTest: { calls, get images() { return images }, get video() { return video }, get conflictNext() { return conflictNext }, set conflictNext(value: boolean) { conflictNext = value } } }) /** 通过 DOM 只读导出隔离请求计数,便于浏览器检查。 */ document.addEventListener('billing-probe', () => { document.documentElement.dataset.billingProbe = JSON.stringify({ calls, images, video, conflictNext }) }) /** * 全量拦截当前隔离页的请求,未知接口直接拒绝,无任何真实付费或业务写入。 * @param input 请求路径 * @param init 原请求参数 * @returns 隔离正式结构响应 */ window.fetch = async (input, init) => { const path = String(input), method = init?.method ?? 'GET' const body = init?.body ? JSON.parse(String(init.body)) : undefined calls.push({ path, method, body }) let data: unknown if (path.endsWith('/generation-models')) data = billingCatalog else if (path.endsWith('/creative-profile')) data = billingProfile else if (path.endsWith('/resolve')) data = { provider: 'openai-image', model: 'gpt-image-2.5-flare', options: {} } else if (path.includes('/workflow-executions/latest')) data = null else if (path.endsWith('/validation/preview')) data = { history: [video.original], blockingTasks: video.audit.billingBlocked ? [video.original] : [] } else if (path.endsWith('/keyframe/preview')) data = { shotId: 'audit-shot', provider: body.provider, model: body.provider === 'openai-image' ? 'gpt-image-2.5-flare' : 'qwen-image-3.0-pro', providerPrompt: '仅供隔离验收的画面描述', previewFingerprint: 'a'.repeat(64), referenceImages: [], generationConfig: { options: {} } } else if (path.endsWith('/keyframe')) data = { id: 'mock-keyframe', shotId: 'audit-shot', status: 'completed' } else if (path.endsWith('/image-authorizations/preview')) data = { provider: 'openai-image', model: 'gpt-image-2.5-flare', context: { projectId: 'billing-project', targetId: 'audit-shot', scope: 'keyframe', sourceFingerprint: 'c'.repeat(64) }, planFingerprint: 'b'.repeat(64), request: {}, currency: 'USD', billingBasis: 'provider_usage', hardProviderCostCap: false, authorization: images } else if (path.endsWith('/image-authorizations') && method === 'GET') data = images else if (path.endsWith('/image-authorizations') && method === 'POST') { const previous = images.authorizations.find(item => item.input.idempotencyKey === body.idempotencyKey) if (previous) data = { authorizationId: previous.id, status: previous.status } else { const id = `mock-grant-${images.authorizations.length}` images.authorizations.push({ id, status: 'approved', input: body as ImageAuthorizationInput, expired: false, usedRequests: 0, remainingRequests: body.maxRequests, accountedMicros: 0, remainingMicros: body.budgetMicros, billingBlocked: false }) data = { authorizationId: id, status: 'approved' } } } else if (path.endsWith('/revoke')) { const id = path.split('/').at(-2)! images.authorizations.find(item => item.id === id)!.status = 'revoked' data = { authorizationId: id, status: 'revoked' } } else if (path.endsWith('/audit')) data = video.audit else if (path.endsWith('/audit-task')) data = video.original else if (path.endsWith('/audit/evidence')) { const value = body as RegisterVideoAudit const step = video.audit.steps[0]! const previous = step.evidence.find(row => JSON.stringify(row.body) === JSON.stringify(value)) const event = previous ?? { id: `mock-evidence-${step.evidence.length}`, createdAt: new Date().toISOString(), taskFingerprint: video.audit.taskFingerprint, stepName: step.name, digest: '1'.repeat(64), body: value } if (!previous) { step.evidence.push(event) video.audit.history.evidence.push(event) step.evidenceSetFingerprint = String(step.evidence.length + 1).repeat(64) step.status = 'unreviewed' video.audit.billingBlocked = true } data = { evidenceId: event.id, reused: !!previous, billingBlocked: true } } else if (path.endsWith('/audit/reviews')) { if (conflictNext) { conflictNext = false video.audit.steps[0]!.reviewFingerprint = '8'.repeat(64) return new Response(JSON.stringify({ code: 'VIDEO_AUDIT_REVIEW_CONFLICT', message: '另一复核先完成' }), { status: 409 }) } const value = body as ReviewVideoAudit, step = video.audit.steps[0]! const released = value.decision === 'release_future_charges' const event = { id: `mock-review-${step.reviews.length}`, createdAt: new Date().toISOString(), taskFingerprint: video.audit.taskFingerprint, stepName: step.name, digest: '3'.repeat(64), revision: step.reviews.length + 1, body: value } step.reviews.push(event) video.audit.history.reviews.push(event) step.status = released ? 'released' : 'blocked' video.audit.billingBlocked = !released step.reviewFingerprint = '7'.repeat(64) data = { reviewId: event.id, status: step.status, billingBlocked: video.audit.billingBlocked, reused: false } } else return new Response(JSON.stringify({ message: '隔离页拒绝未声明请求' }), { status: 403 }) return new Response(JSON.stringify({ data }), { headers: { 'Content-Type': 'application/json' } }) } /** * 切换隔离数据,所有金额均为测试值,与正式批准无关。 * @param kind 本次测试场景 */ function scenario(kind: string) { images = imageAuthorizationsFixture() video = videoAuditFixture() const grant = images.authorizations[0]! if (kind === 'expired') { grant.expired = true grant.input.expiresAt = '2000-01-01T00:00:00Z' } if (kind === 'insufficient') grant.remainingMicros = 1 if (kind === 'unknown' || kind === 'reusable') { images.billingBlocked = true images.blockingRequestIds = ['original-image-request'] const request: ImageChargeRequest = { id: 'original-image-request', status: kind === 'reusable' ? 'completed' : 'unknown', input: { projectId: 'billing-project', authorizationId: grant.id, candidateId: 'original-image', targetId: 'audit-shot', scope: 'keyframe', provider: 'openai-image', model: 'gpt-image-2.5-flare', planFingerprint: 'b'.repeat(64), sourceFingerprint: 'c'.repeat(64), reservedMicros: 500_000, request: { prompt: '隔离原请求' } }, output: { usageCostMicros: null, actualBilledMicros: null, ...(kind === 'reusable' ? { result: { image: 'mock-image' } } : { error: { failureType: 'timeout_unknown' } }) } } images.requests.push(request) } revision.value++ } /** 使用正式抽屉和表单验收布局,顶栏仅为隔离测试控制。 */ const Preview = defineComponent({ setup() { /** 正式项目配置抽屉。 */ const settingsOpen = ref(false) /** 原首帧生成弹窗。 */ const keyframeOpen = ref(false) /** 费用审计使用正式固定底栏。 */ const auditOpen = ref(false) /** 真实审计表单公开的底栏入口。 */ const auditForm = ref | null>(null) return () => h('main', { style: 'padding:24px;height:100dvh;overflow:auto' }, [ h('h1', { style: 'font-size:24px;margin-bottom:16px' }, '图片授权与视频费用审计 · Mock 隔离验收'), h('p', { style: 'margin-bottom:16px' }, '所有请求均拦截在当前页面,不连接真实模型或正式任务。'), h('div', { style: 'display:flex;flex-wrap:wrap;gap:12px;margin-bottom:24px' }, [ h(NButton, { onClick: () => (settingsOpen.value = true) }, () => '项目图片授权'), h(NButton, { onClick: () => (keyframeOpen.value = true) }, () => '原首帧生成入口'), h(NButton, { onClick: () => (auditOpen.value = true) }, () => '视频费用审计'), ...Object.entries({ active: '有效授权', expired: '授权到期', insufficient: '额度不足', unknown: '未知费用', reusable: '同计划免费复用' }).map(([value, label]) => h(NButton, { onClick: () => scenario(value) }, () => label)), h( NButton, { onClick: () => { conflictNext = true } }, () => '下一次复核冲突' ) ]), h(ProjectSettingsDialog, { key: 'settings' + revision.value, projectId: 'billing-project', disabled: false, initialTab: 'image-authorization', open: settingsOpen.value, 'onUpdate:open': value => (settingsOpen.value = value) }), h(KeyframeDialog, { key: 'keyframe' + revision.value, shotId: 'audit-shot', projectId: 'billing-project', shotTitle: '隔离首帧', disabled: false, open: keyframeOpen.value, 'onUpdate:open': value => (keyframeOpen.value = value), submitGeneration: async input => { await fetch('/api/storyboard-shots/audit-shot/keyframe', { method: 'POST', body: JSON.stringify(input) }) return { accepted: true } } }), h( AppDrawer, { key: 'audit' + revision.value, open: auditOpen.value, title: '视频费用审计', busy: auditForm.value?.saving, 'onUpdate:open': value => (auditOpen.value = value) }, { default: () => auditOpen.value ? h(VideoAuditPanel, { ref: auditForm, projectId: 'billing-project', shotId: 'audit-shot', videoId: 'audit-video' }) : null, footer: () => [ h( NButton, { disabled: auditForm.value?.saving, onClick: () => (auditOpen.value = false) }, () => '关闭' ), h( NButton, { type: 'primary', disabled: !auditForm.value?.canSubmit, loading: auditForm.value?.saving, onClick: () => auditForm.value?.submit() }, () => auditForm.value?.submitLabel ?? '登记外部证据' ) ] } ) ]) } }) createApp({ setup() { /** 隔离页复用正式主题、中文控件与状态色。 */ const theme = useTheme() return () => h( NConfigProvider, { theme: theme.theme.value, themeOverrides: theme.overrides.value, locale: zhCN, dateLocale: dateZhCN }, { default: () => [ h(NGlobalStyle), h( NMessageProvider, {}, { default: () => h(ActionMessageProvider, {}, { default: () => h(Preview) }) } ) ] } ) } }).mount('#app')