@@ -357,6 +357,66 @@ describe('[Search-Index] When testing URL construction', () => {
357357 } ) ;
358358} ) ;
359359
360+ describe ( '[Search-Index] When limiting search results' , ( ) => {
361+ it ( 'should limit results to 10 items by default' , async ( ) => {
362+ const factory = new SearchIndexFactory ( ) ;
363+ const index = await factory . getIndex ( 'python' ) ;
364+
365+ if ( ! index ?. index || ! index ?. documents ) {
366+ throw new Error ( 'Python index not properly loaded' ) ;
367+ }
368+
369+ // Mock the lunr search to return more than 10 results
370+ const originalSearch = index . index . search ;
371+ index . index . search = jest . fn ( ) . mockImplementation ( ( ) => {
372+ // Generate 20 mock results
373+ return Array . from ( { length : 20 } , ( _ , i ) => ( {
374+ ref : `doc${ i } .html` ,
375+ score : 100 - i , // Decreasing scores
376+ matchData : { }
377+ } ) ) ;
378+ } ) ;
379+
380+ // Perform the search
381+ const results = searchDocuments ( index . index , index . documents , 'common term' ) ;
382+
383+ // Verify results are limited to 10
384+ expect ( results . length ) . toBe ( 10 ) ;
385+
386+ // Restore original search function
387+ index . index . search = originalSearch ;
388+ } ) ;
389+
390+ it ( 'should allow custom limit values' , async ( ) => {
391+ const factory = new SearchIndexFactory ( ) ;
392+ const index = await factory . getIndex ( 'python' ) ;
393+
394+ if ( ! index ?. index || ! index ?. documents ) {
395+ throw new Error ( 'Python index not properly loaded' ) ;
396+ }
397+
398+ // Mock the lunr search to return more than 5 results
399+ const originalSearch = index . index . search ;
400+ index . index . search = jest . fn ( ) . mockImplementation ( ( ) => {
401+ // Generate 20 mock results
402+ return Array . from ( { length : 20 } , ( _ , i ) => ( {
403+ ref : `doc${ i } .html` ,
404+ score : 100 - i , // Decreasing scores
405+ matchData : { }
406+ } ) ) ;
407+ } ) ;
408+
409+ // Perform the search with custom limit of 5
410+ const results = searchDocuments ( index . index , index . documents , 'common term' , 5 ) ;
411+
412+ // Verify results are limited to 5
413+ expect ( results . length ) . toBe ( 5 ) ;
414+
415+ // Restore original search function
416+ index . index . search = originalSearch ;
417+ } ) ;
418+ } ) ;
419+
360420// Add a final summary after all tests
361421afterAll ( ( ) => {
362422 console . log ( '\n===== FINAL TEST SUMMARY =====' ) ;
0 commit comments