background-icloud-mail-provider.test.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. const test = require('node:test');
  2. const assert = require('node:assert/strict');
  3. const fs = require('node:fs');
  4. const source = fs.readFileSync('background.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('normalizeMailProvider keeps icloud provider', () => {
  49. const bundle = extractFunction('normalizeMailProvider');
  50. const api = new Function(`
  51. const ICLOUD_PROVIDER = 'icloud';
  52. const GMAIL_PROVIDER = 'gmail';
  53. const HOTMAIL_PROVIDER = 'hotmail-api';
  54. const LUCKMAIL_PROVIDER = 'luckmail-api';
  55. const CLOUDFLARE_TEMP_EMAIL_PROVIDER = 'cloudflare-temp-email';
  56. const PERSISTED_SETTING_DEFAULTS = { mailProvider: '163' };
  57. ${bundle}
  58. return { normalizeMailProvider };
  59. `)();
  60. assert.equal(api.normalizeMailProvider('icloud'), 'icloud');
  61. assert.equal(api.normalizeMailProvider('ICLOUD'), 'icloud');
  62. });
  63. test('getMailConfig returns icloud mail tab config with host preference', () => {
  64. const bundle = extractFunction('getMailConfig');
  65. const api = new Function(`
  66. const ICLOUD_PROVIDER = 'icloud';
  67. const GMAIL_PROVIDER = 'gmail';
  68. const HOTMAIL_PROVIDER = 'hotmail-api';
  69. const LUCKMAIL_PROVIDER = 'luckmail-api';
  70. const CLOUDFLARE_TEMP_EMAIL_PROVIDER = 'cloudflare-temp-email';
  71. function normalizeIcloudHost(value = '') {
  72. const normalized = String(value || '').trim().toLowerCase();
  73. return normalized === 'icloud.com' || normalized === 'icloud.com.cn' ? normalized : '';
  74. }
  75. function normalizeInbucketOrigin(value) { return String(value || '').trim(); }
  76. function getConfiguredIcloudHostPreference(state) {
  77. const normalized = String(state?.icloudHostPreference || '').trim().toLowerCase();
  78. return normalized === 'icloud.com' || normalized === 'icloud.com.cn' ? normalized : '';
  79. }
  80. function getIcloudLoginUrlForHost(host) {
  81. return host === 'icloud.com.cn' ? 'https://www.icloud.com.cn/' : 'https://www.icloud.com/';
  82. }
  83. function getIcloudMailUrlForHost(host) {
  84. return host === 'icloud.com.cn' ? 'https://www.icloud.com.cn/mail/' : 'https://www.icloud.com/mail/';
  85. }
  86. ${bundle}
  87. return { getMailConfig };
  88. `)();
  89. assert.deepEqual(api.getMailConfig({
  90. mailProvider: 'icloud',
  91. icloudHostPreference: 'icloud.com.cn',
  92. }), {
  93. source: 'icloud-mail',
  94. url: 'https://www.icloud.com.cn/mail/',
  95. label: 'iCloud 邮箱',
  96. navigateOnReuse: true,
  97. });
  98. });
  99. test('getMailConfig reuses preferred icloud host when preference is auto', () => {
  100. const bundle = extractFunction('getMailConfig');
  101. const api = new Function(`
  102. const ICLOUD_PROVIDER = 'icloud';
  103. const GMAIL_PROVIDER = 'gmail';
  104. const HOTMAIL_PROVIDER = 'hotmail-api';
  105. const LUCKMAIL_PROVIDER = 'luckmail-api';
  106. const CLOUDFLARE_TEMP_EMAIL_PROVIDER = 'cloudflare-temp-email';
  107. function normalizeIcloudHost(value = '') {
  108. const normalized = String(value || '').trim().toLowerCase();
  109. return normalized === 'icloud.com' || normalized === 'icloud.com.cn' ? normalized : '';
  110. }
  111. function normalizeInbucketOrigin(value) { return String(value || '').trim(); }
  112. function getConfiguredIcloudHostPreference() {
  113. return '';
  114. }
  115. function getIcloudLoginUrlForHost(host) {
  116. return host === 'icloud.com.cn' ? 'https://www.icloud.com.cn/' : 'https://www.icloud.com/';
  117. }
  118. function getIcloudMailUrlForHost(host) {
  119. return host === 'icloud.com.cn' ? 'https://www.icloud.com.cn/mail/' : 'https://www.icloud.com/mail/';
  120. }
  121. ${bundle}
  122. return { getMailConfig };
  123. `)();
  124. assert.deepEqual(api.getMailConfig({
  125. mailProvider: 'icloud',
  126. icloudHostPreference: 'auto',
  127. preferredIcloudHost: 'icloud.com.cn',
  128. }), {
  129. source: 'icloud-mail',
  130. url: 'https://www.icloud.com.cn/mail/',
  131. label: 'iCloud 邮箱',
  132. navigateOnReuse: true,
  133. });
  134. });
  135. test('getMailConfig keeps existing A4Sky mailbox tab instead of reopening it', () => {
  136. const bundle = extractFunction('getMailConfig');
  137. const api = new Function(`
  138. const ICLOUD_PROVIDER = 'icloud';
  139. const GMAIL_PROVIDER = 'gmail';
  140. const A4SKY_PROVIDER = 'a4sky';
  141. const HOTMAIL_PROVIDER = 'hotmail-api';
  142. const LUCKMAIL_PROVIDER = 'luckmail-api';
  143. const CLOUDFLARE_TEMP_EMAIL_PROVIDER = 'cloudflare-temp-email';
  144. function normalizeIcloudHost(value = '') {
  145. const normalized = String(value || '').trim().toLowerCase();
  146. return normalized === 'icloud.com' || normalized === 'icloud.com.cn' ? normalized : '';
  147. }
  148. function normalizeInbucketOrigin(value) { return String(value || '').trim(); }
  149. function getConfiguredIcloudHostPreference() {
  150. return '';
  151. }
  152. function getIcloudLoginUrlForHost(host) {
  153. return host === 'icloud.com.cn' ? 'https://www.icloud.com.cn/' : 'https://www.icloud.com/';
  154. }
  155. function getIcloudMailUrlForHost(host) {
  156. return host === 'icloud.com.cn' ? 'https://www.icloud.com.cn/mail/' : 'https://www.icloud.com/mail/';
  157. }
  158. ${bundle}
  159. return { getMailConfig };
  160. `)();
  161. assert.deepEqual(api.getMailConfig({
  162. mailProvider: 'a4sky',
  163. }), {
  164. provider: 'a4sky',
  165. source: 'mail-phplife',
  166. url: 'https://mail.phplife.net/?_task=mail&_mbox=INBOX',
  167. label: 'A4Sky 邮箱(IMAP 助手)',
  168. navigateOnReuse: false,
  169. inject: ['content/activation-utils.js', 'content/utils.js', 'content/phplife-mail.js'],
  170. injectSource: 'mail-phplife',
  171. });
  172. });