|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | + |
| 4 | +function createStorage() { |
| 5 | + const values = new Map(); |
| 6 | + return { |
| 7 | + getItem: (key) => values.get(key) ?? null, |
| 8 | + setItem: (key, value) => values.set(key, String(value)), |
| 9 | + }; |
| 10 | +} |
| 11 | + |
| 12 | +const agentA = "a".repeat(64); |
| 13 | +const agentB = "b".repeat(64); |
| 14 | +const agentC = "c".repeat(64); |
| 15 | +const ownerA = "1".repeat(64); |
| 16 | +const ownerB = "2".repeat(64); |
| 17 | +const storageKey = "buzz:persistent-agent-audiences:v2"; |
| 18 | + |
| 19 | +let loadSequence = 0; |
| 20 | + |
| 21 | +async function loadStore(offset = 0) { |
| 22 | + globalThis.window = { localStorage: createStorage() }; |
| 23 | + loadSequence += 1; |
| 24 | + return import( |
| 25 | + `./persistentAgentAudience.ts?test=${Date.now()}-${offset}-${loadSequence}` |
| 26 | + ); |
| 27 | +} |
| 28 | + |
| 29 | +function savedAudiences() { |
| 30 | + return JSON.parse(window.localStorage.getItem(storageKey)); |
| 31 | +} |
| 32 | + |
| 33 | +test("conversation scopes isolate identities, channels, and threads", async () => { |
| 34 | + const store = await loadStore(); |
| 35 | + const channelA = store.getPersistentAgentAudienceScope({ |
| 36 | + ownerPubkey: ownerA, |
| 37 | + channelId: "channel-a", |
| 38 | + }); |
| 39 | + const channelB = store.getPersistentAgentAudienceScope({ |
| 40 | + ownerPubkey: ownerA, |
| 41 | + channelId: "channel-b", |
| 42 | + }); |
| 43 | + const threadA1 = store.getPersistentAgentAudienceScope({ |
| 44 | + ownerPubkey: ownerA, |
| 45 | + channelId: "channel-a", |
| 46 | + threadRootId: "root-1", |
| 47 | + }); |
| 48 | + const threadA2 = store.getPersistentAgentAudienceScope({ |
| 49 | + ownerPubkey: ownerA, |
| 50 | + channelId: "channel-a", |
| 51 | + threadRootId: "root-2", |
| 52 | + }); |
| 53 | + const otherIdentity = store.getPersistentAgentAudienceScope({ |
| 54 | + ownerPubkey: ownerB, |
| 55 | + channelId: "channel-a", |
| 56 | + }); |
| 57 | + |
| 58 | + for (const scope of [channelA, channelB, threadA1, threadA2, otherIdentity]) { |
| 59 | + assert.ok(scope); |
| 60 | + store.setPersistentAgentAudience(scope, [agentA]); |
| 61 | + } |
| 62 | + |
| 63 | + assert.equal(new Set(Object.keys(savedAudiences())).size, 5); |
| 64 | +}); |
| 65 | + |
| 66 | +test("successful fast send promotes without a persisted draft key", async () => { |
| 67 | + const store = await loadStore(1); |
| 68 | + const scope = store.getPersistentAgentAudienceScope({ |
| 69 | + ownerPubkey: ownerA, |
| 70 | + channelId: "channel-a", |
| 71 | + }); |
| 72 | + store.setPersistentAgentAudienceEnabled(true); |
| 73 | + |
| 74 | + store.promotePersistentAgentAudience({ |
| 75 | + expectedGeneration: store.getPersistentAgentAudienceGeneration(), |
| 76 | + scope, |
| 77 | + expectedRevision: store.getPersistentAgentAudienceRevision(scope), |
| 78 | + explicitAgentPubkeys: [agentA], |
| 79 | + }); |
| 80 | + |
| 81 | + assert.deepEqual(savedAudiences(), { [scope]: [agentA] }); |
| 82 | +}); |
| 83 | + |
| 84 | +test("explicit recipients merge and dedupe after successful send", async () => { |
| 85 | + const store = await loadStore(2); |
| 86 | + const scope = `${ownerA}:channel-a:timeline`; |
| 87 | + store.setPersistentAgentAudienceEnabled(true); |
| 88 | + store.setPersistentAgentAudience(scope, [agentA]); |
| 89 | + const revision = store.getPersistentAgentAudienceRevision(scope); |
| 90 | + |
| 91 | + store.promotePersistentAgentAudience({ |
| 92 | + expectedGeneration: store.getPersistentAgentAudienceGeneration(), |
| 93 | + scope, |
| 94 | + expectedRevision: revision, |
| 95 | + explicitAgentPubkeys: [agentA, agentB], |
| 96 | + }); |
| 97 | + |
| 98 | + assert.deepEqual(savedAudiences(), { [scope]: [agentA, agentB] }); |
| 99 | +}); |
| 100 | + |
| 101 | +test("successful send makes authored mention order authoritative", async () => { |
| 102 | + const store = await loadStore(100); |
| 103 | + const scope = `${ownerA}:channel-a:timeline`; |
| 104 | + store.setPersistentAgentAudienceEnabled(true); |
| 105 | + store.setPersistentAgentAudience(scope, [agentA, agentB]); |
| 106 | + |
| 107 | + store.promotePersistentAgentAudience({ |
| 108 | + expectedGeneration: store.getPersistentAgentAudienceGeneration(), |
| 109 | + scope, |
| 110 | + expectedRevision: store.getPersistentAgentAudienceRevision(scope), |
| 111 | + explicitAgentPubkeys: [agentB, agentA, agentC], |
| 112 | + }); |
| 113 | + |
| 114 | + assert.deepEqual(savedAudiences(), { |
| 115 | + [scope]: [agentB, agentA, agentC], |
| 116 | + }); |
| 117 | +}); |
| 118 | + |
| 119 | +test("successful send retains saved targets absent from the draft", async () => { |
| 120 | + const store = await loadStore(101); |
| 121 | + const scope = `${ownerA}:channel-a:timeline`; |
| 122 | + store.setPersistentAgentAudienceEnabled(true); |
| 123 | + store.setPersistentAgentAudience(scope, [agentA, agentC]); |
| 124 | + |
| 125 | + store.promotePersistentAgentAudience({ |
| 126 | + expectedGeneration: store.getPersistentAgentAudienceGeneration(), |
| 127 | + scope, |
| 128 | + expectedRevision: store.getPersistentAgentAudienceRevision(scope), |
| 129 | + explicitAgentPubkeys: [agentB, agentA], |
| 130 | + }); |
| 131 | + |
| 132 | + assert.deepEqual(savedAudiences(), { |
| 133 | + [scope]: [agentB, agentA, agentC], |
| 134 | + }); |
| 135 | +}); |
| 136 | + |
| 137 | +test("removal while send awaits wins over late success", async () => { |
| 138 | + const store = await loadStore(3); |
| 139 | + const scope = `${ownerA}:channel-a:timeline`; |
| 140 | + store.setPersistentAgentAudienceEnabled(true); |
| 141 | + store.setPersistentAgentAudience(scope, [agentA]); |
| 142 | + const revisionAtSubmit = store.getPersistentAgentAudienceRevision(scope); |
| 143 | + |
| 144 | + store.removePersistentAgentAudienceMember(scope, agentA); |
| 145 | + store.promotePersistentAgentAudience({ |
| 146 | + expectedGeneration: store.getPersistentAgentAudienceGeneration(), |
| 147 | + scope, |
| 148 | + expectedRevision: revisionAtSubmit, |
| 149 | + explicitAgentPubkeys: [agentA], |
| 150 | + }); |
| 151 | + |
| 152 | + assert.deepEqual(savedAudiences(), { [scope]: [] }); |
| 153 | +}); |
| 154 | + |
| 155 | +test("removing final chip preserves an explicit empty scope", async () => { |
| 156 | + const store = await loadStore(4); |
| 157 | + const scope = `${ownerA}:channel-a:thread:root`; |
| 158 | + store.setPersistentAgentAudience(scope, [agentA]); |
| 159 | + store.removePersistentAgentAudienceMember(scope, agentA); |
| 160 | + |
| 161 | + assert.deepEqual(savedAudiences(), { [scope]: [] }); |
| 162 | +}); |
| 163 | + |
| 164 | +test("completion after disabling cannot repopulate audiences", async () => { |
| 165 | + const store = await loadStore(5); |
| 166 | + const scope = `${ownerA}:channel-a:timeline`; |
| 167 | + store.setPersistentAgentAudienceEnabled(true); |
| 168 | + store.setPersistentAgentAudience(scope, [agentA]); |
| 169 | + const revisionAtSubmit = store.getPersistentAgentAudienceRevision(scope); |
| 170 | + store.setPersistentAgentAudienceEnabled(false); |
| 171 | + |
| 172 | + store.promotePersistentAgentAudience({ |
| 173 | + expectedGeneration: store.getPersistentAgentAudienceGeneration(), |
| 174 | + scope, |
| 175 | + expectedRevision: revisionAtSubmit, |
| 176 | + explicitAgentPubkeys: [agentB], |
| 177 | + }); |
| 178 | + |
| 179 | + assert.deepEqual(savedAudiences(), {}); |
| 180 | +}); |
| 181 | + |
| 182 | +test("invalid, duplicate, and differently-cased pubkeys normalize", async () => { |
| 183 | + const store = await loadStore(6); |
| 184 | + const scope = `${ownerA}:channel-a:timeline`; |
| 185 | + store.setPersistentAgentAudience(scope, [ |
| 186 | + agentA.toUpperCase(), |
| 187 | + agentA, |
| 188 | + "bad", |
| 189 | + ]); |
| 190 | + |
| 191 | + assert.deepEqual(savedAudiences(), { [scope]: [agentA] }); |
| 192 | +}); |
| 193 | + |
| 194 | +test("new recipients retain explicit mention order", async () => { |
| 195 | + const store = await loadStore(9); |
| 196 | + const scope = `${ownerA}:channel-a:timeline`; |
| 197 | + store.setPersistentAgentAudienceEnabled(true); |
| 198 | + |
| 199 | + store.promotePersistentAgentAudience({ |
| 200 | + expectedGeneration: store.getPersistentAgentAudienceGeneration(), |
| 201 | + scope, |
| 202 | + expectedRevision: store.getPersistentAgentAudienceRevision(scope), |
| 203 | + explicitAgentPubkeys: [agentB, agentA], |
| 204 | + }); |
| 205 | + |
| 206 | + assert.deepEqual(savedAudiences(), { [scope]: [agentB, agentA] }); |
| 207 | +}); |
| 208 | + |
| 209 | +test("first new-message send resolves its destination after capturing generation", async () => { |
| 210 | + const store = await loadStore(7); |
| 211 | + const capturedGeneration = store.getPersistentAgentAudienceGeneration(); |
| 212 | + store.setPersistentAgentAudienceEnabled(true); |
| 213 | + const scope = store.getPersistentAgentAudienceScope({ |
| 214 | + ownerPubkey: ownerA, |
| 215 | + channelId: "resolved-dm", |
| 216 | + }); |
| 217 | + |
| 218 | + store.promotePersistentAgentAudience({ |
| 219 | + expectedGeneration: capturedGeneration, |
| 220 | + expectedRevision: null, |
| 221 | + scope, |
| 222 | + explicitAgentPubkeys: [agentA], |
| 223 | + }); |
| 224 | + |
| 225 | + assert.deepEqual(savedAudiences(), { [scope]: [agentA] }); |
| 226 | +}); |
| 227 | + |
| 228 | +test("disable during new-message destination preparation invalidates promotion", async () => { |
| 229 | + const store = await loadStore(8); |
| 230 | + store.setPersistentAgentAudienceEnabled(true); |
| 231 | + const capturedGeneration = store.getPersistentAgentAudienceGeneration(); |
| 232 | + store.setPersistentAgentAudienceEnabled(false); |
| 233 | + store.setPersistentAgentAudienceEnabled(true); |
| 234 | + const scope = store.getPersistentAgentAudienceScope({ |
| 235 | + ownerPubkey: ownerA, |
| 236 | + channelId: "resolved-dm", |
| 237 | + }); |
| 238 | + |
| 239 | + store.promotePersistentAgentAudience({ |
| 240 | + expectedGeneration: capturedGeneration, |
| 241 | + expectedRevision: null, |
| 242 | + scope, |
| 243 | + explicitAgentPubkeys: [agentA], |
| 244 | + }); |
| 245 | + |
| 246 | + assert.deepEqual(savedAudiences(), {}); |
| 247 | +}); |
0 commit comments