Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Cxwgnn2pbzQ7rXv3rq7fei
124 lines
5.4 KiB
TypeScript
124 lines
5.4 KiB
TypeScript
import { readFileSync } from "node:fs";
|
|
import { describe, expect, it } from "vitest";
|
|
import { LEGACY_REDIRECTS, legacyRedirect } from "../../routes/console/redirects.ts";
|
|
import {
|
|
ACQUIRE_GROUP_TITLE,
|
|
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-2:4 组,站点 / 订阅 / 规则合为「获取」,没有独立 RSS 项;系统组只有日志、运行状态、设置", () => {
|
|
expect(NAV_GROUPS.map((g) => g.title)).toEqual(["概览", ACQUIRE_GROUP_TITLE, "整理", "系统"]);
|
|
expect(ACQUIRE_GROUP_TITLE).toBe("获取");
|
|
expect(NAV_GROUPS.map((g) => g.items.map((i) => i.label))).toEqual([
|
|
["首页", "智能体会话"],
|
|
["站点", "发现", "订阅", "规则"],
|
|
["下载面板", "整理记录", "文件管理", "媒体库"],
|
|
["日志", "运行状态", "设置"],
|
|
]);
|
|
expect(NAV_ITEMS).toHaveLength(13);
|
|
expect(NAV_ITEMS.map((i) => i.to)).not.toContain("/console/sources");
|
|
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",
|
|
"/console/tasks",
|
|
"/console/audit",
|
|
"/console/schedules",
|
|
])
|
|
expect(paths).not.toContain(p);
|
|
expect(NAV_ITEMS.map((i) => i.label)).not.toContain("待处理");
|
|
expect(pageTitle("/console/inbox")).toBeUndefined();
|
|
});
|
|
|
|
it("旧待处理链接按问题类型落到对应页面", () => {
|
|
expect(legacyRedirect("/console/inbox", "?kind=path_conflict")).toBe("/console/organize?tab=records&attention=1");
|
|
expect(legacyRedirect("/console/inbox", "?kind=download_failed")).toBe("/console/downloads");
|
|
expect(legacyRedirect("/console/inbox", "?kind=space_insufficient")).toBe("/console/status#attention");
|
|
expect(legacyRedirect("/console/inbox", "?kind=unknown_kind")).toBe("/console/organize?tab=records&attention=1");
|
|
});
|
|
|
|
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(pageTitle("/console/tasks/r1")).toBe("日志");
|
|
expect(findNavItem("/console/logs")?.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("downloaders", "cache")).toBe("/console/settings#downloaders/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("任务 / 审计 / 计划的旧地址一律落到日志页;任务详情独立页不跳", () => {
|
|
expect(legacyRedirect("/console/tasks", "?kind=submitDownload")).toBe("/console/logs?type=task");
|
|
expect(legacyRedirect("/console/tasks", "?tab=failed")).toBe("/console/logs?type=task&level=error");
|
|
expect(legacyRedirect("/console/tasks/r1", "")).toBeNull();
|
|
expect(legacyRedirect("/console/logs", "?type=task")).toBeNull();
|
|
});
|
|
|
|
it("不相关的 query 不跳转", () => {
|
|
expect(legacyRedirect("/console/library", "?filter=unidentified")).toBeNull();
|
|
expect(legacyRedirect("/console/subscriptions", "")).toBeNull();
|
|
// 旧 RSS 页的 ?feed= 由 sources.tsx 查所属站点后跳转
|
|
expect(legacyRedirect("/console/sources", "?feed=f1")).toBeNull();
|
|
});
|
|
});
|