fix(oauth): 整合 Step 8/9 回调修复并补充回归测试

- 支持 localhost 和 127.0.0.1 的本地回调识别,同时兼容 /auth/callback 与 /codex/callback
- 修复 Step 8 停止时的监听清理问题,并补充 onCommitted 和 onUpdated 的回调捕获路径
- 修复 Step 9 在本地 CPA 场景下的自动跳过与精确清理逻辑,避免误关正常本地页面
- 补充 Step 8 与 Step 9 的回归测试
This commit is contained in:
QLHazyCoder
2026-04-12 00:57:38 +08:00
parent 1105e9d4d9
commit 3e2457f7c7
8 changed files with 876 additions and 46 deletions
+104
View File
@@ -0,0 +1,104 @@
const assert = require('assert');
const fs = require('fs');
const source = fs.readFileSync('background.js', 'utf8');
function extractFunction(name) {
const start = source.indexOf(`function ${name}(`);
if (start < 0) {
throw new Error(`missing function ${name}`);
}
const braceStart = source.indexOf('{', start);
let depth = 0;
let end = braceStart;
for (; end < source.length; end++) {
const ch = source[end];
if (ch === '{') depth += 1;
if (ch === '}') {
depth -= 1;
if (depth === 0) {
end += 1;
break;
}
}
}
return source.slice(start, end);
}
const bundle = [
extractFunction('parseUrlSafely'),
extractFunction('isLocalhostOAuthCallbackUrl'),
extractFunction('getStep8CallbackUrlFromNavigation'),
extractFunction('getStep8CallbackUrlFromTabUpdate'),
].join('\n');
const api = new Function(`${bundle}; return { getStep8CallbackUrlFromNavigation, getStep8CallbackUrlFromTabUpdate };`)();
const callbackUrl = 'http://127.0.0.1:8317/codex/callback?code=abc&state=xyz';
assert.strictEqual(
api.getStep8CallbackUrlFromNavigation({
tabId: 123,
frameId: 0,
url: callbackUrl,
}, 123),
callbackUrl,
'应识别 onCommitted/onBeforeNavigate 命中的 callback'
);
assert.strictEqual(
api.getStep8CallbackUrlFromNavigation({
tabId: 123,
frameId: 1,
url: callbackUrl,
}, 123),
'',
'子 frame 不应命中 callback'
);
assert.strictEqual(
api.getStep8CallbackUrlFromNavigation({
tabId: 999,
frameId: 0,
url: callbackUrl,
}, 123),
'',
'非 signup tab 不应命中 callback'
);
assert.strictEqual(
api.getStep8CallbackUrlFromTabUpdate(
123,
{ url: callbackUrl },
{ url: callbackUrl },
123
),
callbackUrl,
'tabs.onUpdated 应能从 changeInfo.url 捕获 callback'
);
assert.strictEqual(
api.getStep8CallbackUrlFromTabUpdate(
123,
{},
{ url: callbackUrl },
123
),
callbackUrl,
'tabs.onUpdated 应能从 tab.url 兜底捕获 callback'
);
assert.strictEqual(
api.getStep8CallbackUrlFromTabUpdate(
999,
{ url: callbackUrl },
{ url: callbackUrl },
123
),
'',
'非 signup tab 的 tabs.onUpdated 不应命中 callback'
);
console.log('step8 callback handling tests passed');
+109
View File
@@ -0,0 +1,109 @@
const assert = require('assert');
const fs = require('fs');
const source = fs.readFileSync('background.js', 'utf8');
function extractFunction(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++) {
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++) {
const ch = source[end];
if (ch === '{') depth += 1;
if (ch === '}') {
depth -= 1;
if (depth === 0) {
end += 1;
break;
}
}
}
return source.slice(start, end);
}
const bundle = [
extractFunction('throwIfStopped'),
extractFunction('clickWithDebugger'),
].join('\n');
const api = new Function(`
let stopRequested = false;
const STOP_ERROR_MESSAGE = '流程已被用户停止。';
const commands = [];
let attachCount = 0;
let detachCount = 0;
const chrome = {
debugger: {
async attach(target, version) {
attachCount += 1;
},
async sendCommand(target, command, payload) {
commands.push(command);
if (command === 'Page.bringToFront') {
stopRequested = true;
}
},
async detach(target) {
detachCount += 1;
},
},
};
${bundle}
return {
clickWithDebugger,
snapshot() {
return {
commands,
attachCount,
detachCount,
};
},
};
`)();
(async () => {
const error = await api.clickWithDebugger(123, { centerX: 10, centerY: 20 }).catch((err) => err);
const state = api.snapshot();
assert.strictEqual(error?.message, '流程已被用户停止。', 'debugger 点击过程中收到 Stop 后应终止');
assert.deepStrictEqual(state.commands, ['Page.bringToFront'], 'Stop 后不应继续发送鼠标事件');
assert.strictEqual(state.attachCount, 1, '应先附加 debugger');
assert.strictEqual(state.detachCount, 1, '即使停止也应在 finally 中释放 debugger');
console.log('step8 debugger stop tests passed');
})().catch((error) => {
console.error(error);
process.exit(1);
});
+54
View File
@@ -0,0 +1,54 @@
const assert = require('assert');
const fs = require('fs');
const source = fs.readFileSync('background.js', 'utf8');
function extractFunction(name) {
const start = source.indexOf(`function ${name}(`);
if (start < 0) {
throw new Error(`missing function ${name}`);
}
const braceStart = source.indexOf('{', start);
let depth = 0;
let end = braceStart;
for (; end < source.length; end++) {
const ch = source[end];
if (ch === '{') depth += 1;
if (ch === '}') {
depth -= 1;
if (depth === 0) {
end += 1;
break;
}
}
}
return source.slice(start, end);
}
const bundle = [
extractFunction('isRetryableContentScriptTransportError'),
].join('\n');
const api = new Function(`${bundle}; return { isRetryableContentScriptTransportError };`)();
assert.strictEqual(
api.isRetryableContentScriptTransportError(new Error('Content script on signup-page did not respond in 2s. Try refreshing the tab and retry.')),
true,
'Step 8 状态探测短超时应被视为可重试错误'
);
assert.strictEqual(
api.isRetryableContentScriptTransportError(new Error('Content script on signup-page did not respond in 30s. Try refreshing the tab and retry.')),
true,
'普通内容脚本超时也应沿用可重试分支'
);
assert.strictEqual(
api.isRetryableContentScriptTransportError(new Error('按钮不存在')),
false,
'真实业务错误不应被误判为可重试传输错误'
);
console.log('step8 state timeout retry tests passed');
+209
View File
@@ -0,0 +1,209 @@
const assert = require('assert');
const fs = require('fs');
const source = fs.readFileSync('background.js', 'utf8');
function extractFunction(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++) {
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++) {
const ch = source[end];
if (ch === '{') depth += 1;
if (ch === '}') {
depth -= 1;
if (depth === 0) {
end += 1;
break;
}
}
}
return source.slice(start, end);
}
const bundle = [
extractFunction('throwIfStopped'),
extractFunction('cleanupStep8NavigationListeners'),
extractFunction('rejectPendingStep8'),
extractFunction('throwIfStep8SettledOrStopped'),
extractFunction('requestStop'),
extractFunction('executeStep8'),
].join('\n');
const api = new Function(`
let stopRequested = false;
const STOP_ERROR_MESSAGE = '流程已被用户停止。';
let webNavListener = null;
let webNavCommittedListener = null;
let step8TabUpdatedListener = null;
let step8PendingReject = null;
let autoRunActive = true;
let autoRunCurrentRun = 2;
let autoRunTotalRuns = 3;
let autoRunAttemptRun = 4;
const added = {
beforeNavigate: 0,
committed: 0,
tabUpdated: 0,
};
const removed = {
beforeNavigate: 0,
committed: 0,
tabUpdated: 0,
};
const sentMessages = [];
let clickCount = 0;
let resolveTabId = null;
const chrome = {
webNavigation: {
onBeforeNavigate: {
addListener(listener) {
added.beforeNavigate += 1;
},
removeListener(listener) {
removed.beforeNavigate += 1;
},
},
onCommitted: {
addListener(listener) {
added.committed += 1;
},
removeListener(listener) {
removed.committed += 1;
},
},
},
tabs: {
onUpdated: {
addListener(listener) {
added.tabUpdated += 1;
},
removeListener(listener) {
removed.tabUpdated += 1;
},
},
async update() {},
},
};
const stepWaiters = new Map();
let resumeWaiter = null;
function cancelPendingCommands() {}
async function addLog() {}
async function broadcastStopToContentScripts() {}
async function markRunningStepsStopped() {}
async function broadcastAutoRunStatus() {}
function getStep8CallbackUrlFromNavigation() { return ''; }
function getStep8CallbackUrlFromTabUpdate() { return ''; }
async function completeStepFromBackground() {}
async function getTabId() {
return await new Promise((resolve) => {
resolveTabId = resolve;
});
}
async function reuseOrCreateTab() {
return 999;
}
async function isTabAlive() {
return true;
}
async function sendToContentScript(source, message) {
sentMessages.push({ source, type: message.type });
return { rect: { centerX: 10, centerY: 20 } };
}
async function clickWithDebugger() {
clickCount += 1;
}
${bundle}
return {
executeStep8,
requestStop,
resolveTabId(tabId) {
if (!resolveTabId) {
throw new Error('resolveTabId is not ready');
}
resolveTabId(tabId);
},
snapshot() {
return {
stopRequested,
webNavListener,
webNavCommittedListener,
step8TabUpdatedListener,
step8PendingReject,
added,
removed,
sentMessages,
clickCount,
autoRunActive,
};
},
};
`)();
(async () => {
const step8Promise = api.executeStep8({ oauthUrl: 'https://example.com/oauth' });
const settledStep8Promise = step8Promise.catch((err) => err);
await new Promise((resolve) => setImmediate(resolve));
await api.requestStop();
await new Promise((resolve) => setImmediate(resolve));
api.resolveTabId(123);
await new Promise((resolve) => setImmediate(resolve));
await new Promise((resolve) => setImmediate(resolve));
const error = await settledStep8Promise;
const state = api.snapshot();
assert.strictEqual(error?.message, '流程已被用户停止。', 'Stop 后 Step 8 promise 应被拒绝为停止错误');
assert.deepStrictEqual(
state.added,
{ beforeNavigate: 0, committed: 0, tabUpdated: 0 },
'Stop 先发生时,不应再注册 Step 8 监听'
);
assert.strictEqual(state.sentMessages.length, 0, 'Stop 后不应再发送 STEP8_FIND_AND_CLICK 命令');
assert.strictEqual(state.clickCount, 0, 'Stop 后不应再触发 debugger 点击');
assert.strictEqual(state.webNavListener, null, 'Stop 后 onBeforeNavigate 引用应为空');
assert.strictEqual(state.webNavCommittedListener, null, 'Stop 后 onCommitted 引用应为空');
assert.strictEqual(state.step8TabUpdatedListener, null, 'Stop 后 tabs.onUpdated 引用应为空');
assert.strictEqual(state.step8PendingReject, null, 'Stop 后不应保留 Step 8 挂起 reject');
console.log('step8 stop cleanup tests passed');
})().catch((error) => {
console.error(error);
process.exit(1);
});
+58
View File
@@ -0,0 +1,58 @@
const assert = require('assert');
const fs = require('fs');
const source = fs.readFileSync('background.js', 'utf8');
function extractFunction(name) {
const start = source.indexOf(`function ${name}(`);
if (start < 0) {
throw new Error(`missing function ${name}`);
}
const braceStart = source.indexOf('{', start);
let depth = 0;
let end = braceStart;
for (; end < source.length; end++) {
const ch = source[end];
if (ch === '{') depth += 1;
if (ch === '}') {
depth -= 1;
if (depth === 0) {
end += 1;
break;
}
}
}
return source.slice(start, end);
}
const bundle = [
extractFunction('parseUrlSafely'),
extractFunction('isLocalCpaUrl'),
extractFunction('shouldBypassStep9ForLocalCpa'),
].join('\n');
const api = new Function(`${bundle}; return { isLocalCpaUrl, shouldBypassStep9ForLocalCpa };`)();
assert.strictEqual(api.isLocalCpaUrl('http://127.0.0.1:8317/management.html#/oauth'), true, '127.0.0.1 应视为本地 CPA');
assert.strictEqual(api.isLocalCpaUrl('http://localhost:1455/management.html#/oauth'), true, 'localhost 应视为本地 CPA');
assert.strictEqual(api.isLocalCpaUrl('https://example.com/management.html#/oauth'), false, '远程域名不应视为本地 CPA');
assert.strictEqual(api.isLocalCpaUrl('notaurl'), false, '非法 URL 不应视为本地 CPA');
assert.strictEqual(api.shouldBypassStep9ForLocalCpa({
vpsUrl: 'http://127.0.0.1:8317/management.html#/oauth',
localhostUrl: 'http://127.0.0.1:8317/codex/callback?code=abc&state=xyz',
}), true, '本地 CPA 且已有 callback 时应跳过远程提交流程');
assert.strictEqual(api.shouldBypassStep9ForLocalCpa({
vpsUrl: 'https://example.com/management.html#/oauth',
localhostUrl: 'http://127.0.0.1:8317/codex/callback?code=abc&state=xyz',
}), false, '远程 CPA 不应跳过步骤 9');
assert.strictEqual(api.shouldBypassStep9ForLocalCpa({
vpsUrl: 'http://127.0.0.1:8317/management.html#/oauth',
localhostUrl: '',
}), false, '没有 callback 时不应跳过步骤 9');
console.log('step9 cpa mode tests passed');
+193
View File
@@ -0,0 +1,193 @@
const assert = require('assert');
const fs = require('fs');
const source = fs.readFileSync('background.js', 'utf8');
function extractFunction(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++) {
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++) {
const ch = source[end];
if (ch === '{') depth += 1;
if (ch === '}') {
depth -= 1;
if (depth === 0) {
end += 1;
break;
}
}
}
return source.slice(start, end);
}
const bundle = [
extractFunction('getTabRegistry'),
extractFunction('parseUrlSafely'),
extractFunction('isLocalhostOAuthCallbackUrl'),
extractFunction('isLocalhostOAuthCallbackTabMatch'),
extractFunction('closeLocalhostCallbackTabs'),
extractFunction('handleStepData'),
].join('\n');
const api = new Function(`
let currentState = {
tabRegistry: {
'signup-page': { tabId: 1, ready: true },
'vps-panel': { tabId: 99, ready: true },
},
};
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 setEmailState(email) {
currentState = { ...currentState, email };
}
function broadcastDataUpdate() {}
async function addLog(message) {
logMessages.push(message);
}
${bundle}
return {
handleStepData,
closeLocalhostCallbackTabs,
isLocalhostOAuthCallbackTabMatch,
reset({ tabs, tabRegistry }) {
currentTabs = tabs;
removedBatches.length = 0;
logMessages.length = 0;
currentState = {
tabRegistry: tabRegistry || {},
};
},
snapshot() {
return {
currentState,
removedBatches,
logMessages,
};
},
};
`)();
(async () => {
const codexCallbackUrl = 'http://127.0.0.1:8317/codex/callback?code=abc&state=xyz';
const authCallbackUrl = 'http://localhost:1455/auth/callback?code=def&state=uvw';
assert.strictEqual(
api.isLocalhostOAuthCallbackTabMatch(codexCallbackUrl, codexCallbackUrl),
true,
'真实 callback 页应命中清理规则'
);
assert.strictEqual(
api.isLocalhostOAuthCallbackTabMatch(codexCallbackUrl, authCallbackUrl),
false,
'/codex/callback 不应误伤 /auth/callback'
);
assert.strictEqual(
api.isLocalhostOAuthCallbackTabMatch(authCallbackUrl, codexCallbackUrl),
false,
'/auth/callback 不应误伤 /codex/callback'
);
api.reset({
tabs: [
{ id: 1, url: codexCallbackUrl },
{ id: 2, url: 'http://127.0.0.1:8317/codex/dashboard' },
{ id: 3, url: 'http://127.0.0.1:8317/codex/callback?code=other&state=xyz' },
{ id: 4, url: authCallbackUrl },
],
tabRegistry: {
'signup-page': { tabId: 1, ready: true },
'vps-panel': { tabId: 99, ready: true },
},
});
await api.handleStepData(9, { localhostUrl: codexCallbackUrl });
let snapshot = api.snapshot();
assert.deepStrictEqual(snapshot.removedBatches, [[1]], 'handleStepData(9) 只应关闭当前 callback 页');
assert.strictEqual(
snapshot.currentState.tabRegistry['signup-page'],
null,
'关闭 callback 页后应同步清理 signup-page 的 tabRegistry'
);
assert.deepStrictEqual(
snapshot.currentState.tabRegistry['vps-panel'],
{ tabId: 99, ready: true },
'不相关的 tabRegistry 项不应被误清理'
);
api.reset({
tabs: [
{ id: 1, url: codexCallbackUrl },
{ id: 4, url: authCallbackUrl },
{ id: 5, url: 'http://localhost:1455/auth/dashboard' },
],
tabRegistry: {},
});
const closedCount = await api.closeLocalhostCallbackTabs(authCallbackUrl);
snapshot = api.snapshot();
assert.strictEqual(closedCount, 1, 'auth callback 也应只关闭当前命中的 callback 页');
assert.deepStrictEqual(snapshot.removedBatches, [[4]], '不应按 /auth 前缀批量清理页面');
assert.strictEqual(snapshot.logMessages.length, 1, '发生清理时应记录一条日志');
console.log('step9 localhost cleanup scope tests passed');
})().catch((error) => {
console.error(error);
process.exit(1);
});