char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Aleksander Grygierui: Improve Chat Messages rendering performance (#28460)0afb805b1

char/aliceai-t5-moe
4.7 KiB146 linesraw
1import {
2	extractSearchQuery,
3	extractSearchResults,
4	faviconForUrl,
5	isWebSearchToolName,
6	looksLikeSearchResult
7} from '$lib/utils/search-results';
8import { describe, expect, it } from 'vitest';
9
10describe('extractSearchResults', () => {
11	it('parses the Exa fixture with multiple results', () => {
12		const 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...`;
33		const results = extractSearchResults(fixture);
34
35		expect(results.length).toBe(3);
36		expect(results[0].title).toContain('World Cup 2026');
37		expect(results[0].url).toBe('https://www.fifa.com/articles/match-schedule');
38		expect(results[0].published).toBe('2026-06-22T00:01:00.000Z');
39		// N/A filtered out
40		expect(results[0].author).toBeUndefined();
41		expect(results[0].highlights).toContain('match schedule');
42		expect(results[1].author).toBe('ESPN');
43		expect(results[2].author).toBeUndefined(); // N/A filtered
44	});
45
46	it('returns empty array for empty input', () => {
47		expect(extractSearchResults('')).toEqual([]);
48		expect(extractSearchResults(undefined)).toEqual([]);
49		expect(extractSearchResults(null)).toEqual([]);
50	});
51
52	it('skips chunks missing title or url', () => {
53		const 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`;
64		const results = extractSearchResults(txt);
65
66		// Only middle one should pass (has title + url).
67		expect(results.length).toBe(1);
68		expect(results[0].url).toBe('https://x.com');
69	});
70
71	it('parses a single result without separators', () => {
72		const txt = `Title: only one
73URL: https://example.com/test
74Published: 2026-01-01T00:00:00Z
75Author: alice
76Highlights:
77a highlight`;
78		const results = extractSearchResults(txt);
79
80		expect(results.length).toBe(1);
81		expect(results[0].title).toBe('only one');
82		expect(results[0].author).toBe('alice');
83		expect(results[0].highlights).toBe('a highlight');
84	});
85
86	it('extracts query from JSON toolArgs', () => {
87		expect(extractSearchQuery('{"query":"foo"}')).toBe('foo');
88		expect(extractSearchQuery('  {"query":"  foo  "}  ')).toBe('foo');
89		expect(extractSearchQuery('not json')).toBe('');
90		expect(extractSearchQuery(null)).toBe('');
91		expect(extractSearchQuery('{"query":123}')).toBe('');
92	});
93
94	it('resolves favicon URLs from origins', () => {
95		expect(faviconForUrl('https://example.com/path/to/page')).toBe(
96			'https://example.com/favicon.ico'
97		);
98		expect(faviconForUrl('http://example.com/x')).toBe('http://example.com/favicon.ico');
99		expect(faviconForUrl('not a url')).toBeNull();
100	});
101});
102
103describe('isWebSearchToolName', () => {
104	it('excludes tools that take the same query argument but are not web searches', () => {
105		expect(isWebSearchToolName('search_pull_requests')).toBe(false);
106		expect(isWebSearchToolName('search_code')).toBe(false);
107		expect(isWebSearchToolName('search_repositories')).toBe(false);
108		expect(isWebSearchToolName('search_issues')).toBe(false);
109	});
110
111	it('handles empty / missing input', () => {
112		expect(isWebSearchToolName(null)).toBe(false);
113		expect(isWebSearchToolName(undefined)).toBe(false);
114		expect(isWebSearchToolName('')).toBe(false);
115	});
116
117	it('returns false for unrelated tools', () => {
118		expect(isWebSearchToolName('web_fetch')).toBe(false);
119		expect(isWebSearchToolName('read_file')).toBe(false);
120		expect(isWebSearchToolName('exec_shell_command')).toBe(false);
121	});
122});
123
124describe('extractSearchResults prefilter', () => {
125	it('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
128		const stdout = `${'make[1]: entering directory\n'.repeat(5000)}`;
129
130		expect(extractSearchResults(stdout)).toEqual([]);
131	});
132
133	it('returns an empty result when only one required field is present', () => {
134		expect(extractSearchResults('URL: https://example.com')).toEqual([]);
135		expect(extractSearchResults('Title: only a title')).toEqual([]);
136	});
137});
138
139describe('looksLikeSearchResult', () => {
140	it('requires both Title and URL field markers', () => {
141		expect(looksLikeSearchResult('Title: a\nURL: https://b')).toBe(true);
142		expect(looksLikeSearchResult('URL: https://b')).toBe(false);
143		expect(looksLikeSearchResult('plain stdout')).toBe(false);
144		expect(looksLikeSearchResult(undefined)).toBe(false);
145	});
146});