fix(accounts): 统一流程完成后的账号标记

(cherry picked from commit 4d2016015f1218c2277ba6bc3447f81a81b234ed)
This commit is contained in:
朴圣佑
2026-04-26 22:21:58 +08:00
committed by QLHazyCoder
parent 8cd5346a97
commit 75e259ae5c
4 changed files with 279 additions and 15 deletions
+78
View File
@@ -801,3 +801,81 @@ return {
assert.deepStrictEqual(snapshot.clearedOptions, { clearEmail: true });
assert.equal(snapshot.logs.at(-1).message, '当前 LuckMail 邮箱运行态已清空,下轮将优先复用未用邮箱或重新购买邮箱。');
});
test('handleStepData marks current LuckMail purchase as used on Plus final step 13', async () => {
const bundle = extractFunction('handleStepData');
const factory = new Function(`
let usedMarker = null;
const logs = [];
async function closeLocalhostCallbackTabs() {}
async function getState() {
return {
plusModeEnabled: true,
mailProvider: 'luckmail-api',
currentHotmailAccountId: null,
currentLuckmailPurchase: {
id: 456,
email_address: 'plus@outlook.com',
},
email: 'plus@outlook.com',
};
}
function getLastStepIdForState(state) {
return state.plusModeEnabled ? 13 : 10;
}
function getCurrentLuckmailPurchase(state) {
return state.currentLuckmailPurchase;
}
function isHotmailProvider() {
return false;
}
async function patchHotmailAccount() {}
function isLuckmailProvider(state) {
return state.mailProvider === 'luckmail-api';
}
async function setLuckmailPurchaseUsedState(purchaseId, used) {
usedMarker = { purchaseId, used };
}
async function clearLuckmailRuntimeState() {}
async function addLog(message, level) {
logs.push({ message, level });
}
function buildLocalhostCleanupPrefix() {
return '';
}
async function closeTabsByUrlPrefix() {}
function shouldUseCustomRegistrationEmail() {
return false;
}
async function setEmailStateSilently() {}
async function setState() {}
function broadcastDataUpdate() {}
function isLocalhostOAuthCallbackUrl() {
return true;
}
async function finalizeIcloudAliasAfterSuccessfulFlow() {}
${bundle}
return {
handleStepData,
snapshot() {
return { usedMarker, logs };
},
};
`);
const api = factory();
await api.handleStepData(10, {});
assert.equal(api.snapshot().usedMarker, null);
await api.handleStepData(13, {
localhostUrl: 'http://localhost:1455/auth/callback?code=abc&state=xyz',
});
const snapshot = api.snapshot();
assert.deepStrictEqual(snapshot.usedMarker, { purchaseId: 456, used: true });
assert.equal(snapshot.logs.some((entry) => /已在本地标记为已用/.test(entry.message)), true);
});
@@ -0,0 +1,106 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node: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 += 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);
}
test('markCurrentRegistrationAccountUsed uses fresh state when checkout passes stale state', async () => {
const bundle = extractFunction('markCurrentRegistrationAccountUsed');
const factory = new Function(`
const patchCalls = [];
const logs = [];
async function getState() {
return {
mailProvider: 'hotmail',
currentHotmailAccountId: 'hot-1',
email: 'fresh@example.com',
};
}
function isHotmailProvider(state) {
return String(state.mailProvider || '').toLowerCase() === 'hotmail';
}
function isLuckmailProvider() {
return false;
}
function getCurrentLuckmailPurchase() {
return null;
}
async function patchHotmailAccount(id, updates) {
patchCalls.push({ id, updates });
}
async function setLuckmailPurchaseUsedState() {}
async function clearLuckmailRuntimeState() {}
async function patchMail2925Account() {}
async function finalizeIcloudAliasAfterSuccessfulFlow() {
return { handled: false };
}
async function markCurrentCustomEmailPoolEntryUsed() {
return { updated: false };
}
async function addLog(message, level) {
logs.push({ message, level });
}
${bundle}
return { markCurrentRegistrationAccountUsed, patchCalls, logs };
`);
const api = factory();
const result = await api.markCurrentRegistrationAccountUsed({ email: 'stale@example.com' }, {
logPrefix: 'Plus Checkout:当前账号没有免费试用资格',
});
assert.equal(result.updated, true);
assert.equal(api.patchCalls.length, 1);
assert.equal(api.patchCalls[0].id, 'hot-1');
assert.equal(api.patchCalls[0].updates.used, true);
assert.equal(api.logs.some((entry) => /Hotmail 账号已标记为已用/.test(entry.message)), true);
});