docs: record phone auth country match test

This commit is contained in:
QLHazyCoder
2026-04-26 20:41:25 +08:00
9 changed files with 1154 additions and 142 deletions
+109
View File
@@ -0,0 +1,109 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
test('icloud login helper distinguishes auth-required errors from transient context errors', () => {
const source = fs.readFileSync('background.js', 'utf8');
assert.match(
source,
/function isIcloudLoginRequiredError\(error\) \{[\s\S]*hasAuthStatus401[\s\S]*status \(409\|421\|429\|5\\d\\d\)[\s\S]*could not validate icloud session[\s\S]*return false;/m,
'login-required detection should only force login for auth failures and ignore transient 421/429/5xx statuses'
);
assert.match(
source,
/function isIcloudTransientContextError\(error\) \{[\s\S]*status \(401\|403\|409\|421\|429\|5\\d\\d\)[\s\S]*timeout[\s\S]*timed out/m,
'transient context detection should treat 421/429/5xx and timeout-like network errors as retryable context failures'
);
assert.match(
source,
/if \(isIcloudTransientContextError\(err\)\) \{[\s\S]*iCloud 别名加载受网络\/上下文波动影响,请稍后重试。/m,
'withIcloudLoginHelp should surface transient-context copy instead of forcing login prompt'
);
assert.match(
source,
/ICLOUD_TRANSIENT_RETRY_MAX_ATTEMPTS = 2/,
'icloud transient context handling should retry at least once before failing'
);
assert.match(
source,
/function getIcloudAliasCacheFromState\(state, options = \{\}\)/,
'icloud alias flow should expose cache lookup helper for transient fallback'
);
assert.match(
source,
/已回退最近缓存(\$\{[a-zA-Z0-9_]+\.length\} 条)/,
'icloud alias listing should fallback to cached aliases when transient context errors occur'
);
assert.match(
source,
/PERSISTENT_ALIAS_STATE_KEYS = \[[\s\S]*'icloudAliasCache'[\s\S]*'icloudAliasCacheAt'[\s\S]*\]/m,
'icloud alias cache should be persisted so transient fallback can survive restarts'
);
assert.match(
source,
/function shouldStopIcloudAutoFetchRetries\(error\) \{[\s\S]*网络\/上下文波动[\s\S]*status 421/m,
'icloud auto-fetch retry guard should stop repeated retries for transient session/context failures'
);
assert.match(
source,
/async function validateIcloudSessionViaPageContext\(tabId, setupUrl\) \{[\s\S]*world:\s*'MAIN'[\s\S]*\/validate/m,
'icloud service resolution should support page-context validate fallback when background validate keeps failing'
);
assert.match(
source,
/function isIcloudApiUrl\(url = ''\) \{[\s\S]*new URL\(rawUrl\)[\s\S]*hostname\.endsWith\('\.icloud\.com'\)[\s\S]*hostname\.endsWith\('\.icloud\.com\.cn'\)/m,
'icloud api url detection should match icloud subdomains so maildomainws hosts can trigger page-context fallback'
);
assert.match(
source,
/function appendIcloudClientQueryParams\(rawUrl = ''\) \{[\s\S]*clientBuildNumber[\s\S]*clientMasteringNumber[\s\S]*clientId[\s\S]*dsid/m,
'icloud maildomainws requests should include compatible client query params before sending requests'
);
assert.match(
source,
/function isIcloudMailPageUrl\(rawUrl = ''\) \{[\s\S]*pathname === '\/mail'[\s\S]*pathname\.startsWith\('\/mail\/'\)/m,
'icloud page-context fallback should be able to identify mail pages as preferred execution context'
);
assert.match(
source,
/async function waitForIcloudMailTabReady\(tabId, timeoutMs = 8000\) \{[\s\S]*status === 'complete'/m,
'icloud page-context fallback should wait for a created mail tab to finish loading before using it'
);
assert.match(
source,
/async function icloudRequestViaPageContext\(method, url, options = \{\}\) \{[\s\S]*ensureIcloudMailContextTab\([\s\S]*isIcloudMailPageUrl\(tab\?\.url\) \? 8 : 0[\s\S]*const mailTabs = sortedTabs\.filter/m,
'icloud page-context requests should ensure a mail-context tab exists and prioritize it before other icloud pages'
);
assert.match(
source,
/async function fetchIcloudHideMyEmail\(options = \{\}\) \{[\s\S]*const existingAliases = await listIcloudAliases\(\);/m,
'icloud auto-fetch should load aliases through listIcloudAliases so transient cache/local fallback applies before creation'
);
assert.match(
source,
/保留别名返回鉴权\/网络异常,正在回查别名列表确认是否已创建[\s\S]*保留请求异常,但已在列表确认别名/m,
'icloud auto-fetch should attempt list-confirmation recovery when reserve returns auth/network errors'
);
assert.match(
source,
/当前网络\/上下文波动,暂无法创建新别名,已临时回退复用/,
'icloud auto-fetch should fallback to reusable aliases when create-new fails due transient session/context issues'
);
});
+32 -1
View File
@@ -323,6 +323,18 @@ function getErrorMessage(error) {
function normalizeText(value) {
return String(value || '').replace(/\\s+/g, ' ').trim();
}
function appendIcloudClientQueryParams(url) {
return String(url || '');
}
function isIcloudMaildomainwsHost() {
return false;
}
function shouldTryIcloudRequestPageContextFallback() {
return false;
}
async function icloudRequestViaPageContext() {
throw new Error('not expected');
}
async function fetch() {
fetchCalls += 1;
if (fetchCalls === 1) {
@@ -381,6 +393,18 @@ function getErrorMessage(error) {
function normalizeText(value) {
return String(value || '').replace(/\\s+/g, ' ').trim();
}
function appendIcloudClientQueryParams(url) {
return String(url || '');
}
function isIcloudMaildomainwsHost() {
return false;
}
function shouldTryIcloudRequestPageContextFallback() {
return false;
}
async function icloudRequestViaPageContext() {
throw new Error('not expected');
}
async function fetch() {
fetchCalls += 1;
return {
@@ -451,6 +475,10 @@ async function loadNormalizedIcloudAliases() {
serviceUrl: 'https://p67-maildomainws.icloud.com',
};
}
async function listIcloudAliases() {
const response = await loadNormalizedIcloudAliases();
return response.aliases || [];
}
function pickReusableIcloudAlias() {
return null;
}
@@ -477,6 +505,9 @@ function broadcastIcloudAliasesChanged(payload) {
function findIcloudAliasByEmail(aliases, email) {
return (aliases || []).find((alias) => String(alias.email || '').toLowerCase() === String(email || '').toLowerCase()) || null;
}
function shouldStopIcloudAutoFetchRetries() {
return true;
}
${bundle}
return {
fetchIcloudHideMyEmail,
@@ -491,5 +522,5 @@ return {
assert.equal(email, 'fresh@icloud.com');
assert.deepEqual(api.readSelectedEmails(), ['fresh@icloud.com']);
assert.deepEqual(api.readBroadcasts(), [{ reason: 'created', email: 'fresh@icloud.com' }]);
assert.match(api.readLogs().map((entry) => entry.message).join('\n'), /按保留成功处理/);
assert.match(api.readLogs().map((entry) => entry.message).join('\n'), /已在列表确认别名/);
});
+2
View File
@@ -20,6 +20,8 @@ const {
test('normalizeIcloudHost and host preference helpers resolve supported hosts', () => {
assert.equal(normalizeIcloudHost('www.icloud.com'), 'icloud.com');
assert.equal(normalizeIcloudHost('setup.icloud.com.cn'), 'icloud.com.cn');
assert.equal(normalizeIcloudHost('setup.icloud.com:443'), 'icloud.com');
assert.equal(normalizeIcloudHost('https://setup.icloud.com.cn/setup/ws/1'), 'icloud.com.cn');
assert.equal(normalizeIcloudHost('example.com'), '');
assert.equal(getConfiguredIcloudHostPreference({ icloudHostPreference: 'icloud.com' }), 'icloud.com');