inbox-setup.test.ts (8437B)
1 import { it, expect } from 'vitest'; 2 import { 3 makeSetupFixture, 4 fixturePolicy, 5 setupInput, 6 inbox, 7 discovery, 8 reviewInboxSetup, 9 inboxSetupPreview, 10 captureInboxSetup, 11 publicRecordSnapshot 12 } from '../e2e/harness/inbox-setup.ts'; 13 import { createIdentitySession } from '../../src/lib/runtime/identity-session.ts'; 14 import type { InboxSetupReview } from '../../src/lib/messaging/inbox-setup.ts'; 15 async function reviewed( 16 f: Awaited<ReturnType<typeof makeSetupFixture>>, 17 input = setupInput() 18 ) { 19 const r = await reviewInboxSetup( 20 f.identity, 21 f.own.resolver, 22 fixturePolicy(), 23 input, 24 () => 101 25 ); 26 if (r.status !== 'review') throw new Error(r.reason); 27 return r.review; 28 } 29 it('Connect alone creates no preference; exact preview captures preserved duplicate and unknown entries', async () => { 30 const f = await makeSetupFixture(); 31 try { 32 const before = f.counts(), 33 review = await reviewed(f), 34 view = inboxSetupPreview(review)!; 35 expect(view.author).toBe(f.owner); 36 expect(view.destinations).toEqual([discovery]); 37 expect(view.selectedInboxes).toEqual([inbox]); 38 expect((JSON.parse(view.wire) as { tags: string[][] }).tags).toEqual([ 39 ...f.event.tags, 40 ['relay', inbox] 41 ]); 42 expect((JSON.parse(view.wire) as { content: string }).content).toBe( 43 f.event.content 44 ); 45 expect(view.globalEffect).toContain('other clients'); 46 expect(f.counts()).toEqual(before); 47 const absent = captureInboxSetup( 48 review, 49 f.own.resolver, 50 crypto.randomUUID(), 51 'connect' 52 ); 53 expect(absent.status).toBe('invalid'); 54 const id = crypto.randomUUID(), 55 capture = captureInboxSetup( 56 review, 57 f.own.resolver, 58 id, 59 'reviewed_global_inbox_replacement' 60 ); 61 expect(capture.status).toBe('captured'); 62 if (capture.status !== 'captured') return; 63 const row = publicRecordSnapshot(capture.record, f.owner, id)!; 64 expect(row.family).toBe('preference_operation'); 65 if (row.family !== 'preference_operation') return; 66 expect(row.source.wire).toBe(view.originalWire); 67 expect(row.capture.wire).toBe(view.wire); 68 expect(row.capture.hash).toBe(view.hash); 69 expect(row.capture.targets).toEqual(view.destinations); 70 expect(row.consent).toBe('explicit_review'); 71 expect(row.artifact).toBeNull(); 72 expect(row.receipts).toEqual([]); 73 expect(f.counts()).toEqual(before); 74 } finally { 75 f.close(); 76 } 77 }); 78 it('more than three existing advertisements are never silently truncated', async () => { 79 const tags = [1, 2, 3, 4, 5].map((n) => [ 80 'relay', 81 'wss://old' + n + '.example.org' 82 ]); 83 const f = await makeSetupFixture(tags); 84 try { 85 const view = inboxSetupPreview(await reviewed(f))!; 86 expect((JSON.parse(view.wire) as { tags: string[][] }).tags).toEqual([ 87 ...tags, 88 ['relay', inbox] 89 ]); 90 } finally { 91 f.close(); 92 } 93 }); 94 it('unknown root fields block canonical replacement unless owner explicitly removes each field', async () => { 95 const f = await makeSetupFixture(undefined, { 96 unknown: { nested: ['keep'] }, 97 another: 'keep' 98 }); 99 try { 100 expect( 101 ( 102 await reviewInboxSetup( 103 f.identity, 104 f.own.resolver, 105 fixturePolicy(), 106 setupInput(), 107 () => 101 108 ) 109 ).status 110 ).toBe('blocked'); 111 expect( 112 ( 113 await reviewInboxSetup( 114 f.identity, 115 f.own.resolver, 116 fixturePolicy(), 117 setupInput({ removeExtraFields: ['unknown'] }), 118 () => 101 119 ) 120 ).status 121 ).toBe('blocked'); 122 const view = inboxSetupPreview( 123 await reviewed( 124 f, 125 setupInput({ removeExtraFields: ['unknown', 'another'] }) 126 ) 127 )!; 128 expect( 129 (JSON.parse(view.originalWire!) as Record<string, unknown>).unknown 130 ).toEqual({ 131 nested: ['keep'] 132 }); 133 expect(view.removedExtraFields).toEqual(['unknown', 'another']); 134 expect(JSON.parse(view.wire)).not.toHaveProperty('unknown'); 135 } finally { 136 f.close(); 137 } 138 }); 139 it('malformed relay entries require explicit removal and retain every unrelated unknown tag', async () => { 140 const tags = [ 141 ['relay', 'https://bad.example.org'], 142 ['unknown', 'keep'], 143 ['relay', 'wss://old.example.org'] 144 ]; 145 const f = await makeSetupFixture(tags); 146 try { 147 expect( 148 ( 149 await reviewInboxSetup( 150 f.identity, 151 f.own.resolver, 152 fixturePolicy(), 153 setupInput(), 154 () => 101 155 ) 156 ).status 157 ).toBe('blocked'); 158 const view = inboxSetupPreview( 159 await reviewed(f, setupInput({ removeTagIndices: [0] })) 160 )!; 161 expect(view.removedTagIndices).toEqual([0]); 162 expect((JSON.parse(view.wire) as { tags: string[][] }).tags).toEqual([ 163 tags[1], 164 tags[2], 165 ['relay', inbox] 166 ]); 167 } finally { 168 f.close(); 169 } 170 }); 171 it('known-head change conflicts rather than authorizing stale global replacement', async () => { 172 const f = await makeSetupFixture(); 173 try { 174 const review = await reviewed(f), 175 fresh = f.resolve(f.newerEvent); 176 expect( 177 captureInboxSetup( 178 review, 179 fresh.resolver, 180 crypto.randomUUID(), 181 'reviewed_global_inbox_replacement' 182 ).status 183 ).toBe('conflict'); 184 } finally { 185 f.close(); 186 } 187 }); 188 it('missing bounded observation can capture null base but partial discovery cannot', async () => { 189 const f = await makeSetupFixture(undefined, {}, true); 190 try { 191 const review = await reviewed(f), 192 id = crypto.randomUUID(), 193 r = captureInboxSetup( 194 review, 195 f.own.resolver, 196 id, 197 'reviewed_global_inbox_replacement' 198 ); 199 expect(r.status).toBe('captured'); 200 if (r.status === 'captured') { 201 const row = publicRecordSnapshot(r.record, f.owner, id)!; 202 if (row.family === 'preference_operation') 203 expect(row.source.wire).toBeNull(); 204 } 205 const incomplete = f.resolve(null, false); 206 expect( 207 ( 208 await reviewInboxSetup( 209 f.identity, 210 incomplete.resolver, 211 fixturePolicy(), 212 setupInput(), 213 () => 101 214 ) 215 ).status 216 ).toBe('blocked'); 217 } finally { 218 f.close(); 219 } 220 }); 221 it('detached preview cannot retarget frozen capture and forged reviews cannot capture', async () => { 222 const f = await makeSetupFixture(); 223 try { 224 const review = await reviewed(f), 225 view = inboxSetupPreview(review)!; 226 view.destinations.push('wss://hint.example.org'); 227 view.selectedInboxes.length = 0; 228 view.removedExtraFields.push('not_selected'); 229 const id = crypto.randomUUID(), 230 r = captureInboxSetup( 231 review, 232 f.own.resolver, 233 id, 234 'reviewed_global_inbox_replacement' 235 ); 236 expect(r.status).toBe('captured'); 237 if (r.status === 'captured') { 238 const row = publicRecordSnapshot(r.record, f.owner, id)!; 239 if (row.family === 'preference_operation') 240 expect(row.capture.targets).toEqual([discovery]); 241 } 242 expect( 243 captureInboxSetup( 244 {} as InboxSetupReview, 245 f.own.resolver, 246 id, 247 'reviewed_global_inbox_replacement' 248 ).status 249 ).toBe('invalid'); 250 } finally { 251 f.close(); 252 } 253 }); 254 it('stale owner or resolver, unsupported clock and unapproved target cannot capture', async () => { 255 const f = await makeSetupFixture(); 256 try { 257 for (const changes of [ 258 { createdAt: 100 }, 259 { createdAt: Number.MAX_SAFE_INTEGER + 1 }, 260 { selectedInboxes: ['wss://hint.example.org'] }, 261 { removeTagIndices: [999] }, 262 { removeExtraFields: ['absent'] } 263 ]) 264 expect( 265 ( 266 await reviewInboxSetup( 267 f.identity, 268 f.own.resolver, 269 fixturePolicy(), 270 setupInput(changes), 271 () => 101 272 ) 273 ).status 274 ).toBe('blocked'); 275 expect( 276 ( 277 await reviewInboxSetup( 278 createIdentitySession(), 279 f.own.resolver, 280 fixturePolicy(), 281 setupInput(), 282 () => 101 283 ) 284 ).status 285 ).toBe('blocked'); 286 const review = await reviewed(f); 287 f.own.stale(); 288 expect( 289 captureInboxSetup( 290 review, 291 f.own.resolver, 292 crypto.randomUUID(), 293 'reviewed_global_inbox_replacement' 294 ).status 295 ).toBe('unavailable'); 296 } finally { 297 f.close(); 298 } 299 });