test: assert structuredContent and cover tool error paths
This commit is contained in:
1 parent
71e05d1258
commit
dcd370b2a0
4 files changed
+187
-3
No files matched your search
@@ -46,6 +46,61 @@ describe(name, () => {
|
|||||||
const result = await client.callTool({ name, arguments: { query: 'brave browser' } });
|
const result = await client.callTool({ name, arguments: { query: 'brave browser' } });
|
||||||
|
|
||||||
assert.equal(result.isError ?? false, false, JSON.stringify(result.content));
|
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<void>;
|
||||||
|
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)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -42,6 +42,52 @@ describe(name, () => {
|
|||||||
const result = await client.callTool({ name, arguments: { query: 'brave browser' } });
|
const result = await client.callTool({ name, arguments: { query: 'brave browser' } });
|
||||||
|
|
||||||
assert.equal(result.isError ?? false, false, JSON.stringify(result.content));
|
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<void>;
|
||||||
|
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));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -46,3 +46,86 @@ describe(name, () => {
|
|||||||
assert.ok(Array.isArray(result.content) && result.content.length > 0);
|
assert.ok(Array.isArray(result.content) && result.content.length > 0);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe(`${name} (no locations, web fallback available)`, () => {
|
||||||
|
let client: Client;
|
||||||
|
let close: () => Promise<void>;
|
||||||
|
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<void>;
|
||||||
|
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.",
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -31,6 +31,6 @@ describe(name, () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
assert.equal(result.isError ?? false, false, JSON.stringify(result.content));
|
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' });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
Reference in new issue
Block a user