| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- 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;
- 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('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);
- });
|