import { exec } from '../src/logic/execute-script'; const setBrowser = ( executeScript: () => Promise, getTab: () => Promise<{ status: string }> = async () => ({ status: 'complete' }), ) => { Object.assign(globalThis, { browser: { tabs: { get: getTab }, scripting: { executeScript }, }, }); }; const main = async () => { setBrowser(async () => { throw new Error('Frame was removed'); }); const rejectionStarted = Date.now(); await exec(1, async () => true, undefined, { timeout: 1000 }).then( () => { throw new Error('Expected executeScript to reject'); }, (error) => { if (!String(error).includes('Frame was removed')) throw error; if (Date.now() - rejectionStarted >= 1000) { throw new Error('The original executeScript error was delayed until the timeout'); } }, ); setBrowser(async () => [{ result: 42 }]); const result = await exec(1, async () => 42, undefined, { timeout: 1000 }); if (result !== 42) throw new Error(`Expected 42, received ${result}`); let injectionAttempted = false; setBrowser( async () => { injectionAttempted = true; return [{ result: true }]; }, async () => ({ status: 'loading' }), ); await exec(1, async () => true, undefined, { timeout: 100 }).then( () => { throw new Error('A perpetually loading page should time out before injection'); }, (error) => { if (!String(error).includes('页面加载超时')) throw error; }, ); if (injectionAttempted) throw new Error('Script was injected into a still-loading page'); console.log('Execute-script error propagation tests passed.'); }; void main();