| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- import { describe, expect, it, vi } from "vitest";
- import {
- SIDEBAR_PURPOSE_SECTION_PREFERENCES_STORAGE_KEY,
- persistSidebarPurposeSectionPreferences,
- readSidebarPurposeSectionPreferences,
- reorderSidebarPurposeSections,
- resolveSidebarPurposeSectionExpanded,
- updateSidebarPurposeSectionExpanded,
- } 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("isolates purpose section expansion by source group", () => {
- const initial = readSidebarPurposeSectionPreferences(createStorageMock());
- const next = updateSidebarPurposeSectionExpanded(initial, {
- sectionId: "projects",
- expanded: false,
- sourceGroupKey: "server:source-a",
- });
- expect(
- resolveSidebarPurposeSectionExpanded(next, "projects", "server:source-a"),
- ).toBe(false);
- expect(
- resolveSidebarPurposeSectionExpanded(next, "projects", "server:source-b"),
- ).toBe(true);
- expect(
- resolveSidebarPurposeSectionExpanded(
- next,
- "conversations",
- "server:source-a",
- ),
- ).toBe(true);
- });
- it("persists source-group expansion without changing the legacy fallback", () => {
- const storage = createStorageMock();
- const next = updateSidebarPurposeSectionExpanded(
- readSidebarPurposeSectionPreferences(storage),
- {
- sectionId: "conversations",
- expanded: false,
- sourceGroupKey: "local",
- },
- );
- persistSidebarPurposeSectionPreferences(next, storage);
- const restored = readSidebarPurposeSectionPreferences(storage);
- expect(restored.projectsExpanded).toBe(true);
- expect(restored.conversationsExpanded).toBe(true);
- expect(
- resolveSidebarPurposeSectionExpanded(restored, "conversations", "local"),
- ).toBe(false);
- expect(
- resolveSidebarPurposeSectionExpanded(
- restored,
- "conversations",
- "server:source-a",
- ),
- ).toBe(true);
- });
- 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();
- });
- });
|