抽取cookie清理逻辑为新的第六步

This commit is contained in:
QLHazyCoder
2026-04-17 15:56:19 +08:00
parent b387ebfa91
commit cbe9b1fabf
30 changed files with 630 additions and 603 deletions
+31 -56
View File
@@ -2,19 +2,43 @@ const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const source = fs.readFileSync('background/steps/oauth-login.js', 'utf8');
const globalScope = {};
const api = new Function('self', `${source}; return self.MultiPageBackgroundStep6;`)(globalScope);
test('step 6 runs cookie cleanup and completes from background', async () => {
const source = fs.readFileSync('background/steps/clear-login-cookies.js', 'utf8');
const globalScope = {};
const api = new Function('self', `${source}; return self.MultiPageBackgroundStep6;`)(globalScope);
test('step 6 retries up to configured limit and then fails', async () => {
const events = {
cleanupCalls: 0,
completedSteps: [],
};
const executor = api.createStep6Executor({
completeStepFromBackground: async (step) => {
events.completedSteps.push(step);
},
runPreStep6CookieCleanup: async () => {
events.cleanupCalls += 1;
},
});
await executor.executeStep6();
assert.equal(events.cleanupCalls, 1);
assert.deepStrictEqual(events.completedSteps, [6]);
});
test('step 7 retries up to configured limit and then fails', async () => {
const source = fs.readFileSync('background/steps/oauth-login.js', 'utf8');
const globalScope = {};
const api = new Function('self', `${source}; return self.MultiPageBackgroundStep7;`)(globalScope);
const events = {
refreshCalls: 0,
sendCalls: 0,
completed: 0,
};
const executor = api.createStep6Executor({
const executor = api.createStep7Executor({
addLog: async () => {},
completeStepFromBackground: async () => {
events.completed += 1;
@@ -29,15 +53,12 @@ test('step 6 retries up to configured limit and then fails', async () => {
return `https://oauth.example/${events.refreshCalls}`;
},
reuseOrCreateTab: async () => {},
runPreStep6CookieCleanup: async () => {
events.cleanupCalls += 1;
},
sendToContentScriptResilient: async () => {
events.sendCalls += 1;
return {
step6Outcome: 'recoverable',
state: 'email_page',
message: '当前仍停留在邮箱页',
message: '当前仍停留在邮箱页',
};
},
shouldSkipLoginVerificationForCpaCallback: () => false,
@@ -47,57 +68,11 @@ test('step 6 retries up to configured limit and then fails', async () => {
});
await assert.rejects(
() => executor.executeStep6({ email: 'user@example.com', password: 'secret' }),
() => executor.executeStep7({ email: 'user@example.com', password: 'secret' }),
/已重试 2 次,仍未成功/
);
assert.equal(events.cleanupCalls, 1);
assert.equal(events.refreshCalls, 3);
assert.equal(events.sendCalls, 3);
assert.equal(events.completed, 0);
});
test('step 6 can skip pre-login cleanup during step 7 recovery replay', async () => {
const events = {
cleanupCalls: 0,
completedPayloads: [],
};
const executor = api.createStep6Executor({
addLog: async () => {},
completeStepFromBackground: async (step, payload) => {
events.completedPayloads.push({ step, payload });
},
getErrorMessage: (error) => error?.message || String(error || ''),
getLoginAuthStateLabel: (state) => state || 'unknown',
getState: async () => ({ email: 'user@example.com', password: 'secret' }),
isStep6RecoverableResult: () => false,
isStep6SuccessResult: (result) => result?.step6Outcome === 'success',
refreshOAuthUrlBeforeStep6: async () => 'https://oauth.example/latest',
reuseOrCreateTab: async () => {},
runPreStep6CookieCleanup: async () => {
events.cleanupCalls += 1;
},
sendToContentScriptResilient: async () => ({
step6Outcome: 'success',
loginVerificationRequestedAt: 123,
}),
shouldSkipLoginVerificationForCpaCallback: () => false,
skipLoginVerificationStepsForCpaCallback: async () => {},
STEP6_MAX_ATTEMPTS: 3,
throwIfStopped: () => {},
});
await executor.executeStep6(
{ email: 'user@example.com', password: 'secret' },
{ skipPreLoginCleanup: true }
);
assert.equal(events.cleanupCalls, 0);
assert.deepStrictEqual(events.completedPayloads, [
{
step: 6,
payload: { loginVerificationRequestedAt: 123 },
},
]);
});