phplife-mail-content.test.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. const test = require('node:test');
  2. const assert = require('node:assert/strict');
  3. const fs = require('node:fs');
  4. const source = fs.readFileSync('content/phplife-mail.js', 'utf8');
  5. function extractFunction(name) {
  6. const markers = [`async function ${name}(`, `function ${name}(`];
  7. const start = markers
  8. .map((marker) => source.indexOf(marker))
  9. .find((index) => index >= 0);
  10. if (start < 0) {
  11. throw new Error(`missing function ${name}`);
  12. }
  13. let parenDepth = 0;
  14. let signatureEnded = false;
  15. let braceStart = -1;
  16. for (let i = start; i < source.length; i += 1) {
  17. const ch = source[i];
  18. if (ch === '(') {
  19. parenDepth += 1;
  20. } else if (ch === ')') {
  21. parenDepth -= 1;
  22. if (parenDepth === 0) {
  23. signatureEnded = true;
  24. }
  25. } else if (ch === '{' && signatureEnded) {
  26. braceStart = i;
  27. break;
  28. }
  29. }
  30. if (braceStart < 0) {
  31. throw new Error(`missing body for function ${name}`);
  32. }
  33. let depth = 0;
  34. let end = braceStart;
  35. for (; end < source.length; end += 1) {
  36. const ch = source[end];
  37. if (ch === '{') depth += 1;
  38. if (ch === '}') {
  39. depth -= 1;
  40. if (depth === 0) {
  41. end += 1;
  42. break;
  43. }
  44. }
  45. }
  46. return source.slice(start, end);
  47. }
  48. test('phplife mail parser extracts code and recipient from roundcube message view', () => {
  49. const bundle = [
  50. extractFunction('normalizeText'),
  51. extractFunction('parseRoundcubeTimestamp'),
  52. extractFunction('extractVerificationCodes'),
  53. extractFunction('getMessageDetailsFromDocument'),
  54. ].join('\n');
  55. const api = new Function(`${bundle}
  56. return { getMessageDetailsFromDocument, parseRoundcubeTimestamp, extractVerificationCodes };
  57. `)();
  58. const selectors = {
  59. 'h2.subject': { textContent: 'Your ChatGPT code is 866785' },
  60. '.headers-table .header.from': { textContent: 'noreply@tm.openai.com' },
  61. '.headers-table .header.to': { textContent: 'n2026041807@a4sky.com' },
  62. '.headers-table .header.date': { textContent: '今天 12:30' },
  63. '#messagebody': {
  64. innerText: 'Enter this temporary verification code to continue: 866785. ChatGPT Log-in Code 866785',
  65. textContent: 'Enter this temporary verification code to continue: 866785. ChatGPT Log-in Code 866785',
  66. },
  67. };
  68. const fakeDocument = {
  69. querySelector(selector) {
  70. return selectors[selector] || null;
  71. },
  72. body: {
  73. innerText: '',
  74. textContent: '',
  75. },
  76. };
  77. const details = api.getMessageDetailsFromDocument(fakeDocument);
  78. assert.equal(details.subject, 'Your ChatGPT code is 866785');
  79. assert.equal(details.from, 'noreply@tm.openai.com');
  80. assert.equal(details.to, 'n2026041807@a4sky.com');
  81. assert.equal(details.codes[0], '866785');
  82. assert.equal(Number.isFinite(details.emailTimestamp), true);
  83. });
  84. test('phplife mail parser can read verification code directly from list row title', () => {
  85. const bundle = [
  86. extractFunction('normalizeText'),
  87. extractFunction('parseRoundcubeTimestamp'),
  88. extractFunction('extractVerificationCodes'),
  89. extractFunction('getRowText'),
  90. extractFunction('getRowDetails'),
  91. ].join('\n');
  92. const api = new Function(`${bundle}
  93. return { getRowDetails };
  94. `)();
  95. const subjectNode = {
  96. getAttribute(name) {
  97. return name === 'title' ? 'Your ChatGPT code is 866785' : '';
  98. },
  99. textContent: 'fallback subject',
  100. };
  101. const fromNode = { getAttribute() { return ''; }, textContent: 'noreply@tm.openai.com' };
  102. const toNode = { getAttribute() { return ''; }, textContent: 'n2026041807@a4sky.com' };
  103. const dateNode = { getAttribute() { return ''; }, textContent: '今天 12:30' };
  104. const fakeRow = {
  105. querySelector(selector) {
  106. if (selector === 'td.subject') return subjectNode;
  107. if (selector === 'td.fromto') return fromNode;
  108. if (selector === 'td.to') return toNode;
  109. if (selector === 'td.date') return dateNode;
  110. return null;
  111. },
  112. textContent: 'Your ChatGPT code is 866785 noreply@tm.openai.com n2026041807@a4sky.com 今天 12:30',
  113. };
  114. const details = api.getRowDetails(fakeRow);
  115. assert.equal(details.subject, 'Your ChatGPT code is 866785');
  116. assert.equal(details.codes[0], '866785');
  117. });