|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | + |
| 4 | +import { |
| 5 | + claimUnhydratedRenderScopedReactionIds, |
| 6 | + collectRenderScopedReactionMessageIds, |
| 7 | + hydrateRenderScopedReactions, |
| 8 | + releaseRenderScopedReactionIds, |
| 9 | + resetRenderScopedReactionHydration, |
| 10 | +} from "./renderScopedReactions.ts"; |
| 11 | +import { formatTimelineMessages } from "./formatTimelineMessages.ts"; |
| 12 | +import { channelMessagesKey } from "./messageQueryKeys.ts"; |
| 13 | + |
| 14 | +const CHANNEL_ID = "36411e44-0e2d-4cfe-bd6e-567eb169db9f"; |
| 15 | + |
| 16 | +function hex(char) { |
| 17 | + return char.repeat(64); |
| 18 | +} |
| 19 | + |
| 20 | +function event(id, kind, overrides = {}) { |
| 21 | + return { |
| 22 | + id, |
| 23 | + pubkey: hex("a"), |
| 24 | + kind, |
| 25 | + created_at: 1_700_000_000, |
| 26 | + content: "", |
| 27 | + tags: [["h", CHANNEL_ID]], |
| 28 | + sig: "sig", |
| 29 | + ...overrides, |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +function entry(message) { |
| 34 | + return { message, summary: null }; |
| 35 | +} |
| 36 | + |
| 37 | +function makeQueryClientStub(initialEvents = []) { |
| 38 | + const store = new Map([ |
| 39 | + [JSON.stringify(channelMessagesKey(CHANNEL_ID)), initialEvents], |
| 40 | + ]); |
| 41 | + return { |
| 42 | + getQueryData(key) { |
| 43 | + return store.get(JSON.stringify(key)); |
| 44 | + }, |
| 45 | + setQueryData(key, updater) { |
| 46 | + const k = JSON.stringify(key); |
| 47 | + const next = |
| 48 | + typeof updater === "function" ? updater(store.get(k) ?? []) : updater; |
| 49 | + store.set(k, next); |
| 50 | + return next; |
| 51 | + }, |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +test.afterEach(() => { |
| 56 | + resetRenderScopedReactionHydration(); |
| 57 | +}); |
| 58 | + |
| 59 | +test("collects main timeline and open thread messages without hidden replies", () => { |
| 60 | + const main = event(hex("1"), 9); |
| 61 | + const hiddenCollapsedReply = event(hex("2"), 9, { |
| 62 | + tags: [["e", main.id]], |
| 63 | + }); |
| 64 | + const threadHead = event(hex("3"), 9); |
| 65 | + const visibleThreadReply = event(hex("4"), 9, { |
| 66 | + tags: [["e", threadHead.id]], |
| 67 | + }); |
| 68 | + |
| 69 | + assert.deepEqual( |
| 70 | + collectRenderScopedReactionMessageIds({ |
| 71 | + mainEntries: [entry(main), entry(threadHead)], |
| 72 | + threadHeadMessage: threadHead, |
| 73 | + threadEntries: [entry(visibleThreadReply)], |
| 74 | + }), |
| 75 | + [main.id, threadHead.id, visibleThreadReply.id], |
| 76 | + ); |
| 77 | + |
| 78 | + assert.ok( |
| 79 | + !collectRenderScopedReactionMessageIds({ |
| 80 | + mainEntries: [entry(main), entry(threadHead)], |
| 81 | + threadHeadMessage: threadHead, |
| 82 | + threadEntries: [entry(visibleThreadReply)], |
| 83 | + }).includes(hiddenCollapsedReply.id), |
| 84 | + ); |
| 85 | +}); |
| 86 | + |
| 87 | +test("claims each rendered message id once per channel and can retry released ids", () => { |
| 88 | + assert.deepEqual( |
| 89 | + claimUnhydratedRenderScopedReactionIds(CHANNEL_ID, [ |
| 90 | + hex("1"), |
| 91 | + hex("2"), |
| 92 | + hex("1"), |
| 93 | + ]), |
| 94 | + [hex("1"), hex("2")], |
| 95 | + ); |
| 96 | + assert.deepEqual( |
| 97 | + claimUnhydratedRenderScopedReactionIds(CHANNEL_ID, [hex("1"), hex("2")]), |
| 98 | + [], |
| 99 | + ); |
| 100 | + assert.deepEqual( |
| 101 | + claimUnhydratedRenderScopedReactionIds("other-channel", [hex("1")]), |
| 102 | + [hex("1")], |
| 103 | + ); |
| 104 | + |
| 105 | + releaseRenderScopedReactionIds(CHANNEL_ID, [hex("2")]); |
| 106 | + assert.deepEqual( |
| 107 | + claimUnhydratedRenderScopedReactionIds(CHANNEL_ID, [hex("1"), hex("2")]), |
| 108 | + [hex("2")], |
| 109 | + ); |
| 110 | +}); |
| 111 | + |
| 112 | +test("hydrates visible reactions into the channel timeline cache", async () => { |
| 113 | + const messageId = hex("1"); |
| 114 | + const reactionId = hex("2"); |
| 115 | + const currentUser = hex("c"); |
| 116 | + const message = event(messageId, 9, { |
| 117 | + pubkey: hex("a"), |
| 118 | + content: "ship it?", |
| 119 | + }); |
| 120 | + const reaction = event(reactionId, 7, { |
| 121 | + pubkey: currentUser, |
| 122 | + content: "✅", |
| 123 | + tags: [["e", messageId]], |
| 124 | + }); |
| 125 | + const queryClient = makeQueryClientStub([message]); |
| 126 | + const calls = []; |
| 127 | + |
| 128 | + await hydrateRenderScopedReactions({ |
| 129 | + channelId: CHANNEL_ID, |
| 130 | + messageIds: [messageId], |
| 131 | + queryClient, |
| 132 | + deps: { |
| 133 | + fetchReactionEventsForMessages: async (channelId, messageIds) => { |
| 134 | + calls.push({ channelId, messageIds }); |
| 135 | + return [reaction]; |
| 136 | + }, |
| 137 | + }, |
| 138 | + }); |
| 139 | + |
| 140 | + assert.deepEqual(calls, [{ channelId: CHANNEL_ID, messageIds: [messageId] }]); |
| 141 | + const cached = queryClient.getQueryData(channelMessagesKey(CHANNEL_ID)); |
| 142 | + assert.ok(cached.some((e) => e.id === reactionId)); |
| 143 | + |
| 144 | + const timeline = formatTimelineMessages(cached, null, currentUser, null); |
| 145 | + assert.deepEqual( |
| 146 | + timeline |
| 147 | + .find((m) => m.id === messageId) |
| 148 | + ?.reactions?.map((r) => ({ |
| 149 | + count: r.count, |
| 150 | + emoji: r.emoji, |
| 151 | + mine: r.reactedByCurrentUser, |
| 152 | + })), |
| 153 | + [{ count: 1, emoji: "✅", mine: true }], |
| 154 | + ); |
| 155 | +}); |
| 156 | + |
| 157 | +test("failed hydration releases ids so the next render can retry", async () => { |
| 158 | + const messageId = hex("1"); |
| 159 | + const queryClient = makeQueryClientStub([event(messageId, 9)]); |
| 160 | + |
| 161 | + await hydrateRenderScopedReactions({ |
| 162 | + channelId: CHANNEL_ID, |
| 163 | + messageIds: [messageId], |
| 164 | + queryClient, |
| 165 | + deps: { |
| 166 | + fetchReactionEventsForMessages: async () => { |
| 167 | + throw new Error("relay timeout"); |
| 168 | + }, |
| 169 | + }, |
| 170 | + }); |
| 171 | + |
| 172 | + assert.deepEqual( |
| 173 | + claimUnhydratedRenderScopedReactionIds(CHANNEL_ID, [messageId]), |
| 174 | + [messageId], |
| 175 | + ); |
| 176 | +}); |
0 commit comments