import { describe, expect, test } from "bun:test"; import { parseSidebarCollapsed, readSidebarCollapsed, SIDEBAR_COLLAPSED_STORAGE_KEY, writeSidebarCollapsed, } from "../../../src/web/hooks/use-sidebar-collapsed"; function createStorage(initial?: string): Storage { const values = new Map(); 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(); }); });