Files
short-drama-agent-front/tests/features/breakdown/candidate-panel.test.ts
T

335 lines
15 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { DOMWrapper, flushPromises, mount, type VueWrapper } from '@vue/test-utils'
import BreakdownCandidatePanel from '@/features/breakdown/components/BreakdownCandidatePanel.vue'
import { projectsApi } from '@/features/projects/api'
import { breakdownApi } from '@/features/breakdown/api'
import { breakdownQuality, selectBreakdownSource } from '@/features/breakdown/quality'
import { getOperation } from '@/features/workflows/operations'
import { candidateCheckpoint, candidateReview, candidateState, commitPreview } from '../../fixtures/breakdown-candidate'
import type { CandidateCommitPreview } from '@/features/breakdown/candidate.types'
import { NButton } from 'naive-ui'
/** 真实全屏抽屉挂载于 body,测试仅模拟网络,不接触项目数据。 */
let wrapper: VueWrapper | undefined
beforeEach(() => {
vi.spyOn(projectsApi, 'checkpoints').mockResolvedValue([candidateCheckpoint()])
})
afterEach(() => {
wrapper?.unmount()
wrapper = undefined
document.body.innerHTML = ''
vi.restoreAllMocks()
})
/** 创建尚未打开的安全预检抽屉,不自动执行请求。 */
function openPanel() {
wrapper = mount(BreakdownCandidatePanel, {
attachTo: document.body,
props: {
open: false,
'onUpdate:open': (value: boolean) => wrapper?.setProps({ open: value }),
projectId: 'candidate-project',
checkpointId: 'candidate-current',
state: candidateState(),
quality: breakdownQuality(selectBreakdownSource([candidateCheckpoint()]), candidateReview()),
disabled: false
}
})
return wrapper
}
/** 打开只读预检查抽屉,不直接提交。 */
async function checkCandidate() {
await wrapper!.setProps({ open: true })
await flushPromises()
}
/** 取得当前打开的预检抽屉正文及固定底栏。 */
function drawer() {
return new DOMWrapper(document.querySelector<HTMLElement>('.app-drawer')!)
}
/**
* 点击抽屉内的真实操作按钮。
* @param label 按钮可见名称
*/
async function clickAction(label: string) {
const button = drawer()
.findAll('button')
.find(item => item.text() === label)
expect(button, `缺少操作:${label}`).toBeDefined()
await button!.trigger('click')
await flushPromises()
}
/** 用户勾选固定底栏的确认,再点击最终提交。 */
async function confirmCandidate() {
await drawer().get('.app-drawer-footer [role="checkbox"]').trigger('click')
await clickAction('确认提交候选为正式结果')
}
/** 两条关系仅为隔离测试样例,保留原始证据空格以检验请求不被改写。 */
function relationPreview() {
const result = commitPreview({ ready: false })
result.checks.relationEvidenceValid = false
result.relationIssues = ['@SC0002 位于→测试空地', '@PR0001 悬挂于→测试横木']
result.relationRepairTargets = [
{
subjectRef: '@SC0002',
subjectName: '测试场景',
relation: '位于',
target: '测试空地',
evidence: ' 原始证据 '
},
{
subjectRef: '@PR0001',
subjectName: '测试铜铃',
relation: '悬挂于',
target: '测试横木',
evidence: '另一条证据'
}
]
return result
}
/** 选择第一条真实返回的关系;选择不代表确认。 */
async function selectRelation() {
await drawer().get('input[type="radio"]').setValue(true)
await flushPromises()
}
describe('候选预检查与最终提交门禁', () => {
it('打开仅预检,固定底栏明确确认后重新核对版本和门禁,只提交一次并刷新', async () => {
const preview = vi.spyOn(breakdownApi, 'candidateCommitPreview').mockResolvedValue(commitPreview())
const commit = vi.spyOn(breakdownApi, 'commitCandidate').mockResolvedValue({
projectId: 'candidate-project',
subjectCount: 2,
subjectFormCount: 2,
episodeCount: 1,
beatCount: 1,
shotCount: 2,
shotSubjectCount: 2
})
openPanel()
expect(preview).not.toHaveBeenCalled()
await checkCandidate()
expect(commit).not.toHaveBeenCalled()
expect(drawer().get('.app-drawer-footer [role="checkbox"]').attributes('aria-checked')).toBe('false')
await confirmCandidate()
expect(preview).toHaveBeenCalledTimes(2)
expect(commit).toHaveBeenCalledExactlyOnceWith('candidate-project')
expect(wrapper!.emitted('refresh')).toHaveLength(1)
expect(wrapper!.emitted('update:open')?.at(-1)).toEqual([false])
})
it('ready=false 展示未通过项,禁止最终确认', async () => {
const result = commitPreview({ ready: false })
result.checks.noProtectedShots = false
vi.spyOn(breakdownApi, 'candidateCommitPreview').mockResolvedValue(result)
const commit = vi.spyOn(breakdownApi, 'commitCandidate')
openPanel()
await checkCandidate()
expect(drawer().text()).toContain('镜头资产')
expect(drawer().text()).not.toContain('确认提交候选为正式结果')
expect(commit).not.toHaveBeenCalled()
})
it('已提交分支不伪造缺失检查失败,刷新页面且隐藏所有写入口', async () => {
vi.spyOn(breakdownApi, 'candidateCommitPreview').mockResolvedValue({
projectId: 'candidate-project',
checkpointId: 'new-committed',
readOnly: true,
ready: false,
status: 'already_committed',
reason: '最新候选已正式提交'
})
openPanel()
await checkCandidate()
expect(drawer().text()).toContain('已提交正式结果')
expect(drawer().find('[aria-label="未通过的提交安全检查"]').exists()).toBe(false)
expect(drawer().text()).not.toContain('确认提交候选为正式结果')
expect(wrapper!.emitted('refresh')).toHaveLength(1)
})
it('最终提交时发现来源版本变化,拒绝替换并刷新', async () => {
vi.mocked(projectsApi.checkpoints).mockResolvedValue([candidateCheckpoint(candidateState(), 'new')])
vi.spyOn(breakdownApi, 'candidateCommitPreview').mockResolvedValue(commitPreview())
const commit = vi.spyOn(breakdownApi, 'commitCandidate')
openPanel()
await checkCandidate()
await confirmCandidate()
expect(commit).not.toHaveBeenCalled()
expect(getOperation('candidate-project').error).toContain('版本已变化')
expect(wrapper!.emitted('refresh')).toHaveLength(1)
})
it('最终确认发现安全条件变化,不沿用原许可', async () => {
vi.spyOn(breakdownApi, 'candidateCommitPreview')
.mockResolvedValueOnce(commitPreview())
.mockResolvedValueOnce(relationPreview())
const commit = vi.spyOn(breakdownApi, 'commitCandidate')
openPanel()
await checkCandidate()
await confirmCandidate()
expect(commit).not.toHaveBeenCalled()
expect(getOperation('candidate-project').error).toContain('安全条件已变化')
})
it.each(['close', 'project'] as const)('读取期间 %s,晚到的响应不回填原抽屉', async action => {
let resolve!: (value: CandidateCommitPreview) => void
vi.spyOn(breakdownApi, 'candidateCommitPreview').mockImplementation(
() =>
new Promise(done => {
resolve = done
})
)
openPanel()
await checkCandidate()
if (action === 'close') await wrapper!.setProps({ open: false })
else await wrapper!.setProps({ projectId: 'other-project', disabled: true })
resolve(commitPreview())
await flushPromises()
expect(document.body.textContent).not.toContain('确认提交候选为正式结果')
})
})
describe('候选关系人工修复', () => {
it('真实写入期间按钮保留 loading,锁定关闭,连点不会重复移除', async () => {
vi.spyOn(breakdownApi, 'candidateCommitPreview').mockResolvedValue(relationPreview())
let finish!: (value: { checkpointId: string; subjectRef: string; removed: 1 }) => void
const repair = vi.spyOn(breakdownApi, 'repairCandidateRelation').mockImplementation(
() =>
new Promise(resolve => {
finish = resolve
})
)
openPanel()
await checkCandidate()
await selectRelation()
await drawer().get('.app-drawer-footer [role="checkbox"]').trigger('click')
await clickAction('确认移除这条关系')
await wrapper!.setProps({ disabled: true })
const submit = wrapper!.findAllComponents(NButton).find(button => button.text() === '确认移除这条关系')!
expect(submit.props('loading')).toBe(true)
expect(
drawer()
.findAll('button')
.find(button => button.text() === '关闭')!
.attributes('disabled')
).toBeDefined()
await submit.trigger('click')
expect(repair).toHaveBeenCalledOnce()
finish({ checkpointId: 'new-version', subjectRef: '@SC0002', removed: 1 })
await flushPromises()
})
it('完整展示原始关系,不自动选择、删除或提交', async () => {
vi.spyOn(breakdownApi, 'candidateCommitPreview').mockResolvedValue(relationPreview())
const repair = vi.spyOn(breakdownApi, 'repairCandidateRelation')
const commit = vi.spyOn(breakdownApi, 'commitCandidate')
openPanel()
await checkCandidate()
expect(drawer().text()).toContain('测试场景')
expect(drawer().text()).toContain('测试空地')
expect(drawer().text()).toContain('原始证据')
expect(
drawer()
.findAll<HTMLInputElement>('input[type="radio"]')
.every(item => !item.element.checked)
).toBe(true)
expect(repair).not.toHaveBeenCalled()
expect(commit).not.toHaveBeenCalled()
})
it('每次仅移除用户明确确认的一条原始关系,新版本到达后重新预检,不自动提交', async () => {
const preview = vi.spyOn(breakdownApi, 'candidateCommitPreview').mockResolvedValue(relationPreview())
const repair = vi.spyOn(breakdownApi, 'repairCandidateRelation').mockResolvedValue({
checkpointId: 'repaired-version',
subjectRef: '@SC0002',
removed: 1
})
const commit = vi.spyOn(breakdownApi, 'commitCandidate')
openPanel()
await checkCandidate()
await selectRelation()
expect(repair).not.toHaveBeenCalled()
const button = drawer()
.findAll('button')
.find(item => item.text() === '确认移除这条关系')!
expect(button.attributes('disabled')).toBeDefined()
await drawer().get('.app-drawer-footer [role="checkbox"]').trigger('click')
await button.trigger('click')
await button.trigger('click')
await flushPromises()
expect(repair).toHaveBeenCalledExactlyOnceWith('candidate-project', {
expectedCheckpointId: 'candidate-current',
confirmed: true,
subjectRef: '@SC0002',
relation: '位于',
target: '测试空地',
evidence: ' 原始证据 '
})
expect(wrapper!.emitted('refresh')).toHaveLength(1)
expect(commit).not.toHaveBeenCalled()
expect(drawer().text()).not.toContain('确认移除这条关系')
preview.mockResolvedValue(commitPreview({ checkpointId: 'repaired-version' }))
await wrapper!.setProps({
checkpointId: 'repaired-version',
quality: breakdownQuality(
selectBreakdownSource([candidateCheckpoint(candidateState(), 'repaired-version')]),
candidateReview({ checkpointId: 'repaired-version' })
)
})
await flushPromises()
expect(preview).toHaveBeenCalledTimes(3)
expect(drawer().text()).toContain('确认提交候选为正式结果')
expect(drawer().get('.app-drawer-footer [role="checkbox"]').attributes('aria-checked')).toBe('false')
expect(commit).not.toHaveBeenCalled()
})
it.each(['version', 'target', 'active'] as const)('提交前 %s 变化,拒绝移除并撤销旧确认', async change => {
const next = relationPreview()
if (change === 'version') next.checkpointId = 'other-version'
if (change === 'target') next.relationRepairTargets = []
if (change === 'active') next.checks.noActiveExecutions = false
vi.spyOn(breakdownApi, 'candidateCommitPreview')
.mockResolvedValueOnce(relationPreview())
.mockResolvedValue(next)
const repair = vi.spyOn(breakdownApi, 'repairCandidateRelation')
openPanel()
await checkCandidate()
await selectRelation()
await drawer().get('.app-drawer-footer [role="checkbox"]').trigger('click')
await clickAction('确认移除这条关系')
expect(repair).not.toHaveBeenCalled()
expect(drawer().text()).not.toContain('确认移除这条关系')
expect(wrapper!.emitted('refresh')).toHaveLength(1)
})
it('普通提交失败保留所选关系,清除勾选后允许用户修正重试', async () => {
vi.spyOn(breakdownApi, 'candidateCommitPreview').mockResolvedValue(relationPreview())
vi.spyOn(breakdownApi, 'repairCandidateRelation').mockRejectedValue(new Error('网络中断,请重新读取状态'))
openPanel()
await checkCandidate()
await selectRelation()
await drawer().get('.app-drawer-footer [role="checkbox"]').trigger('click')
await clickAction('确认移除这条关系')
expect(drawer().get<HTMLInputElement>('input[type="radio"]').element.checked).toBe(true)
expect(drawer().text()).toContain('网络中断')
expect(drawer().get('.app-drawer-footer [role="checkbox"]').attributes('aria-checked')).toBe('false')
})
it('完全相同的重复定位关系不能提交删除', async () => {
const result = relationPreview()
result.relationRepairTargets.push({ ...result.relationRepairTargets[0]! })
vi.spyOn(breakdownApi, 'candidateCommitPreview').mockResolvedValue(result)
const repair = vi.spyOn(breakdownApi, 'repairCandidateRelation')
openPanel()
await checkCandidate()
await selectRelation()
expect(drawer().text()).toContain('定位不唯一')
expect(drawer().text()).not.toContain('确认移除这条关系')
expect(repair).not.toHaveBeenCalled()
})
})