/** 页面地址和登录深链回归,不访问真实账号或后端。 */ import test from "node:test"; import assert from "node:assert/strict"; import { digitalTabs, validDigitalId, chatPath, contentPath, safeDigitalReturnTo, digitalLoginPath, expiredDigitalSessionPath, } from "../src/lib/digital-routes.ts"; import { logoutRedirect } from "../src/lib/auth-redirect.ts"; test("主要页面与资源详情都有独立地址,身份页不覆盖旧工作台", () => { assert.equal(new Set(Object.values(digitalTabs)).size, 4); assert.equal(digitalTabs.me, "/profile"); assert.equal(chatPath("chat-1"), "/chats/chat-1"); assert.equal( contentPath({ id: "role-1", kind: "character" }), "/characters/role-1", ); assert.equal( contentPath({ id: "book-1", kind: "worldbook" }), "/library/book-1", ); assert.equal( contentPath({ id: "style-1", kind: "preset" }), "/library/style-1", ); }); test("深链格式遵循后端资源 ID 边界", () => { for (const id of ["chat-1", "a_b", "a".repeat(64)]) assert.equal(validDigitalId(id), true); for (const id of ["", "a/b", "../other", "a?b", "a".repeat(65)]) assert.equal(validDigitalId(id), false); }); test("登录恢复完整会话地址和发现页筛选,不增加固定首页跳转", () => { for (const path of [ "/chats/chat-1", "/characters/role-1", "/library/book-1", "/profile", "/discover?q=hello&favorites=1", ]) { assert.equal(safeDigitalReturnTo(path), path); const login = new URL(digitalLoginPath(path), "https://stellar.invalid"); assert.equal(login.pathname, "/login"); assert.equal(login.searchParams.get("next"), path); const expired = new URL( expiredDigitalSessionPath(path), "https://stellar.invalid", ); assert.equal(expired.pathname, "/api/auth/logout"); assert.equal( logoutRedirect(expired.searchParams.get("next")).headers.get("location"), digitalLoginPath(path), ); } }); test("拒绝外站跳转、登录循环、接口及非法资源路径", () => { for (const path of [ undefined, null, "https://outside.invalid", "//outside.invalid", "/\\outside.invalid", "/login", "/api/auth/logout", "/chats/one/other", "/chats/a%2fb", "/characters/", "/.//outside.invalid", "/\n/outside.invalid", ]) assert.equal(safeDigitalReturnTo(path), "/discover", String(path)); });