import { tap, expect } from '@git.zone/tstest/tapbundle'; import { SmartRequest } from '../ts/index.js'; tap.test('should clear timeout when request completes before timeout', async () => { // Set a long timeout that would keep the process alive if not cleared const response = await SmartRequest.create() .url('https://httpbin.org/delay/1') // 1 second delay .timeout(10000) // 10 second timeout (much longer than needed) .get(); const data = await response.json(); expect(data).toBeDefined(); // The test should complete quickly, not wait for the 10 second timeout // If the timeout isn't cleared, the process would hang for 10 seconds }); tap.test('should timeout when request takes longer than timeout', async () => { let errorThrown = false; try { // Try to fetch with a very short timeout await SmartRequest.create() .url('https://httpbin.org/delay/3') // 3 second delay .timeout(100) // 100ms timeout (will fail) .get(); } catch (error) { errorThrown = true; expect(error.message).toContain('Request timed out'); } expect(errorThrown).toBeTrue(); }); tap.test('should not leak timers with multiple successful requests', async () => { // Make multiple requests with timeouts to ensure no timer leaks const promises = []; for (let i = 0; i < 5; i++) { promises.push( SmartRequest.create() .url('https://httpbin.org/get') .timeout(5000) // 5 second timeout .get() .then(response => response.json()) ); } const results = await Promise.all(promises); // All requests should complete successfully expect(results).toHaveLength(5); results.forEach(result => { expect(result).toBeDefined(); }); // Process should exit cleanly after this test without hanging }); export default tap.start();