feat: 移除 Plus 功能相关的代码和测试用例
This commit is contained in:
@@ -51,11 +51,6 @@ function extractFunction(name) {
|
||||
function createApi({
|
||||
refreshImpl,
|
||||
runCount = 3,
|
||||
plusModeEnabled = false,
|
||||
plusRiskEnabled = false,
|
||||
plusRiskConfirmed = true,
|
||||
plusRiskDismissPrompt = false,
|
||||
plusContributionImpl,
|
||||
persistImpl,
|
||||
} = {}) {
|
||||
const bundle = [
|
||||
@@ -68,8 +63,6 @@ function createApi({
|
||||
return new Function(`
|
||||
const events = [];
|
||||
const latestState = { contributionMode: false };
|
||||
const currentPlusModeEnabled = ${JSON.stringify(Boolean(plusModeEnabled))};
|
||||
const inputPlusModeEnabled = { checked: ${JSON.stringify(Boolean(plusModeEnabled))} };
|
||||
const inputAutoSkipFailures = { checked: false };
|
||||
const inputContributionNickname = { value: 'tester' };
|
||||
const inputContributionQq = { value: '123456' };
|
||||
@@ -115,23 +108,6 @@ function shouldWarnAutoRunFallbackRisk() { return false; }
|
||||
function isAutoRunFallbackRiskPromptDismissed() { return false; }
|
||||
async function openAutoRunFallbackRiskConfirmModal() { throw new Error('should not be called'); }
|
||||
function setAutoRunFallbackRiskPromptDismissed() {}
|
||||
function shouldWarnPlusAutoRunRisk(totalRuns, plusModeEnabled) {
|
||||
return ${JSON.stringify(Boolean(plusRiskEnabled))} && Boolean(plusModeEnabled) && Number(totalRuns) > 3;
|
||||
}
|
||||
function isAutoRunPlusRiskPromptDismissed() { return false; }
|
||||
async function openPlusAutoRunRiskConfirmModal(totalRuns) {
|
||||
events.push({ type: 'plus-risk-modal', totalRuns });
|
||||
return {
|
||||
confirmed: ${JSON.stringify(Boolean(plusRiskConfirmed))},
|
||||
dismissPrompt: ${JSON.stringify(Boolean(plusRiskDismissPrompt))},
|
||||
};
|
||||
}
|
||||
function setAutoRunPlusRiskPromptDismissed(dismissed) {
|
||||
events.push({ type: 'plus-risk-dismiss', dismissed });
|
||||
}
|
||||
async function maybeShowPlusContributionPromptBeforeAutoRun(plusModeEnabled) {
|
||||
${plusContributionImpl ? 'return (' + plusContributionImpl + ')(plusModeEnabled, events);' : 'return true;'}
|
||||
}
|
||||
function normalizeAutoDelayMinutes(value) { return Number(value) || 30; }
|
||||
async function refreshContributionContentHint() {
|
||||
events.push({ type: 'refresh' });
|
||||
@@ -214,61 +190,6 @@ test('startAutoRunFromCurrentSettings freezes run count before async settings sy
|
||||
assert.equal(events[3].message.payload.totalRuns, 20);
|
||||
});
|
||||
|
||||
test('startAutoRunFromCurrentSettings shows Plus risk warning before starting more than 3 runs', async () => {
|
||||
const api = createApi({
|
||||
runCount: 4,
|
||||
plusModeEnabled: true,
|
||||
plusRiskEnabled: true,
|
||||
});
|
||||
|
||||
const result = await api.startAutoRunFromCurrentSettings();
|
||||
const events = api.getEvents();
|
||||
|
||||
assert.equal(result, true);
|
||||
assert.deepEqual(
|
||||
events.map((entry) => entry.type),
|
||||
['refresh', 'sync-settings', 'plus-risk-modal', 'send']
|
||||
);
|
||||
assert.equal(events[2].totalRuns, 4);
|
||||
assert.equal(events[3].message.payload.totalRuns, 4);
|
||||
});
|
||||
|
||||
test('startAutoRunFromCurrentSettings aborts when Plus risk warning is declined', async () => {
|
||||
const api = createApi({
|
||||
runCount: 4,
|
||||
plusModeEnabled: true,
|
||||
plusRiskEnabled: true,
|
||||
plusRiskConfirmed: false,
|
||||
});
|
||||
|
||||
const result = await api.startAutoRunFromCurrentSettings();
|
||||
|
||||
assert.equal(result, false);
|
||||
assert.deepEqual(
|
||||
api.getEvents().map((entry) => entry.type),
|
||||
['refresh', 'sync-settings', 'plus-risk-modal']
|
||||
);
|
||||
});
|
||||
|
||||
test('startAutoRunFromCurrentSettings aborts when Plus contribution prompt opens contribution page', async () => {
|
||||
const api = createApi({
|
||||
plusModeEnabled: true,
|
||||
plusContributionImpl: `async (plusModeEnabled, events) => {
|
||||
events.push({ type: 'plus-contribution-modal', plusModeEnabled });
|
||||
return false;
|
||||
}`,
|
||||
});
|
||||
|
||||
const result = await api.startAutoRunFromCurrentSettings();
|
||||
|
||||
assert.equal(result, false);
|
||||
assert.deepEqual(
|
||||
api.getEvents().map((entry) => entry.type),
|
||||
['refresh', 'sync-settings', 'plus-contribution-modal']
|
||||
);
|
||||
assert.equal(api.getEvents()[2].plusModeEnabled, true);
|
||||
});
|
||||
|
||||
test('persistCurrentSettingsForAction forces a silent save even when settings are not marked dirty', async () => {
|
||||
const bundle = [
|
||||
extractFunction('waitForSettingsSaveIdle'),
|
||||
|
||||
@@ -65,48 +65,6 @@ return { shouldWarnAutoRunFallbackRisk };
|
||||
assert.equal(api.shouldWarnAutoRunFallbackRisk(10, true), true);
|
||||
});
|
||||
|
||||
test('Plus auto-run risk warning only starts above 3 runs in Plus mode', () => {
|
||||
const bundle = extractFunction('shouldWarnPlusAutoRunRisk');
|
||||
|
||||
const api = new Function(`
|
||||
const AUTO_RUN_PLUS_RISK_WARNING_MAX_SAFE_RUNS = 3;
|
||||
${bundle}
|
||||
return { shouldWarnPlusAutoRunRisk };
|
||||
`)();
|
||||
|
||||
assert.equal(api.shouldWarnPlusAutoRunRisk(3, true), false);
|
||||
assert.equal(api.shouldWarnPlusAutoRunRisk(4, true), true);
|
||||
assert.equal(api.shouldWarnPlusAutoRunRisk(10, false), false);
|
||||
});
|
||||
|
||||
test('Plus auto-run risk modal uses PayPal account warning copy', async () => {
|
||||
const bundle = extractFunction('openPlusAutoRunRiskConfirmModal');
|
||||
|
||||
const api = new Function(`
|
||||
let capturedOptions = null;
|
||||
async function openConfirmModalWithOption(options) {
|
||||
capturedOptions = options;
|
||||
return { confirmed: true, optionChecked: true };
|
||||
}
|
||||
${bundle}
|
||||
return {
|
||||
openPlusAutoRunRiskConfirmModal,
|
||||
getCapturedOptions() {
|
||||
return capturedOptions;
|
||||
},
|
||||
};
|
||||
`)();
|
||||
|
||||
const result = await api.openPlusAutoRunRiskConfirmModal(4);
|
||||
const options = api.getCapturedOptions();
|
||||
|
||||
assert.deepStrictEqual(result, { confirmed: true, dismissPrompt: true });
|
||||
assert.equal(options.title, 'Plus 自动轮数提醒');
|
||||
assert.match(options.message, /轮数过多可能造成 PayPal 或账号快速封号/);
|
||||
assert.match(options.message, /不要贪杯哦/);
|
||||
assert.equal(options.confirmLabel, '我知道了,继续');
|
||||
});
|
||||
|
||||
test('auto-run fallback risk modal uses the single-node warning copy', async () => {
|
||||
const bundle = extractFunction('openAutoRunFallbackRiskConfirmModal');
|
||||
|
||||
|
||||
@@ -1,312 +0,0 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
|
||||
const source = fs.readFileSync('sidepanel/sidepanel.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);
|
||||
}
|
||||
|
||||
function extractBundle(names) {
|
||||
return names.map((name) => extractFunction(name)).join('\n');
|
||||
}
|
||||
|
||||
function createPlusRecords(count, contributionMode = false) {
|
||||
return Array.from({ length: count }, (_, index) => ({
|
||||
recordId: `${contributionMode ? 'contribution' : 'plus'}-${index}@example.com`,
|
||||
email: `${contributionMode ? 'contribution' : 'plus'}-${index}@example.com`,
|
||||
finalStatus: 'success',
|
||||
plusModeEnabled: true,
|
||||
contributionMode,
|
||||
}));
|
||||
}
|
||||
|
||||
test('Plus contribution prompt starts every five normal Plus successes', () => {
|
||||
const bundle = extractBundle([
|
||||
'normalizePlusContributionPromptNumber',
|
||||
'normalizePlusContributionPromptLedger',
|
||||
'isSuccessfulPlusAccountRecord',
|
||||
'getPlusContributionPromptTotals',
|
||||
'getPlusContributionPromptProgress',
|
||||
'shouldShowPlusContributionPrompt',
|
||||
]);
|
||||
|
||||
const api = new Function(`
|
||||
const PLUS_CONTRIBUTION_PROMPT_THRESHOLD = 5;
|
||||
const PLUS_CONTRIBUTION_ACCOUNT_CREDIT = 5;
|
||||
${bundle}
|
||||
return {
|
||||
getPlusContributionPromptProgress,
|
||||
shouldShowPlusContributionPrompt,
|
||||
};
|
||||
`)();
|
||||
|
||||
assert.equal(
|
||||
api.shouldShowPlusContributionPrompt(createPlusRecords(4), true, { promptBaseline: 0, donationCredit: 0 }),
|
||||
false
|
||||
);
|
||||
assert.equal(
|
||||
api.shouldShowPlusContributionPrompt(createPlusRecords(5), true, { promptBaseline: 0, donationCredit: 0 }),
|
||||
true
|
||||
);
|
||||
assert.equal(
|
||||
api.shouldShowPlusContributionPrompt(createPlusRecords(8), false, { promptBaseline: 0, donationCredit: 0 }),
|
||||
false
|
||||
);
|
||||
});
|
||||
|
||||
test('Plus contribution success and donated credit delay the next prompt', () => {
|
||||
const bundle = extractBundle([
|
||||
'normalizePlusContributionPromptNumber',
|
||||
'normalizePlusContributionPromptLedger',
|
||||
'isSuccessfulPlusAccountRecord',
|
||||
'getPlusContributionPromptTotals',
|
||||
'getPlusContributionPromptProgress',
|
||||
'shouldShowPlusContributionPrompt',
|
||||
]);
|
||||
|
||||
const api = new Function(`
|
||||
const PLUS_CONTRIBUTION_PROMPT_THRESHOLD = 5;
|
||||
const PLUS_CONTRIBUTION_ACCOUNT_CREDIT = 5;
|
||||
${bundle}
|
||||
return { shouldShowPlusContributionPrompt };
|
||||
`)();
|
||||
|
||||
const afterPromptLedger = { promptBaseline: 5, donationCredit: 0 };
|
||||
assert.equal(
|
||||
api.shouldShowPlusContributionPrompt(
|
||||
[...createPlusRecords(14), ...createPlusRecords(1, true)],
|
||||
true,
|
||||
afterPromptLedger
|
||||
),
|
||||
false
|
||||
);
|
||||
assert.equal(
|
||||
api.shouldShowPlusContributionPrompt(
|
||||
[...createPlusRecords(15), ...createPlusRecords(1, true)],
|
||||
true,
|
||||
afterPromptLedger
|
||||
),
|
||||
true
|
||||
);
|
||||
|
||||
const donatedLedger = { promptBaseline: 5, donationCredit: 20 };
|
||||
assert.equal(api.shouldShowPlusContributionPrompt(createPlusRecords(29), true, donatedLedger), false);
|
||||
assert.equal(api.shouldShowPlusContributionPrompt(createPlusRecords(30), true, donatedLedger), true);
|
||||
});
|
||||
|
||||
test('Plus contribution support modal includes WeChat image and expected actions', async () => {
|
||||
const bundle = extractBundle([
|
||||
'getPlusContributionSupportImageUrl',
|
||||
'buildPlusContributionSupportPromptHtml',
|
||||
'openPlusContributionSupportModal',
|
||||
]);
|
||||
|
||||
const api = new Function(`
|
||||
let capturedOptions = null;
|
||||
const chrome = { runtime: { getURL: (path) => 'chrome-extension://test/' + path } };
|
||||
function escapeHtml(value) { return String(value || ''); }
|
||||
async function openActionModal(options) {
|
||||
capturedOptions = options;
|
||||
return 'donated';
|
||||
}
|
||||
${bundle}
|
||||
return {
|
||||
openPlusContributionSupportModal,
|
||||
getCapturedOptions() {
|
||||
return capturedOptions;
|
||||
},
|
||||
};
|
||||
`)();
|
||||
|
||||
const choice = await api.openPlusContributionSupportModal();
|
||||
const options = api.getCapturedOptions();
|
||||
|
||||
assert.equal(choice, 'donated');
|
||||
assert.equal(options.title, 'Plus 功能使用反馈');
|
||||
assert.match(options.messageHtml, /docs\/images\/微信\.png/);
|
||||
assert.deepEqual(options.actions.map((action) => action.label), ['取消', '去贡献账号', '已打赏']);
|
||||
});
|
||||
|
||||
test('Plus contribution prompt marks shown and donated choice adds twenty credits', async () => {
|
||||
const bundle = extractBundle([
|
||||
'normalizePlusContributionPromptNumber',
|
||||
'normalizePlusContributionPromptLedger',
|
||||
'getPlusContributionPromptLedger',
|
||||
'setPlusContributionPromptLedger',
|
||||
'isSuccessfulPlusAccountRecord',
|
||||
'getPlusContributionPromptTotals',
|
||||
'getPlusContributionPromptProgress',
|
||||
'shouldShowPlusContributionPrompt',
|
||||
'markPlusContributionPromptShown',
|
||||
'addPlusContributionPromptCredit',
|
||||
'enterContributionModeFromPlusPrompt',
|
||||
'maybeShowPlusContributionPromptBeforeAutoRun',
|
||||
]);
|
||||
|
||||
const api = new Function('records', `
|
||||
const PLUS_CONTRIBUTION_PROMPT_LEDGER_STORAGE_KEY = 'multipage-plus-contribution-prompt-ledger';
|
||||
const PLUS_CONTRIBUTION_PROMPT_THRESHOLD = 5;
|
||||
const PLUS_CONTRIBUTION_ACCOUNT_CREDIT = 5;
|
||||
const PLUS_CONTRIBUTION_DONATION_CREDIT = 20;
|
||||
const latestState = { accountRunHistory: records };
|
||||
const storage = {};
|
||||
const events = [];
|
||||
const localStorage = {
|
||||
getItem(key) { return Object.prototype.hasOwnProperty.call(storage, key) ? storage[key] : null; },
|
||||
setItem(key, value) { storage[key] = String(value); },
|
||||
};
|
||||
async function openPlusContributionSupportModal() {
|
||||
events.push({ type: 'modal' });
|
||||
return 'donated';
|
||||
}
|
||||
function showToast(message, type) {
|
||||
events.push({ type: 'toast', message, toastType: type });
|
||||
}
|
||||
function openExternalUrl(url) {
|
||||
events.push({ type: 'open', url });
|
||||
}
|
||||
function getContributionPortalUrl() {
|
||||
return 'https://apikey.qzz.io';
|
||||
}
|
||||
${bundle}
|
||||
return {
|
||||
maybeShowPlusContributionPromptBeforeAutoRun,
|
||||
getLedger() {
|
||||
return JSON.parse(storage[PLUS_CONTRIBUTION_PROMPT_LEDGER_STORAGE_KEY] || '{}');
|
||||
},
|
||||
getEvents() {
|
||||
return events;
|
||||
},
|
||||
};
|
||||
`)(createPlusRecords(5));
|
||||
|
||||
const result = await api.maybeShowPlusContributionPromptBeforeAutoRun(true);
|
||||
|
||||
assert.equal(result, true);
|
||||
assert.deepEqual(api.getEvents().map((event) => event.type), ['modal', 'toast']);
|
||||
assert.deepEqual(api.getLedger(), {
|
||||
promptBaseline: 5,
|
||||
donationCredit: 20,
|
||||
});
|
||||
});
|
||||
|
||||
test('Plus contribution prompt opens portal and aborts normal auto run when contribute is chosen', async () => {
|
||||
const bundle = extractBundle([
|
||||
'normalizePlusContributionPromptNumber',
|
||||
'normalizePlusContributionPromptLedger',
|
||||
'getPlusContributionPromptLedger',
|
||||
'setPlusContributionPromptLedger',
|
||||
'isSuccessfulPlusAccountRecord',
|
||||
'getPlusContributionPromptTotals',
|
||||
'getPlusContributionPromptProgress',
|
||||
'shouldShowPlusContributionPrompt',
|
||||
'markPlusContributionPromptShown',
|
||||
'addPlusContributionPromptCredit',
|
||||
'enterContributionModeFromPlusPrompt',
|
||||
'maybeShowPlusContributionPromptBeforeAutoRun',
|
||||
]);
|
||||
|
||||
const api = new Function('records', `
|
||||
const PLUS_CONTRIBUTION_PROMPT_LEDGER_STORAGE_KEY = 'multipage-plus-contribution-prompt-ledger';
|
||||
const PLUS_CONTRIBUTION_PROMPT_THRESHOLD = 5;
|
||||
const PLUS_CONTRIBUTION_ACCOUNT_CREDIT = 5;
|
||||
const PLUS_CONTRIBUTION_DONATION_CREDIT = 20;
|
||||
const latestState = { accountRunHistory: records };
|
||||
const storage = {};
|
||||
const events = [];
|
||||
const localStorage = {
|
||||
getItem(key) { return Object.prototype.hasOwnProperty.call(storage, key) ? storage[key] : null; },
|
||||
setItem(key, value) { storage[key] = String(value); },
|
||||
};
|
||||
async function openPlusContributionSupportModal() {
|
||||
events.push({ type: 'modal' });
|
||||
return 'contribute';
|
||||
}
|
||||
function showToast(message, type) {
|
||||
events.push({ type: 'toast', message, toastType: type });
|
||||
}
|
||||
function openExternalUrl(url) {
|
||||
events.push({ type: 'open', url });
|
||||
}
|
||||
function getContributionPortalUrl() {
|
||||
return 'https://apikey.qzz.io';
|
||||
}
|
||||
const chrome = {
|
||||
runtime: {
|
||||
async sendMessage(message) {
|
||||
events.push({ type: 'runtime', message });
|
||||
return { state: { contributionMode: true } };
|
||||
},
|
||||
},
|
||||
};
|
||||
function applySettingsState(state) {
|
||||
events.push({ type: 'apply', state });
|
||||
}
|
||||
function renderContributionMode() {
|
||||
events.push({ type: 'render' });
|
||||
}
|
||||
${bundle}
|
||||
return {
|
||||
maybeShowPlusContributionPromptBeforeAutoRun,
|
||||
getEvents() {
|
||||
return events;
|
||||
},
|
||||
};
|
||||
`)(createPlusRecords(5));
|
||||
|
||||
const result = await api.maybeShowPlusContributionPromptBeforeAutoRun(true);
|
||||
|
||||
assert.equal(result, false);
|
||||
assert.deepEqual(api.getEvents().map((event) => event.type), ['modal', 'open', 'runtime', 'apply', 'render', 'toast']);
|
||||
assert.equal(api.getEvents()[1].url, 'https://apikey.qzz.io');
|
||||
assert.deepEqual(api.getEvents()[2].message, {
|
||||
type: 'SET_CONTRIBUTION_MODE',
|
||||
source: 'sidepanel',
|
||||
payload: { enabled: true },
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user