Files
Alfred/tests/web/hooks/use-sidebar-collapsed.test.ts
lanyuanxiaoyao b1dec691e9 refactor(web): 前端目录重构 — consoles/pages → layouts/features + shared
- consoles/admin/ → layouts/admin-layout/
- consoles/workbench/ → layouts/workbench-layout/ + features/chat/
- pages/ → features/ (dashboard, models, projects, not-found)
- components/ → shared/components/
- hooks/ → shared/hooks/
- utils/ → shared/utils/
- 更新所有 import 路径 (src/web/ + tests/web/)
- 更新开发文档 (README.md, frontend.md, architecture.md)
2026-06-02 23:17:28 +08:00

55 lines
1.7 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import {
parseSidebarCollapsed,
readSidebarCollapsed,
SIDEBAR_COLLAPSED_STORAGE_KEY,
writeSidebarCollapsed,
} from "../../../src/web/shared/hooks/use-sidebar-collapsed";
function createStorage(initial?: string): Storage {
const values = new Map<string, string>();
if (initial !== undefined) values.set(SIDEBAR_COLLAPSED_STORAGE_KEY, initial);
return {
clear: () => values.clear(),
getItem: (key) => values.get(key) ?? null,
key: (index) => Array.from(values.keys())[index] ?? null,
get length() {
return values.size;
},
removeItem: (key) => values.delete(key),
setItem: (key, value) => values.set(key, value),
};
}
describe("sidebar collapsed 纯逻辑", () => {
test("仅字符串 true 解析为折叠", () => {
expect(parseSidebarCollapsed("true")).toBe(true);
expect(parseSidebarCollapsed("false")).toBe(false);
expect(parseSidebarCollapsed(true)).toBe(false);
expect(parseSidebarCollapsed(null)).toBe(false);
});
test("读取和写入 localStorage", () => {
const storage = createStorage("true");
expect(readSidebarCollapsed(storage)).toBe(true);
writeSidebarCollapsed(false, storage);
expect(storage.getItem(SIDEBAR_COLLAPSED_STORAGE_KEY)).toBe("false");
});
test("storage 异常时回退且不抛错", () => {
const brokenStorage = {
getItem: () => {
throw new Error("blocked");
},
setItem: () => {
throw new Error("blocked");
},
} as unknown as Storage;
expect(readSidebarCollapsed(brokenStorage)).toBe(false);
expect(() => writeSidebarCollapsed(true, brokenStorage)).not.toThrow();
});
});