inbox-probe-cleanup.test.ts (4840B)
1 import { afterEach, expect, it, vi } from 'vitest'; 2 import type { PrivateSession } from '../../src/lib/runtime/private-session.ts'; 3 import type { PrivateStorageRepository } from '../../src/lib/persistence/private-storage.ts'; 4 import type { RelayPolicy } from '../../src/lib/config/relays.ts'; 5 afterEach(() => vi.unstubAllGlobals()); 6 for (const browser of [false, true]) 7 it(`cold ${browser ? 'browser-shaped' : 'SSR'} probe cleanup refuses forged source, native and retirement authority without effects`, async () => { 8 vi.resetModules(); 9 let effects = 0; 10 const trap = () => { 11 effects++; 12 throw Error('unadmitted cleanup effect'); 13 }; 14 if (browser) { 15 vi.stubGlobal('window', { 16 get nostr() { 17 return trap(); 18 } 19 }); 20 vi.stubGlobal('navigator', { 21 get locks() { 22 return trap(); 23 } 24 }); 25 vi.stubGlobal('indexedDB', { open: trap }); 26 vi.stubGlobal('WebSocket', trap); 27 } 28 const cleanup = 29 await import('../../src/lib/persistence/inbox-probe-cleanup.ts'), 30 journal = await import('../../src/lib/persistence/inbox-probes.ts'), 31 native = await import('../../src/lib/persistence/private-storage.ts'), 32 publisher = await import('../../src/lib/nostr/private-publisher.ts'); 33 const repository = {} as PrivateStorageRepository, 34 session = {} as PrivateSession, 35 policy = {} as RelayPolicy; 36 for (const value of [ 37 undefined, 38 null, 39 false, 40 0, 41 '', 42 Symbol(), 43 {}, 44 { status: 'nonce_verified', current: () => true }, 45 { status: 'removed', removed: 1, selection: {} } 46 ]) { 47 expect( 48 await cleanup.captureInboxProbeCleanup( 49 repository, 50 session, 51 policy, 52 value as Parameters<typeof cleanup.captureInboxProbeCleanup>[3], 53 [], 54 'reviewed_inbox_probe_cleanup_selection' 55 ) 56 ).toBeUndefined(); 57 expect( 58 await cleanup.deleteReviewedInboxProbe( 59 value as Parameters<typeof cleanup.deleteReviewedInboxProbe>[0], 60 'reviewed_delete_local_inbox_probe' 61 ) 62 ).toEqual({ status: 'invalid' }); 63 expect( 64 cleanup.inboxProbeCleanupAdmission( 65 value as Parameters<typeof cleanup.inboxProbeCleanupAdmission>[0], 66 repository, 67 session 68 ) 69 ).toBeUndefined(); 70 expect( 71 await native.removePrivateInboxProbe( 72 repository, 73 session, 74 value as Parameters<typeof native.removePrivateInboxProbe>[2] 75 ) 76 ).toEqual({ 77 ok: false, 78 reason: 'invalid_scope', 79 disposition: 'not_removed' 80 }); 81 expect( 82 native.privateProbeRemovalEvidence( 83 value as Parameters<typeof native.privateProbeRemovalEvidence>[0], 84 repository, 85 session 86 ) 87 ).toBeUndefined(); 88 expect( 89 publisher.acquireInboxProbeCleanupLease( 90 repository, 91 session, 92 policy, 93 value as Parameters<typeof publisher.acquireInboxProbeCleanupLease>[3] 94 ) 95 ).toBeUndefined(); 96 expect( 97 publisher.inboxProbeCleanupReady( 98 repository, 99 session, 100 policy, 101 value as Parameters<typeof publisher.inboxProbeCleanupReady>[3] 102 ) 103 ).toBe(false); 104 expect( 105 journal.appliedInboxProbeArchiveAdmission( 106 value as Parameters< 107 typeof journal.appliedInboxProbeArchiveAdmission 108 >[0] 109 ) 110 ).toBeUndefined(); 111 publisher.reconcileInboxProbeArchiveAdmission( 112 value as Parameters< 113 typeof publisher.reconcileInboxProbeArchiveAdmission 114 >[0] 115 ); 116 expect( 117 publisher.retireInboxProbeCleanupLease( 118 value as Parameters<typeof publisher.retireInboxProbeCleanupLease>[0], 119 {} as Parameters<typeof publisher.retireInboxProbeCleanupLease>[1] 120 ) 121 ).toBe(false); 122 expect( 123 journal.retireInboxProbeCleanupSelection( 124 value as Parameters< 125 typeof journal.retireInboxProbeCleanupSelection 126 >[0], 127 {} as Parameters<typeof journal.retireInboxProbeCleanupSelection>[1] 128 ) 129 ).toBe(false); 130 expect( 131 cleanup.inboxProbeRemovalAcknowledgementBinding( 132 value as Parameters< 133 typeof cleanup.inboxProbeRemovalAcknowledgementBinding 134 >[0], 135 repository, 136 session, 137 policy 138 ) 139 ).toBeUndefined(); 140 cleanup.stopInboxProbeCleanup( 141 value as Parameters<typeof cleanup.stopInboxProbeCleanup>[0] 142 ); 143 publisher.releaseInboxProbeCleanupLease( 144 value as Parameters<typeof publisher.releaseInboxProbeCleanupLease>[0] 145 ); 146 } 147 expect(effects).toBe(0); 148 });