feat: add custom email pool mail compatibility
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
const bundle = [
|
||||
extractFunction('normalizeEmailGenerator'),
|
||||
extractFunction('normalizeCustomEmailPool'),
|
||||
extractFunction('getCustomEmailPool'),
|
||||
extractFunction('getCustomEmailPoolEmailForRun'),
|
||||
extractFunction('getEmailGeneratorLabel'),
|
||||
].join('\n');
|
||||
|
||||
function createApi() {
|
||||
return new Function(`
|
||||
const CUSTOM_EMAIL_POOL_GENERATOR = 'custom-pool';
|
||||
const CLOUDFLARE_TEMP_EMAIL_GENERATOR = 'cloudflare-temp-email';
|
||||
|
||||
${bundle}
|
||||
|
||||
return {
|
||||
normalizeEmailGenerator,
|
||||
normalizeCustomEmailPool,
|
||||
getCustomEmailPool,
|
||||
getCustomEmailPoolEmailForRun,
|
||||
getEmailGeneratorLabel,
|
||||
};
|
||||
`)();
|
||||
}
|
||||
|
||||
test('background recognizes custom email pool generator and label', () => {
|
||||
const api = createApi();
|
||||
|
||||
assert.equal(api.normalizeEmailGenerator('custom-pool'), 'custom-pool');
|
||||
assert.equal(api.getEmailGeneratorLabel('custom-pool'), '自定义邮箱池');
|
||||
});
|
||||
|
||||
test('background normalizes custom email pool input and keeps order', () => {
|
||||
const api = createApi();
|
||||
|
||||
assert.deepEqual(
|
||||
api.normalizeCustomEmailPool(' Foo@Example.com \ninvalid\nbar@example.com;baz@example.com '),
|
||||
['foo@example.com', 'bar@example.com', 'baz@example.com']
|
||||
);
|
||||
});
|
||||
|
||||
test('background selects the matching email for the current auto-run round', () => {
|
||||
const api = createApi();
|
||||
const state = {
|
||||
customEmailPool: ['first@example.com', 'second@example.com', 'third@example.com'],
|
||||
};
|
||||
|
||||
assert.equal(api.getCustomEmailPoolEmailForRun(state, 1), 'first@example.com');
|
||||
assert.equal(api.getCustomEmailPoolEmailForRun(state, 2), 'second@example.com');
|
||||
assert.equal(api.getCustomEmailPoolEmailForRun(state, 4), '');
|
||||
});
|
||||
@@ -76,3 +76,63 @@ test('generated email helper falls back to normal generator when 2925 is in rece
|
||||
['email', 'duck@example.com'],
|
||||
]);
|
||||
});
|
||||
|
||||
test('generated email helper can read the requested address from custom email pool', async () => {
|
||||
const source = fs.readFileSync('background/generated-email-helpers.js', 'utf8');
|
||||
const globalScope = {};
|
||||
const api = new Function('self', `${source}; return self.MultiPageGeneratedEmailHelpers;`)(globalScope);
|
||||
const events = [];
|
||||
|
||||
const helpers = api.createGeneratedEmailHelpers({
|
||||
addLog: async () => {},
|
||||
buildGeneratedAliasEmail: () => {
|
||||
throw new Error('should not build alias');
|
||||
},
|
||||
buildCloudflareTempEmailHeaders: () => ({}),
|
||||
CLOUDFLARE_TEMP_EMAIL_GENERATOR: 'cloudflare-temp-email',
|
||||
CUSTOM_EMAIL_POOL_GENERATOR: 'custom-pool',
|
||||
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: '' }),
|
||||
getCustomEmailPoolEmail: (state, targetRun) => state.customEmailPool?.[targetRun - 1] || '',
|
||||
getState: async () => ({
|
||||
customEmailPool: ['first@example.com', 'second@example.com'],
|
||||
emailGenerator: 'custom-pool',
|
||||
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 () => {
|
||||
throw new Error('should not open duck tab');
|
||||
},
|
||||
setEmailState: async (email) => {
|
||||
events.push(['email', email]);
|
||||
},
|
||||
throwIfStopped: () => {},
|
||||
});
|
||||
|
||||
const email = await helpers.fetchGeneratedEmail({
|
||||
customEmailPool: ['first@example.com', 'second@example.com'],
|
||||
emailGenerator: 'custom-pool',
|
||||
mailProvider: 'gmail',
|
||||
}, {
|
||||
generator: 'custom-pool',
|
||||
poolIndex: 1,
|
||||
});
|
||||
|
||||
assert.equal(email, 'second@example.com');
|
||||
assert.deepStrictEqual(events, [
|
||||
['email', 'second@example.com'],
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
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);
|
||||
}
|
||||
|
||||
test('sidepanel html exposes custom email pool generator option and input row', () => {
|
||||
const html = fs.readFileSync('sidepanel/sidepanel.html', 'utf8');
|
||||
|
||||
assert.match(html, /option value="custom-pool">自定义邮箱池<\/option>/);
|
||||
assert.match(html, /id="row-custom-email-pool"/);
|
||||
assert.match(html, /id="input-custom-email-pool"/);
|
||||
});
|
||||
|
||||
test('sidepanel locks run count to custom email pool size', () => {
|
||||
const bundle = [
|
||||
extractFunction('isCustomMailProvider'),
|
||||
extractFunction('normalizeCustomEmailPoolEntries'),
|
||||
extractFunction('getSelectedEmailGenerator'),
|
||||
extractFunction('usesGeneratedAliasMailProvider'),
|
||||
extractFunction('usesCustomEmailPoolGenerator'),
|
||||
extractFunction('getCustomEmailPoolSize'),
|
||||
extractFunction('getRunCountValue'),
|
||||
].join('\n');
|
||||
|
||||
const api = new Function(`
|
||||
const GMAIL_PROVIDER = 'gmail';
|
||||
const GMAIL_ALIAS_GENERATOR = 'gmail-alias';
|
||||
const CUSTOM_EMAIL_POOL_GENERATOR = 'custom-pool';
|
||||
const selectMailProvider = { value: 'gmail' };
|
||||
const selectEmailGenerator = { value: 'custom-pool' };
|
||||
const inputCustomEmailPool = { value: 'first@example.com\\nsecond@example.com' };
|
||||
const inputRunCount = { value: '99' };
|
||||
|
||||
function isLuckmailProvider() {
|
||||
return false;
|
||||
}
|
||||
|
||||
function isManagedAliasProvider() {
|
||||
return false;
|
||||
}
|
||||
|
||||
function getSelectedMail2925Mode() {
|
||||
return 'provide';
|
||||
}
|
||||
|
||||
function isManagedAliasProvider(provider) {
|
||||
return String(provider || '').trim().toLowerCase() === GMAIL_PROVIDER;
|
||||
}
|
||||
|
||||
${bundle}
|
||||
|
||||
return {
|
||||
getSelectedEmailGenerator,
|
||||
usesGeneratedAliasMailProvider,
|
||||
usesCustomEmailPoolGenerator,
|
||||
getCustomEmailPoolSize,
|
||||
getRunCountValue,
|
||||
};
|
||||
`)();
|
||||
|
||||
assert.equal(api.getSelectedEmailGenerator(), 'custom-pool');
|
||||
assert.equal(api.usesGeneratedAliasMailProvider('gmail', 'provide', 'gmail-alias'), true);
|
||||
assert.equal(api.usesGeneratedAliasMailProvider('gmail', 'provide', 'custom-pool'), false);
|
||||
assert.equal(api.usesCustomEmailPoolGenerator(), true);
|
||||
assert.equal(api.getCustomEmailPoolSize(), 2);
|
||||
assert.equal(api.getRunCountValue(), 2);
|
||||
});
|
||||
Reference in New Issue
Block a user