search-query.test.ts (1965B)
1 import { describe, it, expect } from 'vitest'; 2 import { 3 validateRelayPolicy, 4 readRelayPolicy 5 } from '../../src/lib/config/relays.ts'; 6 import { nip50SearchQuery } from '../../src/lib/nostr/search-query.ts'; 7 import { qualifiedNip50Sources } from '../../src/lib/nostr/search-sources.ts'; 8 const policy = validateRelayPolicy( 9 JSON.stringify({ 10 schemaVersion: 1, 11 public: [ 12 { 13 origin: 'wss://one.example.org', 14 read: true, 15 write: false, 16 nip50: true 17 }, 18 { 19 origin: 'wss://two.example.org', 20 read: true, 21 write: false, 22 nip50: false 23 }, 24 { 25 origin: 'wss://three.example.org', 26 read: false, 27 write: true, 28 nip50: false 29 } 30 ], 31 inbox: [], 32 postingEnabled: false, 33 messagingEnabled: false, 34 operatorDenylist: [] 35 }) 36 )!; 37 describe('optional NIP50 request preparation', () => { 38 it('selects only genuine manifest-qualified readable sources', () => { 39 expect(qualifiedNip50Sources(policy)).toEqual(['wss://one.example.org']); 40 const detached = readRelayPolicy(policy); 41 expect(detached.public).toHaveLength(3); 42 expect(() => qualifiedNip50Sources({} as typeof policy)).toThrow( 43 'relay_policy_invalid' 44 ); 45 }); 46 it('normalizes public query copies and requests one finite kind30402 sample without a cursor', () => { 47 expect(nip50SearchQuery(' CARROTS\n celery ')).toEqual([ 48 { kinds: [30402], search: 'carrots celery', limit: 100 } 49 ]); 50 }); 51 it('empty queries never request relevance sampling', () => { 52 expect(nip50SearchQuery(' \n ')).toEqual([]); 53 }); 54 it('refuses unsafe query inputs under unchanged byte and term bounds', () => { 55 expect(() => nip50SearchQuery('x'.repeat(513))).toThrow('query_too_long'); 56 expect(() => nip50SearchQuery(Array(13).fill('a').join(' '))).toThrow( 57 'too_many_terms' 58 ); 59 expect(() => nip50SearchQuery({})).toThrow(); 60 }); 61 });