') >= 0);
}
testCatalog();
testToolParse();
testCompaction();
testSearchReplace();
testToolSet();
testPrompts();
testNet();
testCustomTools();
testPlanTodosStationarity();
testTruncateAndPerm();
testPaths();
testQvacWorkerDeps();
testDevicePrefersGpu();
testGoogleSearchParseAndFallback();
testWebFetchTimeout()
.then(() => testGoogleSearchFallsBackToDuckDuckGo())
.then(() => {
console.log('ok');
})
.catch((err) => {
console.error(err);
process.exitCode = 1;
});
async function testWebFetchTimeout() {
const orig = globalThis.fetch;
globalThis.fetch = () => new Promise(() => {});
const started = Date.now();
try {
const hung = await tools.webFetch('https://example.com/ip', 40);
assert.ok(hung.error);
assert.ok(/timed out/i.test(hung.error));
assert.strictEqual(hung.url, 'https://example.com/ip');
assert.ok(Date.now() - started < 2000);
} finally {
globalThis.fetch = orig;
}
globalThis.fetch = async (url) => ({
status: 200,
url: String(url),
text: async () => '203.0.113.8',
});
try {
const ok = await tools.webFetch('https://ifconfig.me/ip', 200);
assert.strictEqual(ok.status, 200);
assert.strictEqual(ok.url, 'https://ifconfig.me/ip');
assert.strictEqual(ok.text, '203.0.113.8');
assert.ok(!ok.error);
} finally {
globalThis.fetch = orig;
}
globalThis.fetch = async () => ({ status: 503, url: 'https://example.com', text: async () => 'down' });
try {
const failed = await tools.webFetch('https://example.com/status', 200);
assert.ok(failed.error);
assert.strictEqual(failed.status, 503);
assert.strictEqual(failed.url, 'https://example.com');
} finally {
globalThis.fetch = orig;
}
}
function testGoogleSearchParseAndFallback() {
const parsed = tools.parseGoogleHits(
'Example Domain
'
);
assert.strictEqual(parsed.length, 1);
assert.strictEqual(parsed[0].url, 'https://example.com/page');
assert.strictEqual(parsed[0].title, 'Example Domain');
assert.strictEqual(tools.parseGoogleHits('Google Search').length, 0);
assert.ok(tools.SCHEMAS.find((t) => t.name === 'google_search'));
assert.ok(tools.SCHEMAS.find((t) => t.name === 'fetch_page'));
assert.ok(tools.SCHEMAS.find((t) => t.name === 'wiki_search'));
const rss = require('../agent/web-search.js').parseRssItems(
'- Examplehttps://example.com/rss
'
);
assert.strictEqual(rss[0].url, 'https://example.com/rss');
}
async function testGoogleSearchFallsBackToDuckDuckGo() {
const orig = globalThis.fetch;
globalThis.fetch = async (url) => {
const href = String(url);
if (href.indexOf('google.com') >= 0) {
return {
status: 200,
url: href,
text: async () => 'Google Search',
};
}
return {
status: 200,
url: href,
text: async () => 'DDG Example',
};
};
try {
const hits = await tools.webSearch('example domain', 200);
assert.ok(Array.isArray(hits));
assert.strictEqual(hits.length, 1);
assert.strictEqual(hits[0].source, 'duckduckgo');
assert.strictEqual(hits[0].url, 'https://example.com/ddg');
assert.strictEqual(hits[0].title, 'DDG Example');
} finally {
globalThis.fetch = orig;
}
globalThis.fetch = async (url) => {
const href = String(url);
if (href.indexOf('google.com') >= 0) {
return {
status: 200,
url: href,
text: async () =>
'From Google
',
};
}
throw new Error('duckduckgo should not run when google hits');
};
try {
const hits = await tools.googleSearchWithFallback('example domain', 200);
assert.strictEqual(hits[0].source, 'google');
assert.strictEqual(hits[0].url, 'https://example.com/google');
assert.strictEqual(hits[0].title, 'From Google');
} finally {
globalThis.fetch = orig;
}
const bingHref =
'https://www.bing.com/ck/a?!&&p=ae&u=a1aHR0cDovL3d3dy5leGFtcGxlLmNvbS8&ntb=1';
globalThis.fetch = async (url) => {
const href = String(url);
if (href.indexOf('google.com') >= 0) {
return { status: 200, url: href, text: async () => 'Google Search' };
}
if (href.indexOf('duckduckgo.com') >= 0) {
return {
status: 202,
url: href,
text: async () => 'Unfortunately, bots use DuckDuckGo too.
',
};
}
return {
status: 200,
url: href,
text: async () => '',
};
};
try {
const hits = await tools.googleSearchWithFallback('example domain', 200);
assert.strictEqual(hits[0].source, 'bing');
assert.strictEqual(hits[0].url, 'http://www.example.com/');
} finally {
globalThis.fetch = orig;
}
}