feat: 添加账号运行历史功能,支持独立配置与文本留档
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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('background account history settings are normalized independently from hotmail service mode', () => {
|
||||
const bundle = [
|
||||
extractFunction('normalizeHotmailLocalBaseUrl'),
|
||||
extractFunction('normalizeAccountRunHistoryHelperBaseUrl'),
|
||||
extractFunction('normalizePersistentSettingValue'),
|
||||
].join('\n');
|
||||
|
||||
const api = new Function(`
|
||||
const DEFAULT_HOTMAIL_LOCAL_BASE_URL = 'http://127.0.0.1:17373';
|
||||
const DEFAULT_ACCOUNT_RUN_HISTORY_HELPER_BASE_URL = DEFAULT_HOTMAIL_LOCAL_BASE_URL;
|
||||
const DEFAULT_HOTMAIL_REMOTE_BASE_URL = '';
|
||||
const HOTMAIL_SERVICE_MODE_REMOTE = 'remote';
|
||||
const HOTMAIL_SERVICE_MODE_LOCAL = 'local';
|
||||
const PERSISTED_SETTING_DEFAULTS = {
|
||||
autoStepDelaySeconds: null,
|
||||
mailProvider: '163',
|
||||
};
|
||||
function normalizePanelMode(value) { return value === 'sub2api' ? 'sub2api' : 'cpa'; }
|
||||
function normalizeLocalCpaStep9Mode(value) { return value === 'bypass' ? 'bypass' : 'submit'; }
|
||||
function normalizeCpaCallbackMode(value) { return value === 'step6' ? 'step6' : 'step8'; }
|
||||
function normalizeAutoRunFallbackThreadIntervalMinutes(value) { return Number(value) || 0; }
|
||||
function normalizeAutoRunDelayMinutes(value) { return Number(value) || 30; }
|
||||
function normalizeAutoStepDelaySeconds(value) { return value == null || value === '' ? null : Number(value); }
|
||||
function normalizeMailProvider(value) { return String(value || '').trim().toLowerCase() || '163'; }
|
||||
function normalizeMail2925Mode(value) { return String(value || '').trim().toLowerCase() === 'receive' ? 'receive' : 'provide'; }
|
||||
function normalizeEmailGenerator(value) { return String(value || '').trim().toLowerCase() || 'duck'; }
|
||||
function normalizeIcloudHost(value) { const normalized = String(value || '').trim().toLowerCase(); return normalized === 'icloud.com' || normalized === 'icloud.com.cn' ? normalized : ''; }
|
||||
function normalizeHotmailServiceMode(value) { return String(value || '').trim().toLowerCase() === 'remote' ? 'remote' : 'local'; }
|
||||
function normalizeHotmailRemoteBaseUrl(value) { return String(value || '').trim(); }
|
||||
function normalizeCloudflareDomain(value) { return String(value || '').trim(); }
|
||||
function normalizeCloudflareDomains(value) { return Array.isArray(value) ? value : []; }
|
||||
function normalizeCloudflareTempEmailBaseUrl(value) { return String(value || '').trim(); }
|
||||
function normalizeCloudflareTempEmailReceiveMailbox(value) { return String(value || '').trim().toLowerCase(); }
|
||||
function normalizeCloudflareTempEmailDomain(value) { return String(value || '').trim(); }
|
||||
function normalizeCloudflareTempEmailDomains(value) { return Array.isArray(value) ? value : []; }
|
||||
function normalizeHotmailAccounts(value) { return Array.isArray(value) ? value : []; }
|
||||
${bundle}
|
||||
return {
|
||||
normalizeAccountRunHistoryHelperBaseUrl,
|
||||
normalizePersistentSettingValue,
|
||||
};
|
||||
`)();
|
||||
|
||||
assert.equal(api.normalizePersistentSettingValue('accountRunHistoryTextEnabled', 1), true);
|
||||
assert.equal(
|
||||
api.normalizePersistentSettingValue('accountRunHistoryHelperBaseUrl', 'http://127.0.0.1:17373/append-account-log'),
|
||||
'http://127.0.0.1:17373'
|
||||
);
|
||||
assert.equal(
|
||||
api.normalizeAccountRunHistoryHelperBaseUrl(''),
|
||||
'http://127.0.0.1:17373'
|
||||
);
|
||||
});
|
||||
@@ -31,7 +31,7 @@ test('account run history helper normalizes records and persists without helper
|
||||
const helpers = api.createAccountRunHistoryHelpers({
|
||||
ACCOUNT_RUN_HISTORY_STORAGE_KEY: 'accountRunHistory',
|
||||
addLog: async () => {},
|
||||
buildHotmailLocalEndpoint: (baseUrl, path) => `${baseUrl}${path}`,
|
||||
buildLocalHelperEndpoint: (baseUrl, path) => `${baseUrl}${path}`,
|
||||
chrome: {
|
||||
storage: {
|
||||
local: {
|
||||
@@ -46,11 +46,10 @@ test('account run history helper normalizes records and persists without helper
|
||||
getState: async () => ({
|
||||
email: ' latest@example.com ',
|
||||
password: ' secret ',
|
||||
hotmailServiceMode: 'remote',
|
||||
hotmailLocalBaseUrl: '',
|
||||
accountRunHistoryTextEnabled: false,
|
||||
accountRunHistoryHelperBaseUrl: '',
|
||||
}),
|
||||
HOTMAIL_SERVICE_MODE_LOCAL: 'local',
|
||||
normalizeHotmailLocalBaseUrl: (value) => String(value || '').trim(),
|
||||
normalizeAccountRunHistoryHelperBaseUrl: (value) => String(value || '').trim(),
|
||||
});
|
||||
|
||||
const record = helpers.buildAccountRunHistoryRecord(
|
||||
@@ -72,5 +71,6 @@ test('account run history helper normalizes records and persists without helper
|
||||
assert.equal(storedHistory.length, 2);
|
||||
assert.equal(storedHistory[1].reason, 'boom');
|
||||
assert.equal(fetchCalled, false);
|
||||
assert.equal(helpers.shouldAppendAccountRunTextFile({ hotmailServiceMode: 'remote', hotmailLocalBaseUrl: 'http://127.0.0.1:17373' }), false);
|
||||
assert.equal(helpers.shouldAppendAccountRunTextFile({ accountRunHistoryTextEnabled: false, accountRunHistoryHelperBaseUrl: 'http://127.0.0.1:17373' }), false);
|
||||
assert.equal(helpers.shouldAppendAccountRunTextFile({ accountRunHistoryTextEnabled: true, accountRunHistoryHelperBaseUrl: 'http://127.0.0.1:17373' }), true);
|
||||
});
|
||||
|
||||
@@ -55,16 +55,23 @@ test('sidepanel html contains account run history strip under log header', () =>
|
||||
assert.match(html, /id="account-run-history-meta"/);
|
||||
assert.match(html, /id="account-run-history-stats"/);
|
||||
assert.match(html, /id="account-run-history-list"/);
|
||||
assert.match(html, /id="input-account-run-history-text-enabled"/);
|
||||
assert.match(html, /id="input-account-run-history-helper-base-url"/);
|
||||
});
|
||||
|
||||
test('sidepanel account run history helpers classify statuses and summarize counts', () => {
|
||||
const bundle = [
|
||||
extractFunction('normalizeAccountRunHistoryHelperBaseUrlValue'),
|
||||
extractFunction('parseAccountRunStatus'),
|
||||
extractFunction('summarizeAccountRunHistory'),
|
||||
extractFunction('buildAccountRunHistoryDetailText'),
|
||||
].join('\n');
|
||||
|
||||
const api = new Function(`${bundle}; return { parseAccountRunStatus, summarizeAccountRunHistory, buildAccountRunHistoryDetailText };`)();
|
||||
const api = new Function(`
|
||||
const DEFAULT_ACCOUNT_RUN_HISTORY_HELPER_BASE_URL = 'http://127.0.0.1:17373';
|
||||
${bundle}
|
||||
return { normalizeAccountRunHistoryHelperBaseUrlValue, parseAccountRunStatus, summarizeAccountRunHistory, buildAccountRunHistoryDetailText };
|
||||
`)();
|
||||
|
||||
assert.deepStrictEqual(api.parseAccountRunStatus('step7_failed'), {
|
||||
kind: 'failed',
|
||||
@@ -104,4 +111,8 @@ test('sidepanel account run history helpers classify statuses and summarize coun
|
||||
api.buildAccountRunHistoryDetailText({ status: 'stopped', reason: '手动停止' }),
|
||||
'手动停止'
|
||||
);
|
||||
assert.equal(
|
||||
api.normalizeAccountRunHistoryHelperBaseUrlValue('http://127.0.0.1:17373/append-account-log'),
|
||||
'http://127.0.0.1:17373'
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user