feat(short-drama-agent-front): 接入联合修订与图片费用审计
This commit is contained in:
1 parent
87427d9d43
commit
f6edc4eb70
52 files changed
+5060
-516
No files matched your search
@@ -0,0 +1,302 @@
|
||||
import { flushPromises, mount, type VueWrapper } from '@vue/test-utils'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { NCheckbox, NInput, NInputNumber, NSelect } from 'naive-ui'
|
||||
import { additionalImageFixture } from '../../fixtures/image-additional'
|
||||
import { additionalImageEligibility, imageChargeEligibility } from '@/features/image-authorization/model'
|
||||
import { billingErrorText } from '@/lib/billing-errors'
|
||||
import { imageAuthorizationApi } from '@/features/image-authorization/api'
|
||||
import ImageAuthorizationSettings from '@/features/image-authorization/ImageAuthorizationSettings.vue'
|
||||
import KeyframeDialog from '@/features/production/components/KeyframeDialog.vue'
|
||||
import { billingCatalog, billingProfile } from '../../fixtures/billing'
|
||||
import { imageRequestNotice, runGuardedImageRequest } from '@/features/subject-images/image-request-guard'
|
||||
|
||||
/** 释放弹层、查询和单次请求标记。 */
|
||||
const wrappers: VueWrapper[] = []
|
||||
afterEach(() => {
|
||||
wrappers.splice(0).forEach(wrapper => wrapper.unmount())
|
||||
sessionStorage.clear()
|
||||
})
|
||||
/**
|
||||
* 创建正式包装结构的隔离响应。
|
||||
* @param data Mock原数据
|
||||
* @returns 独立响应
|
||||
*/
|
||||
function response(data: unknown) {
|
||||
return new Response(JSON.stringify({ data }))
|
||||
}
|
||||
/**
|
||||
* 以原精确范围核对人工许可。
|
||||
* @param list 当前隔离费用投影
|
||||
* @returns 当前范围资格
|
||||
*/
|
||||
function eligibility(list: ReturnType<typeof additionalImageFixture>) {
|
||||
return additionalImageEligibility(
|
||||
list,
|
||||
'additional-permit',
|
||||
'billing-project',
|
||||
'audit-shot',
|
||||
'gpt-image-2.5-flare',
|
||||
'c'.repeat(64),
|
||||
'b'.repeat(64)
|
||||
)
|
||||
}
|
||||
|
||||
describe('人工追加不解除原unknown与费用', () => {
|
||||
it('默认unknown阻断;许可仅放行本精确计划,并显示已用和剩余次数', () => {
|
||||
const list = additionalImageFixture()
|
||||
expect(
|
||||
imageChargeEligibility(list, 'keyframe', 'audit-shot', 'gpt-image-2.5-flare', 'b'.repeat(64)).allowed
|
||||
).toBe(false)
|
||||
expect(eligibility(list).allowed).toBe(true)
|
||||
expect(eligibility(list).message).toContain('剩余 2 次')
|
||||
expect(list.requests[0]!.status).toBe('unknown')
|
||||
expect(list.requests[0]!.output?.actualBilledMicros).toBeNull()
|
||||
expect(list.billingBlocked).toBe(true)
|
||||
})
|
||||
it.each([
|
||||
'revoked',
|
||||
'exhausted',
|
||||
'budget',
|
||||
'expired',
|
||||
'project',
|
||||
'shot',
|
||||
'model',
|
||||
'source',
|
||||
'plan',
|
||||
'inflight',
|
||||
'another-unknown'
|
||||
])('拒绝撤销/用尽/跨范围/未核清并发:%s', scenario => {
|
||||
const list = additionalImageFixture(),
|
||||
permit = list.additionalAuthorizations![0]!,
|
||||
parent = list.requests[0]!,
|
||||
grant = list.authorizations[0]!
|
||||
if (scenario === 'revoked') permit.status = 'revoked'
|
||||
if (scenario === 'exhausted') permit.remainingRequests = 0
|
||||
if (scenario === 'budget') grant.remainingMicros = 1
|
||||
if (scenario === 'expired') grant.expired = true
|
||||
if (scenario === 'project') parent.input.projectId = 'other-project'
|
||||
if (scenario === 'shot') parent.input.targetId = 'other-shot'
|
||||
if (scenario === 'model') parent.input.model = 'other-model'
|
||||
if (scenario === 'source') parent.input.sourceFingerprint = 'e'.repeat(64)
|
||||
if (scenario === 'plan') parent.input.planFingerprint = 'f'.repeat(64)
|
||||
if (scenario === 'inflight') parent.status = 'running'
|
||||
if (scenario === 'another-unknown') {
|
||||
list.requests.push({ ...structuredClone(parent), id: 'unapproved-request' })
|
||||
list.blockingRequestIds.push('unapproved-request')
|
||||
}
|
||||
expect(eligibility(list).allowed).toBe(false)
|
||||
})
|
||||
it('完整同计划结果优先免费复用,许可撤销或剩余次数不能触发继续收费', () => {
|
||||
const list = additionalImageFixture()
|
||||
const completed = {
|
||||
...structuredClone(list.requests[0]!),
|
||||
id: 'returned-request',
|
||||
status: 'completed',
|
||||
output: { usageCostMicros: 1, actualBilledMicros: null, result: { image: 'test-saved-result' } }
|
||||
}
|
||||
list.requests.push(completed)
|
||||
list.additionalAuthorizations![0]!.status = 'revoked'
|
||||
const result = imageChargeEligibility(list, 'keyframe', 'audit-shot', 'gpt-image-2.5-flare', 'b'.repeat(64), {
|
||||
id: 'additional-permit',
|
||||
projectId: 'billing-project',
|
||||
sourceFingerprint: 'c'.repeat(64)
|
||||
})
|
||||
expect(result).toMatchObject({ allowed: true, reusable: true })
|
||||
expect(list.billingBlocked).toBe(true)
|
||||
})
|
||||
it('费用核清但没有结果的原unknown精确计划仍不能普通重发', () => {
|
||||
const list = additionalImageFixture()
|
||||
list.billingBlocked = false
|
||||
list.blockingRequestIds = []
|
||||
expect(
|
||||
imageChargeEligibility(list, 'keyframe', 'audit-shot', 'gpt-image-2.5-flare', 'b'.repeat(64)).allowed
|
||||
).toBe(false)
|
||||
})
|
||||
it('原网络连接与真实Provider额度错误分开,底层原因不伪装为额度不足', () => {
|
||||
expect(
|
||||
billingErrorText('OPENAI_IMAGE_REQUEST_UNKNOWN', {
|
||||
failureType: 'timeout_unknown',
|
||||
transportMode: 'proxy',
|
||||
causeCode: 'UND_ERR_CONNECT_TIMEOUT'
|
||||
})
|
||||
).toContain('底层连接超时')
|
||||
expect(
|
||||
billingErrorText('OPENAI_IMAGE_PROVIDER_ERROR', {
|
||||
failureType: 'billing_quota_exceeded',
|
||||
transportMode: 'proxy',
|
||||
causeCode: 'UND_ERR_CONNECT_TIMEOUT'
|
||||
})
|
||||
).toContain('计费配额不足')
|
||||
expect(
|
||||
billingErrorText('OPENAI_IMAGE_PROVIDER_ERROR', {
|
||||
failureType: 'billing_quota_exceeded',
|
||||
transportMode: 'proxy',
|
||||
causeCode: 'UND_ERR_CONNECT_TIMEOUT'
|
||||
})
|
||||
).not.toContain('底层连接超时')
|
||||
})
|
||||
})
|
||||
|
||||
describe('原授权设置与首帧入口追加交互', () => {
|
||||
it('用户逐项接受风险才登记additional,不提交新金额/模型/目标;撤销仍用原接口', async () => {
|
||||
const list = additionalImageFixture(),
|
||||
fetcher = vi.fn<typeof fetch>(async (url, init) => {
|
||||
if (String(url).endsWith('/generation-models')) return response(billingCatalog)
|
||||
if (init?.method === 'POST')
|
||||
return response({ authorizationId: 'additional-created', status: 'approved' })
|
||||
return response(list)
|
||||
})
|
||||
vi.stubGlobal('fetch', fetcher)
|
||||
const wrapper = mount(ImageAuthorizationSettings, { props: { projectId: 'billing-project', disabled: false } })
|
||||
wrappers.push(wrapper)
|
||||
await flushPromises()
|
||||
await wrapper
|
||||
.findAll('.n-collapse-item__header-main')
|
||||
.find(item => item.text() === '原生成请求与费用状态')!
|
||||
.trigger('click')
|
||||
await flushPromises()
|
||||
await wrapper
|
||||
.findAll('button')
|
||||
.find(button => button.text() === '人工有限追加')!
|
||||
.trigger('click')
|
||||
await flushPromises()
|
||||
expect(wrapper.text()).toContain('原精确计划人工有限追加')
|
||||
expect(
|
||||
wrapper
|
||||
.findAllComponents(NInputNumber)
|
||||
.find(input => input.attributes('aria-label') === '人工追加次数')!
|
||||
.props('value')
|
||||
).toBeNull()
|
||||
await wrapper.vm.save()
|
||||
expect(fetcher.mock.calls.filter(([, init]) => init?.method === 'POST')).toHaveLength(0)
|
||||
wrapper
|
||||
.findAllComponents(NInputNumber)
|
||||
.find(input => input.attributes('aria-label') === '人工追加次数')!
|
||||
.vm.$emit('update:value', 2)
|
||||
wrapper
|
||||
.findAllComponents(NInput)
|
||||
.find(input => input.attributes('aria-label') === '追加批准人')!
|
||||
.vm.$emit('update:value', '测试明确批准人')
|
||||
wrapper
|
||||
.findAllComponents(NInput)
|
||||
.find(input => input.attributes('aria-label') === '追加批准说明')!
|
||||
.vm.$emit('update:value', '接受原未知费用,仅追加此精确计划两次')
|
||||
wrapper
|
||||
.findAllComponents(NCheckbox)
|
||||
.find(input => input.text().includes('我接受原请求'))!
|
||||
.vm.$emit('update:checked', true)
|
||||
await flushPromises()
|
||||
await wrapper.vm.save()
|
||||
const call = fetcher.mock.calls.find(([, init]) => init?.method === 'POST')!
|
||||
expect(JSON.parse(String(call[1]?.body))).toEqual({
|
||||
mode: 'additional',
|
||||
idempotencyKey: expect.any(String),
|
||||
parentRequestId: 'original-image-request',
|
||||
maxRequests: 2,
|
||||
acknowledgeUnknownPriorBilling: true,
|
||||
approvedBy: '测试明确批准人',
|
||||
approvalNote: '接受原未知费用,仅追加此精确计划两次'
|
||||
})
|
||||
expect(list.requests[0]!.status).toBe('unknown')
|
||||
await imageAuthorizationApi.revoke('billing-project', 'additional-created', {
|
||||
revokedBy: '测试操作者',
|
||||
reason: '关闭剩余次数'
|
||||
})
|
||||
expect(fetcher.mock.calls.at(-1)?.[0]).toBe(
|
||||
'/api/projects/billing-project/image-authorizations/additional-created/revoke'
|
||||
)
|
||||
expect(fetcher.mock.calls.some(([url]) => /\/keyframe$|\/generate|\/primary|\/vision/.test(String(url)))).toBe(
|
||||
false
|
||||
)
|
||||
})
|
||||
it('首帧必须主动选许可并重新预览,仅提交一次;原本地unknown标记不清除', async () => {
|
||||
const list = additionalImageFixture(),
|
||||
submit = vi.fn<() => Promise<{ accepted: boolean }>>().mockResolvedValue({ accepted: true })
|
||||
const fetcher = vi.fn<typeof fetch>(async url => {
|
||||
const path = String(url)
|
||||
if (path.endsWith('/generation-models')) return response(billingCatalog)
|
||||
if (path.endsWith('/creative-profile')) return response(billingProfile)
|
||||
if (path.endsWith('/resolve'))
|
||||
return response({ provider: 'openai-image', model: 'gpt-image-2.5-flare', options: {} })
|
||||
if (path.endsWith('/image-authorizations')) return response(list)
|
||||
if (path.endsWith('/image-authorizations/preview'))
|
||||
return response({
|
||||
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),
|
||||
authorization: list
|
||||
})
|
||||
if (path.endsWith('/keyframe/preview')) {
|
||||
return response({
|
||||
shotId: 'audit-shot',
|
||||
provider: 'openai-image',
|
||||
model: 'gpt-image-2.5-flare',
|
||||
providerPrompt: 'Mock首帧输入',
|
||||
previewFingerprint: 'a'.repeat(64),
|
||||
referenceImages: [],
|
||||
generationConfig: {}
|
||||
})
|
||||
}
|
||||
throw new Error('未模拟 ' + path)
|
||||
})
|
||||
vi.stubGlobal('fetch', fetcher)
|
||||
await expect(
|
||||
runGuardedImageRequest('keyframe:audit-shot', async () => {
|
||||
throw new Error('原连接结果未知')
|
||||
})
|
||||
).rejects.toThrow('原连接结果未知')
|
||||
const wrapper = mount(KeyframeDialog, {
|
||||
attachTo: document.body,
|
||||
props: {
|
||||
open: true,
|
||||
projectId: 'billing-project',
|
||||
shotId: 'audit-shot',
|
||||
shotTitle: '隔离镜头',
|
||||
disabled: true,
|
||||
additionalAllowed: true,
|
||||
submitGeneration: submit
|
||||
}
|
||||
})
|
||||
wrappers.push(wrapper)
|
||||
await flushPromises()
|
||||
const selection = wrapper
|
||||
.findAllComponents(NSelect)
|
||||
.find(select => select.attributes('aria-label') === '本次人工追加许可')!
|
||||
expect(selection.props('value')).toBeNull()
|
||||
selection.vm.$emit('update:value', 'additional-permit')
|
||||
await flushPromises()
|
||||
const preview = [...document.querySelectorAll<HTMLButtonElement>('button')].find(
|
||||
button => button.textContent?.trim() === '预览实际生成输入'
|
||||
)!
|
||||
preview.click()
|
||||
await flushPromises()
|
||||
const confirm = [...document.querySelectorAll<HTMLButtonElement>('button')].find(
|
||||
button => button.textContent?.trim() === '确认生成首帧'
|
||||
)!
|
||||
expect(
|
||||
JSON.parse(String(fetcher.mock.calls.find(([url]) => String(url).endsWith('/keyframe/preview'))![1]?.body))
|
||||
).not.toHaveProperty('additionalAuthorizationId')
|
||||
expect(confirm.disabled).toBe(false)
|
||||
confirm.click()
|
||||
await flushPromises()
|
||||
confirm.click()
|
||||
await flushPromises()
|
||||
expect(submit).toHaveBeenCalledTimes(1)
|
||||
expect(submit).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
provider: 'openai-image',
|
||||
additionalAuthorizationId: 'additional-permit',
|
||||
expectedPreviewFingerprint: 'a'.repeat(64),
|
||||
setPrimary: false
|
||||
})
|
||||
)
|
||||
expect(imageRequestNotice('keyframe:audit-shot')).toContain('待核对')
|
||||
expect(list.requests[0]!.status).toBe('unknown')
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user