diff --git a/src/client/SideCardSection.tsx b/src/client/SideCardSection.tsx index 10b322ea..e7e29a5d 100644 --- a/src/client/SideCardSection.tsx +++ b/src/client/SideCardSection.tsx @@ -738,14 +738,20 @@ export function SideCardSection({ store, service, guideCatalog }: SideCardSectio setPrefs(outcome.ok ? outcome.prefs : previous) } - /** Optimistically apply one pref patch, then commit (revert on failure). */ - const applyPref = (patch: Record): void => { + /** + * Optimistically apply one pref patch, then commit (revert on failure). + * @param patch - the full local value (the UI reads this shape). + * @param wirePatch - the patch to send (defaults to `patch`); the host + * deep-merges nested plain objects, so a sparse local map must send only + * the touched key. + */ + const applyPref = (patch: Record, wirePatch: Record = patch): void => { const previous = optimisticRef.current const next = { ...previous, ...patch } as SidebarPrefs optimisticRef.current = next setPrefs(next) setError(null) - void commit(patch).then(outcome => applyOutcome(previous, outcome)) + void commit(wirePatch).then(outcome => applyOutcome(previous, outcome)) } /** Flip one per-tab enable switch (merge into the tabsEnabled map). */ @@ -763,7 +769,9 @@ export function SideCardSection({ store, service, guideCatalog }: SideCardSectio const next = { ...optimisticRef.current.guideHidden } if (shown) delete next[key] else next[key] = true - applyPref({ guideHidden: next }) + // A deletion cannot ride the host's deep merge, so a re-shown card sends + // the explicit `false` while the UI keeps the sparse map. + applyPref({ guideHidden: next }, { guideHidden: { [key]: !shown } }) } /** diff --git a/src/client/native/guide-prefs.ts b/src/client/native/guide-prefs.ts index 95be38a6..adf7068d 100644 --- a/src/client/native/guide-prefs.ts +++ b/src/client/native/guide-prefs.ts @@ -27,11 +27,11 @@ export function saveGuideOrder(store: SidebarStore, order: readonly string[]): v /** Persist one card's start-page visibility. */ export function setGuideEntryHidden(store: SidebarStore, key: string, hidden: boolean): void { - const current = store.getPrefs().guideHidden - const next = { ...current } - if (hidden) next[key] = true - else delete next[key] - writeGuidePrefs(store, { guideHidden: next }) + // The host deep-merges nested plain objects, so deleting the key from the + // patch cannot express "shown": the stored true would survive the merge. + // Send the explicit boolean for the touched key instead. Every reader asks + // `guideHidden[key] === true`, so the stored false already reads as shown. + writeGuidePrefs(store, { guideHidden: { [key]: hidden } }) } /** Persist one Better Sidebar type's feature switch (the sidebar-content state). */ diff --git a/tests/native-guide.spec.tsx b/tests/native-guide.spec.tsx index d0e65ae9..84e15cb9 100644 --- a/tests/native-guide.spec.tsx +++ b/tests/native-guide.spec.tsx @@ -17,6 +17,23 @@ import type { NativeGuideEntry } from '../src/client/native/guide-catalog.ts' import { SIDEBAR_PREFS_DEFAULTS } from '../src/prefs-shared.ts' import { renderRoot, setupReactAct } from './test-utils.ts' +/** The settings wire the start-page writes end up on (asserted below). */ +const settingsUpdate = vi.fn< + (patch: Record) => Promise<{ value: unknown; revision: number }> +>(async () => ({ value: {}, revision: 2 })) +// Partial mock: only the write this spec drives is stubbed, everything else +// keeps the real client. +vi.mock('../src/client/api.ts', async (importOriginal) => { + const actual = await importOriginal() + return { + ...actual, + api: { + ...actual.api, + settingsUpdate: (patch: Record) => settingsUpdate(patch), + }, + } +}) + setupReactAct() beforeAll(() => { @@ -291,6 +308,48 @@ describe('NativeGuide', () => { expect(close).toHaveBeenCalled() mounted.unmount() }) + + it('re-showing a hidden card sends the explicit false on the wire (#1212)', async () => { + settingsUpdate.mockClear() + // The host resolves the merged document: the touched key is now false. + settingsUpdate.mockResolvedValue({ value: { guideHidden: { a: false } }, revision: 2 }) + const store = createSidebarStore() + store.setSession('s1') + store.setPrefs({ ...store.getPrefs(), guideHidden: { a: true } }) + const service = createBetterSidebarService(store) + // One stable snapshot: useSyncExternalStore loops on a fresh array. + const catalogEntries = [entry('a')] + const catalog = { getSnapshot: () => catalogEntries, subscribe: () => () => {} } + const mounted = renderRoot(createElement(NativeGuide, { catalog, store, service })) + await act(async () => { + ;(mounted.container.querySelector('[data-guide-more]') as HTMLButtonElement).click() + await Promise.resolve() + }) + const reorder = [...document.querySelectorAll('[role="menuitem"]')] + .find(node => node.textContent?.includes('Reorder')) + expect(reorder).toBeDefined() + await act(async () => { + ;(reorder as HTMLElement).click() + await Promise.resolve() + }) + const toggle = document.querySelector('input[aria-label="Show on the start page: A"]') as HTMLInputElement + expect(toggle).not.toBeNull() + expect(toggle.checked).toBe(false) + await act(async () => { + toggle.click() + // The host deep-merges nested plain objects, so a patch with the key + // absent would leave the stored true in place: the wire must carry false. + await vi.waitFor(() => { + expect(settingsUpdate).toHaveBeenCalledWith({ guideHidden: { a: false } }) + }) + // Every reader asks `guideHidden[key] === true`, so a stored false + // already reads as shown; no map normalization is needed. + await vi.waitFor(() => { + expect(store.getPrefs().guideHidden.a).not.toBe(true) + }) + }) + mounted.unmount() + }) }) describe('registerNativeSurface start-page takeover', () => {