import { createApp, effectScope, type EffectScope } from 'vue' import { createMemoryHistory, createRouter } from 'vue-router' import { afterEach, describe, expect, it } from 'vitest' import { useShotSelection } from '../../../src/features/storyboard/useShotSelection' /** 测试作用域在每例结束后释放地址监听。 */ let scope: EffectScope | undefined afterEach(() => scope?.stop()) /** * 使用真实路由和注入环境创建镜头选择状态。 * @param url 初始页面地址 * @returns 地址同步状态与真实路由 */ async function selection(url: string) { const router = createRouter({ history: createMemoryHistory(), routes: [{ path: '/projects/:projectId/:section', component: {} }] }) await router.push(url) const app = createApp({}) app.use(router) scope = effectScope() const state = app.runWithContext(() => scope!.run(() => useShotSelection(() => 2))!) return { router, state } } /** 等待 replace 导航完成,不替换路由实现。 */ async function settled() { await new Promise(resolve => setTimeout(resolve, 0)) } describe('镜头地址选择', () => { it('刷新后的分镜和生产地址恢复剧集与正式镜头', async () => { for (const section of ['storyboard', 'production']) { scope?.stop() const { state } = await selection(`/projects/p1/${section}?episodeNo=2&shotId=shot-9`) expect(state.selectedEpisode.value).toBe(2) expect(state.selectedShot.value).toBe('shot-9') } }) it('选择镜头记录当前剧集并保留来源动作及其它参数', async () => { const { router, state } = await selection('/projects/p1/production?action=keyframe&filter=ready') state.selectedShot.value = 'shot-8' await settled() expect(router.currentRoute.value.query).toEqual({ action: 'keyframe', filter: 'ready', episodeNo: '2', shotId: 'shot-8' }) }) it('切换剧集清除上集镜头且保留其它参数', async () => { const { router, state } = await selection('/projects/p1/storyboard?episodeNo=2&shotId=old-shot&filter=ready') state.selectedEpisode.value = 3 await settled() expect(router.currentRoute.value.query).toEqual({ episodeNo: '3', filter: 'ready' }) expect(state.selectedShot.value).toBe('') }) it('浏览器导航同步目标,拒绝无效或多值剧集', async () => { const { router, state } = await selection('/projects/p1/storyboard?episodeNo=2&shotId=shot-9') await router.push('/projects/p1/storyboard?episodeNo=3&shotId=shot-11') expect(state.selectedEpisode.value).toBe(3) expect(state.selectedShot.value).toBe('shot-11') await router.push('/projects/p1/storyboard?episodeNo=-1&shotId=missing') expect(state.selectedEpisode.value).toBeUndefined() expect(state.selectedShot.value).toBe('missing') await router.push('/projects/p1/storyboard?episodeNo=2&episodeNo=3') expect(state.selectedEpisode.value).toBeUndefined() }) })