diff --git a/src/tools/images/index.test.ts b/src/tools/images/index.test.ts index f2374e3..fbe1756 100644 --- a/src/tools/images/index.test.ts +++ b/src/tools/images/index.test.ts @@ -46,6 +46,61 @@ describe(name, () => { const result = await client.callTool({ name, arguments: { query: 'brave browser' } }); assert.equal(result.isError ?? false, false, JSON.stringify(result.content)); - assert.ok(Array.isArray(result.content) && result.content.length > 0); + assert.deepEqual(result.structuredContent, { + type: 'object', + items: [ + { + title: 'Brave Browser Logo', + url: 'https://example.com/logo.png', + page_fetched: '2024-01-01T00:00:00Z', + confidence: 'high', + properties: { + url: 'https://example.com/logo-full.png', + width: 512, + height: 512, + }, + }, + ], + count: 1, + might_be_offensive: false, + }); + }); +}); + +describe(`${name} (malformed upstream response)`, () => { + let client: Client; + let close: () => Promise; + let restoreFetch: () => void; + + before(async () => { + restoreFetch = stubFetch((url) => { + if (url.pathname === '/res/v1/images/search') { + return { + type: 'images', + query: { original: 'brave browser' }, + results: [], + // `might_be_offensive` is required by the output schema; omitting it + // simulates an upstream response that fails validation. + extra: {}, + }; + } + }); + + ({ client, close } = await connectTestClient()); + }); + + after(async () => { + restoreFetch(); + await close(); + }); + + it('surfaces the validation failure as an error result', async () => { + const result = await client.callTool({ name, arguments: { query: 'brave browser' } }); + + assert.equal(result.isError, true); + assert.ok( + JSON.stringify(result.content).includes('might_be_offensive'), + JSON.stringify(result.content) + ); }); }); diff --git a/src/tools/llm_context/index.test.ts b/src/tools/llm_context/index.test.ts index ec60019..b32032a 100644 --- a/src/tools/llm_context/index.test.ts +++ b/src/tools/llm_context/index.test.ts @@ -42,6 +42,52 @@ describe(name, () => { const result = await client.callTool({ name, arguments: { query: 'brave browser' } }); assert.equal(result.isError ?? false, false, JSON.stringify(result.content)); - assert.ok(Array.isArray(result.content) && result.content.length > 0); + assert.deepEqual(result.structuredContent, { + grounding: { + generic: [ + { + url: 'https://search.brave.com/', + title: 'Brave Search', + snippets: ['Brave is a privacy-focused browser and search engine.'], + }, + ], + map: [], + }, + sources: { + 'https://search.brave.com/': { title: 'Brave Search' }, + }, + }); + }); +}); + +describe(`${name} (malformed upstream response)`, () => { + let client: Client; + let close: () => Promise; + let restoreFetch: () => void; + + before(async () => { + restoreFetch = stubFetch((url) => { + if (url.pathname === '/res/v1/llm/context') { + // `sources` is required by the output schema; omitting it simulates + // an upstream response that fails validation. + return { + grounding: { generic: [], map: [] }, + }; + } + }); + + ({ client, close } = await connectTestClient()); + }); + + after(async () => { + restoreFetch(); + await close(); + }); + + it('surfaces the validation failure as an error result', async () => { + const result = await client.callTool({ name, arguments: { query: 'brave browser' } }); + + assert.equal(result.isError, true); + assert.ok(JSON.stringify(result.content).includes('sources'), JSON.stringify(result.content)); }); }); diff --git a/src/tools/local/index.test.ts b/src/tools/local/index.test.ts index 410534a..8649e6a 100644 --- a/src/tools/local/index.test.ts +++ b/src/tools/local/index.test.ts @@ -46,3 +46,86 @@ describe(name, () => { assert.ok(Array.isArray(result.content) && result.content.length > 0); }); }); + +describe(`${name} (no locations, web fallback available)`, () => { + let client: Client; + let close: () => Promise; + let restoreFetch: () => void; + + before(async () => { + restoreFetch = stubFetch((url) => { + if (url.pathname === '/res/v1/web/search') { + return { + type: 'search', + web: { + results: [ + { + url: 'https://example.com/cafe', + title: 'Local Cafe', + description: 'A nice cafe.', + extra_snippets: [], + }, + ], + }, + }; + } + }); + + ({ client, close } = await connectTestClient()); + }); + + after(async () => { + restoreFetch(); + await close(); + }); + + it('falls back to formatted web results', async () => { + const result = await client.callTool({ + name, + arguments: { query: 'coffee shops in san francisco' }, + }); + + assert.equal(result.isError ?? false, false, JSON.stringify(result.content)); + + const content = result.content as { text: string }[]; + assert.equal(content.length, 2); + assert.match(content[0].text, /Falling back to general web search/); + assert.match(content[1].text, /Local Cafe/); + }); +}); + +describe(`${name} (no locations, no web results)`, () => { + let client: Client; + let close: () => Promise; + let restoreFetch: () => void; + + before(async () => { + restoreFetch = stubFetch((url) => { + if (url.pathname === '/res/v1/web/search') { + return { type: 'search' }; + } + }); + + ({ client, close } = await connectTestClient()); + }); + + after(async () => { + restoreFetch(); + await close(); + }); + + it('reports that no location data was found', async () => { + const result = await client.callTool({ + name, + arguments: { query: 'coffee shops in san francisco' }, + }); + + assert.equal(result.isError ?? false, false, JSON.stringify(result.content)); + assert.deepEqual(result.content, [ + { + type: 'text', + text: "No location data was returned. User's plan does not support local search, or the query may be unclear.", + }, + ]); + }); +}); diff --git a/src/tools/place_search/index.test.ts b/src/tools/place_search/index.test.ts index 97ba9d4..51677a3 100644 --- a/src/tools/place_search/index.test.ts +++ b/src/tools/place_search/index.test.ts @@ -31,6 +31,6 @@ describe(name, () => { }); assert.equal(result.isError ?? false, false, JSON.stringify(result.content)); - assert.ok(Array.isArray(result.content) && result.content.length > 0); + assert.deepEqual(result.structuredContent, { type: 'locations' }); }); });