Skip to content

Full Toolkit for Next-Level Domain Analysis

Add more tests 🧫

Felix Wotschofsky47cc5535273a
diff --git a/lib/resolvers/BaseDoHResolver.test.ts b/lib/resolvers/BaseDoHResolver.test.ts
new file mode 100644
index 0000000..ef07d7f
--- /dev/null
+++ b/lib/resolvers/BaseDoHResolver.test.ts
@@ -0,0 +1,74 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+import BaseDoHResolver, { type DoHResponse } from './BaseDoHResolver';
+
+describe('BaseDoHResolver', () => {
+ const mockSendRequest = vi.fn();
+
+ const mockDoHResponse = (
+ answers: Partial<DoHResponse['Answer'][number]>[] = []
+ ): DoHResponse => ({
+ Status: 0,
+ TC: false,
+ RD: true,
+ RA: true,
+ AD: false,
+ CD: false,
+ Question: [{ name: 'example.com', type: 1 }],
+ Answer: answers.map((ans) => ({
+ name: ans.name || 'example.com',
+ type: ans.type || 1,
+ TTL: ans.TTL || 300,
+ data: ans.data || 'data',
+ })),
+ });
+
+ beforeEach(() => {
+ mockSendRequest.mockReset();
+ });
+
+ it('should correctly resolve records from DoH response', async () => {
+ mockSendRequest.mockResolvedValue({
+ ok: true,
+ json: () => Promise.resolve(mockDoHResponse([{ type: 1 }])),
+ });
+
+ const resolver = new BaseDoHResolver(mockSendRequest);
+ const records = await resolver.resolveRecordType('example.com', 'A');
+
+ expect(records).toEqual([
+ {
+ name: 'example.com',
+ type: 'A',
+ TTL: 300,
+ data: 'data',
+ },
+ ]);
+ expect(mockSendRequest).toHaveBeenCalledWith('example.com', 'A');
+ });
+
+ it('should handle empty answers by returning an empty array', async () => {
+ mockSendRequest.mockResolvedValue({
+ ok: true,
+ json: () => Promise.resolve(mockDoHResponse([])),
+ });
+
+ const resolver = new BaseDoHResolver(mockSendRequest);
+ const records = await resolver.resolveRecordType('example.com', 'A');
+
+ expect(records).toEqual([]);
+ });
+
+ it('should throw an error for bad HTTP responses', async () => {
+ mockSendRequest.mockResolvedValue({
+ ok: false,
+ statusText: 'Internal Server Error',
+ });
+
+ const resolver = new BaseDoHResolver(mockSendRequest);
+
+ await expect(
+ resolver.resolveRecordType('example.com', 'A')
+ ).rejects.toThrow('Bad response from Google: Internal Server Error');
+ });
+});
diff --git a/lib/resolvers/DnsResolver.test.ts b/lib/resolvers/DnsResolver.test.ts
new file mode 100644
index 0000000..cd7a5f8
--- /dev/null
+++ b/lib/resolvers/DnsResolver.test.ts
@@ -0,0 +1,61 @@
+import { describe, expect, it } from 'vitest';
+
+import DnsResolver, {
+ type RawRecord,
+ RECORD_TYPES,
+ type RecordType,
+} from './DnsResolver';
+
+class MockResolver extends DnsResolver {
+ public async resolveRecordType(
+ domain: string,
+ type: RecordType
+ ): Promise<RawRecord[]> {
+ return [
+ {
+ name: domain,
+ type,
+ TTL: 3600,
+ data: `data-for-${type}`,
+ },
+ ];
+ }
+}
+
+describe('DnsResolver', () => {
+ describe('resolveRecordType', () => {
+ it('should resolve a specific record type for a domain', async () => {
+ const resolver = new MockResolver();
+
+ const records = await resolver.resolveRecordType('example.com', 'A');
+
+ expect(records).toEqual([
+ {
+ name: 'example.com',
+ type: 'A',
+ TTL: 3600,
+ data: 'data-for-A',
+ },
+ ]);
+ });
+ });
+
+ describe('resolveAllRecords', () => {
+ it('should resolve all supported record types for a domain', async () => {
+ const resolver = new MockResolver();
+
+ const resolvedRecords = await resolver.resolveAllRecords('example.com');
+
+ // Ensure all record types are present and have the expected data
+ RECORD_TYPES.forEach((type) => {
+ expect(resolvedRecords[type]).toBeDefined();
+ expect(resolvedRecords[type][0].type).toBe(type);
+ });
+
+ // Ensure all and only the expected keys are present
+ const expectedKeys = RECORD_TYPES;
+ const resolvedKeys = Object.keys(resolvedRecords);
+ expect(resolvedKeys.toSorted()).toEqual(expectedKeys.toSorted());
+ });
+ });
+});
diff --git a/lib/utils.test.ts b/lib/utils.test.ts
index fdeb699..ea24341 100644
--- a/lib/utils.test.ts
+++ b/lib/utils.test.ts
@@ -1,6 +1,63 @@
-import { afterEach, describe, expect, it, vi } from 'vitest';
+import {
+ afterEach,
+ beforeEach,
+ describe,
+ expect,
+ it,
+ type MockInstance,
+ vi,
+} from 'vitest';
-import { isAppleDevice, isValidDomain } from './utils';
+import { isAppleDevice, isValidDomain, retry } from './utils';
+
+describe('retry', () => {
+ let warnSpy: MockInstance;
+
+ beforeEach(() => {
+ warnSpy = vi.spyOn(console, '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(error.message.toString());
+ });
+
+ 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('validates typical domains', () => {