import { readFileSync } from "node:fs"; import { describe, expect, it } from "vitest"; import { LEGACY_REDIRECTS, legacyRedirect } from "../../routes/console/redirects.ts"; import { consolePath, findNavItem, NAV_GROUPS, NAV_ITEMS, pageTitle, settingsPath } from "./nav.ts"; /** routes.ts 里登记的控制台路由(相对 /console) */ const ROUTE_PATTERNS = [ "", ...[...readFileSync(new URL("../../routes.ts", import.meta.url), "utf8").matchAll(/route\("([^"*]+)",/g)].map( (m) => m[1] as string, ), ]; const matchesRoute = (pathname: string) => { const rest = pathname.replace(/^\/console\/?/, ""); return ROUTE_PATTERNS.some((p) => new RegExp(`^${p.replace(/:[^/]+/g, "[^/]+")}$`).test(rest)); }; describe("控制台导航", () => { it("IA-3:5 组,按定稿清单逐项列出,路径唯一且都在 /console 下", () => { expect(NAV_GROUPS.map((g) => g.title)).toEqual(["概览", "资源", "订阅", "整理", "系统"]); expect(NAV_GROUPS.map((g) => g.items.map((i) => i.label))).toEqual([ ["首页", "任务", "计划", "Agent 会话"], ["站点", "RSS 订阅"], ["订阅", "规则"], ["下载面板", "整理记录", "文件管理", "媒体概览"], ["设置", "审计", "运行状态"], ]); // IA-3 逐项清单 14 项 + FE-6 计划页 expect(NAV_ITEMS).toHaveLength(15); expect(new Set(NAV_ITEMS.map((i) => i.to)).size).toBe(NAV_ITEMS.length); expect(NAV_ITEMS.every((i) => i.to.startsWith("/console"))).toBe(true); }); it("取消的入口不在侧栏:待处理走顶栏收件箱,引擎面板 / 身份并入别处", () => { const paths = NAV_ITEMS.map((i) => i.to); for (const p of ["/console/inbox", "/console/engine", "/console/identity"]) expect(paths).not.toContain(p); expect(pageTitle("/console/inbox")).toBe("待处理"); }); it("每个导航项都有路由", () => { for (const i of NAV_ITEMS) expect(matchesRoute(i.to), i.to).toBe(true); }); it("按最长前缀匹配当前项", () => { expect(findNavItem("/console")?.label).toBe("首页"); expect(findNavItem("/console/")?.label).toBe("首页"); expect(findNavItem("/console/library/abc")?.label).toBe("媒体概览"); expect(findNavItem("/console/tasks/r1")?.label).toBe("任务"); expect(findNavItem("/console/schedules")?.label).toBe("计划"); expect(findNavItem("/console/downloads")?.label).toBe("下载面板"); expect(findNavItem("/console/downloads/plans")?.label).toBe("下载面板"); expect(findNavItem("/console/downloads/abc")?.label).toBe("下载面板"); expect(findNavItem("/console/sites/abc")?.label).toBe("站点"); expect(findNavItem("/console/status")?.label).toBe("运行状态"); expect(findNavItem("/nope")).toBeUndefined(); }); it("consolePath 拼路径并编码", () => { expect(consolePath()).toBe("/console"); expect(consolePath("library", "a b")).toBe("/console/library/a%20b"); expect(consolePath("downloads", "plans")).toBe("/console/downloads/plans"); }); it("settingsPath 只认新分区名", () => { expect(settingsPath("automation", "cache")).toBe("/console/settings#automation/cache"); expect(settingsPath("agent")).toBe("/console/settings#agent"); }); }); describe("旧链接重定向", () => { it.each(LEGACY_REDIRECTS)("$from → $to", ({ from, to }) => { const u = new URL(from, "http://x"); expect(legacyRedirect(u.pathname, u.search)).toBe(to); }); it("新地址都有路由,且不会再次跳转", () => { for (const { to } of LEGACY_REDIRECTS) { const u = new URL(to, "http://x"); expect(matchesRoute(u.pathname), to).toBe(true); if (u.pathname !== "/console/sites/abc") expect(legacyRedirect(u.pathname, u.search), to).toBeNull(); } }); it("不相关的 query 不跳转", () => { expect(legacyRedirect("/console/tasks", "?kind=submitDownload")).toBeNull(); expect(legacyRedirect("/console/schedules", "?job=trash.purge")).toBeNull(); expect(legacyRedirect("/console/tasks", "?tab=failed")).toBeNull(); expect(legacyRedirect("/console/library", "?filter=unidentified")).toBeNull(); expect(legacyRedirect("/console/subscriptions", "")).toBeNull(); expect(legacyRedirect("/console/sources", "?health=failing")).toBeNull(); }); });