Skip to content

Commit bfbaf5e

Browse files
npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7wpfleger96
andcommitted
fix(unread): make removeChannel and clearAll fully transactional + boundary tests
removeChannel and clearAll in useObservedUnreadPersistence now own all mutation of the shared observed refs — the parent (useUnreadChannels) no longer touches latestByChannelRef or observedUnreadEventsByChannelRef directly on the clear/mark-all-read paths. Before this change, markChannelRead deleted from both refs before calling the fenced removeChannel(), and markAllChannelsRead reset both refs to empty Maps before calling the fenced clearAll(). A stale scope-A callback could therefore corrupt scope B's in-memory badges before the scope fence rejected the call. Both owner operations now validate the captured scope first, then mutate refs, then write to storage — stale scope invocations are complete no-ops. Adds useUnreadChannels.test.mjs: five boundary tests that mount the full production hook with QueryClientProvider, covering: - markChannelRead happy path (clears channel from refs + storage) - markChannelRead topLevelOnly=true (leaves refs intact) - markChannelRead stale scope-A callback (B refs and storage untouched) - markAllChannelsRead happy path (clears entire bucket) - markAllChannelsRead stale scope-A callback (B refs and storage untouched) Also rewrites the A→B late-timer test in useObservedUnreadPersistence to accurately describe what it proves (synchronous flush invariant), and fixes a vacuous removeChannelAndPersistCurrent API stability assertion. Co-authored-by: Will Pfleger <[email protected]> Signed-off-by: Will Pfleger <[email protected]>
1 parent 2fa9a05 commit bfbaf5e

4 files changed

Lines changed: 501 additions & 40 deletions

File tree

‎desktop/src/features/channels/useObservedUnreadPersistence.test.mjs‎

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -698,14 +698,19 @@ test("isScopeLoaded returns false before identity-reset effect commits, true aft
698698
await harness.unmount();
699699
});
700700

701-
test("A→B late-timer: scheduled write owns immutable scope snapshot, not mutable refs", async () => {
702-
// Prove that scheduleObservedUnreadWrite captures an immutable snapshot at
703-
// schedule time, so a timer that fires AFTER a scope switch writes A's
704-
// snapshot under A's key — not B's mutable refs under A's key.
701+
test("A→B scope switch: pending A-timer is cancelled by flush, A data persisted synchronously", async () => {
702+
// The hydration effect cleanup calls flushObservedUnreadWrite when scope
703+
// switches, which cancels the pending A-timer and synchronously writes A's
704+
// current snapshot to A's bucket. After the switch:
705+
// - A's bucket holds the flushed data.
706+
// - B's bucket has no A contamination.
707+
// - A new B-scope timer can schedule independently.
705708
//
706-
// The important invariant: after A schedules a snapshot and we switch to B,
707-
// the pending A-timer must be CANCELLED by the scope-switch flush (hydration
708-
// effect), so its snapshot never lands after B's data is loaded.
709+
// A genuinely late scope-A timer cannot exist after a scope switch, because
710+
// flushObservedUnreadWrite always cancels the pending timer before resetting
711+
// the refs. The immutability of a snapshot at schedule time (that a timer
712+
// fired BEFORE flush would write A's snapshot, not B's live refs) is proven
713+
// directly in observedUnreadStorage.test.mjs via scheduleObservedUnreadWrite.
709714
installFreshStorage();
710715

711716
const PUBKEY_AT = "pubkey-a-t";
@@ -736,42 +741,39 @@ test("A→B late-timer: scheduled write owns immutable scope snapshot, not mutab
736741

737742
const harness = await mountHook(propsAT, refsAT);
738743

739-
// After mount, hydration loaded the pre-seeded event into refs.
744+
// Hydration loaded A's pre-seeded event.
740745
assert.ok(
741746
refsAT.eventsRef.current.has("ch-at"),
742747
"hydration must restore ch-at from storage",
743748
);
744749

745750
const scopeAT = harness.api.currentScope;
746751

747-
// Schedule a write for scope A (1-second debounce — timer pending).
748-
// The timer snapshot captures A's current event map (ch-at present).
752+
// Schedule a write for scope A (timer pending with immutable snapshot).
749753
harness.api.schedule(scopeAT);
750754

751-
// Switch to scope B BEFORE the timer fires. The hydration effect cleanup
752-
// flushes A synchronously (cancelling the timer and writing A's snapshot),
753-
// then resets refs to new Maps, then loads B's storage.
755+
// Switch to scope B. The hydration effect cleanup fires first:
756+
// 1. flushObservedUnreadWrite — cancels the pending timer, writes A's
757+
// snapshot synchronously to A's bucket.
758+
// 2. Refs are reset to new Maps, B's storage is loaded.
754759
const propsBT = { ...propsAT, pubkey: PUBKEY_BT, relay: RELAY_BT };
755760
await harness.render(propsBT);
756761

757-
// A's pending timer was cancelled during the flush — A's data is in storage
758-
// from the synchronous flush, not from the timer.
762+
// A's data must be in storage from the synchronous flush.
759763
const storedAT = readObservedUnreadFromStorage(PUBKEY_AT, RELAY_AT);
760764
assert.ok(
761765
storedAT?.has("ch-at"),
762-
"A scope must be flushed on scope switch (synchronous flush in hydration effect cleanup)",
766+
"A scope must be flushed synchronously on scope switch",
763767
);
764768

765-
// B's bucket must NOT contain A's channel (no cross-scope contamination).
769+
// B's bucket must not contain A's channel.
766770
const storedBT = readObservedUnreadFromStorage(PUBKEY_BT, RELAY_BT);
767-
if (storedBT !== null) {
768-
assert.ok(
769-
!storedBT.has("ch-at"),
770-
"B's bucket must not contain A's channel after scope switch",
771-
);
772-
}
771+
assert.ok(
772+
storedBT == null || !storedBT.has("ch-at"),
773+
"B's bucket must not contain A's channel after scope switch",
774+
);
773775

774-
// Now seed B and schedule a B write. Flush via pagehide to advance it.
776+
// Seed B, schedule a B write, flush via pagehide — B persists independently.
775777
const chBT = new Map();
776778
chBT.set("evt-bt", makeEvent("evt-bt", "ch-bt", NOW_S + 300));
777779
refsAT.eventsRef.current.set("ch-bt", chBT);
@@ -938,7 +940,7 @@ test("unrelated rerenders do not change API object identity (catch-up stability)
938940
const harness = await mountHook(props, refs);
939941
const api1 = harness.api;
940942
const schedule1 = api1.schedule;
941-
const removeChannelAndPersistCurrent1 = api1.removeChannelAndPersistCurrent;
943+
const removeChannel1 = api1.removeChannel;
942944
const clearAll1 = api1.clearAll;
943945
const isScopeLoaded1 = api1.isScopeLoaded;
944946

@@ -956,9 +958,9 @@ test("unrelated rerenders do not change API object identity (catch-up stability)
956958
);
957959
assert.equal(schedule1, api2.schedule, "schedule must be stable");
958960
assert.equal(
959-
removeChannelAndPersistCurrent1,
960-
api2.removeChannelAndPersistCurrent,
961-
"removeChannelAndPersistCurrent must be stable",
961+
removeChannel1,
962+
api2.removeChannel,
963+
"removeChannel must be stable",
962964
);
963965
assert.equal(clearAll1, api2.clearAll, "clearAll must be stable");
964966
assert.equal(

‎desktop/src/features/channels/useObservedUnreadPersistence.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,23 @@ export function useObservedUnreadPersistence(
167167
// Reject if the loaded scope has drifted — a stale callback must not
168168
// cancel the new scope's pending snapshot or clear the wrong bucket.
169169
if (scopeLoadedRef.current !== currentScope) return;
170-
// Cancel any pending snapshot — mark-all clears both in-memory refs and
171-
// storage, so the pending debounce snapshot must not resurrect any channels.
170+
// Cancel any pending snapshot and clear both in-memory refs before touching
171+
// storage — the parent no longer resets the refs directly, so this is the
172+
// single transactional clear path for mark-all-read.
172173
if (timerRef.current !== null) {
173174
clearTimeout(timerRef.current);
174175
timerRef.current = null;
175176
}
177+
observedUnreadEventsByChannelRef.current = new Map();
178+
latestByChannelRef.current = new Map();
176179
clearObservedUnreadStorage(normalizedPubkey ?? "", normalizedRelayUrl);
177-
}, [currentScope, normalizedPubkey, normalizedRelayUrl]);
180+
}, [
181+
currentScope,
182+
normalizedPubkey,
183+
normalizedRelayUrl,
184+
observedUnreadEventsByChannelRef,
185+
latestByChannelRef,
186+
]);
178187

179188
// isScopeLoaded reads the ref at call time — always fresh, never a stale
180189
// snapshot from a closed-over useMemo value.

0 commit comments

Comments
 (0)