@@ -264,4 +264,76 @@ describe('util handlers', () => {
264264 expect ( handleFilter ( event as any , { '#t' : [ 'nostr' ] } as any ) . matches ) . toBe ( true )
265265 } )
266266 } )
267+
268+ // --- Fetch branches ---
269+
270+ describe ( 'handleFetch — all nip19 types' , ( ) => {
271+ it ( 'fetches by nevent' , async ( ) => {
272+ const pool = { query : vi . fn ( ) . mockResolvedValue ( [ ] ) }
273+ const nevent = handleEncodeNevent ( 'b' . repeat ( 64 ) )
274+ await handleFetch ( pool as any , 'npub1test' , nevent )
275+ expect ( pool . query ) . toHaveBeenCalledWith ( 'npub1test' , { ids : [ 'b' . repeat ( 64 ) ] } )
276+ } )
277+
278+ it ( 'fetches by nprofile' , async ( ) => {
279+ const pool = { query : vi . fn ( ) . mockResolvedValue ( [ ] ) }
280+ const nprofile = handleEncodeNprofile ( pk , [ 'wss://test.com' ] )
281+ await handleFetch ( pool as any , 'npub1test' , nprofile )
282+ expect ( pool . query ) . toHaveBeenCalledWith ( 'npub1test' , { authors : [ pk ] , kinds : [ 0 ] , limit : 1 } )
283+ } )
284+
285+ it ( 'fetches by naddr' , async ( ) => {
286+ const pool = { query : vi . fn ( ) . mockResolvedValue ( [ ] ) }
287+ const naddr = handleEncodeNaddr ( pk , 30078 , 'test-d' )
288+ await handleFetch ( pool as any , 'npub1test' , naddr )
289+ expect ( pool . query ) . toHaveBeenCalledWith ( 'npub1test' , { authors : [ pk ] , kinds : [ 30078 ] , '#d' : [ 'test-d' ] , limit : 1 } )
290+ } )
291+ } )
292+
293+ // --- NIP list/show (mocked fetch) ---
294+
295+ describe ( 'handleNipList' , ( ) => {
296+ it ( 'parses NIP list from markdown' , async ( ) => {
297+ const md = '- [NIP-01](01.md) — Basic protocol\n- [NIP-17](17.md) — Private DMs\n'
298+ vi . stubGlobal ( 'fetch' , vi . fn ( ) . mockResolvedValue ( { ok : true , text : ( ) => Promise . resolve ( md ) } ) )
299+ const { handleNipList : nipList } = await import ( '../../src/util/handlers.js' )
300+ const result = await nipList ( )
301+ expect ( result . length ) . toBe ( 2 )
302+ expect ( result [ 0 ] . number ) . toBe ( 1 )
303+ expect ( result [ 1 ] . title ) . toBe ( 'Private DMs' )
304+ vi . unstubAllGlobals ( )
305+ } )
306+
307+ it ( 'throws on fetch failure' , async ( ) => {
308+ vi . stubGlobal ( 'fetch' , vi . fn ( ) . mockResolvedValue ( { ok : false , status : 500 } ) )
309+ const { handleNipList : nipList } = await import ( '../../src/util/handlers.js' )
310+ await expect ( nipList ( ) ) . rejects . toThrow ( / 5 0 0 / )
311+ vi . unstubAllGlobals ( )
312+ } )
313+
314+ it ( 'throws on oversized response' , async ( ) => {
315+ vi . stubGlobal ( 'fetch' , vi . fn ( ) . mockResolvedValue ( { ok : true , text : ( ) => Promise . resolve ( 'x' . repeat ( 2_000_000 ) ) } ) )
316+ const { handleNipList : nipList } = await import ( '../../src/util/handlers.js' )
317+ await expect ( nipList ( ) ) . rejects . toThrow ( / t o o l a r g e / )
318+ vi . unstubAllGlobals ( )
319+ } )
320+ } )
321+
322+ describe ( 'handleNipShow' , ( ) => {
323+ it ( 'fetches NIP content' , async ( ) => {
324+ vi . stubGlobal ( 'fetch' , vi . fn ( ) . mockResolvedValue ( { ok : true , text : ( ) => Promise . resolve ( '# NIP-01\n\nBasic protocol' ) } ) )
325+ const { handleNipShow : nipShow } = await import ( '../../src/util/handlers.js' )
326+ const result = await nipShow ( 1 )
327+ expect ( result . number ) . toBe ( 1 )
328+ expect ( result . content ) . toContain ( 'NIP-01' )
329+ vi . unstubAllGlobals ( )
330+ } )
331+
332+ it ( 'throws on not found' , async ( ) => {
333+ vi . stubGlobal ( 'fetch' , vi . fn ( ) . mockResolvedValue ( { ok : false , status : 404 } ) )
334+ const { handleNipShow : nipShow } = await import ( '../../src/util/handlers.js' )
335+ await expect ( nipShow ( 999 ) ) . rejects . toThrow ( / n o t f o u n d / )
336+ vi . unstubAllGlobals ( )
337+ } )
338+ } )
267339} )
0 commit comments