import { describe, expect, test } from 'bun:test'; import { providerFromNameservers, normaliseDomain, looksLikeDomain } from './dns'; /** * The pure half of the DNS check. * * `checkDomain` itself talks to a resolver and to a public-IP service, so a test of it would be a test of * this machine's network. What can actually be WRONG without anybody noticing is in here: which provider a * set of nameservers means, and what counts as a domain. */ describe('providerFromNameservers', () => { test('the common ones', () => { expect(providerFromNameservers(['ana.ns.cloudflare.com', 'bob.ns.cloudflare.com'])).toBe('cloudflare'); expect(providerFromNameservers(['ns01.domaincontrol.com'])).toBe('godaddy'); expect(providerFromNameservers(['ns-1234.awsdns.org'])).toBe('route53'); expect(providerFromNameservers(['ns1.digitalocean.com'])).toBe('digitalocean'); }); test('a trailing dot and capitals, which is how resolvers actually answer', () => { expect(providerFromNameservers(['ANA.NS.CLOUDFLARE.COM.'])).toBe('cloudflare'); }); // `ns-cloud-a1.googledomains.com` ends with `google.com`? No — but `ns1.google.com` does, and a shorter // entry must not win over a longer one that also matches. Longest suffix, always. test('the longest matching suffix wins', () => { expect(providerFromNameservers(['ns-cloud-a1.googledomains.com'])).toBe('google'); }); test('an unrecognised nameserver is null rather than a guess', () => { expect(providerFromNameservers(['ns1.some-small-host.example'])).toBeNull(); expect(providerFromNameservers([])).toBeNull(); }); }); describe('normaliseDomain', () => { test('strips what people actually paste', () => { expect(normaliseDomain(' https://Officer.dev/some/path ')).toBe('officer.dev'); expect(normaliseDomain('officer.dev.')).toBe('officer.dev'); expect(normaliseDomain('HTTP://OFFICER.DEV')).toBe('officer.dev'); }); }); describe('looksLikeDomain', () => { test('accepts real ones, including a subdomain and a long TLD', () => { expect(looksLikeDomain('officer.dev')).toBe(true); expect(looksLikeDomain('sub.officer.dev')).toBe(true); expect(looksLikeDomain('a-b.example.museum')).toBe(true); }); test('rejects the obvious nonsense, and leaves the rest to DNS', () => { expect(looksLikeDomain('officer')).toBe(false); expect(looksLikeDomain('-officer.dev')).toBe(false); expect(looksLikeDomain('officer .dev')).toBe(false); expect(looksLikeDomain('')).toBe(false); }); });