sidebarPurposeSectionPreferences.test.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. SIDEBAR_PURPOSE_SECTION_PREFERENCES_STORAGE_KEY,
  4. persistSidebarPurposeSectionPreferences,
  5. readSidebarPurposeSectionPreferences,
  6. reorderSidebarPurposeSections,
  7. resolveSidebarPurposeSectionExpanded,
  8. updateSidebarPurposeSectionExpanded,
  9. } from "@/lib/sidebarPurposeSectionPreferences.js";
  10. function createStorageMock(initial: Record<string, string> = {}) {
  11. const storage = new Map(Object.entries(initial));
  12. return {
  13. getItem(key: string) {
  14. return storage.get(key) ?? null;
  15. },
  16. setItem(key: string, value: string) {
  17. storage.set(key, value);
  18. },
  19. };
  20. }
  21. describe("sidebarPurposeSectionPreferences", () => {
  22. it("defaults both purpose sections to expanded", () => {
  23. expect(readSidebarPurposeSectionPreferences(createStorageMock())).toEqual({
  24. projectsExpanded: true,
  25. conversationsExpanded: true,
  26. sectionOrder: ["projects", "conversations"],
  27. });
  28. });
  29. it("persists and restores each purpose section independently", () => {
  30. const storage = createStorageMock();
  31. persistSidebarPurposeSectionPreferences(
  32. {
  33. projectsExpanded: false,
  34. conversationsExpanded: true,
  35. sectionOrder: ["conversations", "projects"],
  36. },
  37. storage,
  38. );
  39. expect(storage.getItem(SIDEBAR_PURPOSE_SECTION_PREFERENCES_STORAGE_KEY)).toBe(
  40. JSON.stringify({
  41. projectsExpanded: false,
  42. conversationsExpanded: true,
  43. sectionOrder: ["conversations", "projects"],
  44. }),
  45. );
  46. expect(readSidebarPurposeSectionPreferences(storage)).toEqual({
  47. projectsExpanded: false,
  48. conversationsExpanded: true,
  49. sectionOrder: ["conversations", "projects"],
  50. });
  51. });
  52. it("isolates purpose section expansion by source group", () => {
  53. const initial = readSidebarPurposeSectionPreferences(createStorageMock());
  54. const next = updateSidebarPurposeSectionExpanded(initial, {
  55. sectionId: "projects",
  56. expanded: false,
  57. sourceGroupKey: "server:source-a",
  58. });
  59. expect(
  60. resolveSidebarPurposeSectionExpanded(next, "projects", "server:source-a"),
  61. ).toBe(false);
  62. expect(
  63. resolveSidebarPurposeSectionExpanded(next, "projects", "server:source-b"),
  64. ).toBe(true);
  65. expect(
  66. resolveSidebarPurposeSectionExpanded(
  67. next,
  68. "conversations",
  69. "server:source-a",
  70. ),
  71. ).toBe(true);
  72. });
  73. it("persists source-group expansion without changing the legacy fallback", () => {
  74. const storage = createStorageMock();
  75. const next = updateSidebarPurposeSectionExpanded(
  76. readSidebarPurposeSectionPreferences(storage),
  77. {
  78. sectionId: "conversations",
  79. expanded: false,
  80. sourceGroupKey: "local",
  81. },
  82. );
  83. persistSidebarPurposeSectionPreferences(next, storage);
  84. const restored = readSidebarPurposeSectionPreferences(storage);
  85. expect(restored.projectsExpanded).toBe(true);
  86. expect(restored.conversationsExpanded).toBe(true);
  87. expect(
  88. resolveSidebarPurposeSectionExpanded(restored, "conversations", "local"),
  89. ).toBe(false);
  90. expect(
  91. resolveSidebarPurposeSectionExpanded(
  92. restored,
  93. "conversations",
  94. "server:source-a",
  95. ),
  96. ).toBe(true);
  97. });
  98. it("falls back missing or invalid fields independently", () => {
  99. const storage = createStorageMock({
  100. [SIDEBAR_PURPOSE_SECTION_PREFERENCES_STORAGE_KEY]: JSON.stringify({
  101. projectsExpanded: false,
  102. conversationsExpanded: "no",
  103. }),
  104. });
  105. expect(readSidebarPurposeSectionPreferences(storage)).toEqual({
  106. projectsExpanded: false,
  107. conversationsExpanded: true,
  108. sectionOrder: ["projects", "conversations"],
  109. });
  110. storage.setItem(
  111. SIDEBAR_PURPOSE_SECTION_PREFERENCES_STORAGE_KEY,
  112. JSON.stringify({ conversationsExpanded: false }),
  113. );
  114. expect(readSidebarPurposeSectionPreferences(storage)).toEqual({
  115. projectsExpanded: true,
  116. conversationsExpanded: false,
  117. sectionOrder: ["projects", "conversations"],
  118. });
  119. });
  120. it("falls back to the default order for duplicate or unknown section ids", () => {
  121. const storage = createStorageMock({
  122. [SIDEBAR_PURPOSE_SECTION_PREFERENCES_STORAGE_KEY]: JSON.stringify({
  123. sectionOrder: ["conversations", "conversations"],
  124. }),
  125. });
  126. expect(readSidebarPurposeSectionPreferences(storage).sectionOrder).toEqual([
  127. "projects",
  128. "conversations",
  129. ]);
  130. storage.setItem(
  131. SIDEBAR_PURPOSE_SECTION_PREFERENCES_STORAGE_KEY,
  132. JSON.stringify({ sectionOrder: ["conversations", "unknown"] }),
  133. );
  134. expect(readSidebarPurposeSectionPreferences(storage).sectionOrder).toEqual([
  135. "projects",
  136. "conversations",
  137. ]);
  138. });
  139. it("reorders the whole purpose section without changing its identity", () => {
  140. expect(
  141. reorderSidebarPurposeSections(["projects", "conversations"], "conversations", "projects"),
  142. ).toEqual(["conversations", "projects"]);
  143. expect(
  144. reorderSidebarPurposeSections(["conversations", "projects"], "projects", "conversations"),
  145. ).toEqual(["projects", "conversations"]);
  146. });
  147. it("falls back to defaults for invalid JSON", () => {
  148. const storage = createStorageMock({
  149. [SIDEBAR_PURPOSE_SECTION_PREFERENCES_STORAGE_KEY]: "{",
  150. });
  151. expect(readSidebarPurposeSectionPreferences(storage)).toEqual({
  152. projectsExpanded: true,
  153. conversationsExpanded: true,
  154. sectionOrder: ["projects", "conversations"],
  155. });
  156. });
  157. it("falls back safely when storage cannot be read", () => {
  158. const storage = {
  159. getItem: vi.fn(() => {
  160. throw new Error("storage unavailable");
  161. }),
  162. setItem: vi.fn(),
  163. };
  164. expect(readSidebarPurposeSectionPreferences(storage)).toEqual({
  165. projectsExpanded: true,
  166. conversationsExpanded: true,
  167. sectionOrder: ["projects", "conversations"],
  168. });
  169. });
  170. it("does not throw when storage cannot be written", () => {
  171. const storage = {
  172. getItem: vi.fn(() => null),
  173. setItem: vi.fn(() => {
  174. throw new Error("storage unavailable");
  175. }),
  176. };
  177. expect(() =>
  178. persistSidebarPurposeSectionPreferences(
  179. {
  180. projectsExpanded: false,
  181. conversationsExpanded: false,
  182. sectionOrder: ["conversations", "projects"],
  183. },
  184. storage,
  185. ),
  186. ).not.toThrow();
  187. });
  188. });