import { describe, expect, it } from 'vitest'; // navigation.ts 在模块加载时读 location/window,这里只测纯函数,用最小的桩 Object.assign(globalThis, { location: { pathname: '/', origin: 'http://x' }, window: { addEventListener: () => {} }, history: { state: null }, }); const { parseRoute } = await import('./navigation'); describe('parseRoute', () => { it('根路径是首页', () => expect(parseRoute('/')).toEqual({ name: 'home' })); it('/c/:id 是频道', () => { expect(parseRoute('/c/k3Fq9xWm2LpA7sTdQ8vZ1n')).toEqual({ name: 'channel', id: 'k3Fq9xWm2LpA7sTdQ8vZ1n' }); }); it('/s/:id 是便捷分享', () => { expect(parseRoute('/s/k3Fq9xWm2LpA7sTdQ8vZ1n')).toEqual({ name: 'share', id: 'k3Fq9xWm2LpA7sTdQ8vZ1n' }); }); it('id 格式不对回到首页', () => expect(parseRoute('/c/short')).toEqual({ name: 'home' })); it('未知路径回到首页', () => expect(parseRoute('/other')).toEqual({ name: 'home' })); });