247 lines
11 KiB
TypeScript
247 lines
11 KiB
TypeScript
import { defineComponent, h } from 'vue'
|
||
import { flushPromises, mount, type VueWrapper } from '@vue/test-utils'
|
||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||
import { useImageBillingAudit } from '@/features/image-authorization/useImageBillingAudit'
|
||
import { imageBillingAuditApi } from '@/features/image-authorization/billing-audit-api'
|
||
import {
|
||
imageBillingAmountMicros,
|
||
imageBillingEvidenceIssues,
|
||
imageBillingReleaseIssues
|
||
} from '@/features/image-authorization/billing-audit'
|
||
import { imageAuditFixture, imageAuditFacts } from '../../fixtures/image-billing-audit'
|
||
import type {
|
||
ImageBillingEvidenceInput,
|
||
ImageBillingReviewInput
|
||
} from '@/features/image-authorization/billing-audit.types'
|
||
|
||
/** 释放查询,避免迟到请求串到其它用例。 */
|
||
const wrappers: VueWrapper[] = []
|
||
afterEach(() => wrappers.splice(0).forEach(wrapper => wrapper.unmount()))
|
||
/**
|
||
* 返回正式包装结构的隔离响应。
|
||
* @param data Mock业务值
|
||
* @returns 独立响应
|
||
*/
|
||
function response(data: unknown) {
|
||
return new Response(JSON.stringify({ data }))
|
||
}
|
||
|
||
describe('图片原请求费用事实与正式API', () => {
|
||
it.each([
|
||
{ amount: null, micros: null },
|
||
{ amount: 0, micros: 0 },
|
||
{ amount: 0.000001, micros: 1 },
|
||
{ amount: 1.000001, micros: 1000001 },
|
||
{ amount: 0.0000001, micros: null },
|
||
{ amount: -1, micros: null },
|
||
{ amount: 10000000000, micros: null }
|
||
])('微美元精确换算,不把unknown显示成0:$amount', ({ amount, micros }) =>
|
||
expect(imageBillingAmountMicros(amount)).toBe(micros)
|
||
)
|
||
it('本地和LangSmith编号、弱证据和原成功事实冲突不能用于核清', () => {
|
||
const view = imageAuditFixture(),
|
||
facts = imageAuditFacts()
|
||
facts.providerRequestId = view.requestId
|
||
expect(imageBillingEvidenceIssues(facts, view)).toContain(
|
||
'本地任务、请求或 LangSmith 编号不能作为供应方请求号。'
|
||
)
|
||
facts.providerRequestId = 'test-trace'
|
||
facts.artifacts.push({
|
||
id: 'trace',
|
||
kind: 'langsmith',
|
||
recordId: 'test-trace',
|
||
sha256: '3'.repeat(64),
|
||
reference: 'Mock观察'
|
||
})
|
||
expect(imageBillingEvidenceIssues(facts, view)).toContain(
|
||
'本地任务、请求或 LangSmith 编号不能作为供应方请求号。'
|
||
)
|
||
facts.providerRequestId = 'test-provider-request'
|
||
facts.execution.outcome = 'unknown'
|
||
expect(imageBillingReleaseIssues({ ...facts, identity: view.identity }, view).join(' ')).toContain('仍未确认')
|
||
facts.execution.outcome = 'failed'
|
||
view.hasOriginalReceipt = true
|
||
expect(imageBillingReleaseIssues({ ...facts, identity: view.identity }, view)).toContain(
|
||
'外部声明与原成功回执冲突。'
|
||
)
|
||
})
|
||
it('原项目、原应用请求和原完整身份通过正式图片路径提交,不混用视频或当前新镜头', async () => {
|
||
const view = imageAuditFixture('p/1', 'r/1'),
|
||
fetcher = vi.fn<typeof fetch>().mockImplementation(async () => response(view))
|
||
vi.stubGlobal('fetch', fetcher)
|
||
await imageBillingAuditApi.get('p/1', 'r/1')
|
||
const body: ImageBillingEvidenceInput = {
|
||
expectedTaskFingerprint: view.taskFingerprint,
|
||
operator: '测试登记人',
|
||
evidence: { ...imageAuditFacts(), identity: view.identity }
|
||
}
|
||
await imageBillingAuditApi.evidence('p/1', 'r/1', body)
|
||
expect(fetcher.mock.calls.map(([url]) => url)).toEqual([
|
||
'/api/projects/p%2F1/image-requests/r%2F1/audit',
|
||
'/api/projects/p%2F1/image-requests/r%2F1/audit/evidence'
|
||
])
|
||
expect(JSON.parse(String(fetcher.mock.calls[1]![1]?.body))).toEqual(body)
|
||
expect(body.evidence.identity).toBe(view.identity)
|
||
})
|
||
})
|
||
|
||
/**
|
||
* 主动登记一组隔离材料,绝不自动选择复核或解除。
|
||
* @param form 真实审计表单
|
||
*/
|
||
async function register(form: ReturnType<typeof useImageBillingAudit>) {
|
||
form.operator.value = '测试登记人'
|
||
form.facts.value = imageAuditFacts()
|
||
form.confirmed.value = true
|
||
await form.submit()
|
||
await flushPromises()
|
||
}
|
||
|
||
describe('图片证据登记与独立复核编排', () => {
|
||
/**
|
||
* 挂载真实Hook,所有写入只进入内存投影。
|
||
* @param projectId 隔离项目
|
||
* @returns 表单、Mock控制和只读状态
|
||
*/
|
||
async function harness(projectId = 'image-audit-flow') {
|
||
const view = imageAuditFixture(projectId)
|
||
let conflictNext = false
|
||
const changed = vi.fn<() => void>()
|
||
const fetcher = vi.fn<typeof fetch>(async (url, init) => {
|
||
const path = String(url)
|
||
if (init?.method !== 'POST')
|
||
return response({
|
||
...view,
|
||
projectId: path.includes('/other-project/') ? 'other-project' : view.projectId,
|
||
identity: {
|
||
...view.identity,
|
||
projectId: path.includes('/other-project/') ? 'other-project' : view.projectId
|
||
}
|
||
})
|
||
const body = JSON.parse(String(init.body))
|
||
if (path.endsWith('/evidence')) {
|
||
view.history.evidence.push({
|
||
id: 'registered-evidence',
|
||
requestId: view.requestId,
|
||
taskFingerprint: view.taskFingerprint,
|
||
digest: '3'.repeat(64),
|
||
body: body as ImageBillingEvidenceInput
|
||
})
|
||
view.evidenceSetFingerprint = '4'.repeat(64)
|
||
return response({ ...view, evidenceId: 'registered-evidence', reused: false })
|
||
}
|
||
if (conflictNext) {
|
||
conflictNext = false
|
||
view.reviewFingerprint = '5'.repeat(64)
|
||
return new Response(
|
||
JSON.stringify({ code: 'IMAGE_BILLING_AUDIT_REVIEW_CONFLICT', message: '另一项复核已先完成' }),
|
||
{ status: 409 }
|
||
)
|
||
}
|
||
view.history.reviews.push({
|
||
id: 'reviewed-evidence',
|
||
requestId: view.requestId,
|
||
taskFingerprint: view.taskFingerprint,
|
||
digest: '6'.repeat(64),
|
||
revision: view.history.reviews.length + 1,
|
||
body: body as ImageBillingReviewInput
|
||
})
|
||
view.status = body.decision === 'release_future_charges' ? 'released' : 'blocked'
|
||
view.released = view.status === 'released'
|
||
view.billingBlocked = !view.released
|
||
view.reviewedAmountMicros = view.released ? 1 : null
|
||
return response({ ...view, reviewId: 'reviewed-evidence', reused: false })
|
||
})
|
||
vi.stubGlobal('fetch', fetcher)
|
||
let form!: ReturnType<typeof useImageBillingAudit>
|
||
const wrapper = mount(
|
||
defineComponent({
|
||
props: { projectId: { type: String, required: true } },
|
||
setup(props) {
|
||
form = useImageBillingAudit(
|
||
{
|
||
get projectId() {
|
||
return props.projectId
|
||
},
|
||
requestId: 'unknown-image'
|
||
},
|
||
changed
|
||
)
|
||
return () => h('div')
|
||
}
|
||
}),
|
||
{ props: { projectId } }
|
||
)
|
||
wrappers.push(wrapper)
|
||
await flushPromises()
|
||
return {
|
||
form,
|
||
fetcher,
|
||
view,
|
||
changed,
|
||
wrapper,
|
||
conflict: () => {
|
||
conflictNext = true
|
||
}
|
||
}
|
||
}
|
||
it('登记不解锁;用户另行选择和复核才核清,原unknown/null/预留仍保留', async () => {
|
||
const { form, fetcher, view, changed } = await harness()
|
||
expect(fetcher).toHaveBeenCalledTimes(1)
|
||
expect(form.facts.value.billing.amount).toBeNull()
|
||
await register(form)
|
||
expect(view.billingBlocked).toBe(true)
|
||
expect(view.originalRequestStatus).toBe('unknown')
|
||
expect(form.action.value).toBe('evidence')
|
||
expect(form.confirmed.value).toBe(false)
|
||
expect(fetcher.mock.calls.filter(([, init]) => init?.method === 'POST')).toHaveLength(1)
|
||
form.action.value = 'review'
|
||
form.evidenceId.value = 'registered-evidence'
|
||
form.decision.value = 'release_future_charges'
|
||
form.reviewer.value = '测试独立复核人'
|
||
form.reason.value = 'Mock原件与唯一请求已经核对'
|
||
form.confirmationSha256.value = '7'.repeat(64)
|
||
form.confirmed.value = true
|
||
expect(form.canSubmit.value).toBe(true)
|
||
await form.submit()
|
||
await flushPromises()
|
||
expect(view.billingBlocked).toBe(false)
|
||
expect(view.originalRequestStatus).toBe('unknown')
|
||
expect(view.originalActualBilledMicros).toBeNull()
|
||
expect(view.reservedMicros).toBe(1000000)
|
||
expect(changed).toHaveBeenCalledTimes(2)
|
||
expect(fetcher.mock.calls.filter(([, init]) => init?.method === 'POST')).toHaveLength(2)
|
||
expect(fetcher.mock.calls.every(([url]) => String(url).includes('/image-requests/'))).toBe(true)
|
||
await form.refresh()
|
||
await form.submit()
|
||
expect(fetcher.mock.calls.filter(([, init]) => init?.method === 'POST')).toHaveLength(2)
|
||
})
|
||
it('409刷新正式GET并保留表单、撤销确认,不自动重复提交', async () => {
|
||
const setup = await harness('image-conflict')
|
||
await register(setup.form)
|
||
const form = setup.form
|
||
form.action.value = 'review'
|
||
form.evidenceId.value = 'registered-evidence'
|
||
form.reviewer.value = '测试复核人'
|
||
form.reason.value = '保留此前已填理由'
|
||
form.confirmationSha256.value = '8'.repeat(64)
|
||
form.confirmed.value = true
|
||
setup.conflict()
|
||
await form.submit()
|
||
expect(form.reason.value).toBe('保留此前已填理由')
|
||
expect(form.confirmationSha256.value).toBe('8'.repeat(64))
|
||
expect(form.confirmed.value).toBe(false)
|
||
expect(form.query.data.value?.reviewFingerprint).toBe('5'.repeat(64))
|
||
expect(setup.view.history.reviews).toHaveLength(0)
|
||
await form.submit()
|
||
expect(setup.fetcher.mock.calls.filter(([url]) => String(url).endsWith('/reviews'))).toHaveLength(1)
|
||
await setup.wrapper.setProps({ projectId: 'other-project' })
|
||
await flushPromises()
|
||
expect(form.operator.value).toBe('')
|
||
expect(form.reason.value).toBe('')
|
||
expect(form.confirmationSha256.value).toBe('')
|
||
expect(form.evidenceId.value).toBe('')
|
||
expect(form.facts.value.artifacts).toEqual([])
|
||
})
|
||
})
|