icloud-mail-content.test.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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/icloud-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('readOpenedMailBody falls back to thread detail pane and extracts verification code', () => {
  49. const bundle = [
  50. extractFunction('normalizeText'),
  51. extractFunction('extractVerificationCode'),
  52. extractFunction('getOpenedMailBodyRoot'),
  53. extractFunction('readOpenedMailBody'),
  54. ].join('\n');
  55. const api = new Function(`
  56. const detailPane = {
  57. innerText: '此邮件包含远程内容。 你的 ChatGPT 代码为 731091 输入此临时验证码以继续:731091',
  58. textContent: '此邮件包含远程内容。 你的 ChatGPT 代码为 731091 输入此临时验证码以继续:731091',
  59. };
  60. const document = {
  61. querySelector(selector) {
  62. if (selector.includes('.pane.thread-detail-pane')) {
  63. return detailPane;
  64. }
  65. return null;
  66. },
  67. };
  68. ${bundle}
  69. return { readOpenedMailBody, extractVerificationCode };
  70. `)();
  71. const bodyText = api.readOpenedMailBody();
  72. assert.match(bodyText, /731091/);
  73. assert.equal(api.extractVerificationCode(bodyText), '731091');
  74. });
  75. test('readOpenedMailBody ignores conversation list rows when no detail pane is open', () => {
  76. const bundle = [
  77. extractFunction('normalizeText'),
  78. extractFunction('getOpenedMailBodyRoot'),
  79. extractFunction('readOpenedMailBody'),
  80. ].join('\n');
  81. const api = new Function(`
  82. const document = {
  83. querySelector(selector) {
  84. if (selector === '.mail-message-defaults, .pane.thread-detail-pane') {
  85. return null;
  86. }
  87. throw new Error('unexpected selector: ' + selector);
  88. },
  89. };
  90. ${bundle}
  91. return { readOpenedMailBody };
  92. `)();
  93. assert.equal(api.readOpenedMailBody(), '');
  94. });
  95. test('isThreadItemSelected follows the selected thread-list-item instead of the content container itself', () => {
  96. const bundle = [
  97. extractFunction('normalizeText'),
  98. extractFunction('getThreadItemMetadata'),
  99. extractFunction('buildItemSignature'),
  100. extractFunction('getThreadListItemRoot'),
  101. extractFunction('isThreadItemSelected'),
  102. ].join('\n');
  103. const selectedRoot = {
  104. getAttribute(name) {
  105. return name === 'aria-selected' ? 'true' : '';
  106. },
  107. className: 'thread-list-item ic-z3c00x',
  108. };
  109. const selectedItem = {
  110. closest() {
  111. return selectedRoot;
  112. },
  113. getAttribute() {
  114. return '';
  115. },
  116. querySelector(selector) {
  117. const map = {
  118. '.thread-participants': { textContent: 'OpenAI' },
  119. '.thread-subject': { textContent: '你的 ChatGPT 代码为 731091' },
  120. '.thread-preview': { textContent: '输入此临时验证码以继续:731091' },
  121. '.thread-timestamp': { textContent: '下午1:35' },
  122. };
  123. return map[selector] || null;
  124. },
  125. };
  126. const staleRoot = {
  127. getAttribute() {
  128. return '';
  129. },
  130. className: 'thread-list-item ic-z3c00x',
  131. };
  132. const staleItem = {
  133. closest() {
  134. return staleRoot;
  135. },
  136. getAttribute() {
  137. return '';
  138. },
  139. querySelector(selector) {
  140. const map = {
  141. '.thread-participants': { textContent: 'JetBrains Sales' },
  142. '.thread-subject': { textContent: '旧邮件' },
  143. '.thread-preview': { textContent: '旧摘要' },
  144. '.thread-timestamp': { textContent: '2026/3/4' },
  145. };
  146. return map[selector] || null;
  147. },
  148. };
  149. const api = new Function(`
  150. let threadItems = [];
  151. function collectThreadItems() {
  152. return threadItems;
  153. }
  154. ${bundle}
  155. return {
  156. buildItemSignature,
  157. isThreadItemSelected,
  158. setThreadItems(next) {
  159. threadItems = next;
  160. },
  161. };
  162. `)();
  163. api.setThreadItems([selectedItem, staleItem]);
  164. assert.equal(api.isThreadItemSelected(staleItem, api.buildItemSignature(selectedItem)), true);
  165. assert.equal(api.isThreadItemSelected(staleItem, api.buildItemSignature(staleItem)), false);
  166. });