sidebarPurposeSectionPreferences.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. SIDEBAR_PURPOSE_SECTION_PREFERENCES_STORAGE_KEY,
  4. persistSidebarPurposeSectionPreferences,
  5. readSidebarPurposeSectionPreferences,
  6. reorderSidebarPurposeSections,
  7. } from "@/lib/sidebarPurposeSectionPreferences.js";
  8. function createStorageMock(initial: Record<string, string> = {}) {
  9. const storage = new Map(Object.entries(initial));
  10. return {
  11. getItem(key: string) {
  12. return storage.get(key) ?? null;
  13. },
  14. setItem(key: string, value: string) {
  15. storage.set(key, value);
  16. },
  17. };
  18. }
  19. describe("sidebarPurposeSectionPreferences", () => {
  20. it("defaults both purpose sections to expanded", () => {
  21. expect(readSidebarPurposeSectionPreferences(createStorageMock())).toEqual({
  22. projectsExpanded: true,
  23. conversationsExpanded: true,
  24. sectionOrder: ["projects", "conversations"],
  25. });
  26. });
  27. it("persists and restores each purpose section independently", () => {
  28. const storage = createStorageMock();
  29. persistSidebarPurposeSectionPreferences(
  30. {
  31. projectsExpanded: false,
  32. conversationsExpanded: true,
  33. sectionOrder: ["conversations", "projects"],
  34. },
  35. storage,
  36. );
  37. expect(storage.getItem(SIDEBAR_PURPOSE_SECTION_PREFERENCES_STORAGE_KEY)).toBe(
  38. JSON.stringify({
  39. projectsExpanded: false,
  40. conversationsExpanded: true,
  41. sectionOrder: ["conversations", "projects"],
  42. }),
  43. );
  44. expect(readSidebarPurposeSectionPreferences(storage)).toEqual({
  45. projectsExpanded: false,
  46. conversationsExpanded: true,
  47. sectionOrder: ["conversations", "projects"],
  48. });
  49. });
  50. it("falls back missing or invalid fields independently", () => {
  51. const storage = createStorageMock({
  52. [SIDEBAR_PURPOSE_SECTION_PREFERENCES_STORAGE_KEY]: JSON.stringify({
  53. projectsExpanded: false,
  54. conversationsExpanded: "no",
  55. }),
  56. });
  57. expect(readSidebarPurposeSectionPreferences(storage)).toEqual({
  58. projectsExpanded: false,
  59. conversationsExpanded: true,
  60. sectionOrder: ["projects", "conversations"],
  61. });
  62. storage.setItem(
  63. SIDEBAR_PURPOSE_SECTION_PREFERENCES_STORAGE_KEY,
  64. JSON.stringify({ conversationsExpanded: false }),
  65. );
  66. expect(readSidebarPurposeSectionPreferences(storage)).toEqual({
  67. projectsExpanded: true,
  68. conversationsExpanded: false,
  69. sectionOrder: ["projects", "conversations"],
  70. });
  71. });
  72. it("falls back to the default order for duplicate or unknown section ids", () => {
  73. const storage = createStorageMock({
  74. [SIDEBAR_PURPOSE_SECTION_PREFERENCES_STORAGE_KEY]: JSON.stringify({
  75. sectionOrder: ["conversations", "conversations"],
  76. }),
  77. });
  78. expect(readSidebarPurposeSectionPreferences(storage).sectionOrder).toEqual([
  79. "projects",
  80. "conversations",
  81. ]);
  82. storage.setItem(
  83. SIDEBAR_PURPOSE_SECTION_PREFERENCES_STORAGE_KEY,
  84. JSON.stringify({ sectionOrder: ["conversations", "unknown"] }),
  85. );
  86. expect(readSidebarPurposeSectionPreferences(storage).sectionOrder).toEqual([
  87. "projects",
  88. "conversations",
  89. ]);
  90. });
  91. it("reorders the whole purpose section without changing its identity", () => {
  92. expect(
  93. reorderSidebarPurposeSections(["projects", "conversations"], "conversations", "projects"),
  94. ).toEqual(["conversations", "projects"]);
  95. expect(
  96. reorderSidebarPurposeSections(["conversations", "projects"], "projects", "conversations"),
  97. ).toEqual(["projects", "conversations"]);
  98. });
  99. it("falls back to defaults for invalid JSON", () => {
  100. const storage = createStorageMock({
  101. [SIDEBAR_PURPOSE_SECTION_PREFERENCES_STORAGE_KEY]: "{",
  102. });
  103. expect(readSidebarPurposeSectionPreferences(storage)).toEqual({
  104. projectsExpanded: true,
  105. conversationsExpanded: true,
  106. sectionOrder: ["projects", "conversations"],
  107. });
  108. });
  109. it("falls back safely when storage cannot be read", () => {
  110. const storage = {
  111. getItem: vi.fn(() => {
  112. throw new Error("storage unavailable");
  113. }),
  114. setItem: vi.fn(),
  115. };
  116. expect(readSidebarPurposeSectionPreferences(storage)).toEqual({
  117. projectsExpanded: true,
  118. conversationsExpanded: true,
  119. sectionOrder: ["projects", "conversations"],
  120. });
  121. });
  122. it("does not throw when storage cannot be written", () => {
  123. const storage = {
  124. getItem: vi.fn(() => null),
  125. setItem: vi.fn(() => {
  126. throw new Error("storage unavailable");
  127. }),
  128. };
  129. expect(() =>
  130. persistSidebarPurposeSectionPreferences(
  131. {
  132. projectsExpanded: false,
  133. conversationsExpanded: false,
  134. sectionOrder: ["conversations", "projects"],
  135. },
  136. storage,
  137. ),
  138. ).not.toThrow();
  139. });
  140. });