110 lines
5.5 KiB
JavaScript
110 lines
5.5 KiB
JavaScript
// Isolated real-extension test host. The parent sends a pairing link over stdin,
|
|
// never arguments or logs. Requires a test Chromium and playwright-core.
|
|
import { createInterface } from 'node:readline';
|
|
import { mkdtemp, rm } from 'node:fs/promises';
|
|
import { pathToFileURL } from 'node:url';
|
|
|
|
const lines = createInterface({ input: process.stdin });
|
|
const first = await new Promise(resolve => lines.once('line', resolve));
|
|
const { pairing, extension, chromium: executablePath, playwright } = JSON.parse(first);
|
|
const { chromium } = await import(pathToFileURL(playwright).href);
|
|
const profile = await mkdtemp('/tmp/wkb-chrome-');
|
|
let browser;
|
|
let closing = false;
|
|
async function close() {
|
|
if (closing) return;
|
|
closing = true;
|
|
await browser?.close();
|
|
await rm(profile, { recursive: true, force: true });
|
|
lines.close();
|
|
}
|
|
process.on('SIGTERM', () => void close().finally(() => process.exit(0)));
|
|
try {
|
|
browser = await chromium.launchPersistentContext(profile, {
|
|
executablePath, headless: process.env.BROWSERSKILL_TEST_HEADED !== '1',
|
|
args: [`--disable-extensions-except=${extension}`, `--load-extension=${extension}`],
|
|
});
|
|
const worker = browser.serviceWorkers()[0] ?? await browser.waitForEvent('serviceworker');
|
|
const taskWindow = process.env.BROWSERSKILL_TEST_TASK_WINDOW === '1';
|
|
const popup = await browser.newPage();
|
|
await popup.goto(new URL('popup.html', worker.url()).href);
|
|
await popup.locator('details summary').click();
|
|
await popup.locator('#remote-pairing').fill(pairing);
|
|
await popup.locator('details button').first().click();
|
|
await popup.waitForFunction(() => document.querySelector('#remote-pairing')?.value === '' || document.querySelector('details [role=alert]'));
|
|
if (await popup.locator('details [role=alert]').count()) throw new Error('Extension authorization failed before browser tests');
|
|
if (taskWindow) {
|
|
await popup.locator('#bsk-task-window-mode').selectOption('window');
|
|
await popup.waitForFunction(() => {
|
|
const select = document.querySelector('#bsk-task-window-mode');
|
|
return select?.value === 'window' && !select.disabled;
|
|
});
|
|
}
|
|
const initial = await worker.evaluate(async () => {
|
|
const window = await chrome.windows.getLastFocused();
|
|
const tabs = await chrome.tabs.query({windowId:window.id,active:true});
|
|
return {windowId:window.id,tabId:tabs[0].id,tabIds:(await chrome.tabs.query({})).map(t=>t.id)};
|
|
});
|
|
const originalWindow = { windowId: initial.windowId, tabId: initial.tabId };
|
|
process.stdout.write('ready\n');
|
|
for await (const command of lines) {
|
|
if (command === 'close') break;
|
|
if (command === 'check-detached') {
|
|
// getTargets().attached includes Playwright's own debugger. Probe only
|
|
// this extension's attachment without attaching or changing the page.
|
|
const attached = await worker.evaluate(async () => {
|
|
const tabs = await chrome.tabs.query({});
|
|
const used = [];
|
|
for (const tab of tabs) {
|
|
try {
|
|
await chrome.debugger.sendCommand({tabId:tab.id}, 'Runtime.evaluate', {expression:'0', returnByValue:true});
|
|
used.push(tab.id);
|
|
} catch (error) {
|
|
if (!/not attached|No tab|No target/i.test(String(error))) throw error;
|
|
}
|
|
}
|
|
return used;
|
|
});
|
|
process.stdout.write(JSON.stringify({detached:attached.length===0})+'\n');
|
|
}
|
|
if (command === 'check-cleanup') {
|
|
const state = await worker.evaluate(async () => ({ids:(await chrome.tabs.query({})).map(t=>t.id), groups:await chrome.tabGroups.query({})}));
|
|
process.stdout.write(JSON.stringify({cleaned:state.groups.length===0 && state.ids.length===initial.tabIds.length && state.ids.every(id=>initial.tabIds.includes(id))})+'\n');
|
|
}
|
|
if (command === 'remember-foreground') {
|
|
const foreground = await worker.evaluate(async () => {
|
|
const window = await chrome.windows.getLastFocused();
|
|
const [tab] = await chrome.tabs.query({windowId:window.id,active:true});
|
|
return {windowId:window.id,tabId:tab.id};
|
|
});
|
|
Object.assign(initial, foreground);
|
|
process.stdout.write('remembered\n');
|
|
}
|
|
if (command === 'check-background') {
|
|
if (taskWindow) {
|
|
const valid = await worker.evaluate(async (original) => {
|
|
const tabs = await chrome.tabs.query({});
|
|
const groups = await chrome.tabGroups.query({});
|
|
const taskWindows = [...new Set(tabs.filter(t => t.windowId !== original.windowId).map(t => t.windowId))];
|
|
return tabs.find(t => t.id === original.tabId)?.active === true &&
|
|
groups.length === 0 && taskWindows.length > 0 && taskWindows.every(id =>
|
|
tabs.filter(t => t.windowId === id && t.active).length === 1);
|
|
}, originalWindow);
|
|
process.stdout.write(JSON.stringify({background:valid})+'\n');
|
|
continue;
|
|
}
|
|
const current = await worker.evaluate(async () => {
|
|
const window=await chrome.windows.getLastFocused();
|
|
const tabs=await chrome.tabs.query({windowId:window.id,active:true});
|
|
const windows=await chrome.windows.getAll({windowTypes:['normal']});
|
|
const groups=await chrome.tabGroups.query({windowId:window.id});
|
|
return {windowId:window.id,tabId:tabs[0].id,windowCount:windows.length,labeled:groups.some(g=>g.title?.startsWith('WeKnora'))};
|
|
});
|
|
const background = current.windowId === initial.windowId && current.tabId === initial.tabId && current.windowCount === 1 && current.labeled;
|
|
process.stdout.write(JSON.stringify(background ? {background} : {background, initial, current}) + '\n');
|
|
}
|
|
}
|
|
} finally {
|
|
await close();
|
|
}
|