fix(tabs): stop fallback outside locked window

This commit is contained in:
QLHazyCoder
2026-05-12 15:35:43 +08:00
parent 7a5b7a26cf
commit 8b9adb9085
6 changed files with 142 additions and 12 deletions
@@ -260,3 +260,82 @@ test('tab runtime scopes tab queries to the locked automation window', async ()
assert.deepEqual(queries[0], { active: true, windowId: 22 });
});
test('tab runtime does not create tabs outside an unavailable locked window', async () => {
const source = fs.readFileSync('background/tab-runtime.js', 'utf8');
const globalScope = {};
const api = new Function('self', `${source}; return self.MultiPageBackgroundTabRuntime;`)(globalScope);
const created = [];
const runtime = api.createTabRuntime({
LOG_PREFIX: '[test]',
addLog: async () => {},
chrome: {
tabs: {
create: async (payload) => {
created.push(payload);
if (payload.windowId === 44) {
throw new Error('No window with id: 44');
}
return { id: 99, windowId: payload.windowId, url: payload.url };
},
get: async () => ({ id: 1, windowId: 44, url: 'https://example.com' }),
query: async () => [],
},
},
getSourceLabel: (sourceName) => sourceName || 'unknown',
getState: async () => ({
automationWindowId: 44,
tabRegistry: {},
sourceLastUrls: {},
}),
matchesSourceUrlFamily: () => false,
setState: async () => {},
throwIfStopped: () => {},
});
await assert.rejects(
() => runtime.createAutomationTab({ url: 'https://example.com', active: true }),
/自动任务窗口已不可用/
);
assert.deepEqual(created, [{ url: 'https://example.com', active: true, windowId: 44 }]);
});
test('tab runtime does not query all windows when the locked window is unavailable', async () => {
const source = fs.readFileSync('background/tab-runtime.js', 'utf8');
const globalScope = {};
const api = new Function('self', `${source}; return self.MultiPageBackgroundTabRuntime;`)(globalScope);
const queries = [];
const runtime = api.createTabRuntime({
LOG_PREFIX: '[test]',
addLog: async () => {},
chrome: {
tabs: {
get: async () => ({ id: 1, windowId: 55, url: 'https://example.com' }),
query: async (queryInfo) => {
queries.push(queryInfo);
if (queryInfo.windowId === 55) {
throw new Error('No window with id: 55');
}
return [{ id: 7, windowId: 1, url: 'https://other.example/' }];
},
},
},
getSourceLabel: (sourceName) => sourceName || 'unknown',
getState: async () => ({
automationWindowId: 55,
tabRegistry: {},
sourceLastUrls: {},
}),
matchesSourceUrlFamily: () => false,
setState: async () => {},
throwIfStopped: () => {},
});
await assert.rejects(
() => runtime.queryTabsInAutomationWindow({ active: true }),
/自动任务窗口已不可用/
);
assert.deepEqual(queries, [{ active: true, windowId: 55 }]);
});