|
| 1 | +import { describe, expect, test } from 'bun:test' |
| 2 | +import { extractBraveResults } from '../adapters/braveAdapter' |
| 3 | + |
| 4 | +describe('extractBraveResults', () => { |
| 5 | + test('extracts generic grounding results', () => { |
| 6 | + const results = extractBraveResults({ |
| 7 | + grounding: { |
| 8 | + generic: [ |
| 9 | + { |
| 10 | + title: 'Example Title 1', |
| 11 | + url: 'https://example.com/page1', |
| 12 | + snippets: ['First result description'], |
| 13 | + }, |
| 14 | + { |
| 15 | + title: 'Example Title 2', |
| 16 | + url: 'https://example.com/page2', |
| 17 | + snippets: ['Second result description'], |
| 18 | + }, |
| 19 | + ], |
| 20 | + }, |
| 21 | + }) |
| 22 | + |
| 23 | + expect(results).toEqual([ |
| 24 | + { |
| 25 | + title: 'Example Title 1', |
| 26 | + url: 'https://example.com/page1', |
| 27 | + snippet: 'First result description', |
| 28 | + }, |
| 29 | + { |
| 30 | + title: 'Example Title 2', |
| 31 | + url: 'https://example.com/page2', |
| 32 | + snippet: 'Second result description', |
| 33 | + }, |
| 34 | + ]) |
| 35 | + }) |
| 36 | + |
| 37 | + test('combines generic, poi, and map grounding results', () => { |
| 38 | + const results = extractBraveResults({ |
| 39 | + grounding: { |
| 40 | + generic: [{ title: 'Generic', url: 'https://example.com/generic' }], |
| 41 | + poi: { title: 'POI', url: 'https://maps.example.com/poi' }, |
| 42 | + map: [{ title: 'Map', url: 'https://maps.example.com/map' }], |
| 43 | + }, |
| 44 | + }) |
| 45 | + |
| 46 | + expect(results).toEqual([ |
| 47 | + { title: 'Generic', url: 'https://example.com/generic', snippet: undefined }, |
| 48 | + { title: 'POI', url: 'https://maps.example.com/poi', snippet: undefined }, |
| 49 | + { title: 'Map', url: 'https://maps.example.com/map', snippet: undefined }, |
| 50 | + ]) |
| 51 | + }) |
| 52 | + |
| 53 | + test('joins multiple snippets into one summary string', () => { |
| 54 | + const results = extractBraveResults({ |
| 55 | + grounding: { |
| 56 | + generic: [ |
| 57 | + { |
| 58 | + title: 'Joined Snippets', |
| 59 | + url: 'https://example.com/joined', |
| 60 | + snippets: ['First snippet.', 'Second snippet.'], |
| 61 | + }, |
| 62 | + ], |
| 63 | + }, |
| 64 | + }) |
| 65 | + |
| 66 | + expect(results[0].snippet).toBe('First snippet. Second snippet.') |
| 67 | + }) |
| 68 | + |
| 69 | + test('skips entries without a title or URL', () => { |
| 70 | + const results = extractBraveResults({ |
| 71 | + grounding: { |
| 72 | + generic: [ |
| 73 | + { title: 'Missing URL' }, |
| 74 | + { url: 'https://example.com/missing-title' }, |
| 75 | + { title: 'Valid', url: 'https://example.com/valid' }, |
| 76 | + ], |
| 77 | + }, |
| 78 | + }) |
| 79 | + |
| 80 | + expect(results).toEqual([ |
| 81 | + { title: 'Valid', url: 'https://example.com/valid', snippet: undefined }, |
| 82 | + ]) |
| 83 | + }) |
| 84 | + |
| 85 | + test('deduplicates repeated URLs across grounding buckets', () => { |
| 86 | + const results = extractBraveResults({ |
| 87 | + grounding: { |
| 88 | + generic: [{ title: 'First', url: 'https://example.com/dup' }], |
| 89 | + poi: { title: 'Second', url: 'https://example.com/dup' }, |
| 90 | + map: [{ title: 'Third', url: 'https://example.com/dup' }], |
| 91 | + }, |
| 92 | + }) |
| 93 | + |
| 94 | + expect(results).toEqual([ |
| 95 | + { title: 'First', url: 'https://example.com/dup', snippet: undefined }, |
| 96 | + ]) |
| 97 | + }) |
| 98 | + |
| 99 | + test('returns empty array when grounding is missing', () => { |
| 100 | + expect(extractBraveResults({})).toEqual([]) |
| 101 | + }) |
| 102 | + |
| 103 | + test('returns empty array when grounding arrays are absent', () => { |
| 104 | + expect(extractBraveResults({ grounding: {} })).toEqual([]) |
| 105 | + }) |
| 106 | +}) |
0 commit comments