private-publisher.test.ts (2772B)
1 import { describe, it, expect } from 'vitest'; 2 import { 3 capturePrivatePublication, 4 privatePublicationSnapshot, 5 takePrivatePublication, 6 type PrivatePublication 7 } from '../../src/lib/nostr/private-publisher.ts'; 8 import { 9 publishPrivateGiftWrapAttempt, 10 type PrivatePool 11 } from '../../src/lib/nostr/private-pool.ts'; 12 import type { PrivateSession } from '../../src/lib/runtime/private-session.ts'; 13 import type { PrivateStorageRepository } from '../../src/lib/persistence/private-storage.ts'; 14 import type { PairedDeliveryAcknowledgement } from '../../src/lib/persistence/private-sends.ts'; 15 import type { PairedDeliveryContext } from '../../src/lib/messaging/prepare-send.ts'; 16 describe('private publication has no arbitrary event sink', () => { 17 it('SSR refuses forged local acknowledgement and publication without effects', async () => { 18 const session = {} as PrivateSession; 19 expect( 20 capturePrivatePublication( 21 {} as PrivateStorageRepository, 22 session, 23 {} as PairedDeliveryAcknowledgement, 24 {} as PairedDeliveryContext, 25 'peer', 26 'wss://peer.example.org', 27 'reviewed_private_delivery' 28 ) 29 ).toBeUndefined(); 30 expect( 31 privatePublicationSnapshot({} as PrivatePublication) 32 ).toBeUndefined(); 33 expect( 34 await takePrivatePublication( 35 {} as PrivatePublication, 36 session, 37 {} as PairedDeliveryContext['policy'], 38 'wss://peer.example.org' 39 ) 40 ).toBeUndefined(); 41 expect( 42 await publishPrivateGiftWrapAttempt( 43 {} as PrivatePool, 44 {} as PrivatePublication, 45 new AbortController().signal 46 ) 47 ).toEqual({ status: 'stopped' }); 48 }); 49 it.each([14, 13, 1059])( 50 'a caller event of kind %i cannot act as publication permission', 51 async (kind) => { 52 const raw = { 53 kind, 54 content: 'PRIVATE_TEXT', 55 tags: [], 56 id: '0'.repeat(64) 57 } as unknown as PrivatePublication; 58 expect(privatePublicationSnapshot(raw)).toBeUndefined(); 59 expect( 60 await publishPrivateGiftWrapAttempt( 61 {} as PrivatePool, 62 raw, 63 new AbortController().signal 64 ) 65 ).toEqual({ status: 'stopped' }); 66 } 67 ); 68 it('review objects and forged capabilities are not coerced', () => { 69 let calls = 0; 70 const review = { 71 toString() { 72 calls++; 73 return 'reviewed_private_delivery'; 74 } 75 }; 76 expect( 77 capturePrivatePublication( 78 {} as PrivateStorageRepository, 79 {} as PrivateSession, 80 {} as PairedDeliveryAcknowledgement, 81 {} as PairedDeliveryContext, 82 'peer', 83 'wss://peer.example.org', 84 review 85 ) 86 ).toBeUndefined(); 87 expect(calls).toBe(0); 88 }); 89 });