| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- import { describe, expect, it, vi } from "vitest";
- import {
- SIDEBAR_PURPOSE_SECTION_PREFERENCES_STORAGE_KEY,
- persistSidebarPurposeSectionPreferences,
- readSidebarPurposeSectionPreferences,
- reorderSidebarPurposeSections,
- } from "@/lib/sidebarPurposeSectionPreferences.js";
- function createStorageMock(initial: Record<string, string> = {}) {
- const storage = new Map(Object.entries(initial));
- return {
- getItem(key: string) {
- return storage.get(key) ?? null;
- },
- setItem(key: string, value: string) {
- storage.set(key, value);
- },
- };
- }
- describe("sidebarPurposeSectionPreferences", () => {
- it("defaults both purpose sections to expanded", () => {
- expect(readSidebarPurposeSectionPreferences(createStorageMock())).toEqual({
- projectsExpanded: true,
- conversationsExpanded: true,
- sectionOrder: ["projects", "conversations"],
- });
- });
- it("persists and restores each purpose section independently", () => {
- const storage = createStorageMock();
- persistSidebarPurposeSectionPreferences(
- {
- projectsExpanded: false,
- conversationsExpanded: true,
- sectionOrder: ["conversations", "projects"],
- },
- storage,
- );
- expect(storage.getItem(SIDEBAR_PURPOSE_SECTION_PREFERENCES_STORAGE_KEY)).toBe(
- JSON.stringify({
- projectsExpanded: false,
- conversationsExpanded: true,
- sectionOrder: ["conversations", "projects"],
- }),
- );
- expect(readSidebarPurposeSectionPreferences(storage)).toEqual({
- projectsExpanded: false,
- conversationsExpanded: true,
- sectionOrder: ["conversations", "projects"],
- });
- });
- it("falls back missing or invalid fields independently", () => {
- const storage = createStorageMock({
- [SIDEBAR_PURPOSE_SECTION_PREFERENCES_STORAGE_KEY]: JSON.stringify({
- projectsExpanded: false,
- conversationsExpanded: "no",
- }),
- });
- expect(readSidebarPurposeSectionPreferences(storage)).toEqual({
- projectsExpanded: false,
- conversationsExpanded: true,
- sectionOrder: ["projects", "conversations"],
- });
- storage.setItem(
- SIDEBAR_PURPOSE_SECTION_PREFERENCES_STORAGE_KEY,
- JSON.stringify({ conversationsExpanded: false }),
- );
- expect(readSidebarPurposeSectionPreferences(storage)).toEqual({
- projectsExpanded: true,
- conversationsExpanded: false,
- sectionOrder: ["projects", "conversations"],
- });
- });
- it("falls back to the default order for duplicate or unknown section ids", () => {
- const storage = createStorageMock({
- [SIDEBAR_PURPOSE_SECTION_PREFERENCES_STORAGE_KEY]: JSON.stringify({
- sectionOrder: ["conversations", "conversations"],
- }),
- });
- expect(readSidebarPurposeSectionPreferences(storage).sectionOrder).toEqual([
- "projects",
- "conversations",
- ]);
- storage.setItem(
- SIDEBAR_PURPOSE_SECTION_PREFERENCES_STORAGE_KEY,
- JSON.stringify({ sectionOrder: ["conversations", "unknown"] }),
- );
- expect(readSidebarPurposeSectionPreferences(storage).sectionOrder).toEqual([
- "projects",
- "conversations",
- ]);
- });
- it("reorders the whole purpose section without changing its identity", () => {
- expect(
- reorderSidebarPurposeSections(["projects", "conversations"], "conversations", "projects"),
- ).toEqual(["conversations", "projects"]);
- expect(
- reorderSidebarPurposeSections(["conversations", "projects"], "projects", "conversations"),
- ).toEqual(["projects", "conversations"]);
- });
- it("falls back to defaults for invalid JSON", () => {
- const storage = createStorageMock({
- [SIDEBAR_PURPOSE_SECTION_PREFERENCES_STORAGE_KEY]: "{",
- });
- expect(readSidebarPurposeSectionPreferences(storage)).toEqual({
- projectsExpanded: true,
- conversationsExpanded: true,
- sectionOrder: ["projects", "conversations"],
- });
- });
- it("falls back safely when storage cannot be read", () => {
- const storage = {
- getItem: vi.fn(() => {
- throw new Error("storage unavailable");
- }),
- setItem: vi.fn(),
- };
- expect(readSidebarPurposeSectionPreferences(storage)).toEqual({
- projectsExpanded: true,
- conversationsExpanded: true,
- sectionOrder: ["projects", "conversations"],
- });
- });
- it("does not throw when storage cannot be written", () => {
- const storage = {
- getItem: vi.fn(() => null),
- setItem: vi.fn(() => {
- throw new Error("storage unavailable");
- }),
- };
- expect(() =>
- persistSidebarPurposeSectionPreferences(
- {
- projectsExpanded: false,
- conversationsExpanded: false,
- sectionOrder: ["conversations", "projects"],
- },
- storage,
- ),
- ).not.toThrow();
- });
- });
|