feat: 添加进入贡献模式的功能,并更新相关测试用例
This commit is contained in:
+29
-1
@@ -1190,6 +1190,29 @@ function openPlusContributionSupportModal() {
|
||||
});
|
||||
}
|
||||
|
||||
async function enterContributionModeFromPlusPrompt() {
|
||||
if (typeof chrome === 'undefined' || !chrome.runtime?.sendMessage) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const response = await chrome.runtime.sendMessage({
|
||||
type: 'SET_CONTRIBUTION_MODE',
|
||||
source: 'sidepanel',
|
||||
payload: { enabled: true },
|
||||
});
|
||||
|
||||
if (response?.error) {
|
||||
throw new Error(response.error);
|
||||
}
|
||||
if (response?.state && typeof applySettingsState === 'function') {
|
||||
applySettingsState(response.state);
|
||||
}
|
||||
if (typeof renderContributionMode === 'function') {
|
||||
renderContributionMode();
|
||||
}
|
||||
return response?.state || null;
|
||||
}
|
||||
|
||||
async function maybeShowPlusContributionPromptBeforeAutoRun(plusModeEnabled) {
|
||||
const records = Array.isArray(latestState?.accountRunHistory) ? latestState.accountRunHistory : [];
|
||||
if (!shouldShowPlusContributionPrompt(records, plusModeEnabled)) {
|
||||
@@ -1205,7 +1228,12 @@ async function maybeShowPlusContributionPromptBeforeAutoRun(plusModeEnabled) {
|
||||
}
|
||||
if (choice === 'contribute') {
|
||||
openExternalUrl(getContributionPortalUrl());
|
||||
showToast('已打开贡献页面,可以按页面提示贡献 Plus 账号。', 'info', 2200);
|
||||
try {
|
||||
await enterContributionModeFromPlusPrompt();
|
||||
showToast('已进入贡献模式,并打开贡献页面。', 'info', 2200);
|
||||
} catch (error) {
|
||||
showToast(`贡献模式开启失败:${error.message}`, 'error', 2600);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
@@ -184,6 +184,7 @@ test('Plus contribution prompt marks shown and donated choice adds twenty credit
|
||||
'shouldShowPlusContributionPrompt',
|
||||
'markPlusContributionPromptShown',
|
||||
'addPlusContributionPromptCredit',
|
||||
'enterContributionModeFromPlusPrompt',
|
||||
'maybeShowPlusContributionPromptBeforeAutoRun',
|
||||
]);
|
||||
|
||||
@@ -246,6 +247,7 @@ test('Plus contribution prompt opens portal and aborts normal auto run when cont
|
||||
'shouldShowPlusContributionPrompt',
|
||||
'markPlusContributionPromptShown',
|
||||
'addPlusContributionPromptCredit',
|
||||
'enterContributionModeFromPlusPrompt',
|
||||
'maybeShowPlusContributionPromptBeforeAutoRun',
|
||||
]);
|
||||
|
||||
@@ -274,6 +276,20 @@ function openExternalUrl(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,
|
||||
@@ -286,6 +302,11 @@ return {
|
||||
const result = await api.maybeShowPlusContributionPromptBeforeAutoRun(true);
|
||||
|
||||
assert.equal(result, false);
|
||||
assert.deepEqual(api.getEvents().map((event) => event.type), ['modal', 'open', 'toast']);
|
||||
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