Full Toolkit for Next-Level Domain Analysis
import { log } from 'evlog';
import {
afterEach,
beforeEach,
describe,
expect,
it,
type MockInstance,
vi,
} from 'vitest';
import {
deduplicate,
getBaseDomain,
IPV4_REGEX,
IPV6_REGEX,
isAppleDevice,
isValidDomain,
isWildcardDomain,
retry,
} from './utils';
describe('deduplicate', () => {
it('removes duplicate numbers from an array', () => {
expect(deduplicate([1, 2, 2, 3])).toEqual([1, 2, 3]);
});
it('removes duplicate strings from an array', () => {
expect(deduplicate(['a', 'b', 'b', 'c'])).toEqual(['a', 'b', 'c']);
});
it('handles arrays of objects based on strict equality', () => {
const obj1 = { id: 1 };
const obj2 = { id: 2 };
// These are two different references, so both should remain
expect(deduplicate([obj1, obj2, obj1])).toEqual([obj1, obj2]);
});
it('returns an empty array when given an empty array', () => {
expect(deduplicate([])).toEqual([]);
});
});
describe('retry', () => {
let warnSpy: MockInstance;
beforeEach(() => {
warnSpy = vi.spyOn(log, 'warn').mockImplementation(() => {});
});
afterEach(() => {
warnSpy.mockRestore();
});
it('should succeed without retrying if the function does not throw', async () => {
const mockFn = vi.fn().mockResolvedValue('success');
const result = await retry(mockFn, 2);
expect(result).toBe('success');
expect(mockFn).toHaveBeenCalledTimes(1);
expect(warnSpy).not.toHaveBeenCalled();
});
it('should retry the specified number of times on failure, then succeed', async () => {
const error = new Error('Test error');
const mockFn = vi
.fn()
.mockRejectedValueOnce(error)
.mockRejectedValueOnce(error)
.mockResolvedValue('success');
const result = await retry(mockFn, 2);
expect(result).toBe('success');
expect(mockFn).toHaveBeenCalledTimes(3);
expect(warnSpy).toHaveBeenCalledTimes(2);
expect(warnSpy).toHaveBeenCalledWith({
message: 'retrying_after_error',
error: error.message,
});
});
it('should throw the last error after exhausting all retries', async () => {
const error = new Error('Test error');
const mockFn = vi.fn().mockRejectedValue(error);
try {
await retry(mockFn, 2);
throw new Error(
'This should not execute if retry throws the expected error',
);
} catch (err) {
expect(err).toBe(error);
expect(mockFn).toHaveBeenCalledTimes(3);
expect(warnSpy).toHaveBeenCalledTimes(2);
}
});
});
describe('isValidDomain', () => {
it('filters non-strings', () => {
// @ts-expect-error Testing invalid input
expect(isValidDomain(null)).toBe(false);
// @ts-expect-error Testing invalid input
expect(isValidDomain(undefined)).toBe(false);
// @ts-expect-error Testing invalid input
expect(isValidDomain(123)).toBe(false);
// @ts-expect-error Testing invalid input
expect(isValidDomain({})).toBe(false);
});
it('validates typical domains', () => {
expect(isValidDomain('example.com')).toBe(true);
expect(isValidDomain('subdomain.example.com')).toBe(true);
expect(isValidDomain('www.example.co.uk')).toBe(true);
expect(isValidDomain('xn--fiqs8s')).toBe(true); // Punycode example for Chinese "中国"
expect(isValidDomain('com')).toBe(true); // Only TLD
});
it('validates odd domains', () => {
expect(
isValidDomain(
'theofficialabsolutelongestdomainnameregisteredontheworldwideweb.international',
),
).toBe(true);
expect(isValidDomain('_dmarc.example.com')).toBe(true);
});
it('validates wildcard domains', () => {
expect(isValidDomain('*.example.com')).toBe(true);
});
it('rejects invalid domains', () => {
expect(isValidDomain('')).toBe(false);
expect(isValidDomain('-example.com')).toBe(false); // Starts with a hyphen
expect(isValidDomain('.com')).toBe(false); // No domain name
expect(isValidDomain('test.bad')).toBe(false); // Invalid TLD
expect(isValidDomain('example')).toBe(false); // Only Invalid TLD
expect(isValidDomain('exa_mple.com')).toBe(false); // Underscore in domain
expect(isValidDomain('example..com')).toBe(false); // Double dot
});
});
describe('getBaseDomain', () => {
it('extracts the base domain from a given domain', () => {
expect(getBaseDomain('example.com')).toBe('example.com');
expect(getBaseDomain('subdomain.example.com')).toBe('example.com');
expect(getBaseDomain('www.example.co.uk')).toBe('example.co.uk');
expect(getBaseDomain('*.example.com')).toBe('example.com');
});
it('removes wildcard prefixes', () => {
expect(getBaseDomain('*.example.com')).toBe('example.com');
});
it('removes trailing dots', () => {
expect(getBaseDomain('example.com.')).toBe('example.com');
expect(getBaseDomain('subdomain.example.com.')).toBe('example.com');
});
it('returns the input when no base domain is found', () => {
expect(getBaseDomain('')).toBe('');
expect(getBaseDomain('xn--fiqs8s')).toBe('xn--fiqs8s');
expect(getBaseDomain('com')).toBe('com');
});
it('always returns lowercase, including when no base domain is found', () => {
expect(getBaseDomain('WWW.Example.COM')).toBe('example.com');
expect(getBaseDomain('*.Example.COM.')).toBe('example.com');
expect(getBaseDomain('COM')).toBe('com');
expect(getBaseDomain('ARPA')).toBe('arpa');
});
});
describe('isWildcardDomain', () => {
it('returns true for wildcard domains', () => {
expect(isWildcardDomain('*.example.com')).toBe(true);
expect(isWildcardDomain('*.sub.domain.com')).toBe(true);
});
it('returns false for non-wildcard domains', () => {
expect(isWildcardDomain('example.com')).toBe(false);
expect(isWildcardDomain('sub.domain.com')).toBe(false);
expect(isWildcardDomain('www.*.com')).toBe(false); // Invalid wildcard usage
});
it('handles edge cases', () => {
expect(isWildcardDomain('')).toBe(false);
expect(isWildcardDomain('*')).toBe(false);
expect(isWildcardDomain('*.')).toBe(false);
expect(isWildcardDomain('*.com')).toBe(true);
});
});
describe('IPV4_REGEX', () => {
it('matches IPv4 addresses in SPF records', () => {
const spf = 'v=spf1 ip4:166.84.6.31 ip4:166.84.7.238 ~all';
const matches = spf.match(IPV4_REGEX);
expect(matches).toEqual(['166.84.6.31', '166.84.7.238']);
});
it('does not match partial IPv4 from ip4: prefix in SPF records', () => {
const spf = 'v=spf1 ip4:166.84.6.31 ip4:166.84.7.238 ~all';
const matches = spf.match(IPV4_REGEX);
expect(matches).toEqual(['166.84.6.31', '166.84.7.238']);
expect(matches).not.toContain('4.166.84.6');
});
it('matches standard IPv4 addresses', () => {
expect('192.168.1.1'.match(IPV4_REGEX)).toEqual(['192.168.1.1']);
expect('255.255.255.255'.match(IPV4_REGEX)).toEqual(['255.255.255.255']);
expect('0.0.0.0'.match(IPV4_REGEX)).toEqual(['0.0.0.0']);
});
});
describe('IPV6_REGEX', () => {
it('does not match partial IPv6 from ip6: prefix in SPF records', () => {
const spf =
'v=spf1 ip4:166.84.6.31 ip6:2602:f977:800::e276:63ff:fe66:3400 ~all';
const matches = spf.match(IPV6_REGEX);
expect(matches).toEqual(['2602:f977:800::e276:63ff:fe66:3400']);
});
it('matches multiple IPv6 addresses in SPF records', () => {
const spf =
'v=spf1 ip6:2602:f977:800:f7f6::/64 ip6:2602:f977:800::e276:63ff:fe66:3400 ~all';
const matches = spf.match(IPV6_REGEX);
expect(matches).toEqual([
'2602:f977:800:f7f6::',
'2602:f977:800::e276:63ff:fe66:3400',
]);
});
it('matches standard IPv6 addresses', () => {
expect('2001:db8::1'.match(IPV6_REGEX)).toEqual(['2001:db8::1']);
expect('::1'.match(IPV6_REGEX)).toEqual(['::1']);
expect('fe80::1%eth0'.match(IPV6_REGEX)).toEqual(['fe80::1%eth0']);
});
it('matches full IPv6 addresses', () => {
expect('2001:0db8:85a3:0000:0000:8a2e:0370:7334'.match(IPV6_REGEX)).toEqual(
['2001:0db8:85a3:0000:0000:8a2e:0370:7334'],
);
});
});
// User agent strings from https://gist.github.com/pzb/b4b6f57144aea7827ae4
describe('isAppleDevice', () => {
it('detects Apple devices', () => {
vi.spyOn(window.navigator, 'userAgent', 'get').mockReturnValue(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.22 Safari/537.36',
);
expect(isAppleDevice()).toBe(true);
vi.spyOn(window.navigator, 'userAgent', 'get').mockReturnValue(
'Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12A365 Safari/600.1.4',
);
expect(isAppleDevice()).toBe(true);
vi.spyOn(window.navigator, 'userAgent', 'get').mockReturnValue(
'Mozilla/5.0 (iPad;U;CPU OS 5_1_1 like Mac OS X; zh-cn)AppleWebKit/534.46.0(KHTML, like Gecko)CriOS/19.0.1084.60 Mobile/9B206 Safari/7534.48.3',
);
});
it('detects non-Apple devices', () => {
vi.spyOn(window.navigator, 'userAgent', 'get').mockReturnValue(
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/37.0.2062.94 Chrome/37.0.2062.94 Safari/537.36',
);
expect(isAppleDevice()).toBe(false);
vi.spyOn(window.navigator, 'userAgent', 'get').mockReturnValue(
'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko',
);
expect(isAppleDevice()).toBe(false);
vi.spyOn(window.navigator, 'userAgent', 'get').mockReturnValue(
'Mozilla/5.0 (Android 11; Mobile; rv:68.0) Gecko/68.0 Firefox/68.0',
);
expect(isAppleDevice()).toBe(false);
});
it('returns false when window is undefined', () => {
expect(isAppleDevice()).toBe(false);
});
});