|
1 | | -import { describe, expect, test } from 'bun:test'; |
| 1 | + |
| 2 | +import { describe, expect, test, beforeEach, afterEach, mock } from 'bun:test'; |
| 3 | + |
| 4 | +const runtimeFetchMock = mock(() => Promise.resolve({ status: 200 })); |
| 5 | +mock.module('../src/utils/runtime', () => ({ |
| 6 | + runtimeFetch: runtimeFetchMock, |
| 7 | +})); |
| 8 | + |
2 | 9 | import { promiseAny, testUrls } from '../src/utils/http-helper'; |
3 | 10 |
|
4 | 11 | describe('promiseAny', () => { |
@@ -41,3 +48,45 @@ describe('testUrls', () => { |
41 | 48 | expect(result).toBeNull(); |
42 | 49 | }); |
43 | 50 | }); |
| 51 | + |
| 52 | +describe('testUrls edge cases', () => { |
| 53 | + afterEach(() => { |
| 54 | + runtimeFetchMock.mockReset(); |
| 55 | + }); |
| 56 | + |
| 57 | + test('Happy Path: returns successful URL', async () => { |
| 58 | + runtimeFetchMock.mockImplementation((url: string) => { |
| 59 | + if (url === 'http://success.local') { |
| 60 | + return Promise.resolve({ status: 200 }); |
| 61 | + } |
| 62 | + return Promise.reject(new Error('fail')); |
| 63 | + }); |
| 64 | + |
| 65 | + const result = await testUrls(['http://fail.local', 'http://success.local']); |
| 66 | + expect(result).toBe('http://success.local'); |
| 67 | + }); |
| 68 | + |
| 69 | + test('Fastest Response: returns the URL that resolves first', async () => { |
| 70 | + runtimeFetchMock.mockImplementation((url: string) => { |
| 71 | + if (url === 'http://fast.local') { |
| 72 | + return new Promise((resolve) => setTimeout(() => resolve({ status: 200 }), 10)); |
| 73 | + } |
| 74 | + if (url === 'http://slow.local') { |
| 75 | + return new Promise((resolve) => setTimeout(() => resolve({ status: 200 }), 50)); |
| 76 | + } |
| 77 | + return Promise.reject(new Error('fail')); |
| 78 | + }); |
| 79 | + |
| 80 | + const result = await testUrls(['http://slow.local', 'http://fast.local']); |
| 81 | + expect(result).toBe('http://fast.local'); |
| 82 | + }); |
| 83 | + |
| 84 | + test('All Failures (Fallback): returns urls[0] without throwing', async () => { |
| 85 | + runtimeFetchMock.mockImplementation(() => { |
| 86 | + return Promise.resolve({ status: 500 }); |
| 87 | + }); |
| 88 | + |
| 89 | + const result = await testUrls(['http://fail1.local', 'http://fail2.local']); |
| 90 | + expect(result).toBe('http://fail1.local'); |
| 91 | + }); |
| 92 | +}); |
0 commit comments