import { tap, expect } from '@git.zone/tstest/tapbundle'; import { SmartProxy, createHttpRoute } from '../ts/index.js'; import * as http from 'http'; tap.test('memory leak fixes verification', async () => { // Test 1: MetricsCollector requestTimestamps cleanup console.log('\n=== Test 1: MetricsCollector requestTimestamps cleanup ==='); const proxy = new SmartProxy({ ports: [8081], routes: [ createHttpRoute('test.local', { host: 'localhost', port: 3200 }), ] }); // Override route port proxy.settings.routes[0].match.ports = 8081; await proxy.start(); const metricsCollector = (proxy.getStats() as any); // Check initial state console.log('Initial timestamps:', metricsCollector.requestTimestamps.length); // Simulate many requests to test cleanup for (let i = 0; i < 6000; i++) { metricsCollector.recordRequest(); } // Should be cleaned up to MAX_TIMESTAMPS (5000) console.log('After 6000 requests:', metricsCollector.requestTimestamps.length); expect(metricsCollector.requestTimestamps.length).toBeLessThanOrEqual(5000); await proxy.stop(); // Test 2: Verify intervals are cleaned up console.log('\n=== Test 2: Verify cleanup methods exist ==='); // Check RequestHandler has destroy method const { RequestHandler } = await import('../ts/proxies/http-proxy/request-handler.js'); const requestHandler = new RequestHandler({}, null as any); expect(typeof requestHandler.destroy).toEqual('function'); console.log('āœ“ RequestHandler has destroy method'); // Check FunctionCache has destroy method const { FunctionCache } = await import('../ts/proxies/http-proxy/function-cache.js'); const functionCache = new FunctionCache({ debug: () => {}, info: () => {} } as any); expect(typeof functionCache.destroy).toEqual('function'); console.log('āœ“ FunctionCache has destroy method'); // Cleanup requestHandler.destroy(); functionCache.destroy(); console.log('\nāœ… All memory leak fixes verified!'); }); tap.start();