dns-auto-checker.test.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import assert from 'node:assert/strict';
  2. import { test } from 'node:test';
  3. import { runDnsAutoCheck, shouldAutoCheckDomain } from '../src/dns-auto-checker.js';
  4. test('dns auto-check selects unchecked, stale, and unverified domains only', () => {
  5. const now = new Date('2026-07-09T03:00:00.000Z');
  6. assert.equal(shouldAutoCheckDomain({ status: {} }, { now, minIntervalMs: 60000 }), true);
  7. assert.equal(shouldAutoCheckDomain({
  8. status: { verified: false, checkedAt: '2026-07-09T02:58:30.000Z' }
  9. }, { now, minIntervalMs: 60000 }), true);
  10. assert.equal(shouldAutoCheckDomain({
  11. status: { verified: false, checkedAt: '2026-07-09T02:59:30.000Z' }
  12. }, { now, minIntervalMs: 60000 }), false);
  13. assert.equal(shouldAutoCheckDomain({
  14. status: { verified: true, checkedAt: '2026-07-01T00:00:00.000Z' }
  15. }, { now, minIntervalMs: 60000 }), false);
  16. });
  17. test('dns auto-check refreshes eligible domains and continues after failures', async () => {
  18. const saved = [];
  19. const warnings = [];
  20. const domains = [
  21. { id: 1, userId: 10, domain: 'ready.example', status: {} },
  22. {
  23. id: 2,
  24. userId: 10,
  25. domain: 'fresh.example',
  26. status: { verified: false, checkedAt: '2026-07-09T02:59:30.000Z' }
  27. },
  28. { id: 3, userId: 11, domain: 'broken.example', status: {} }
  29. ];
  30. const result = await runDnsAutoCheck({
  31. listDomains: () => domains,
  32. buildGuide: async (domain) => {
  33. if (domain.domain === 'broken.example') throw new Error('DNS timeout');
  34. return { checkedAt: '2026-07-09T03:00:00.000Z', verified: true, records: [] };
  35. },
  36. saveStatus: (id, userId, status) => saved.push({ id, userId, status }),
  37. logger: { warn: (message) => warnings.push(message) },
  38. now: () => new Date('2026-07-09T03:00:00.000Z'),
  39. minIntervalMs: 60000,
  40. limit: 10
  41. });
  42. assert.deepEqual(saved.map((item) => item.id), [1]);
  43. assert.equal(saved[0].userId, 10);
  44. assert.equal(saved[0].status.verified, true);
  45. assert.equal(result.checked, 1);
  46. assert.equal(result.failed, 1);
  47. assert.equal(result.skipped, 1);
  48. assert.equal(warnings.length, 1);
  49. assert.match(warnings[0], /broken\.example/);
  50. });