fix(short-drama-agent-front): 修复测试警告与请求模拟

This commit is contained in:
GJ committed 2026-10-15 18:10:19 +08:00
1 parent 46f54548c7
commit a9370cdf50
8 files changed
+67 -18

No files matched your search

+5 -1
View File
@@ -1,4 +1,5 @@
import { h } from 'vue'
import { routerLinkStubs } from '../../fixtures/router'
import { mount, type VueWrapper } from '@vue/test-utils'
import { afterEach, describe, expect, it, vi } from 'vitest'
import {
@@ -280,7 +281,10 @@ describe('全站平面主题', () => {
it('风格参考图页内只保留添加入口,缩略图在宽屏下保持紧凑', () => {
const css = readAllStyles()
wrapper = mount(StyleImages, { props: { projectId: 'theme-test-project', images: [], disabled: false } })
wrapper = mount(StyleImages, {
global: { stubs: routerLinkStubs },
props: { projectId: 'theme-test-project', images: [], disabled: false }
})
expect(wrapper.find('form').exists()).toBe(false)
expect(wrapper.findAll('button').map(button => button.text())).toEqual(['从资产库添加', '通过链接添加'])
expect(css).toMatch(
+6 -1
View File
@@ -1,4 +1,5 @@
import { imageCatalog, generationProfile } from '../../fixtures/generation'
import { routerLinkStubs } from '../../fixtures/router'
import { defineComponent, h, reactive } from 'vue'
import { flushPromises, mount, type VueWrapper } from '@vue/test-utils'
import { afterEach, describe, expect, it, vi } from 'vitest'
@@ -99,7 +100,8 @@ describe('统一的 Naive UI 表单校验', () => {
title: '雨夜来信标题',
topic: '雨夜来信',
style: '',
episodeCount: 3
/** 与后端 Auto 时长预算契约一致,不再发送旧的顶层集数。 */
durationBudget: { mode: 'auto', episodeCount: 3 }
})
expect(wrapper.emitted('created')).toEqual([['created', 'create-execution-1']])
})
@@ -147,6 +149,7 @@ describe('统一的 Naive UI 表单校验', () => {
it('登记参考图拦截非法地址和小数排序,允许有效存储地址与负整数排序', async () => {
wrapper = mount(StyleImages, {
attachTo: document.body,
global: { stubs: routerLinkStubs },
props: { projectId: 'form-test-project', images: [], disabled: false }
})
expect(document.querySelector('[aria-label="风格图片地址"]')).toBeNull()
@@ -191,6 +194,7 @@ describe('统一的 Naive UI 表单校验', () => {
vi.stubGlobal('fetch', fetcher)
wrapper = mount(StyleImages, {
attachTo: document.body,
global: { stubs: routerLinkStubs },
props: { projectId: 'form-test-project', images: [], disabled: false }
})
@@ -227,6 +231,7 @@ describe('统一的 Naive UI 表单校验', () => {
vi.stubGlobal('fetch', fetcher)
wrapper = mount(StyleImages, {
attachTo: document.body,
global: { stubs: routerLinkStubs },
props: { projectId: 'form-test-project', images: [], disabled: true, hasStyle: false }
})
expect(wrapper.text()).toContain('先保存视觉风格')
+14 -2
View File
@@ -1,4 +1,5 @@
import { flushPromises, mount, type VueWrapper } from '@vue/test-utils'
import { generationProfile, imageCatalog } from '../../fixtures/generation'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { readFileSync } from 'node:fs'
import { mediaAssetUrl } from '@/lib/assets'
@@ -197,6 +198,15 @@ describe('镜头生产数据契约', () => {
})
it('所有首帧生成都先成为候选,并要求费用确认', async () => {
/** 弹窗打开时只读取模型目录和项目配置,测试不依赖真实后端。 */
const fetcher = vi.fn<typeof fetch>().mockImplementation(async url => {
const path = String(url)
if (path === '/api/generation-models') return new Response(JSON.stringify({ data: imageCatalog }))
if (path === '/api/projects/project-1/creative-profile')
return new Response(JSON.stringify({ data: generationProfile('project-1') }))
throw new Error(`首帧弹窗出现未预期请求:${path}`)
})
vi.stubGlobal('fetch', fetcher)
wrapper = mount(KeyframeDialog, {
attachTo: document.body,
props: { open: true, shotId: 'shot-1', shotTitle: '开场', projectId: 'project-1', disabled: false }
@@ -208,7 +218,7 @@ describe('镜头生产数据契约', () => {
button('确认生成首帧').click()
await flushPromises()
expect(wrapper.emitted('generate')).toEqual([
[{ provider: undefined, generationOptions: {}, setPrimary: false }]
[{ provider: 'qwen-image', generationOptions: { size: '2048*2048' }, setPrimary: false }]
])
await wrapper.setProps({ open: false })
@@ -221,8 +231,10 @@ describe('镜头生产数据契约', () => {
button('确认生成首帧').click()
await flushPromises()
expect(wrapper.emitted('generate')?.at(-1)).toEqual([
{ provider: undefined, generationOptions: {}, setPrimary: false }
{ provider: 'qwen-image', generationOptions: { size: '2048*2048' }, setPrimary: false }
])
expect(fetcher).toHaveBeenCalledTimes(4)
expect(fetcher.mock.calls.every(([, init]) => init?.method === 'GET')).toBe(true)
})
it('首帧生成前可预览后端最终选图规划和 Prompt', async () => {
@@ -167,7 +167,7 @@ describe('形态正式提示词与批量配置', () => {
const form = formFixture()
wrapper = mount(FormPromptDialog, {
attachTo: document.body,
props: { open: true, disabled: false, form, catalog: imageCatalog, profile: creativeProfile }
props: { open: true, disabled: false, form }
})
await flushPromises()
find('生成正式提示词').click()
+5 -13
View File
@@ -1,9 +1,10 @@
import { generationProfile, imageCatalog } from '../../fixtures/generation'
import { routerLinkStubs } from '../../fixtures/router'
import { computed, ref } from 'vue'
import { NImage, NRadioGroup, NScrollbar } from 'naive-ui'
import { createMemoryHistory, createRouter, type RouteRecordRaw } from 'vue-router'
import { DOMWrapper, flushPromises, mount, RouterLinkStub, type VueWrapper } from '@vue/test-utils'
import { afterEach, beforeEach, describe, expect, it, vi, type Mock } from 'vitest'
import { DOMWrapper, flushPromises, mount, type VueWrapper } from '@vue/test-utils'
import { afterEach, describe, expect, it, vi, type Mock } from 'vitest'
import CreateProjectDialog from '@/features/projects/components/CreateProjectDialog.vue'
import BreakdownPage from '@/features/breakdown/BreakdownPage.vue'
import CreateDramaPage from '@/features/create-drama/CreateDramaPage.vue'
@@ -38,9 +39,6 @@ import { drawerPanel, expandSections, selectControl } from '@/testing/naive'
import ProductionPage from '@/features/production/ProductionPage.vue'
import { getProductionSession } from '@/features/production/model'
/** 单独挂载的页面和组件只替换导航链接,保留链接插槽和目标参数。 */
const routerLinkStubs = { RouterLink: RouterLinkStub }
/**
* 为内存路由补齐页面链接使用的正式项目工作区,避免链接解析时缺少目标路由。
* @param routes 当前测试页面的入口路由
@@ -259,13 +257,8 @@ async function mountSubjectImages() {
await expandSections()
}
/** 保留警告的原始输出,并在每例结束时检查页面挂载和清理过程是否出现警告。 */
beforeEach(() => {
vi.spyOn(console, 'warn')
})
/** 释放页面与会话状态,并检查当前用例没有产生 Vue 或路由警告。 */
afterEach(testContext => {
/** 释放页面与会话状态;警告和未模拟请求由全局测试配置统一检查。 */
afterEach(() => {
getProductionSession(fixture.id).receipt = null
getProductionSession(fixture.id).pipelineReceipt = null
getIdentitySession(fixture.id).receipt = null
@@ -278,7 +271,6 @@ afterEach(testContext => {
getImageSession(fixture.id).receipt = null
getImageSession(fixture.id).promptReceipt = null
Object.assign(getOperation(fixture.id), { pending: false, label: '', error: '', notice: '', noticeType: null })
testContext.expect(console.warn).not.toHaveBeenCalled()
})
describe('内容级滚动下的生产反馈', () => {
+4
View File
@@ -0,0 +1,4 @@
import { RouterLinkStub } from '@vue/test-utils'
/** 独立挂载页面和组件时保留链接插槽与目标参数,不依赖完整路由环境。 */
export const routerLinkStubs = { RouterLink: RouterLinkStub }
+30
View File
@@ -0,0 +1,30 @@
import { afterEach, beforeEach, vi } from 'vitest'
/** 保留原始警告输出,避免把测试失败原因静默屏蔽。 */
const originalWarn = console.warn.bind(console)
/** 当前用例产生的警告,独立保存以免组件测试恢复 mock 时清空记录。 */
let warnings: unknown[][] = []
/** 当前用例漏掉模拟的请求,禁止自动连接本地或外部真实服务。 */
let unmockedRequests: string[] = []
/** 为每个用例收集警告并阻止未显式模拟的 API 请求。 */
beforeEach(() => {
warnings = []
unmockedRequests = []
vi.spyOn(console, 'warn').mockImplementation((...args: unknown[]) => {
warnings.push(args)
originalWarn(...args)
})
vi.stubGlobal('fetch', async (input: RequestInfo | URL) => {
const url = input instanceof Request ? input.url : String(input)
unmockedRequests.push(url)
throw new Error(`测试请求未模拟:${url}`)
})
})
/** 全量测试共同检查挂载警告和请求隔离,不允许测试通过时仍然输出异常。 */
afterEach(testContext => {
testContext.expect(warnings, '测试不应产生 Vue 或路由等警告').toEqual([])
testContext.expect(unmockedRequests, '测试必须显式模拟全部 API 请求').toEqual([])
vi.unstubAllGlobals()
})
+2
View File
@@ -8,6 +8,8 @@ export default defineConfig({
resolve: { alias: { '@': fileURLToPath(new URL('./src', import.meta.url)) } },
test: {
environment: 'happy-dom',
/** 全量测试统一检测警告,并阻止遗漏模拟的请求连接真实服务。 */
setupFiles: ['./tests/setup.ts'],
restoreMocks: true,
/** 限制同时挂载的页面测试数量,避免 Naive UI 渲染争抢资源造成超时。 */
maxWorkers: 2,