frontend-domain-model.test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. getVisibleDnsRecords
  10. } from '../src/frontend/domain-model.js';
  11. test('maps DNS record states to stable UI status metadata', () => {
  12. assert.deepEqual(getRecordStatusMeta({ status: 'ok' }), {
  13. key: 'success',
  14. label: '已通过',
  15. color: 'success'
  16. });
  17. assert.deepEqual(getRecordStatusMeta({ status: 'pending' }), {
  18. key: 'pending',
  19. label: '等待生效',
  20. color: 'warning'
  21. });
  22. assert.deepEqual(getRecordStatusMeta({ status: 'warn' }), {
  23. key: 'error',
  24. label: '配置错误',
  25. color: 'error'
  26. });
  27. assert.deepEqual(getRecordStatusMeta({ status: 'missing' }), {
  28. key: 'idle',
  29. label: '未配置',
  30. color: 'default'
  31. });
  32. });
  33. test('builds domain health from required DNS records only', () => {
  34. const domain = {
  35. domain: 'example.com',
  36. senderHost: 'mail.example.com',
  37. sendingIp: '203.0.113.10',
  38. status: {
  39. checkedAt: '2026-07-08T10:30:00.000Z',
  40. records: [
  41. record('verification', 'ok'),
  42. record('dkim', 'ok'),
  43. record('spf', 'pending'),
  44. record('dmarc', 'warn'),
  45. record('sender-a', 'missing'),
  46. record('ptr', 'missing'),
  47. record('optional-mta-sts', 'ok')
  48. ]
  49. }
  50. };
  51. assert.deepEqual(getRequiredDnsRecords(domain).map((item) => item.key), [
  52. 'verification',
  53. 'dkim',
  54. 'spf',
  55. 'dmarc',
  56. 'sender-a'
  57. ]);
  58. assert.deepEqual(buildDomainHealth(domain), {
  59. status: 'error',
  60. label: '需要处理',
  61. passed: 2,
  62. total: 5,
  63. percent: 40,
  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. test('hides legacy PTR records from per-domain DNS record lists', () => {
  97. assert.deepEqual(getVisibleDnsRecords([
  98. record('ptr', 'ok'),
  99. record('spf', 'ok'),
  100. record('verification', 'ok'),
  101. record('unknown', 'ok')
  102. ]).map((item) => item.key), ['verification', 'spf']);
  103. });
  104. function record(key, status) {
  105. return {
  106. key,
  107. label: key,
  108. type: 'TXT',
  109. host: `${key}.example.com`,
  110. value: 'value',
  111. status
  112. };
  113. }