| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571 |
- const test = require('node:test');
- const assert = require('node:assert/strict');
- const fs = require('node:fs');
- const source = fs.readFileSync('content/phplife-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;
- let inString = null;
- let escaped = false;
- for (; end < source.length; end += 1) {
- const ch = source[end];
- if (escaped) {
- escaped = false;
- continue;
- }
- if (ch === '\\') {
- escaped = true;
- continue;
- }
- if (inString) {
- if (ch === inString) {
- inString = null;
- }
- continue;
- }
- if (ch === '"' || ch === '\'' || ch === '`') {
- inString = ch;
- continue;
- }
- if (ch === '{') depth += 1;
- if (ch === '}') {
- depth -= 1;
- if (depth === 0) {
- end += 1;
- break;
- }
- }
- }
- return source.slice(start, end);
- }
- test('phplife mail parser extracts code and recipient from roundcube message view', () => {
- const bundle = [
- extractFunction('normalizeText'),
- extractFunction('parseRoundcubeTimestamp'),
- extractFunction('extractVerificationCodes'),
- extractFunction('getMessageDetailsFromDocument'),
- ].join('\n');
- const api = new Function(`${bundle}
- return { getMessageDetailsFromDocument, parseRoundcubeTimestamp, extractVerificationCodes };
- `)();
- const selectors = {
- 'h2.subject': { textContent: 'Your ChatGPT code is 866785' },
- '.headers-table .header.from': { textContent: 'noreply@tm.openai.com' },
- '.headers-table .header.to': { textContent: 'n2026041807@a4sky.com' },
- '.headers-table .header.date': { textContent: '今天 12:30' },
- '#messagebody': {
- innerText: 'Enter this temporary verification code to continue: 866785. ChatGPT Log-in Code 866785',
- textContent: 'Enter this temporary verification code to continue: 866785. ChatGPT Log-in Code 866785',
- },
- };
- const fakeDocument = {
- querySelector(selector) {
- return selectors[selector] || null;
- },
- body: {
- innerText: '',
- textContent: '',
- },
- };
- const details = api.getMessageDetailsFromDocument(fakeDocument);
- assert.equal(details.subject, 'Your ChatGPT code is 866785');
- assert.equal(details.from, 'noreply@tm.openai.com');
- assert.equal(details.to, 'n2026041807@a4sky.com');
- assert.equal(details.codes[0], '866785');
- assert.equal(Number.isFinite(details.emailTimestamp), true);
- });
- test('phplife mail parser can read verification code directly from list row title', () => {
- const bundle = [
- extractFunction('normalizeText'),
- extractFunction('parseRoundcubeTimestamp'),
- extractFunction('extractVerificationCodes'),
- extractFunction('getRowText'),
- extractFunction('getRowDetails'),
- ].join('\n');
- const api = new Function(`${bundle}
- return { getRowDetails };
- `)();
- const subjectNode = {
- getAttribute(name) {
- return name === 'title' ? 'Your ChatGPT code is 866785' : '';
- },
- textContent: 'fallback subject',
- };
- const fromNode = { getAttribute() { return ''; }, textContent: 'noreply@tm.openai.com' };
- const toNode = { getAttribute() { return ''; }, textContent: 'n2026041807@a4sky.com' };
- const dateNode = { getAttribute() { return ''; }, textContent: '今天 12:30' };
- const fakeRow = {
- querySelector(selector) {
- if (selector === 'td.subject') return subjectNode;
- if (selector === 'td.fromto') return fromNode;
- if (selector === 'td.to') return toNode;
- if (selector === 'td.date') return dateNode;
- return null;
- },
- textContent: 'Your ChatGPT code is 866785 noreply@tm.openai.com n2026041807@a4sky.com 今天 12:30',
- };
- const details = api.getRowDetails(fakeRow);
- assert.equal(details.subject, 'Your ChatGPT code is 866785');
- assert.equal(details.codes[0], '866785');
- });
- test('phplife refreshMessageList clicks inbox before refresh button', async () => {
- const bundle = [
- extractFunction('ensureInboxActive'),
- extractFunction('refreshMessageList'),
- ].join('\n');
- const api = new Function(`${bundle}
- const clickOrder = [];
- const inboxLink = {
- closest() {
- return { classList: { contains() { return true; } } };
- },
- click() {
- clickOrder.push('inbox');
- },
- };
- const refreshButton = {
- click() {
- clickOrder.push('refresh');
- },
- };
- function findInboxLink() {
- return inboxLink;
- }
- function findRefreshButton() {
- return refreshButton;
- }
- async function sleep() {}
- return {
- refreshMessageList,
- getClickOrder() {
- return clickOrder.slice();
- },
- };
- `)();
- await api.refreshMessageList();
- assert.deepEqual(api.getClickOrder(), ['inbox', 'refresh']);
- });
- test('phplife temporary login title opens detail to extract verification code', async () => {
- const bundle = [
- extractFunction('normalizeText'),
- extractFunction('normalizeMinuteTimestamp'),
- extractFunction('shouldOpenRowForCodeDetection'),
- extractFunction('selectCandidateCode'),
- extractFunction('matchesCurrentMessage'),
- extractFunction('scoreRowCandidate'),
- extractFunction('tryOpenRowsAndRead'),
- ].join('\n');
- const api = new Function(`${bundle}
- let opened = 0;
- const seenCodes = new Set();
- function throwIfStopped() {}
- function getMessageListRows() {
- return [{ id: 'row-1' }];
- }
- function getRowDetails() {
- return {
- row: { id: 'row-1' },
- subject: 'Your temporary ChatGPT login code',
- from: 'noreply@tm.openai.com',
- to: 'n2026041807@a4sky.com',
- dateText: '今天 12:30',
- emailTimestamp: Date.now(),
- codes: [],
- combinedText: 'Your temporary ChatGPT login code noreply@tm.openai.com n2026041807@a4sky.com',
- };
- }
- function matchesMailFilters() {
- return true;
- }
- function getTargetEmailMatchState() {
- return { matches: true, hasExplicitEmail: true };
- }
- function log() {}
- function getCurrentMessageUid() {
- return '';
- }
- function openMessageRow() {
- opened += 1;
- }
- async function sleep() {}
- async function waitForPreviewLoaded() {
- return {
- subject: 'Your temporary ChatGPT login code',
- combinedText: 'Your temporary ChatGPT login code 998877 noreply@tm.openai.com n2026041807@a4sky.com',
- emailTimestamp: Date.now(),
- codes: ['998877'],
- };
- }
- return {
- tryOpenRowsAndRead,
- getOpenedCount() {
- return opened;
- },
- };
- `)();
- const result = await api.tryOpenRowsAndRead(4, {
- senderFilters: ['openai'],
- subjectFilters: ['login', 'code'],
- targetEmail: 'n2026041807@a4sky.com',
- }, new Set(), 0);
- assert.equal(result.code, '998877');
- assert.equal(api.getOpenedCount(), 1);
- });
- test('phplife Chinese temporary login title also opens detail to extract verification code', async () => {
- const bundle = [
- extractFunction('normalizeText'),
- extractFunction('normalizeMinuteTimestamp'),
- extractFunction('shouldOpenRowForCodeDetection'),
- extractFunction('selectCandidateCode'),
- extractFunction('matchesCurrentMessage'),
- extractFunction('scoreRowCandidate'),
- extractFunction('tryOpenRowsAndRead'),
- ].join('\n');
- const api = new Function(`${bundle}
- let opened = 0;
- const seenCodes = new Set();
- function throwIfStopped() {}
- function getMessageListRows() {
- return [{ id: 'row-1' }];
- }
- function getRowDetails() {
- return {
- row: { id: 'row-1' },
- subject: '你的临时 ChatGPT 登录代码',
- from: 'noreply@tm.openai.com',
- to: 'n2026041807@a4sky.com',
- dateText: '今天 12:30',
- emailTimestamp: Date.now(),
- codes: [],
- combinedText: '你的临时 ChatGPT 登录代码 noreply@tm.openai.com n2026041807@a4sky.com',
- };
- }
- function matchesMailFilters() {
- return true;
- }
- function getTargetEmailMatchState() {
- return { matches: true, hasExplicitEmail: true };
- }
- function log() {}
- function getCurrentMessageUid() {
- return '';
- }
- function openMessageRow() {
- opened += 1;
- }
- async function sleep() {}
- async function waitForPreviewLoaded() {
- return {
- subject: '你的临时 ChatGPT 登录代码',
- combinedText: '你的临时 ChatGPT 登录代码 112233 noreply@tm.openai.com n2026041807@a4sky.com',
- emailTimestamp: Date.now(),
- codes: ['112233'],
- };
- }
- return {
- tryOpenRowsAndRead,
- getOpenedCount() {
- return opened;
- },
- };
- `)();
- const result = await api.tryOpenRowsAndRead(4, {
- senderFilters: ['openai'],
- subjectFilters: ['login', 'code', '登录', '代码'],
- targetEmail: 'n2026041807@a4sky.com',
- }, new Set(), 0);
- assert.equal(result.code, '112233');
- assert.equal(api.getOpenedCount(), 1);
- });
- test('phplife remote payload parser extracts target mailbox verification code directly from refresh API payload', () => {
- const bundle = [
- extractFunction('normalizeText'),
- extractFunction('parseRoundcubeTimestamp'),
- extractFunction('extractVerificationCodes'),
- extractFunction('extractEmails'),
- extractFunction('collectPayloadStringFragments'),
- extractFunction('extractRemoteExecText'),
- extractFunction('readBalancedJsonObject'),
- extractFunction('parseRemoteMessageRowEntries'),
- extractFunction('extractRemotePayloadFragments'),
- extractFunction('getTargetEmailMatchState'),
- extractFunction('matchesMailFilters'),
- extractFunction('normalizeFragmentText'),
- extractFunction('buildRemoteRowDetails'),
- extractFunction('findExactTargetRemoteRows'),
- extractFunction('scoreRowCandidate'),
- extractFunction('normalizeMinuteTimestamp'),
- extractFunction('matchesCurrentMessage'),
- extractFunction('shouldOpenRowForCodeDetection'),
- extractFunction('scoreRemotePayloadCandidate'),
- extractFunction('selectCandidateCode'),
- extractFunction('findVerificationCodeFromRemotePayload'),
- ].join('\n');
- const api = new Function(`${bundle}
- const seenCodes = new Set();
- class DOMParser {
- parseFromString(text) {
- return {
- body: { textContent: String(text || '').replace(/<[^>]+>/g, ' ') },
- documentElement: { textContent: String(text || '').replace(/<[^>]+>/g, ' ') },
- };
- }
- }
- return { findVerificationCodeFromRemotePayload };
- `)();
- const payload = JSON.stringify({
- exec: [
- "<tr><td class='subject' title='Your ChatGPT code is 445566'>Your ChatGPT code is 445566</td><td class='to'>n20260419170738@a4sky.com</td></tr>",
- ],
- });
- const result = api.findVerificationCodeFromRemotePayload(payload, {
- senderFilters: ['openai'],
- subjectFilters: ['code', 'verification', '验证码'],
- targetEmail: 'n20260419170738@a4sky.com',
- }, new Set(), 0);
- assert.equal(result.code, '445566');
- });
- test('phplife remote payload parser ignores codes for other mailbox addresses', () => {
- const bundle = [
- extractFunction('normalizeText'),
- extractFunction('parseRoundcubeTimestamp'),
- extractFunction('extractVerificationCodes'),
- extractFunction('extractEmails'),
- extractFunction('collectPayloadStringFragments'),
- extractFunction('extractRemoteExecText'),
- extractFunction('readBalancedJsonObject'),
- extractFunction('parseRemoteMessageRowEntries'),
- extractFunction('extractRemotePayloadFragments'),
- extractFunction('getTargetEmailMatchState'),
- extractFunction('matchesMailFilters'),
- extractFunction('normalizeFragmentText'),
- extractFunction('buildRemoteRowDetails'),
- extractFunction('findExactTargetRemoteRows'),
- extractFunction('scoreRowCandidate'),
- extractFunction('normalizeMinuteTimestamp'),
- extractFunction('matchesCurrentMessage'),
- extractFunction('shouldOpenRowForCodeDetection'),
- extractFunction('scoreRemotePayloadCandidate'),
- extractFunction('selectCandidateCode'),
- extractFunction('findVerificationCodeFromRemotePayload'),
- ].join('\n');
- const api = new Function(`${bundle}
- const seenCodes = new Set();
- class DOMParser {
- parseFromString(text) {
- return {
- body: { textContent: String(text || '').replace(/<[^>]+>/g, ' ') },
- documentElement: { textContent: String(text || '').replace(/<[^>]+>/g, ' ') },
- };
- }
- }
- return { findVerificationCodeFromRemotePayload };
- `)();
- const payload = JSON.stringify({
- exec: [
- "<tr><td class='subject'>Your ChatGPT code is 111111</td><td class='to'>other@a4sky.com</td></tr>",
- "<tr><td class='subject'>Your ChatGPT code is 222222</td><td class='to'>target@a4sky.com</td></tr>",
- ],
- });
- const result = api.findVerificationCodeFromRemotePayload(payload, {
- senderFilters: ['openai'],
- subjectFilters: ['code', 'verification', '验证码'],
- targetEmail: 'target@a4sky.com',
- }, new Set(), 0);
- assert.equal(result.code, '222222');
- });
- test('phplife remote payload parser does not fall back to other mailbox rows when target mailbox rows exist without direct code', () => {
- const bundle = [
- extractFunction('normalizeText'),
- extractFunction('parseRoundcubeTimestamp'),
- extractFunction('extractVerificationCodes'),
- extractFunction('extractEmails'),
- extractFunction('collectPayloadStringFragments'),
- extractFunction('extractRemoteExecText'),
- extractFunction('readBalancedJsonObject'),
- extractFunction('parseRemoteMessageRowEntries'),
- extractFunction('extractRemotePayloadFragments'),
- extractFunction('getTargetEmailMatchState'),
- extractFunction('matchesMailFilters'),
- extractFunction('normalizeFragmentText'),
- extractFunction('buildRemoteRowDetails'),
- extractFunction('findExactTargetRemoteRows'),
- extractFunction('scoreRowCandidate'),
- extractFunction('normalizeMinuteTimestamp'),
- extractFunction('matchesCurrentMessage'),
- extractFunction('shouldOpenRowForCodeDetection'),
- extractFunction('scoreRemotePayloadCandidate'),
- extractFunction('selectCandidateCode'),
- extractFunction('findVerificationCodeFromRemotePayload'),
- ].join('\n');
- const api = new Function(`${bundle}
- const seenCodes = new Set();
- class DOMParser {
- parseFromString(text) {
- return {
- body: { textContent: String(text || '').replace(/<[^>]+>/g, ' ') },
- documentElement: { textContent: String(text || '').replace(/<[^>]+>/g, ' ') },
- };
- }
- }
- return { findVerificationCodeFromRemotePayload };
- `)();
- const payload = JSON.stringify({
- exec: [
- "this.add_message_row(900,{\"subject\":\"Your temporary ChatGPT login code\",\"fromto\":\"<span title=\\\"otp@tm1.openai.com\\\">OpenAI<\\/span>\",\"date\":\"今天 18:24\",\"to\":\"<span title=\\\"target@a4sky.com\\\">target@a4sky.com<\\/span>\"},{\"ctype\":\"text/html\",\"mbox\":\"INBOX\"},false);",
- "this.add_message_row(899,{\"subject\":\"Your ChatGPT code is 999999\",\"fromto\":\"<span title=\\\"otp@tm1.openai.com\\\">OpenAI<\\/span>\",\"date\":\"今天 18:24\",\"to\":\"<span title=\\\"other@a4sky.com\\\">other@a4sky.com<\\/span>\"},{\"ctype\":\"text/html\",\"mbox\":\"INBOX\"},false);",
- ],
- });
- const result = api.findVerificationCodeFromRemotePayload(payload, {
- senderFilters: ['openai'],
- subjectFilters: ['code', 'verification', '验证码'],
- targetEmail: 'target@a4sky.com',
- }, new Set(), 0);
- assert.equal(result, null);
- });
- test('phplife tryReadRemoteMessageList prefers search payload scoped to target mailbox', async () => {
- const bundle = [
- extractFunction('normalizeText'),
- extractFunction('parseRoundcubeTimestamp'),
- extractFunction('extractVerificationCodes'),
- extractFunction('extractEmails'),
- extractFunction('collectPayloadStringFragments'),
- extractFunction('extractRemoteExecText'),
- extractFunction('readBalancedJsonObject'),
- extractFunction('parseRemoteMessageRowEntries'),
- extractFunction('extractRemotePayloadFragments'),
- extractFunction('getTargetEmailMatchState'),
- extractFunction('matchesMailFilters'),
- extractFunction('normalizeFragmentText'),
- extractFunction('buildRemoteRowDetails'),
- extractFunction('findExactTargetRemoteRows'),
- extractFunction('scoreRowCandidate'),
- extractFunction('normalizeMinuteTimestamp'),
- extractFunction('matchesCurrentMessage'),
- extractFunction('shouldOpenRowForCodeDetection'),
- extractFunction('scoreRemotePayloadCandidate'),
- extractFunction('selectCandidateCode'),
- extractFunction('findVerificationCodeFromRemotePayload'),
- extractFunction('tryReadRemoteMessageList'),
- ].join('\n');
- const api = new Function(`${bundle}
- const PHPLIFE_MAIL_PREFIX = '[MultiPage:mail-phplife]';
- function log() {}
- const seenCodes = new Set();
- class DOMParser {
- parseFromString(text) {
- return {
- body: { textContent: String(text || '').replace(/<[^>]+>/g, ' ') },
- documentElement: { textContent: String(text || '').replace(/<[^>]+>/g, ' ') },
- };
- }
- }
- async function requestRemoteMessageSearch() {
- const row = {
- subject: '你的 ChatGPT 代码为 991895',
- fromto: '<span title="otp@tm1.openai.com">OpenAI</span>',
- date: '今天 18:56',
- to: '<span title="n20260419185525@a4sky.com">n20260419185525@a4sky.com</span>',
- };
- const meta = {
- ctype: 'text/html',
- mbox: 'INBOX',
- };
- return JSON.stringify({
- exec: [
- 'this.add_message_row(524,' + JSON.stringify(row) + ',' + JSON.stringify(meta) + ',false);'
- ],
- });
- }
- async function requestRemoteMessageList() {
- throw new Error('list api should not be needed when search already matched');
- }
- return { tryReadRemoteMessageList };
- `)();
- const result = await api.tryReadRemoteMessageList(4, {
- senderFilters: ['openai'],
- subjectFilters: ['code', 'verification', '验证码'],
- targetEmail: 'n20260419185525@a4sky.com',
- filterAfterTimestamp: 0,
- }, new Set());
- assert.equal(result.code, '991895');
- });
|