Files
2026-11-16 04:21:21 -07:00

205 lines
9.1 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { TabSession } from '../../src/lib/browser/tabSession.ts';
function runtime() {
const tabs = new Map(), calls = [];
let sequence = 0;
const snapshot = () => ({ profiles: [{ id: 'user' }], tabs: [...tabs.values()], bookmarks: [], settings: {} });
const request = async (command, args = {}) => {
calls.push({ command, args });
if (command === 'state') return snapshot();
if (command === 'newTab') {
const id = `runtime-${++sequence}`;
tabs.set(id, { id, profileId: args.profileId, url: args.url || 'about:blank' });
return { tabId: id };
}
if (!tabs.has(args.tabId)) throw new Error('Unknown browser tab.');
if (command === 'closeTab') tabs.delete(args.tabId);
return snapshot();
};
return { tabs, calls, request, snapshot };
}
test('a moved tab keeps its runtime identity and survives source cleanup', async () => {
const engine = runtime();
const source = new TabSession(engine.request, { url: 'https://example.com/' }, () => {});
await source.connect();
const tabId = source.value.tabId;
source.detach();
await source.close();
const target = new TabSession(engine.request, { tabId }, () => {});
await target.connect();
assert.equal(target.value.tabId, tabId);
assert.equal(target.value.url, 'https://example.com/');
assert.equal(engine.calls.filter(call => call.command === 'newTab').length, 1);
await target.close();
assert.equal(engine.tabs.size, 0);
});
for (const reason of [
'The browser stopped responding. Open a new browser tab to restart it.',
'An existing connection was forcibly closed by the remote host. (os error 10054)',
'An established connection was aborted by the software in your host machine. (os error 10053)',
]) for (const failureAt of [1, 2, 3]) {
test(`opening recovers at request ${failureAt}: ${reason}`, async () => {
const engine = runtime(), phases = []; let requests = 0;
const session = new TabSession(async (command, args) => {
if (++requests === failureAt) {
engine.tabs.clear();
throw new Error(reason);
}
return engine.request(command, args);
}, { profileId: 'research', url: 'https://example.com/bookmark' }, value => phases.push(value.phase));
await session.connect();
assert.equal(session.value.phase, 'ready');
assert.equal(engine.tabs.size, 1);
assert.equal(engine.tabs.get(session.value.tabId).profileId, 'research');
assert.equal(engine.tabs.get(session.value.tabId).url, 'https://example.com/bookmark');
assert.ok(!phases.includes('disconnected'));
});
}
test('a failed state response reuses a tab that still exists', async () => {
const engine = runtime(); let requests = 0;
const session = new TabSession((command, args) => {
if (++requests === 3) throw new Error('The browser stopped responding.');
return engine.request(command, args);
}, {}, () => {});
await session.connect();
assert.equal(session.value.phase, 'ready');
assert.equal(session.value.tabId, 'runtime-1');
assert.equal(engine.calls.filter(call => call.command === 'newTab').length, 1);
});
test('repeated opening failures stop after one retry and allow manual reconnect', async () => {
const engine = runtime(); let requests = 0, fail = true;
const session = new TabSession((command, args) => {
requests++;
if (fail) throw new Error('The browser stopped responding.');
return engine.request(command, args);
}, {}, () => {});
await session.connect();
assert.equal(requests, 2);
assert.equal(session.value.phase, 'disconnected');
assert.equal(session.value.detail.operation, 'state');
assert.match(session.value.detail.reason, /stopped responding/);
await session.send('resize');
assert.equal(requests, 2);
fail = false;
await session.connect();
assert.equal(session.value.phase, 'ready');
});
test('an invalid profile does not retry tab creation', async () => {
const engine = runtime(); let creates = 0;
const session = new TabSession((command, args) => {
if (command === 'newTab') { creates++; throw new Error('Unknown browser profile.'); }
return engine.request(command, args);
}, { profileId: 'missing' }, () => {});
await session.connect();
assert.equal(creates, 1);
assert.equal(session.value.phase, 'disconnected');
assert.equal(session.value.detail.operation, 'newTab');
});
test('closing while the first request fails prevents the retry', async () => {
let rejectRequest, requests = 0;
const session = new TabSession(() => {
requests++;
return new Promise((_resolve, reject) => { rejectRequest = reject; });
}, {}, () => {});
const opening = session.connect();
assert.equal(session.connect(), opening);
await session.close();
rejectRequest(new Error('The browser stopped responding.'));
await opening;
assert.equal(requests, 1);
assert.equal(session.value.phase, 'closed');
});
test('a restart during the first open creates a valid replacement tab', async () => {
const engine = runtime(); let restart = true;
const session = new TabSession(async (...args) => {
const value = await engine.request(...args);
if (args[0] === 'newTab' && restart) { restart = false; engine.tabs.clear(); }
return value;
}, { profileId: 'user', url: 'https://example.com/' }, () => {});
await session.connect();
assert.equal(session.value.phase, 'ready');
assert.equal(engine.tabs.size, 1);
assert.equal(session.value.tabId, 'runtime-2');
assert.equal(engine.tabs.get('runtime-2').url, 'https://example.com/');
});
test('connection loss restores the same profile and URL without repeating input', async () => {
const engine = runtime();
const session = new TabSession(engine.request, { profileId: 'research', url: 'https://example.com/' }, () => {});
await session.connect(); engine.tabs.clear();
await session.send('input', { input: { type: 'char', keyCode: 'x' } });
assert.equal(session.value.phase, 'ready');
assert.equal(session.value.tabId, 'runtime-2');
assert.equal(engine.calls.filter(call => call.command === 'input').length, 1);
assert.equal(engine.tabs.get('runtime-2').profileId, 'research');
assert.equal(session.value.detail.operation, 'input');
assert.match(session.value.detail.reason, /Unknown browser tab/);
});
test('failed recovery stops background requests and keeps a specific retry error', async () => {
const engine = runtime(); let fail = false;
const session = new TabSession((command, args) => {
if (fail && command === 'newTab') throw new Error('The browser runtime is missing.');
return engine.request(command, args);
}, {}, () => {});
await session.connect(); engine.tabs.clear(); fail = true;
await session.send('resize', { width: 900, height: 600 });
assert.equal(session.value.phase, 'disconnected');
assert.equal(session.value.detail.operation, 'newTab');
assert.match(session.value.detail.reason, /runtime is missing/);
const requests = engine.calls.length;
await session.send('input'); await session.send('visible');
assert.equal(engine.calls.length, requests);
fail = false; await session.connect();
assert.equal(session.value.phase, 'ready');
});
test('late errors from the old tab cannot break the replacement', async () => {
const engine = runtime(); let rejectOld;
const session = new TabSession((command, args) => command === 'input'
? new Promise((_resolve, reject) => rejectOld = reject) : engine.request(command, args), {}, () => {});
await session.connect();
const pending = session.send('input'); engine.tabs.clear();
await session.send('resize');
rejectOld(new Error('Old tab timed out.')); await pending;
assert.equal(session.value.phase, 'ready');
assert.equal(session.value.tabId, 'runtime-2');
assert.equal(session.value.detail.operation, 'resize');
});
test('closing during creation removes the late tab and leaves no running view', async () => {
const engine = runtime(); let finish;
const session = new TabSession((command, args) => command === 'newTab'
? new Promise(resolve => finish = () => resolve(engine.request(command, args))) : engine.request(command, args), {}, () => {});
const starting = session.connect();
await new Promise(resolve => setImmediate(resolve));
await session.close(); finish(); await starting;
assert.equal(session.value.phase, 'closed');
assert.equal(engine.tabs.size, 0);
});
test('an invalid address keeps the tab usable and does not restart it', async () => {
const engine = runtime();
const session = new TabSession((command, args) => {
if (command === 'navigate' && args.input.startsWith('javascript:')) throw new Error('Use an HTTP or HTTPS address without embedded credentials.');
return engine.request(command, args);
}, {}, () => {});
await session.connect();
await session.send('navigate', { input: 'javascript:alert(1)' });
assert.equal(session.value.phase, 'ready');
assert.equal(session.value.tabId, 'runtime-1');
assert.match(session.value.detail.reason, /HTTP or HTTPS/);
await session.send('navigate', { input: 'https://example.com/' });
assert.equal(session.value.message, '');
assert.equal(engine.calls.filter(call => call.command === 'newTab').length, 1);
});