inbox-access-check.test.ts (3038B)
1 import { afterEach, expect, it, vi } from 'vitest'; 2 afterEach(() => vi.unstubAllGlobals()); 3 for (const browser of [false, true]) { 4 it(`cold ${browser ? 'browser-shaped' : 'SSR'} check and review forgeries have no effects`, async () => { 5 vi.resetModules(); 6 let effects = 0; 7 const trap = () => { 8 effects++; 9 throw Error('unadmitted check effect'); 10 }; 11 if (browser) { 12 vi.stubGlobal('window', { 13 get nostr() { 14 return trap(); 15 } 16 }); 17 vi.stubGlobal('navigator', { 18 get locks() { 19 return trap(); 20 } 21 }); 22 vi.stubGlobal('indexedDB', { open: trap }); 23 vi.stubGlobal('WebSocket', trap); 24 } 25 const api = await import('../../src/lib/messaging/inbox-access-check.ts'); 26 const access = 27 await import('../../src/lib/messaging/inbox-access-controller.ts'); 28 type Args = Parameters<typeof api.createInboxAccessCheck>; 29 const args = [{}, {}, {}, {}, {}, {}] as unknown as Args; 30 expect(api.createInboxAccessCheck(...args)).toBeUndefined(); 31 for (const value of [ 32 undefined, 33 null, 34 false, 35 0, 36 '', 37 Symbol(), 38 {}, 39 { 40 current: () => true, 41 state: 'qualified', 42 sourceOnly: false, 43 mode: 'original', 44 disclosures: ['reviewed'], 45 command: 'a'.repeat(64) 46 } 47 ]) { 48 const token = value as Parameters<typeof api.inboxAccessCheckSnapshot>[0]; 49 const review = value as Parameters< 50 typeof api.inboxAccessCheckReviewSnapshot 51 >[0]; 52 expect(api.inboxAccessCheckSnapshot(token)).toBeUndefined(); 53 expect(api.inboxAccessCheckQualification(token)).toBeUndefined(); 54 expect(api.inboxAccessCheckReviewSnapshot(review)).toBeUndefined(); 55 expect( 56 api.reviewInboxAccessCheck(token, 'original', 'wss://inbox.example.org') 57 ).toBeUndefined(); 58 expect(api.reviewInboxAccessCheckCleanup(token)).toBeUndefined(); 59 expect(api.reviewInboxAccessCheckReconciliation(token)).toBeUndefined(); 60 expect( 61 (await api.runInboxAccessCheck(review, 'reviewed_inbox_access_check')) 62 .status 63 ).toBe('invalid'); 64 expect( 65 ( 66 await api.cleanupInboxAccessCheck( 67 review, 68 'reviewed_delete_local_inbox_probe' 69 ) 70 ).status 71 ).toBe('invalid'); 72 expect(api.stopInboxAccessCheck(token)).toBe(false); 73 expect( 74 ( 75 await api.reconcileInboxAccessCheck( 76 review, 77 'reviewed_inbox_access_check_reconciliation' 78 ) 79 ).status 80 ).toBe('invalid'); 81 expect( 82 api.retryInboxAccessCheckCleanup( 83 token, 84 'reviewed_inbox_access_check_cleanup_retry' 85 ) 86 ).toBe(false); 87 expect( 88 access.inboxAccessControllerSourceSnapshot( 89 value as Parameters< 90 typeof access.inboxAccessControllerSourceSnapshot 91 >[0] 92 ) 93 ).toBeUndefined(); 94 } 95 expect(effects).toBe(0); 96 }); 97 }