frontend-domain-model.test.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import assert from 'node:assert/strict';
  2. import { test } from 'node:test';
  3. import {
  4. buildDomainHealth,
  5. getRecordStatusMeta,
  6. getRequiredDnsRecords
  7. } from '../src/frontend/domain-model.js';
  8. test('maps DNS record states to stable UI status metadata', () => {
  9. assert.deepEqual(getRecordStatusMeta({ status: 'ok' }), {
  10. key: 'success',
  11. label: '已通过',
  12. color: 'success'
  13. });
  14. assert.deepEqual(getRecordStatusMeta({ status: 'pending' }), {
  15. key: 'pending',
  16. label: '等待生效',
  17. color: 'warning'
  18. });
  19. assert.deepEqual(getRecordStatusMeta({ status: 'warn' }), {
  20. key: 'error',
  21. label: '配置错误',
  22. color: 'error'
  23. });
  24. assert.deepEqual(getRecordStatusMeta({ status: 'missing' }), {
  25. key: 'idle',
  26. label: '未配置',
  27. color: 'default'
  28. });
  29. });
  30. test('builds domain health from required DNS records only', () => {
  31. const domain = {
  32. domain: 'example.com',
  33. senderHost: 'mail.example.com',
  34. sendingIp: '203.0.113.10',
  35. status: {
  36. checkedAt: '2026-07-08T10:30:00.000Z',
  37. records: [
  38. record('verification', 'ok'),
  39. record('dkim', 'ok'),
  40. record('spf', 'pending'),
  41. record('dmarc', 'warn'),
  42. record('sender-a', 'missing'),
  43. record('ptr', 'ok'),
  44. record('optional-mta-sts', 'ok')
  45. ]
  46. }
  47. };
  48. assert.deepEqual(getRequiredDnsRecords(domain).map((item) => item.key), [
  49. 'verification',
  50. 'dkim',
  51. 'spf',
  52. 'dmarc',
  53. 'sender-a',
  54. 'ptr'
  55. ]);
  56. assert.deepEqual(buildDomainHealth(domain), {
  57. status: 'error',
  58. label: '需要处理',
  59. passed: 3,
  60. total: 6,
  61. percent: 50,
  62. dnsIssues: 2,
  63. checkedAt: '2026-07-08T10:30:00.000Z'
  64. });
  65. });
  66. function record(key, status) {
  67. return {
  68. key,
  69. label: key,
  70. type: 'TXT',
  71. host: `${key}.example.com`,
  72. value: 'value',
  73. status
  74. };
  75. }