it("retains a local section order when the initial cache lacks its entry", () => { const { orderAtom, queryClient, store } = createHarness(); const localOrder = ["threads", "projects", "pinned"]; store.set(orderAtom, localOrder); const response = serverResponse(); response.preferences = Object.fromEntries( Object.entries(response.preferences).filter(([key]) => key !== "sidebar.sectionOrder"), ) as UiPreferencesResponse["preferences"]; setCachedUiPreferences(queryClient, response); startUiPreferencesSync({ queryClient, store }); expect(store.get(orderAtom)).toEqual(localOrder); expect(mocks.set).not.toHaveBeenCalled(); }); it("keeps edits local without an error notification when the server lacks the entry", async () => { const { orderAtom, queryClient, store } = createHarness(); startUiPreferencesSync({ queryClient, store }); const response = serverResponse(); response.preferences = Object.fromEntries( Object.entries(response.preferences).filter(([key]) => key !== "sidebar.sectionOrder"), ) as UiPreferencesResponse["preferences"]; setCachedUiPreferences(queryClient, response); store.set(orderAtom, ["threads"]); await waitForUiPreferenceWrites(); expect(store.get(orderAtom)).toEqual(["threads"]); expect(mocks.set).not.toHaveBeenCalled(); expect(mocks.toastError).not.toHaveBeenCalled(); expect(hasPendingUiPreferenceWrite("sidebar.sectionOrder")).toBe(false); }); it("records an acknowledged write after its cached entry disappears", async () => { const { orderAtom, queryClient, store } = createHarness(); startUiPreferencesSync({ queryClient, store }); setCachedUiPreferences(queryClient, serverResponse()); const response = serverResponse(); response.preferences = Object.fromEntries( Object.entries(response.preferences).filter(([key]) => key !== "sidebar.sectionOrder"), ) as UiPreferencesResponse["preferences"]; mocks.set.mockImplementationOnce(async (input) => { setCachedUiPreferences(queryClient, response); return { key: input.key, revision: 1, value: input.value }; }); store.set(orderAtom, ["threads"]); await waitForUiPreferenceWrites(); expect(getCachedUiPreferences(queryClient)?.preferences["sidebar.sectionOrder"]) .toEqual({ revision: 1, value: ["threads"] }); expect(mocks.toastError).not.toHaveBeenCalled(); expect(store.get(orderAtom)).toEqual(["threads"]); });