import { describe, expect, it } from 'vitest'; import type { DockTab } from '../../store/app/appTypes'; import { MAX_DOCK_TABS, upsertDockTab } from './dockTabs'; const tab = (key: string, label = key): DockTab => ({ key, label }); describe('upsertDockTab', () => { it('追加新页签', () => { expect(upsertDockTab([], '/students', '学生管理')).toEqual([tab('/students', '学生管理')]); }); it('标题未变时保持原引用,避免无谓渲染', () => { const tabs = [tab('/students', '学生管理')]; expect(upsertDockTab(tabs, '/students', '学生管理')).toBe(tabs); }); it('菜单标题变化时更新页签标题', () => { const tabs = [tab('/students', '学生管理')]; expect(upsertDockTab(tabs, '/students', '学生档案')).toEqual([tab('/students', '学生档案')]); }); it('超过上限时保留当前页签 + 最新页签(LRU 淘汰最旧)', () => { const tabs = Array.from({ length: MAX_DOCK_TABS }, (_, i) => tab(`/p${i + 1}`)); const next = upsertDockTab(tabs, '/new', '新页'); expect(next).toHaveLength(MAX_DOCK_TABS); expect(next[0]).toEqual(tab('/new', '新页')); expect(next[next.length - 1]).toEqual(tab(`/p${MAX_DOCK_TABS}`)); expect(next.some((t) => t.key === '/p1')).toBe(false); }); it('恢复持久化的超量页签时压缩到上限,并保留当前页', () => { const tabs = Array.from({ length: 25 }, (_, i) => tab(`/p${i + 1}`)); tabs.push(tab('/wallets', '学生余额')); const next = upsertDockTab(tabs, '/wallets', '学生余额'); expect(next).toHaveLength(MAX_DOCK_TABS); expect(next[0]).toEqual(tab('/wallets', '学生余额')); expect(next.some((t) => t.key === '/p1')).toBe(false); expect(next.some((t) => t.key === '/p6')).toBe(false); expect(next.some((t) => t.key === '/p7')).toBe(true); }); });