fix(tests): update tests and test helpers to current email/DNS APIs, use non-privileged ports, and improve robustness and resilience

This commit is contained in:
2026-02-01 18:10:30 +00:00
parent 2206abd04b
commit b90650c660
23 changed files with 2006 additions and 1756 deletions

View File

@@ -4,160 +4,138 @@ import * as plugins from '../ts/plugins.js';
let dcRouter: DcRouter;
tap.test('should NOT instantiate DNS server when dnsDomain is not set', async () => {
tap.test('should NOT instantiate DNS server when dnsNsDomains is not set', async () => {
dcRouter = new DcRouter({
smartProxyConfig: {
routes: []
}
});
await dcRouter.start();
// Check that DNS server is not created
expect((dcRouter as any).dnsServer).toBeUndefined();
await dcRouter.stop();
});
tap.test('should instantiate DNS server when dnsDomain is set', async () => {
// Use a non-standard port to avoid conflicts
const testPort = 8443;
tap.test('should generate DNS routes when dnsNsDomains is set', async () => {
// This test checks the route generation logic WITHOUT starting the full DcRouter
// Starting DcRouter would require DNS port 53 and cause conflicts
dcRouter = new DcRouter({
dnsDomain: 'dns.test.local',
dnsNsDomains: ['ns1.test.local', 'ns2.test.local'],
dnsScopes: ['test.local'],
smartProxyConfig: {
routes: [],
portMappings: {
443: testPort // Map port 443 to test port
}
} as any
routes: []
}
});
try {
await dcRouter.start();
} catch (error) {
// If start fails due to port conflict, that's OK for this test
// We're mainly testing the route generation logic
}
// Check that DNS server is created
expect((dcRouter as any).dnsServer).toBeDefined();
// Check routes were generated (even if SmartProxy failed to start)
// Check routes are generated correctly (without starting)
const generatedRoutes = (dcRouter as any).generateDnsRoutes();
expect(generatedRoutes.length).toEqual(2); // /dns-query and /resolve
// Check that routes have socket-handler action
generatedRoutes.forEach((route: any) => {
expect(route.action.type).toEqual('socket-handler');
expect(route.action.socketHandler).toBeDefined();
});
try {
await dcRouter.stop();
} catch (error) {
// Ignore stop errors
}
// Verify routes target the primary nameserver
const dnsQueryRoute = generatedRoutes.find((r: any) => r.name === 'dns-over-https-dns-query');
expect(dnsQueryRoute).toBeDefined();
expect(dnsQueryRoute.match.domains).toContain('ns1.test.local');
});
tap.test('should create DNS routes with correct configuration', async () => {
dcRouter = new DcRouter({
dnsDomain: 'dns.example.com',
dnsNsDomains: ['ns1.example.com', 'ns2.example.com'],
dnsScopes: ['example.com'],
smartProxyConfig: {
routes: []
}
});
// Access the private method to generate routes
const dnsRoutes = (dcRouter as any).generateDnsRoutes();
expect(dnsRoutes.length).toEqual(2);
// Check first route (dns-query)
// Check first route (dns-query) - uses primary nameserver (first in array)
const dnsQueryRoute = dnsRoutes.find((r: any) => r.name === 'dns-over-https-dns-query');
expect(dnsQueryRoute).toBeDefined();
expect(dnsQueryRoute.match.ports).toContain(443);
expect(dnsQueryRoute.match.domains).toContain('dns.example.com');
expect(dnsQueryRoute.match.domains).toContain('ns1.example.com');
expect(dnsQueryRoute.match.path).toEqual('/dns-query');
// Check second route (resolve)
const resolveRoute = dnsRoutes.find((r: any) => r.name === 'dns-over-https-resolve');
expect(resolveRoute).toBeDefined();
expect(resolveRoute.match.ports).toContain(443);
expect(resolveRoute.match.domains).toContain('dns.example.com');
expect(resolveRoute.match.domains).toContain('ns1.example.com');
expect(resolveRoute.match.path).toEqual('/resolve');
});
tap.test('DNS socket handler should handle sockets correctly', async () => {
tap.test('DNS socket handler should be created correctly', async () => {
// This test verifies the socket handler creation WITHOUT starting the full router
dcRouter = new DcRouter({
dnsDomain: 'dns.test.local',
dnsNsDomains: ['ns1.test.local', 'ns2.test.local'],
dnsScopes: ['test.local'],
smartProxyConfig: {
routes: [],
portMappings: { 443: 8444 } // Use different test port
} as any
routes: []
}
});
try {
await dcRouter.start();
} catch (error) {
// Ignore start errors for this test
}
// Create a mock socket
const mockSocket = new plugins.net.Socket();
let socketEnded = false;
let socketDestroyed = false;
mockSocket.end = () => {
socketEnded = true;
};
mockSocket.destroy = () => {
socketDestroyed = true;
};
// Get the socket handler
// Get the socket handler (this doesn't require DNS server to be started)
const socketHandler = (dcRouter as any).createDnsSocketHandler();
expect(socketHandler).toBeDefined();
expect(typeof socketHandler).toEqual('function');
// Test with DNS server initialized
// Create a mock socket to test the handler behavior without DNS server
const mockSocket = new plugins.net.Socket();
let socketEnded = false;
mockSocket.end = () => {
socketEnded = true;
return mockSocket;
};
// When DNS server is not initialized, the handler should end the socket
try {
await socketHandler(mockSocket);
} catch (error) {
// Expected - mock socket won't work properly
}
// Socket should be handled by DNS server (even if it errors)
expect(socketHandler).toBeDefined();
try {
await dcRouter.stop();
} catch (error) {
// Ignore stop errors
// Expected - DNS server not initialized
}
// Socket should be ended because DNS server wasn't started
expect(socketEnded).toEqual(true);
});
tap.test('DNS server should have manual HTTPS mode enabled', async () => {
tap.test('DNS routes should only be generated when dnsNsDomains is configured', async () => {
// Test without DNS configuration - should return empty routes
dcRouter = new DcRouter({
dnsDomain: 'dns.test.local'
smartProxyConfig: {
routes: []
}
});
// Don't actually start it to avoid port conflicts
// Instead, directly call the setup method
try {
await (dcRouter as any).setupDnsWithSocketHandler();
} catch (error) {
// May fail but that's OK
}
// Check that DNS server was created with correct options
const dnsServer = (dcRouter as any).dnsServer;
expect(dnsServer).toBeDefined();
// The important thing is that the DNS routes are created correctly
// and that the socket handler is set up
const socketHandler = (dcRouter as any).createDnsSocketHandler();
const routesWithoutDns = (dcRouter as any).generateDnsRoutes();
expect(routesWithoutDns.length).toEqual(0);
// Test with DNS configuration - should return routes
const dcRouterWithDns = new DcRouter({
dnsNsDomains: ['ns1.example.com'],
dnsScopes: ['example.com'],
smartProxyConfig: {
routes: []
}
});
const routesWithDns = (dcRouterWithDns as any).generateDnsRoutes();
expect(routesWithDns.length).toEqual(2);
// Verify socket handler can be created
const socketHandler = (dcRouterWithDns as any).createDnsSocketHandler();
expect(socketHandler).toBeDefined();
expect(typeof socketHandler).toEqual('function');
});