| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- const assert = require('assert');
- const fs = require('fs');
- const helperSource = fs.readFileSync('background.js', 'utf8');
- const tabRuntimeSource = fs.readFileSync('background/tab-runtime.js', 'utf8');
- function extractFunction(source, name) {
- const markers = [`async function ${name}(`, `function ${name}(`];
- const start = markers
- .map(marker => source.indexOf(marker))
- .find(index => index >= 0);
- if (start < 0) throw new Error(`missing function ${name}`);
- let parenDepth = 0;
- let signatureEnded = false;
- let braceStart = -1;
- for (let i = start; i < source.length; i += 1) {
- const ch = source[i];
- if (ch === '(') parenDepth += 1;
- else if (ch === ')') {
- parenDepth -= 1;
- if (parenDepth === 0) signatureEnded = true;
- } else if (ch === '{' && signatureEnded) {
- braceStart = i;
- break;
- }
- }
- if (braceStart < 0) throw new Error(`missing body for function ${name}`);
- let depth = 0;
- let end = braceStart;
- for (; end < source.length; end += 1) {
- const ch = source[end];
- if (ch === '{') depth += 1;
- if (ch === '}') {
- depth -= 1;
- if (depth === 0) {
- end += 1;
- break;
- }
- }
- }
- return source.slice(start, end);
- }
- const helperBundle = [
- extractFunction(helperSource, 'parseUrlSafely'),
- extractFunction(helperSource, 'isSignupPageHost'),
- extractFunction(helperSource, 'isSignupEntryHost'),
- extractFunction(helperSource, 'matchesSourceUrlFamily'),
- ].join('\n');
- const api = new Function('tabRuntimeSource', `
- const self = {};
- let currentState = {
- sourceLastUrls: {},
- tabRegistry: {},
- };
- let currentTabs = [];
- const removedBatches = [];
- const logMessages = [];
- const chrome = {
- tabs: {
- async query() {
- return currentTabs;
- },
- async remove(ids) {
- removedBatches.push(ids);
- currentTabs = currentTabs.filter((tab) => !ids.includes(tab.id));
- },
- },
- };
- async function getState() {
- return currentState;
- }
- async function setState(updates) {
- currentState = { ...currentState, ...updates };
- }
- async function addLog(message, level = 'info') {
- logMessages.push({ message, level });
- }
- function getSourceLabel(source) {
- return source;
- }
- function isLocalhostOAuthCallbackUrl() {
- return false;
- }
- function isRetryableContentScriptTransportError() {
- return false;
- }
- function throwIfStopped() {}
- const LOG_PREFIX = '[test:bg]';
- const STOP_ERROR_MESSAGE = 'Flow stopped.';
- ${helperBundle}
- ${tabRuntimeSource}
- const runtime = self.MultiPageBackgroundTabRuntime.createTabRuntime({
- addLog,
- chrome,
- getSourceLabel,
- getState,
- isLocalhostOAuthCallbackUrl,
- isRetryableContentScriptTransportError,
- LOG_PREFIX,
- matchesSourceUrlFamily,
- setState,
- STOP_ERROR_MESSAGE,
- throwIfStopped,
- });
- return {
- matchesSourceUrlFamily,
- closeConflictingTabsForSource: runtime.closeConflictingTabsForSource,
- reset({ tabs, state }) {
- currentTabs = tabs;
- removedBatches.length = 0;
- logMessages.length = 0;
- currentState = {
- sourceLastUrls: {},
- tabRegistry: {},
- ...(state || {}),
- };
- },
- snapshot() {
- return {
- currentState,
- currentTabs,
- removedBatches,
- logMessages,
- };
- },
- };
- `)(tabRuntimeSource);
- (async () => {
- assert.strictEqual(
- api.matchesSourceUrlFamily('signup-page', 'https://chatgpt.com/', 'https://chatgpt.com/'),
- true,
- 'signup-page family should include chatgpt.com'
- );
- assert.strictEqual(
- api.matchesSourceUrlFamily('signup-page', 'https://chat.openai.com/', 'https://auth.openai.com/authorize'),
- true,
- 'signup-page family should include legacy chat.openai.com'
- );
- assert.strictEqual(
- api.matchesSourceUrlFamily('mail-phplife', 'https://mail.phplife.net/?_task=mail&_mbox=INBOX', 'https://mail.phplife.net/?_task=mail&_mbox=INBOX'),
- true,
- 'mail-phplife family should include mail.phplife.net'
- );
- api.reset({
- tabs: [
- { id: 1, url: 'https://chatgpt.com/' },
- { id: 2, url: 'https://chat.openai.com/' },
- { id: 3, url: 'https://auth.openai.com/authorize?client_id=test' },
- { id: 4, url: 'https://example.com/' },
- ],
- state: {
- sourceLastUrls: {
- 'signup-page': 'https://chatgpt.com/',
- },
- tabRegistry: {
- 'signup-page': { tabId: 3, ready: true },
- },
- },
- });
- await api.closeConflictingTabsForSource('signup-page', 'https://auth.openai.com/authorize', {
- excludeTabIds: [3],
- });
- let snapshot = api.snapshot();
- assert.deepStrictEqual(snapshot.removedBatches, [[1, 2]]);
- assert.deepStrictEqual(snapshot.currentTabs, [
- { id: 3, url: 'https://auth.openai.com/authorize?client_id=test' },
- { id: 4, url: 'https://example.com/' },
- ]);
- api.reset({
- tabs: [
- { id: 11, url: 'https://chatgpt.com/' },
- { id: 12, url: 'https://auth.openai.com/authorize?client_id=test' },
- ],
- state: {
- sourceLastUrls: {
- 'signup-page': 'https://auth.openai.com/authorize?client_id=test',
- },
- tabRegistry: {
- 'signup-page': { tabId: 11, ready: true },
- },
- },
- });
- await api.closeConflictingTabsForSource('signup-page', 'https://chatgpt.com/');
- snapshot = api.snapshot();
- assert.deepStrictEqual(snapshot.removedBatches, [[11, 12]]);
- assert.strictEqual(snapshot.currentState.tabRegistry['signup-page'], null);
- console.log('signup page tab cleanup tests passed');
- })().catch((error) => {
- console.error(error);
- process.exit(1);
- });
|