private-outbox-settlement.test.ts (5827B)
1 import { describe, expect, it } from 'vitest'; 2 import type { 3 PrivateSendOperation, 4 PrivateTargetReceipt 5 } from '../../src/lib/persistence/private-records.ts'; 6 import { 7 privateRecordSettlement, 8 privateInventoryRow, 9 PRIVATE_OUTBOX_CLEANUP_CONSEQUENCES 10 } from '../../src/lib/persistence/local-inventory.ts'; 11 const owner = 'a'.repeat(64), 12 peer = 'b'.repeat(64), 13 id = '12345678-1234-4234-8234-123456789abc'; 14 const route = (role: 'peer' | 'self_archive', origin: string) => ({ 15 role, 16 author: role === 'peer' ? peer : owner, 17 targets: [origin], 18 knownBase: { 19 author: role === 'peer' ? peer : owner, 20 id: 'c'.repeat(64), 21 createdAt: 100 22 }, 23 sources: [{ source: 'wss://discovery.example.org', state: 'eose' as const }] 24 }); 25 const routes = { 26 peer: route('peer', 'wss://peer.example.org'), 27 archive: route('self_archive', 'wss://archive.example.org') 28 }; 29 function fact( 30 role: 'peer' | 'self_archive', 31 status: PrivateTargetReceipt['status'] = 'accepted', 32 origin = role === 'peer' ? routes.peer.targets[0] : routes.archive.targets[0] 33 ): PrivateTargetReceipt { 34 return { 35 actionId: id, 36 origin, 37 role, 38 attempt: 1, 39 eventId: role === 'peer' ? 'd'.repeat(64) : 'e'.repeat(64), 40 status, 41 observedAtMilliseconds: 1000, 42 readbackWire: null 43 }; 44 } 45 // Pure structural bookkeeping inputs only. These objects cannot admit storage, 46 // mint genuine sender/relay receipts, authorize deletion or qualify delivery. 47 const operation: PrivateSendOperation = { 48 schema: 1, 49 family: 'private_send_operation', 50 owner, 51 peer, 52 id, 53 revision: 1, 54 rumorHash: 'f'.repeat(64), 55 createdAt: 100, 56 self: { eventId: 'e'.repeat(64), wire: 'controlled' }, 57 peerArtifact: { eventId: 'd'.repeat(64), wire: 'controlled' }, 58 deliveryPlan: { state: 'prepared', routes }, 59 receipts: [fact('peer'), fact('self_archive')] 60 }; 61 describe('conservative settled private-outbox eligibility', () => { 62 it('requires both artifacts, an exact prepared route and every current named acceptance', () => { 63 expect(privateRecordSettlement(operation)).toBe('settled'); 64 expect(privateRecordSettlement({ ...operation, peerArtifact: null })).toBe( 65 'protected' 66 ); 67 expect( 68 privateRecordSettlement({ ...operation, deliveryPlan: undefined }) 69 ).toBe('protected'); 70 expect(privateRecordSettlement({ ...operation, receipts: [] })).toBe( 71 'protected' 72 ); 73 expect( 74 privateRecordSettlement({ ...operation, receipts: [fact('peer')] }) 75 ).toBe('protected'); 76 expect( 77 privateRecordSettlement({ 78 ...operation, 79 family: 'private_send_reservation', 80 revision: 0 81 }) 82 ).toBe('protected'); 83 }); 84 for (const status of [ 85 'refused', 86 'readback', 87 'unknown', 88 'timed_out', 89 'stopped' 90 ] as const) 91 it('a current ' + status + ' target is not acceptance', () => { 92 expect( 93 privateRecordSettlement({ 94 ...operation, 95 receipts: [fact('peer', status), fact('self_archive')] 96 }) 97 ).toBe('protected'); 98 }); 99 it('a different event, role or origin cannot satisfy the current target', () => { 100 for (const changed of [ 101 { eventId: '1'.repeat(64) }, 102 { role: 'self_archive' as const }, 103 { origin: 'wss://other.example.org' } 104 ]) 105 expect( 106 privateRecordSettlement({ 107 ...operation, 108 receipts: [{ ...fact('peer'), ...changed }, fact('self_archive')] 109 }) 110 ).toBe('protected'); 111 }); 112 for (const status of ['unknown', 'timed_out', 'stopped'] as const) 113 it( 114 'later exact acceptance resolves ' + 115 status + 116 ' while a later uncertainty stays protected', 117 () => { 118 expect( 119 privateRecordSettlement({ 120 ...operation, 121 receipts: [fact('peer', status), ...operation.receipts!] 122 }) 123 ).toBe('settled'); 124 expect( 125 privateRecordSettlement({ 126 ...operation, 127 receipts: [...operation.receipts!, fact('peer', status)] 128 }) 129 ).toBe('protected'); 130 } 131 ); 132 it('changing routes or using readback does not resolve an uncertain old target', () => { 133 const uncertain = fact('peer', 'unknown', 'wss://old.example.org'); 134 const changed = { 135 ...operation, 136 deliveryPlan: { 137 ...operation.deliveryPlan!, 138 previousRoutes: [{ ...routes, peer: route('peer', uncertain.origin) }] 139 } 140 }; 141 expect( 142 privateRecordSettlement({ 143 ...changed, 144 receipts: [uncertain, ...operation.receipts!] 145 }) 146 ).toBe('protected'); 147 expect( 148 privateRecordSettlement({ 149 ...changed, 150 receipts: [ 151 uncertain, 152 fact('peer', 'readback', uncertain.origin), 153 ...operation.receipts! 154 ] 155 }) 156 ).toBe('protected'); 157 expect( 158 privateRecordSettlement({ 159 ...changed, 160 receipts: [ 161 uncertain, 162 fact('peer', 'accepted', uncertain.origin), 163 ...operation.receipts! 164 ] 165 }) 166 ).toBe('settled'); 167 }); 168 it('an earlier definitive refusal on an old route does not create an unresolved effect', () => { 169 expect( 170 privateRecordSettlement({ 171 ...operation, 172 receipts: [ 173 fact('peer', 'refused', 'wss://old.example.org'), 174 ...operation.receipts! 175 ] 176 }) 177 ).toBe('settled'); 178 }); 179 it('inventory exposes only local bounded status and discloses recovery/deduplication loss', () => { 180 expect(privateInventoryRow(operation, 321)).toEqual({ 181 id, 182 revision: 1, 183 logicalBytes: 321, 184 state: 'settled' 185 }); 186 expect(PRIVATE_OUTBOX_CLEANUP_CONSEQUENCES).toContain( 187 'duplicate-prevention' 188 ); 189 expect(PRIVATE_OUTBOX_CLEANUP_CONSEQUENCES).toContain( 190 'does not erase remote' 191 ); 192 }); 193 });