feat: 添加对 Duck 生成的邮箱的比较基线支持,增强邮箱生成逻辑

This commit is contained in:
QLHazyCoder
2026-05-11 16:41:54 +08:00
parent c063561a11
commit 1b0f4bb174
4 changed files with 356 additions and 24 deletions
@@ -78,6 +78,64 @@ test('generated email helper falls back to normal generator when 2925 is in rece
]);
});
test('generated email helper forwards the previous email to Duck generation as a comparison baseline', async () => {
const api = loadGeneratedEmailHelpersApi();
const requests = [];
const helpers = api.createGeneratedEmailHelpers({
addLog: async () => {},
buildGeneratedAliasEmail: () => {
throw new Error('should not build alias');
},
buildCloudflareTempEmailHeaders: () => ({}),
CLOUDFLARE_TEMP_EMAIL_GENERATOR: 'cloudflare-temp-email',
DUCK_AUTOFILL_URL: 'https://duckduckgo.com/email',
fetch: async () => ({ ok: true, text: async () => '{}' }),
fetchIcloudHideMyEmail: async () => {
throw new Error('should not use icloud generator');
},
getCloudflareTempEmailAddressFromResponse: () => '',
getCloudflareTempEmailConfig: () => ({ baseUrl: '', adminAuth: '', domain: '' }),
getState: async () => ({
email: 'Previous@Duck.com',
emailGenerator: 'duck',
mailProvider: 'gmail',
}),
ensureMail2925AccountForFlow: async () => {
throw new Error('should not allocate 2925 account');
},
joinCloudflareTempEmailUrl: () => '',
normalizeCloudflareDomain: () => '',
normalizeCloudflareTempEmailAddress: () => '',
normalizeEmailGenerator: (value) => String(value || '').trim().toLowerCase(),
isGeneratedAliasProvider: () => false,
reuseOrCreateTab: async () => {},
sendToContentScript: async (_source, message) => {
requests.push(message);
return { email: 'fresh@duck.com', generated: true };
},
setEmailState: async () => {},
throwIfStopped: () => {},
});
const email = await helpers.fetchGeneratedEmail({
email: 'Previous@Duck.com',
emailGenerator: 'duck',
mailProvider: 'gmail',
}, {
generator: 'duck',
generateNew: true,
});
assert.equal(email, 'fresh@duck.com');
assert.equal(requests.length, 1);
assert.equal(requests[0].type, 'FETCH_DUCK_EMAIL');
assert.deepEqual(requests[0].payload, {
generateNew: true,
previousEmail: 'previous@duck.com',
});
});
test('generated email helper can read the requested address from custom email pool', async () => {
const api = loadGeneratedEmailHelpersApi();
const events = [];
+187
View File
@@ -0,0 +1,187 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const source = fs.readFileSync('content/duck-mail.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 createInput(initialValue = '') {
return {
value: initialValue,
textContent: '',
innerText: '',
getAttribute(name) {
if (name === 'value') {
return this.value;
}
return '';
},
};
}
function createButton(onClick) {
return {
clickCount: 0,
textContent: 'Generate Private Duck Address',
getAttribute() {
return '';
},
click() {
this.clickCount += 1;
if (typeof onClick === 'function') {
onClick();
}
},
};
}
function createDocument(input, button) {
return {
querySelector(selector) {
if (selector === 'input.AutofillSettingsPanel__PrivateDuckAddressValue') {
return input;
}
if (selector === 'button.AutofillSettingsPanel__GeneratorButton') {
return button;
}
return null;
},
querySelectorAll(selector) {
switch (selector) {
case 'input.AutofillSettingsPanel__PrivateDuckAddressValue':
case 'input[class*="PrivateDuckAddressValue"]':
return input ? [input] : [];
case 'input[data-testid*="PrivateDuckAddressValue"]':
return [];
case 'input[value*="@duck.com" i]':
return input?.value?.includes('@duck.com') ? [input] : [];
case 'button.AutofillSettingsPanel__GeneratorButton':
return button ? [button] : [];
default:
return [];
}
},
};
}
function createApi({ document, sleep, log = () => {} }) {
const bundle = [extractFunction('fetchDuckEmail')].join('\n');
return new Function('window', 'document', 'waitForElement', 'humanPause', 'sleep', 'log', `
${bundle}
return { fetchDuckEmail };
`)(
{
CodexOperationDelay: {
async performOperationWithDelay(_meta, fn) {
return fn();
},
},
},
document,
async () => true,
async () => {},
sleep,
log
);
}
test('fetchDuckEmail waits for the existing page address to hydrate before accepting a new Duck email', async () => {
const input = createInput('');
const button = createButton(() => {});
let sleepCount = 0;
const api = createApi({
document: createDocument(input, button),
sleep: async () => {
sleepCount += 1;
if (sleepCount === 1) {
input.value = 'existing@duck.com';
} else if (sleepCount === 2) {
input.value = 'fresh@duck.com';
}
},
});
const result = await api.fetchDuckEmail({ generateNew: true });
assert.deepEqual(result, {
email: 'fresh@duck.com',
generated: true,
});
assert.equal(button.clickCount, 1);
});
test('fetchDuckEmail falls back to the previous Duck email when the page baseline never becomes visible', async () => {
const input = createInput('');
const button = createButton(() => {
input.value = 'previous@duck.com';
});
let sleepCount = 0;
const api = createApi({
document: createDocument(input, button),
sleep: async () => {
sleepCount += 1;
if (sleepCount === 13) {
input.value = 'fresh@duck.com';
}
},
});
const result = await api.fetchDuckEmail({
generateNew: true,
previousEmail: 'previous@duck.com',
});
assert.deepEqual(result, {
email: 'fresh@duck.com',
generated: true,
});
assert.equal(button.clickCount, 1);
});