retry-send.test.ts (4796B)
1 import { describe, it, expect } from 'vitest'; 2 import { 3 generateSecretKey, 4 getPublicKey, 5 finalizeEvent 6 } from 'applesauce-core/helpers'; 7 import { nip44 } from 'applesauce-core/helpers/encryption'; 8 import { 9 decodePrivateRecord, 10 type PrivateRecordHandle 11 } from '../../src/lib/persistence/private-records.ts'; 12 import { 13 privateRetryTargets, 14 runPrivateRetry, 15 type PrivateRetry 16 } from '../../src/lib/messaging/retry-send.ts'; 17 function fixture(status?: string, role = 'peer') { 18 const key = generateSecretKey(), 19 other = generateSecretKey(); 20 try { 21 const owner = getPublicKey(key), 22 peer = getPublicKey(other), 23 id = '12345678-1234-4234-8234-123456789abc'; 24 function artifact(destination: string) { 25 const conversation = nip44.v2.utils.getConversationKey(key, destination); 26 try { 27 const event = finalizeEvent( 28 { 29 kind: 1059, 30 created_at: 1700000000, 31 tags: [['p', destination]], 32 content: nip44.v2.encrypt('Structural fixture only', conversation) 33 }, 34 key 35 ); 36 return { eventId: event.id, wire: JSON.stringify(event) }; 37 } finally { 38 conversation.fill(0); 39 } 40 } 41 function route(author: string, role: string, origin: string) { 42 return { 43 author, 44 role, 45 targets: [origin], 46 knownBase: { author, id: 'b'.repeat(64), createdAt: 1 }, 47 sources: [{ source: 'wss://discovery.example.org', state: 'eose' }] 48 }; 49 } 50 const self = artifact(owner), 51 peerArtifact = artifact(peer); 52 const row = { 53 schema: 1, 54 family: 'private_send_operation', 55 owner, 56 id, 57 revision: 2, 58 peer, 59 rumorHash: 'a'.repeat(64), 60 createdAt: 1700000000, 61 self, 62 peerArtifact, 63 deliveryPlan: { 64 state: 'prepared', 65 routes: { 66 peer: route(peer, 'peer', 'wss://peer.example.org'), 67 archive: route(owner, 'self_archive', 'wss://archive.example.org') 68 } 69 }, 70 ...(status 71 ? { 72 receipts: [ 73 { 74 actionId: '12345678-1234-4234-8234-123456789abd', 75 role, 76 origin: 77 role === 'peer' 78 ? 'wss://peer.example.org' 79 : 'wss://archive.example.org', 80 attempt: 1, 81 eventId: role === 'peer' ? peerArtifact.eventId : self.eventId, 82 status, 83 observedAtMilliseconds: 100, 84 readbackWire: status === 'readback' ? peerArtifact.wire : null 85 } 86 ] 87 } 88 : {}) 89 }; 90 const decoded = decodePrivateRecord(JSON.stringify(row), owner, id); 91 if (!decoded.ok) throw Error('invalid structural fixture'); 92 return { owner, id, handle: decoded.value, row }; 93 } finally { 94 key.fill(0); 95 other.fill(0); 96 } 97 } 98 describe('explicit exact-artifact retry target selection', () => { 99 it('retains the exact stored role event IDs and target order', () => { 100 const f = fixture(); 101 expect(privateRetryTargets(f.handle, f.owner, f.id)).toEqual([ 102 { 103 role: 'peer', 104 origin: 'wss://peer.example.org', 105 eventId: f.row.peerArtifact.eventId 106 }, 107 { 108 role: 'self_archive', 109 origin: 'wss://archive.example.org', 110 eventId: f.row.self.eventId 111 } 112 ]); 113 }); 114 it('skips only the exact accepted role and preserves the other artifact target', () => { 115 for (const role of ['peer', 'self_archive']) { 116 const f = fixture('accepted', role), 117 targets = privateRetryTargets(f.handle, f.owner, f.id)!; 118 expect(targets).toHaveLength(1); 119 expect(targets[0].role).toBe(role === 'peer' ? 'self_archive' : 'peer'); 120 } 121 }); 122 it('codec-only readback does not impersonate authenticated remote retrieval or ACK', () => { 123 const f = fixture('readback'); 124 expect(privateRetryTargets(f.handle, f.owner, f.id)).toHaveLength(2); 125 }); 126 it('unknown timeout refusal and stopped facts do not supply success', () => { 127 for (const status of ['unknown', 'timed_out', 'refused', 'stopped']) { 128 const f = fixture(status); 129 expect(privateRetryTargets(f.handle, f.owner, f.id)).toHaveLength(2); 130 } 131 }); 132 it('forged handles and a foreign owner cannot select private targets', () => { 133 const f = fixture(); 134 expect( 135 privateRetryTargets({} as PrivateRecordHandle, f.owner, f.id) 136 ).toBeUndefined(); 137 expect(privateRetryTargets(f.handle, 'b'.repeat(64), f.id)).toBeUndefined(); 138 }); 139 it('an arbitrary object cannot start a retry or cause an implicit action', async () => { 140 expect( 141 await runPrivateRetry({} as PrivateRetry, 'reviewed_private_retry') 142 ).toEqual({ status: 'invalid' }); 143 }); 144 });