From a9370cdf50c59a5ab4e01137952361ec663317f0 Mon Sep 17 00:00:00 2001 From: GJ Date: Thu, 15 Oct 2026 18:10:19 +0800 Subject: [PATCH] =?UTF-8?q?fix(short-drama-agent-front):=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E6=B5=8B=E8=AF=95=E8=AD=A6=E5=91=8A=E4=B8=8E=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E6=A8=A1=E6=8B=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/components/ui/flatTheme.test.ts | 6 +++- tests/components/ui/form.test.ts | 7 ++++- tests/features/production/production.test.ts | 16 ++++++++-- tests/features/subject-images/prompts.test.ts | 2 +- tests/features/workflows/pages.test.ts | 18 ++++------- tests/fixtures/router.ts | 4 +++ tests/setup.ts | 30 +++++++++++++++++++ vitest.config.ts | 2 ++ 8 files changed, 67 insertions(+), 18 deletions(-) create mode 100644 tests/fixtures/router.ts create mode 100644 tests/setup.ts diff --git a/tests/components/ui/flatTheme.test.ts b/tests/components/ui/flatTheme.test.ts index add2b6d..fe2c0e5 100644 --- a/tests/components/ui/flatTheme.test.ts +++ b/tests/components/ui/flatTheme.test.ts @@ -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( diff --git a/tests/components/ui/form.test.ts b/tests/components/ui/form.test.ts index 49132dd..d69f51f 100644 --- a/tests/components/ui/form.test.ts +++ b/tests/components/ui/form.test.ts @@ -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('先保存视觉风格') diff --git a/tests/features/production/production.test.ts b/tests/features/production/production.test.ts index 43921b2..1b18e30 100644 --- a/tests/features/production/production.test.ts +++ b/tests/features/production/production.test.ts @@ -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().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 () => { diff --git a/tests/features/subject-images/prompts.test.ts b/tests/features/subject-images/prompts.test.ts index d4c365f..44e7680 100644 --- a/tests/features/subject-images/prompts.test.ts +++ b/tests/features/subject-images/prompts.test.ts @@ -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() diff --git a/tests/features/workflows/pages.test.ts b/tests/features/workflows/pages.test.ts index ac85184..2528101 100644 --- a/tests/features/workflows/pages.test.ts +++ b/tests/features/workflows/pages.test.ts @@ -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('内容级滚动下的生产反馈', () => { diff --git a/tests/fixtures/router.ts b/tests/fixtures/router.ts new file mode 100644 index 0000000..79b5e6b --- /dev/null +++ b/tests/fixtures/router.ts @@ -0,0 +1,4 @@ +import { RouterLinkStub } from '@vue/test-utils' + +/** 独立挂载页面和组件时保留链接插槽与目标参数,不依赖完整路由环境。 */ +export const routerLinkStubs = { RouterLink: RouterLinkStub } diff --git a/tests/setup.ts b/tests/setup.ts new file mode 100644 index 0000000..3df9717 --- /dev/null +++ b/tests/setup.ts @@ -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() +}) diff --git a/vitest.config.ts b/vitest.config.ts index 66b06b0..2fd15c5 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -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,