Skip to content

Commit 04a9d10

Browse files
tellahonpub1223z34hd7vtwc6qj4s7flsxkj644nlre2nthu7lrrmkumhu3xddsrx9r6w
andauthored
sync channel sort preferences across clients (#1556)
Signed-off-by: Taylor Ho <[email protected]> Co-authored-by: npub1223z34hd7vtwc6qj4s7flsxkj644nlre2nthu7lrrmkumhu3xddsrx9r6w <52a228d6edf316ec6812ac3c9fc0d696ab59fc7954d77e7be31eedcddf91335b@sprout-oss.stage.blox.sqprod.co>
1 parent 2ef26ec commit 04a9d10

4 files changed

Lines changed: 509 additions & 1 deletion

File tree

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
import assert from "node:assert/strict";
2+
import test, { mock } from "node:test";
3+
4+
import { relayClient } from "@/shared/api/relayClient";
5+
import { ChannelSortSyncManager } from "./channelSortSync.ts";
6+
7+
function makeStore(groups = {}) {
8+
return { version: 1, groups };
9+
}
10+
11+
// ─── destroy() must cancel pending publish, not flush ─────────────────────────
12+
13+
// Regression guard for the workspace-switch cross-relay publish vector:
14+
// change a sort mode in relay A → destroy() is called (relayUrl dep change) →
15+
// no publish should fire. The scoped localStorage write is durable; when the
16+
// user returns to relay A the seed-publish path handles it.
17+
test("destroy: cancels pending publish without flushing to the relay", () => {
18+
const publishCalls = [];
19+
mock.method(relayClient, "fetchEvents", () => Promise.resolve([]));
20+
mock.method(relayClient, "publishEvent", (...args) => {
21+
publishCalls.push(args);
22+
return Promise.resolve();
23+
});
24+
25+
let timerCallback = null;
26+
const fakeTimers = [];
27+
let nextId = 1;
28+
if (typeof globalThis.window === "undefined") {
29+
globalThis.window = {};
30+
}
31+
const originalSetTimeout = globalThis.window.setTimeout;
32+
const originalClearTimeout = globalThis.window.clearTimeout;
33+
globalThis.window.setTimeout = (fn, _ms) => {
34+
const id = nextId++;
35+
fakeTimers.push({ id, fn });
36+
timerCallback = fn;
37+
return id;
38+
};
39+
globalThis.window.clearTimeout = (id) => {
40+
const idx = fakeTimers.findIndex((t) => t.id === id);
41+
if (idx !== -1) {
42+
fakeTimers.splice(idx, 1);
43+
timerCallback = null;
44+
}
45+
};
46+
47+
try {
48+
const manager = new ChannelSortSyncManager("pk-test");
49+
const store = makeStore({ channels: "recent" });
50+
51+
manager.publishSortPrefs(store);
52+
assert.ok(timerCallback !== null, "debounce timer should be set");
53+
54+
manager.destroy();
55+
56+
assert.ok(
57+
timerCallback === null,
58+
"debounce timer should be cleared on destroy",
59+
);
60+
assert.equal(
61+
publishCalls.length,
62+
0,
63+
"no publish event should have been sent after destroy",
64+
);
65+
} finally {
66+
if (originalSetTimeout !== undefined) {
67+
globalThis.window.setTimeout = originalSetTimeout;
68+
}
69+
if (originalClearTimeout !== undefined) {
70+
globalThis.window.clearTimeout = originalClearTimeout;
71+
}
72+
mock.reset();
73+
}
74+
});
75+
76+
// Regression guard for the timer-fired race: debounce fires → doPublish starts
77+
// awaiting fetchOwnBlobBeforePublish → destroy() is called (relayUrl dep
78+
// change) → publishEvent must never be called even though the timer already
79+
// fired and cleared itself before destroy() ran.
80+
test("destroy: aborts in-flight doPublish after fetchOwnBlobBeforePublish resolves", async () => {
81+
let releaseFetch = null;
82+
const publishCalls = [];
83+
84+
mock.method(relayClient, "fetchEvents", () => {
85+
return new Promise((resolve) => {
86+
releaseFetch = () => resolve([]);
87+
});
88+
});
89+
mock.method(relayClient, "publishEvent", (...args) => {
90+
publishCalls.push(args);
91+
return Promise.resolve();
92+
});
93+
94+
if (typeof globalThis.window === "undefined") {
95+
globalThis.window = {};
96+
}
97+
let capturedCallback = null;
98+
let nextId = 1;
99+
const origSetTimeout = globalThis.window.setTimeout;
100+
const origClearTimeout = globalThis.window.clearTimeout;
101+
globalThis.window.setTimeout = (fn, _ms) => {
102+
capturedCallback = fn;
103+
return nextId++;
104+
};
105+
globalThis.window.clearTimeout = (_id) => {
106+
capturedCallback = null;
107+
};
108+
109+
try {
110+
const manager = new ChannelSortSyncManager("pk-race");
111+
const store = makeStore({ dms: "recent" });
112+
113+
manager.publishSortPrefs(store);
114+
assert.ok(capturedCallback !== null, "debounce timer should be set");
115+
116+
const timerFn = capturedCallback;
117+
capturedCallback = null; // timer cleared itself inside the callback
118+
timerFn();
119+
120+
manager.destroy();
121+
122+
releaseFetch();
123+
124+
await new Promise((resolve) => setTimeout(resolve, 0));
125+
126+
assert.equal(
127+
publishCalls.length,
128+
0,
129+
"publishEvent must not be called after destroy() even when timer already fired",
130+
);
131+
} finally {
132+
globalThis.window.setTimeout = origSetTimeout;
133+
globalThis.window.clearTimeout = origClearTimeout;
134+
mock.reset();
135+
}
136+
});
137+
138+
test("destroy: is safe to call with no pending publish", () => {
139+
const manager = new ChannelSortSyncManager("pk-no-pending");
140+
assert.doesNotThrow(() => manager.destroy());
141+
});
142+
143+
test("destroy: cancelPendingPublish clears pendingStore", () => {
144+
let timerCallback = null;
145+
let nextId = 1;
146+
if (typeof globalThis.window === "undefined") {
147+
globalThis.window = {};
148+
}
149+
const orig = globalThis.window.setTimeout;
150+
const origClear = globalThis.window.clearTimeout;
151+
globalThis.window.setTimeout = (fn, _ms) => {
152+
timerCallback = fn;
153+
return nextId++;
154+
};
155+
globalThis.window.clearTimeout = (_id) => {
156+
timerCallback = null;
157+
};
158+
159+
try {
160+
const manager = new ChannelSortSyncManager("pk-pending-null");
161+
const store = makeStore({ starred: "recent" });
162+
manager.publishSortPrefs(store);
163+
assert.deepEqual(manager.getPendingStore(), store);
164+
165+
manager.destroy();
166+
assert.equal(
167+
manager.getPendingStore(),
168+
null,
169+
"pendingStore must be null after destroy",
170+
);
171+
assert.ok(timerCallback === null, "timer must be cleared after destroy");
172+
} finally {
173+
globalThis.window.setTimeout = orig;
174+
globalThis.window.clearTimeout = origClear;
175+
}
176+
});

0 commit comments

Comments
 (0)