sidepanel-icloud-provider.test.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. const test = require('node:test');
  2. const assert = require('node:assert/strict');
  3. const fs = require('node:fs');
  4. const source = fs.readFileSync('sidepanel/sidepanel.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('getMailProviderLoginUrl reuses preferred icloud host when preference is auto', () => {
  49. const bundle = [
  50. extractFunction('getSelectedIcloudHostPreference'),
  51. extractFunction('getMailProviderLoginUrl'),
  52. ].join('\n');
  53. const api = new Function(`
  54. const ICLOUD_PROVIDER = 'icloud';
  55. const selectMailProvider = { value: ICLOUD_PROVIDER };
  56. const selectIcloudHostPreference = { value: 'auto' };
  57. const latestState = { icloudHostPreference: 'auto', preferredIcloudHost: 'icloud.com.cn' };
  58. function normalizeIcloudHost(value = '') {
  59. const normalized = String(value || '').trim().toLowerCase();
  60. return normalized === 'icloud.com' || normalized === 'icloud.com.cn' ? normalized : '';
  61. }
  62. function getIcloudLoginUrlForHost(host) {
  63. return host === 'icloud.com.cn' ? 'https://www.icloud.com.cn/' : 'https://www.icloud.com/';
  64. }
  65. function getMailProviderLoginConfig() {
  66. return { label: 'iCloud 邮箱' };
  67. }
  68. ${bundle}
  69. return { getSelectedIcloudHostPreference, getMailProviderLoginUrl };
  70. `)();
  71. assert.equal(api.getSelectedIcloudHostPreference(), 'icloud.com.cn');
  72. assert.equal(api.getMailProviderLoginUrl(), 'https://www.icloud.com.cn/');
  73. });