frontend-domain-model.test.js 2.4 KB

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