smartproxy/test/test.route-update-logger-errors.ts

99 lines
3.4 KiB
TypeScript
Raw Normal View History

2025-05-20 15:44:48 +00:00
import * as plugins from '../ts/plugins.js';
import { SmartProxy } from '../ts/index.js';
import { SmartCertManager } from '../ts/proxies/smart-proxy/certificate-manager.js';
import { tap, expect } from '@git.zone/tstest/tapbundle';
// Create test routes using high ports to avoid permission issues
const createRoute = (id: number, domain: string, port: number = 8443) => ({
name: `test-route-${id}`,
match: {
ports: [port],
domains: [domain]
},
action: {
type: 'forward' as const,
target: {
host: 'localhost',
port: 3000 + id
}
}
});
// Test function to check if error handling is applied to logger calls
tap.test('should have error handling around logger calls in route update callbacks', async () => {
// Create a simple cert manager instance for testing
const certManager = new SmartCertManager(
[createRoute(1, 'test.example.com', 8443)],
'./certs',
{ email: 'test@example.com', useProduction: false }
);
// Create a mock update routes callback that tracks if it was called
let callbackCalled = false;
const mockCallback = async (routes: any[]) => {
callbackCalled = true;
// Just return without doing anything
return Promise.resolve();
};
// Set the callback
certManager.setUpdateRoutesCallback(mockCallback);
// Verify the callback was successfully set
expect(callbackCalled).toEqual(false);
// Create a test route
const testRoute = createRoute(2, 'test2.example.com', 8444);
// Verify we can add a challenge route without error
// This tests the try/catch we added around addChallengeRoute logger calls
try {
// Accessing private method for testing
// @ts-ignore
await (certManager as any).addChallengeRoute();
// If we got here without error, the error handling works
expect(true).toEqual(true);
} catch (error) {
// This shouldn't happen if our error handling is working
// Error handling failed in addChallengeRoute
expect(false).toEqual(true);
2025-05-20 15:44:48 +00:00
}
// Verify that we handle errors in removeChallengeRoute
try {
// Set the flag to active so we can test removal logic
// @ts-ignore
certManager.challengeRouteActive = true;
// @ts-ignore
await (certManager as any).removeChallengeRoute();
// If we got here without error, the error handling works
expect(true).toEqual(true);
} catch (error) {
// This shouldn't happen if our error handling is working
// Error handling failed in removeChallengeRoute
expect(false).toEqual(true);
2025-05-20 15:44:48 +00:00
}
});
// Test verifyChallengeRouteRemoved error handling
tap.test('should have error handling in verifyChallengeRouteRemoved', async () => {
// Create a SmartProxy for testing
const testProxy = new SmartProxy({
routes: [createRoute(1, 'test1.domain.test')]
});
// Verify that verifyChallengeRouteRemoved has error handling
try {
// @ts-ignore - Access private method for testing
await (testProxy as any).verifyChallengeRouteRemoved();
// If we got here without error, the try/catch is working
// (This will still throw at the end after max retries, but we're testing that
// the logger calls have try/catch blocks around them)
} catch (error) {
// This error is expected since we don't have a real challenge route
// But we're testing that the logger calls don't throw
expect(error.message).toContain('Failed to verify challenge route removal');
}
});
tap.start();