feat: 修复停止逻辑

This commit is contained in:
祁连海
2026-04-17 23:29:10 +08:00
parent adf0cccd77
commit 73f9deb53e
12 changed files with 460 additions and 63 deletions
@@ -47,6 +47,7 @@ test('tab runtime waitForTabComplete waits until tab status becomes complete', a
registerTab: async () => {},
setState: async () => {},
shouldBypassStep9ForLocalCpa: () => false,
throwIfStopped: () => {},
});
const result = await runtime.waitForTabComplete(9, {
@@ -57,3 +58,43 @@ test('tab runtime waitForTabComplete waits until tab status becomes complete', a
assert.equal(result?.status, 'complete');
assert.equal(getCalls, 3);
});
test('tab runtime waitForTabComplete aborts promptly when stop is requested', async () => {
const source = fs.readFileSync('background/tab-runtime.js', 'utf8');
const globalScope = {};
const api = new Function('self', `${source}; return self.MultiPageBackgroundTabRuntime;`)(globalScope);
let throwCalls = 0;
const runtime = api.createTabRuntime({
LOG_PREFIX: '[test]',
addLog: async () => {},
chrome: {
tabs: {
get: async () => ({
id: 9,
url: 'https://example.com',
status: 'loading',
}),
query: async () => [],
},
},
getSourceLabel: (sourceName) => sourceName || 'unknown',
getState: async () => ({ tabRegistry: {}, sourceLastUrls: {} }),
matchesSourceUrlFamily: () => false,
setState: async () => {},
throwIfStopped: () => {
throwCalls += 1;
if (throwCalls >= 2) {
throw new Error('Flow stopped.');
}
},
});
await assert.rejects(
runtime.waitForTabComplete(9, {
timeoutMs: 2000,
retryDelayMs: 1,
}),
/Flow stopped\./
);
});