char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
0afb805b1
char/aliceai-t5-moe
1import { 2extractSearchQuery , 3extractSearchResults , 4faviconForUrl , 5isWebSearchToolName , 6looksLikeSearchResult 7} from '$lib/utils/search-results' ; 8import { describe , expect , it } from 'vitest' ; 9 10describe ( 'extractSearchResults' , () => { 11it ( 'parses the Exa fixture with multiple results' , () => { 12const fixture = `Title: World Cup 2026 | Match schedule, fixtures 13URL: https://www.fifa.com/articles/match-schedule 14Published: 2026-06-22T00:01:00.000Z 15Author: N/A 16Highlights: 17Find out the full match schedule for World Cup 2026 18--- 19Title: 2026 FIFA World Cup match schedule 20URL: https://www.espn.com/soccer/story/abc/def 21Published: 2026-07-08T07:07:00.000Z 22Author: ESPN 23Highlights: 24Round of 32 · Tuesday, July 7 25--- 26Title: BBC 27URL: https://www.bbc.co.uk/sport/football/world-cup/schedule 28Published: N/A 29Author: N/A 30Highlights: 31# FIFA World Cup Schedule 32...` ; 33const results = extractSearchResults ( fixture ); 34 35expect ( results . length ). toBe ( 3 ); 36expect ( results [ 0 ]. title ). toContain ( 'World Cup 2026' ); 37expect ( results [ 0 ]. url ). toBe ( 'https://www.fifa.com/articles/match-schedule' ); 38expect ( results [ 0 ]. published ). toBe ( '2026-06-22T00:01:00.000Z' ); 39// N/A filtered out 40expect ( results [ 0 ]. author ). toBeUndefined (); 41expect ( results [ 0 ]. highlights ). toContain ( 'match schedule' ); 42expect ( results [ 1 ]. author ). toBe ( 'ESPN' ); 43expect ( results [ 2 ]. author ). toBeUndefined (); // N/A filtered 44}); 45 46it ( 'returns empty array for empty input' , () => { 47expect ( extractSearchResults ( '' )). toEqual ([]); 48expect ( extractSearchResults ( undefined )). toEqual ([]); 49expect ( extractSearchResults ( null )). toEqual ([]); 50}); 51 52it ( 'skips chunks missing title or url' , () => { 53const txt = `Title: no url here 54Highlights: 55foo 56--- 57Title: foo 58URL: https://x.com 59--- 60just a paragraph 61--- 62Title: b 63URL: not a url` ; 64const results = extractSearchResults ( txt ); 65 66// Only middle one should pass (has title + url). 67expect ( results . length ). toBe ( 1 ); 68expect ( results [ 0 ]. url ). toBe ( 'https://x.com' ); 69}); 70 71it ( 'parses a single result without separators' , () => { 72const txt = `Title: only one 73URL: https://example.com/test 74Published: 2026-01-01T00:00:00Z 75Author: alice 76Highlights: 77a highlight` ; 78const results = extractSearchResults ( txt ); 79 80expect ( results . length ). toBe ( 1 ); 81expect ( results [ 0 ]. title ). toBe ( 'only one' ); 82expect ( results [ 0 ]. author ). toBe ( 'alice' ); 83expect ( results [ 0 ]. highlights ). toBe ( 'a highlight' ); 84}); 85 86it ( 'extracts query from JSON toolArgs' , () => { 87expect ( extractSearchQuery ( '{"query":"foo"}' )). toBe ( 'foo' ); 88expect ( extractSearchQuery ( ' {"query":" foo "} ' )). toBe ( 'foo' ); 89expect ( extractSearchQuery ( 'not json' )). toBe ( '' ); 90expect ( extractSearchQuery ( null )). toBe ( '' ); 91expect ( extractSearchQuery ( '{"query":123}' )). toBe ( '' ); 92}); 93 94it ( 'resolves favicon URLs from origins' , () => { 95expect ( faviconForUrl ( 'https://example.com/path/to/page' )). toBe ( 96'https://example.com/favicon.ico' 97); 98expect ( faviconForUrl ( 'http://example.com/x' )). toBe ( 'http://example.com/favicon.ico' ); 99expect ( faviconForUrl ( 'not a url' )). toBeNull (); 100}); 101}); 102 103describe ( 'isWebSearchToolName' , () => { 104it ( 'excludes tools that take the same query argument but are not web searches' , () => { 105expect ( isWebSearchToolName ( 'search_pull_requests' )). toBe ( false ); 106expect ( isWebSearchToolName ( 'search_code' )). toBe ( false ); 107expect ( isWebSearchToolName ( 'search_repositories' )). toBe ( false ); 108expect ( isWebSearchToolName ( 'search_issues' )). toBe ( false ); 109}); 110 111it ( 'handles empty / missing input' , () => { 112expect ( isWebSearchToolName ( null )). toBe ( false ); 113expect ( isWebSearchToolName ( undefined )). toBe ( false ); 114expect ( isWebSearchToolName ( '' )). toBe ( false ); 115}); 116 117it ( 'returns false for unrelated tools' , () => { 118expect ( isWebSearchToolName ( 'web_fetch' )). toBe ( false ); 119expect ( isWebSearchToolName ( 'read_file' )). toBe ( false ); 120expect ( isWebSearchToolName ( 'exec_shell_command' )). toBe ( false ); 121}); 122}); 123 124describe ( 'extractSearchResults prefilter' , () => { 125it ( 'returns the shared empty array for blobs without the wire format' , () => { 126// exec/file tool results never carry Title:/URL: field lines; the 127// cheap prefilter must skip the line-split parse for them 128const stdout = ` ${ 'make[1]: entering directory\n' . repeat ( 5000 )} ` ; 129 130expect ( extractSearchResults ( stdout )). toEqual ([]); 131}); 132 133it ( 'returns an empty result when only one required field is present' , () => { 134expect ( extractSearchResults ( 'URL: https://example.com' )). toEqual ([]); 135expect ( extractSearchResults ( 'Title: only a title' )). toEqual ([]); 136}); 137}); 138 139describe ( 'looksLikeSearchResult' , () => { 140it ( 'requires both Title and URL field markers' , () => { 141expect ( looksLikeSearchResult ( 'Title: a\nURL: https://b' )). toBe ( true ); 142expect ( looksLikeSearchResult ( 'URL: https://b' )). toBe ( false ); 143expect ( looksLikeSearchResult ( 'plain stdout' )). toBe ( false ); 144expect ( looksLikeSearchResult ( undefined )). toBe ( false ); 145}); 146});