Open-source AI browser agent for Chrome and Firefox (monorepo) ðŸ§
JavaScript ai-agentai-agentsai-toolsbrowser-aibrowser-automationbrowser-extensionchrome-aichrome-devtoolschrome-extensionchrome-extensionsfirefox-addonfirefox-addonsfirefox-extensionfirefox-extensionsgemmagemma4minimaxoffline-aioffline-llmqwen
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { test } from 'node:test';
import vm from 'node:vm';
for (const build of ['chrome', 'firefox']) {
test(`${build}: long media waits beyond three minutes, reports once, and retains timed-out job`, async () => {
const { executeHttpSkillTool } = await import(`../src/${build}/src/network/network-tools.js`);
const { normalizeCustomSkills, buildSkillToolRegistry } = await import(`../src/${build}/src/agent/skills.js`);
const skills = normalizeCustomSkills([{
id: 'freeskillz-xyz', name: 'FreeSkillz.xyz', enabled: true,
content: fs.readFileSync(new URL(`../src/${build}/skills/freeskillz-xyz.md`, import.meta.url), 'utf8'),
}]);
const tool = buildSkillToolRegistry(skills).get('download_public_media');
assert.equal(tool.job.timeoutMs, 1200000);
const originalFetch = globalThis.fetch;
const originalNow = Date.now;
const originalTimeout = globalThis.setTimeout;
try {
for (const scenario of ['duration', 'size', 'unknown', 'short']) {
let clock = 0;
let polls = 0;
const notices = [];
const methods = [];
Date.now = () => clock;
globalThis.setTimeout = callback => { clock += 1000; callback(); return 0; };
const metadata = scenario === 'duration' ? { duration: 204, title: '<untrusted>' }
: scenario === 'size' ? { filesize_approx: 80 * 1024 * 1024 }
: scenario === 'short' ? { duration: 10 } : {};
globalThis.fetch = async (_url, opts) => {
methods.push(opts.method);
if (opts.method === 'GET') {
polls++;
clock += scenario === 'short' ? 1 : 60000;
}
const data = opts.method === 'POST' ? { job_id: 'slow', metadata }
: { status: scenario === 'short' ? 'failed' : 'running', metadata };
return { ok: true, status: 200, text: async () => JSON.stringify(data) };
};
const result = await executeHttpSkillTool(tool, { url: 'https://x.com/user/status/123', kind: 'video' }, {
onProgress: message => notices.push(message),
});
if (scenario === 'short') {
assert.deepEqual(notices, []);
assert.ok(methods.includes('DELETE'));
} else {
assert.ok(polls > 3, 'must keep polling beyond original three-minute limit');
assert.equal(notices.length, 1);
assert.ok(!notices[0].includes('<untrusted>'));
assert.match(notices[0], scenario === 'duration' ? /4 minutes/ : scenario === 'size' ? /80 MB/ : /still downloading/);
assert.equal(result.jobId, 'slow');
assert.equal(result.statusUrl, 'https://freeskillz.xyz/v1/media/jobs/slow');
assert.equal(result.jobStatus, 'running');
assert.equal(result.executionContext, 'remote_service');
assert.equal(result.browserLoginAffectsRequest, false);
assert.ok(!methods.includes('DELETE'), 'timeout must preserve a running job');
}
}
} finally {
globalThis.fetch = originalFetch;
Date.now = originalNow;
globalThis.setTimeout = originalTimeout;
}
});
}
for (const build of ['chrome', 'firefox']) {
test(`${build}: long metadata does not notify when the job finishes fast`, async () => {
const { executeHttpSkillTool } = await import(`../src/${build}/src/network/network-tools.js`);
const { normalizeCustomSkills, buildSkillToolRegistry } = await import(`../src/${build}/src/agent/skills.js`);
const skills = normalizeCustomSkills([{
id: 'freeskillz-xyz', name: 'FreeSkillz.xyz', enabled: true,
content: fs.readFileSync(new URL(`../src/${build}/skills/freeskillz-xyz.md`, import.meta.url), 'utf8'),
}]);
const tool = buildSkillToolRegistry(skills).get('download_public_media');
const originalFetch = globalThis.fetch;
const originalNow = Date.now;
const originalTimeout = globalThis.setTimeout;
try {
let clock = 0;
const notices = [];
Date.now = () => clock;
globalThis.setTimeout = callback => { clock += 1000; callback(); return 0; };
// Long video metadata, but completes on first poll well before 30s elapsed.
globalThis.fetch = async (_url, opts) => {
if (opts.method === 'POST') return { ok: true, status: 200, text: async () => JSON.stringify({ job_id: 'fast', metadata: { duration: 300 } }) };
clock += 2000;
return { ok: true, status: 200, text: async () => JSON.stringify({ status: 'complete', metadata: { duration: 300 } }) };
};
// Stub the final file download to avoid browser APIs in this unit test.
const originalChrome = globalThis.chrome;
const originalBrowser = globalThis.browser;
try {
if (build === 'chrome') {
delete globalThis.browser;
globalThis.chrome = { runtime: { lastError: null }, downloads: { download(_o, cb) { cb(1); }, search(_q, cb) { cb([{ id: 1, state: 'complete', filename: 'f.mp4' }]); } } };
} else {
delete globalThis.chrome;
globalThis.browser = { downloads: { async download() { return 1; }, async search() { return [{ id: 1, state: 'complete' }]; } } };
}
// fetchSkillDownloadData path uses data: URLs in this stubbed fetch; force it
// to return a tiny file so downloadSkillFile can proceed without network.
globalThis.fetch = async (_url, opts) => {
if (opts.method === 'POST') return { ok: true, status: 200, text: async () => JSON.stringify({ job_id: 'fast', metadata: { duration: 300 } }) };
if (String(_url).endsWith('/file')) {
return { ok: true, status: 200, url: _url, headers: { get: () => 'video/mp4' }, text: async () => '', arrayBuffer: async () => new ArrayBuffer(4), body: null };
}
clock += 2000;
return { ok: true, status: 200, text: async () => JSON.stringify({ status: 'complete', metadata: { duration: 300 } }) };
};
await executeHttpSkillTool(tool, { url: 'https://x.com/user/status/123', kind: 'video' }, {
onProgress: message => notices.push(message),
});
assert.deepEqual(notices, []);
} finally {
if (originalChrome === undefined) delete globalThis.chrome;
else globalThis.chrome = originalChrome;
if (originalBrowser === undefined) delete globalThis.browser;
else globalThis.browser = originalBrowser;
}
} finally {
globalThis.fetch = originalFetch;
Date.now = originalNow;
globalThis.setTimeout = originalTimeout;
}
});
}
for (const build of ['chrome', 'firefox']) {
test(`${build}: skill download wait aborts promptly on Stop`, async () => {
const { executeHttpSkillTool } = await import(`../src/${build}/src/network/network-tools.js`);
const { normalizeCustomSkills, buildSkillToolRegistry } = await import(`../src/${build}/src/agent/skills.js`);
const skills = normalizeCustomSkills([{
id: 'freeskillz-xyz', name: 'FreeSkillz.xyz', enabled: true,
content: fs.readFileSync(new URL(`../src/${build}/skills/freeskillz-xyz.md`, import.meta.url), 'utf8'),
}]);
const tool = buildSkillToolRegistry(skills).get('download_public_media');
const originalFetch = globalThis.fetch;
try {
const methods = [];
const polling = Promise.withResolvers();
globalThis.fetch = async (_url, opts = {}) => {
methods.push(opts.method);
if (opts.signal?.aborted) throw new DOMException('The operation was aborted', 'AbortError');
if (opts.method === 'POST') return { ok: true, status: 200, text: async () => JSON.stringify({ job_id: 'slow' }) };
// Never resolve until aborted: the abortable sleep/fetch must win.
await new Promise((_resolve, reject) => {
opts.signal?.addEventListener('abort', () => reject(new DOMException('The operation was aborted', 'AbortError')), { once: true });
polling.resolve();
});
throw new Error('unreachable');
};
const controller = new AbortController();
const pending = executeHttpSkillTool(tool, { url: 'https://x.com/user/status/123', kind: 'video' }, {
signal: controller.signal,
onProgress: () => {},
});
await polling.promise;
controller.abort();
await assert.rejects(pending, err => err?.name === 'AbortError');
assert.deepEqual(methods, ['POST', 'GET'], 'test must stop a running provider job');
assert.ok(!methods.includes('DELETE'), 'abort must not delete the running provider job');
} finally {
globalThis.fetch = originalFetch;
}
});
}
for (const build of ['chrome', 'firefox']) {
test(`${build}: skill progress reaches the chat tool-progress channel`, async () => {
const source = fs.readFileSync(new URL(`../src/${build}/src/agent/agent.js`, import.meta.url), 'utf8');
const start = source.lastIndexOf(' const skillTool = this._activeSkillToolForName(tabId, name);');
const end = source.indexOf(' const skillEndpointRedirect', start);
assert.ok(start > 0 && end > start);
const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor;
const handler = new AsyncFunction('tabId', 'name', 'args', 'onUpdate', 'isTrustedChromeWebStoreSkillTool', 'executeHttpSkillTool', source.slice(start, end));
const updates = [];
const result = await handler.call({ _activeSkillToolForName: () => ({ skillId: 'freeskillz-xyz' }) },
1, 'download_public_media', {}, (...update) => updates.push(update), () => false,
async (_tool, _args, ctx) => { ctx.onProgress('Preparing a large file'); return { success: true }; });
assert.equal(result.success, true);
assert.deepEqual(updates, [['tool_progress', { name: 'download_public_media', message: 'Preparing a large file' }]]);
});
}
// Exercise the real Chrome offscreen handler and Firefox Blob path, including
// response bodies that stay open until Stop. Only browser APIs and OPFS are fake.
async function stagedDownloadHarness(t, build, stopAt, { stallCleanup = false } = {}) {
const { executeHttpSkillTool } = await import(`../src/${build}/src/network/network-tools.js`);
const { normalizeCustomSkills, buildSkillToolRegistry } = await import(`../src/${build}/src/agent/skills.js`);
const tool = buildSkillToolRegistry(normalizeCustomSkills([{
id: 'freeskillz-xyz', name: 'FreeSkillz.xyz', enabled: true,
content: fs.readFileSync(new URL(`../src/${build}/skills/freeskillz-xyz.md`, import.meta.url), 'utf8'),
}])).get('download_public_media');
const original = { fetch: globalThis.fetch, chrome: globalThis.chrome, browser: globalThis.browser,
create: URL.createObjectURL, revoke: URL.revokeObjectURL };
const cleanup = Promise.withResolvers();
t.after(() => {
cleanup.resolve();
globalThis.fetch = original.fetch;
for (const key of ['chrome', 'browser']) {
if (original[key] === undefined) delete globalThis[key];
else globalThis[key] = original[key];
}
URL.createObjectURL = original.create;
URL.revokeObjectURL = original.revoke;
});
const ready = Promise.withResolvers();
const proceed = Promise.withResolvers();
const stageSettled = Promise.withResolvers();
const released = Promise.withResolvers();
const controller = new AbortController();
const events = [], messages = [], files = new Map(), created = [], revoked = [], downloads = [];
let stageSignal, cleanupSignal, fileRequests = 0;
URL.createObjectURL = blob => {
const url = original.create(blob);
created.push(url);
return url;
};
URL.revokeObjectURL = url => { events.push('release'); revoked.push(url); original.revoke(url); };
const headers = { 'content-type': 'video/mp4', 'content-length': String(26 * 1024 * 1024) };
globalThis.fetch = async (url, opts) => {
if (opts.method === 'POST') return new Response(JSON.stringify({ job_id: 'staged' }));
if (opts.method === 'DELETE') {
events.push('delete');
cleanupSignal = opts.signal;
if (stallCleanup) await cleanup.promise;
return new Response(null, { status: 204 });
}
if (!url.endsWith('/file')) return new Response(JSON.stringify({ status: 'complete' }));
if (++fileRequests === 1) return new Response(null, { headers });
stageSignal = opts.signal;
assert.ok(stageSignal, 'staging fetch must receive an abort signal');
assert.equal(opts.credentials, 'omit');
assert.equal(opts.redirect, 'manual');
if (stopAt === 'headers') {
ready.resolve();
return await new Promise((_resolve, reject) => {
stageSignal.addEventListener('abort', () => reject(stageSignal.reason), { once: true });
});
}
if (stopAt === 'body') {
const body = new ReadableStream({
start(stream) {
stream.enqueue(new Uint8Array([1, 2, 3]));
stageSignal.addEventListener('abort', () => stream.error(stageSignal.reason), { once: true });
},
});
const response = new Response(body, { headers });
if (build === 'firefox') {
const blob = response.blob.bind(response);
response.blob = () => { ready.resolve(); return blob(); };
}
return response;
}
return new Response(new Uint8Array([1, 2, 3]), { headers });
};
const api = {
storage: { local: { async get() {
if (stopAt === 'directory') { ready.resolve(); await proceed.promise; }
return {};
} } },
downloads: build === 'chrome' ? {
download(opts, cb) { downloads.push(opts); cb(99); },
search(_query, cb) { cb([{ id: 99, state: 'in_progress' }]); ready.resolve(); },
cancel(id, cb) { assert.equal(id, 99); events.push('cancel'); cb(); },
} : {
async download(opts) { downloads.push(opts); return 99; },
async search() { ready.resolve(); return [{ id: 99, state: 'in_progress' }]; },
async cancel(id) { assert.equal(id, 99); events.push('cancel'); },
},
};
if (build === 'chrome') {
const listeners = [];
api.runtime = { lastError: null, onMessage: { addListener(fn) { listeners.push(fn); } },
sendMessage(message, cb) {
messages.push(message);
for (const listener of listeners) listener(message, {}, response => {
const respond = () => {
cb(response);
if (message.type === 'skill-download-prepare') stageSettled.resolve();
if (message.type === 'skill-download-release') released.resolve();
};
if (stopAt === 'reply' && message.type === 'skill-download-prepare') {
ready.resolve();
void proceed.promise.then(respond);
} else respond();
});
} };
api.offscreen = { async hasDocument() { return true; } };
const dir = {
async *keys() { yield* files.keys(); },
async removeEntry(name) { files.delete(name); },
async getFileHandle(name) {
const chunks = [];
files.set(name, chunks);
return {
async createWritable() { return {
async write(chunk) { chunks.push(chunk); if (stopAt === 'body') ready.resolve(); },
async close() {}, async abort() { events.push('abort-write'); },
}; },
async getFile() { return new Blob(chunks); },
};
},
};
vm.runInNewContext(fs.readFileSync(new URL('../src/chrome/src/offscreen/skill-download.js', import.meta.url), 'utf8'), {
chrome: api, navigator: { storage: { async getDirectory() { return { async getDirectoryHandle() { return dir; } }; } } },
fetch: (...args) => globalThis.fetch(...args), URL, Blob, Uint8Array, crypto, AbortController, DOMException,
});
globalThis.chrome = api;
delete globalThis.browser;
} else {
globalThis.browser = api;
delete globalThis.chrome;
}
const pending = executeHttpSkillTool(tool, { url: 'https://x.com/user/status/123', kind: 'video' }, { signal: controller.signal });
return { controller, pending, ready: ready.promise, proceed, stageSettled: stageSettled.promise,
released: released.promise, events, messages, files, created, revoked, downloads,
get stageSignal() { return stageSignal; }, get cleanupSignal() { return cleanupSignal; } };
}
for (const build of ['chrome', 'firefox']) {
test(`${build}: Stop rejects while completed-job cleanup is still pending`, { timeout: 3000 }, async t => {
const h = await stagedDownloadHarness(t, build, 'save', { stallCleanup: true });
const rejected = assert.rejects(h.pending, error => error?.name === 'AbortError');
await h.ready;
h.controller.abort();
let timer;
try {
await Promise.race([rejected, new Promise((_resolve, reject) => {
timer = setTimeout(() => reject(new Error('Stop waited for stalled provider cleanup.')), 1000);
})]);
} finally { clearTimeout(timer); }
assert.deepEqual(h.events, ['cancel', 'release', 'delete']);
assert.notEqual(h.cleanupSignal?.aborted, true, 'cleanup must survive cancellation of the tool');
assert.equal(h.files.size, 0);
assert.deepEqual(h.revoked, h.created);
});
for (const phase of ['headers', 'body', 'directory', 'save', ...(build === 'chrome' ? ['reply'] : [])]) {
test(`${build}: Stop during staged download ${phase} cancels transfer and releases ownership`, { timeout: 3000 }, async t => {
const h = await stagedDownloadHarness(t, build, phase);
const rejected = assert.rejects(h.pending, error => error?.name === 'AbortError');
await h.ready;
h.controller.abort();
h.proceed.resolve();
await rejected;
if (build === 'chrome') await h.stageSettled;
if (phase === 'reply') await h.released;
assert.equal(h.events.filter(event => event === 'delete').length, 1, 'completed provider job must be cleaned up');
assert.equal(h.files.size, 0, 'no partial OPFS file may remain after Stop');
assert.deepEqual(h.revoked, h.created, 'every completed stage must release its Blob URL');
if (phase === 'headers' || phase === 'body') {
assert.equal(h.stageSignal.aborted, true, 'Stop must reach the actual staging request');
assert.equal(h.downloads.length, 0);
if (build === 'chrome') {
const prepare = h.messages.find(message => message.type === 'skill-download-prepare');
assert.equal(h.messages.find(message => message.type === 'skill-download-cancel')?.requestId, prepare.requestId);
if (phase === 'body') assert.ok(h.events.includes('abort-write'), 'partial OPFS write must be aborted');
}
} else {
assert.equal(h.created.length, 1, 'test must reach a fully staged file');
assert.equal(h.downloads.length, phase === 'save' ? 1 : 0);
if (phase === 'save') {
assert.deepEqual(h.events, ['cancel', 'release', 'delete'], 'cancel local download before releasing the stage and remote job');
}
}
});
}
}