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, 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("7 组 18 项(另加系统组的定时任务),路径唯一且都在 /console 下", () => { expect(NAV_GROUPS.map((g) => g.title)).toEqual(["概览", "资源", "订阅", "下载", "整理", "Agent", "系统"]); const core = NAV_ITEMS.filter((i) => i.to !== "/console/schedules"); expect(core).toHaveLength(18); 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); for (const g of NAV_GROUPS) expect(g.items.length).toBeLessThanOrEqual(4); }); 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/engine/r1")?.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/tasks/abc")?.label).toBe("业务任务"); expect(findNavItem("/console/sites/abc")?.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").startsWith("/console/settings")).toBe(true); expect(settingsPath("agent").startsWith("/console/settings")).toBe(true); }); }); 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)).toBeNull(); } }); it("不相关的 query 不跳转", () => { expect(legacyRedirect("/console/tasks", "?kind=submitDownload")).toBeNull(); expect(legacyRedirect("/console/subscriptions", "")).toBeNull(); expect(legacyRedirect("/console/sources", "?health=failing")).toBeNull(); }); });