frontend-domain-model.test.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import assert from 'node:assert/strict';
  2. import { test } from 'node:test';
  3. import {
  4. buildDnsApplyFeedback,
  5. buildDomainHealth,
  6. getDnsCurrentValues,
  7. getRecordStatusMeta,
  8. getRequiredDnsRecords
  9. } from '../src/frontend/domain-model.js';
  10. test('maps DNS record states to stable UI status metadata', () => {
  11. assert.deepEqual(getRecordStatusMeta({ status: 'ok' }), {
  12. key: 'success',
  13. label: '已通过',
  14. color: 'success'
  15. });
  16. assert.deepEqual(getRecordStatusMeta({ status: 'pending' }), {
  17. key: 'pending',
  18. label: '等待生效',
  19. color: 'warning'
  20. });
  21. assert.deepEqual(getRecordStatusMeta({ status: 'warn' }), {
  22. key: 'error',
  23. label: '配置错误',
  24. color: 'error'
  25. });
  26. assert.deepEqual(getRecordStatusMeta({ status: 'missing' }), {
  27. key: 'idle',
  28. label: '未配置',
  29. color: 'default'
  30. });
  31. });
  32. test('builds domain health from required DNS records only', () => {
  33. const domain = {
  34. domain: 'example.com',
  35. senderHost: 'mail.example.com',
  36. sendingIp: '203.0.113.10',
  37. status: {
  38. checkedAt: '2026-07-08T10:30:00.000Z',
  39. records: [
  40. record('verification', 'ok'),
  41. record('dkim', 'ok'),
  42. record('spf', 'pending'),
  43. record('dmarc', 'warn'),
  44. record('sender-a', 'missing'),
  45. record('ptr', 'ok'),
  46. record('optional-mta-sts', 'ok')
  47. ]
  48. }
  49. };
  50. assert.deepEqual(getRequiredDnsRecords(domain).map((item) => item.key), [
  51. 'verification',
  52. 'dkim',
  53. 'spf',
  54. 'dmarc',
  55. 'sender-a',
  56. 'ptr'
  57. ]);
  58. assert.deepEqual(buildDomainHealth(domain), {
  59. status: 'error',
  60. label: '需要处理',
  61. passed: 3,
  62. total: 6,
  63. percent: 50,
  64. dnsIssues: 2,
  65. checkedAt: '2026-07-08T10:30:00.000Z'
  66. });
  67. });
  68. test('builds warning feedback for partial DNS apply failures', () => {
  69. const feedback = buildDnsApplyFeedback({
  70. ok: false,
  71. results: [
  72. { key: 'verification', type: 'TXT', host: '_mailhub.notify.example.com', ok: true },
  73. {
  74. key: 'dkim',
  75. type: 'TXT',
  76. host: 'mh._domainkey.notify.example.com',
  77. ok: false,
  78. error: 'domain not found'
  79. }
  80. ]
  81. }, {
  82. completed: 'DNS 写入请求已完成',
  83. partial: 'DNS 写入部分失败'
  84. });
  85. assert.deepEqual(feedback, {
  86. type: 'warning',
  87. message: 'DNS 写入部分失败:domain not found'
  88. });
  89. });
  90. test('normalizes DNS current values with legacy successful record fallback only', () => {
  91. assert.deepEqual(getDnsCurrentValues({ current: ['one', 'two'], value: 'target', status: 'ok' }), ['one', 'two']);
  92. assert.deepEqual(getDnsCurrentValues({ current: 'one', value: 'target', status: 'ok' }), ['one']);
  93. assert.deepEqual(getDnsCurrentValues({ value: 'target', status: 'ok' }), ['target']);
  94. assert.deepEqual(getDnsCurrentValues({ value: 'target', status: 'missing' }), []);
  95. });
  96. function record(key, status) {
  97. return {
  98. key,
  99. label: key,
  100. type: 'TXT',
  101. host: `${key}.example.com`,
  102. value: 'value',
  103. status
  104. };
  105. }