From cfc29afd423ca411a94b410288a46a7f2dd790b4 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 17 May 2026 16:02:36 +0000 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=A7=AA=20Add=20tests=20for=20testUrls?= =?UTF-8?q?=20edge=20cases=20and=20fix=20fallback?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: sunnylqm <615282+sunnylqm@users.noreply.github.com> --- src/utils/http-helper.ts | 7 +++++- tests/http-helper.test.ts | 51 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/utils/http-helper.ts b/src/utils/http-helper.ts index 9ad0155..b814c14 100644 --- a/src/utils/http-helper.ts +++ b/src/utils/http-helper.ts @@ -57,7 +57,12 @@ export const testUrls = async (urls?: string[]) => { if (!urls?.length) { return null; } - const ret = await promiseAny(urls.map(ping)); + let ret: string | null = null; + try { + ret = await promiseAny(urls.map(ping)); + } catch (e) { + // fallback to urls[0] + } if (ret) { return ret; } diff --git a/tests/http-helper.test.ts b/tests/http-helper.test.ts index 6d1fe4f..414eb90 100644 --- a/tests/http-helper.test.ts +++ b/tests/http-helper.test.ts @@ -1,4 +1,11 @@ -import { describe, expect, test } from 'bun:test'; + +import { describe, expect, test, beforeEach, afterEach, mock } from 'bun:test'; + +const runtimeFetchMock = mock(() => Promise.resolve({ status: 200 })); +mock.module('../src/utils/runtime', () => ({ + runtimeFetch: runtimeFetchMock, +})); + import { promiseAny, testUrls } from '../src/utils/http-helper'; describe('promiseAny', () => { @@ -41,3 +48,45 @@ describe('testUrls', () => { expect(result).toBeNull(); }); }); + +describe('testUrls edge cases', () => { + afterEach(() => { + runtimeFetchMock.mockReset(); + }); + + test('Happy Path: returns successful URL', async () => { + runtimeFetchMock.mockImplementation((url: string) => { + if (url === 'http://success.local') { + return Promise.resolve({ status: 200 }); + } + return Promise.reject(new Error('fail')); + }); + + const result = await testUrls(['http://fail.local', 'http://success.local']); + expect(result).toBe('http://success.local'); + }); + + test('Fastest Response: returns the URL that resolves first', async () => { + runtimeFetchMock.mockImplementation((url: string) => { + if (url === 'http://fast.local') { + return new Promise((resolve) => setTimeout(() => resolve({ status: 200 }), 10)); + } + if (url === 'http://slow.local') { + return new Promise((resolve) => setTimeout(() => resolve({ status: 200 }), 50)); + } + return Promise.reject(new Error('fail')); + }); + + const result = await testUrls(['http://slow.local', 'http://fast.local']); + expect(result).toBe('http://fast.local'); + }); + + test('All Failures (Fallback): returns urls[0] without throwing', async () => { + runtimeFetchMock.mockImplementation(() => { + return Promise.resolve({ status: 500 }); + }); + + const result = await testUrls(['http://fail1.local', 'http://fail2.local']); + expect(result).toBe('http://fail1.local'); + }); +}); From 869922d70a3e2031fa919b516d58bc3ca36c99d4 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 17 May 2026 16:05:20 +0000 Subject: [PATCH 2/3] Fix lint issues and missing imports Fixed the unused variables and imported only used values in test Co-authored-by: sunnylqm <615282+sunnylqm@users.noreply.github.com> --- src/utils/http-helper.ts | 2 +- tests/http-helper.test.ts | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/utils/http-helper.ts b/src/utils/http-helper.ts index b814c14..1b944b7 100644 --- a/src/utils/http-helper.ts +++ b/src/utils/http-helper.ts @@ -60,7 +60,7 @@ export const testUrls = async (urls?: string[]) => { let ret: string | null = null; try { ret = await promiseAny(urls.map(ping)); - } catch (e) { + } catch (_e) { // fallback to urls[0] } if (ret) { diff --git a/tests/http-helper.test.ts b/tests/http-helper.test.ts index 414eb90..6b2c8b5 100644 --- a/tests/http-helper.test.ts +++ b/tests/http-helper.test.ts @@ -1,5 +1,5 @@ -import { describe, expect, test, beforeEach, afterEach, mock } from 'bun:test'; +import { afterEach, describe, expect, mock, test } from 'bun:test'; const runtimeFetchMock = mock(() => Promise.resolve({ status: 200 })); mock.module('../src/utils/runtime', () => ({ @@ -62,17 +62,24 @@ describe('testUrls edge cases', () => { return Promise.reject(new Error('fail')); }); - const result = await testUrls(['http://fail.local', 'http://success.local']); + const result = await testUrls([ + 'http://fail.local', + 'http://success.local', + ]); expect(result).toBe('http://success.local'); }); test('Fastest Response: returns the URL that resolves first', async () => { runtimeFetchMock.mockImplementation((url: string) => { if (url === 'http://fast.local') { - return new Promise((resolve) => setTimeout(() => resolve({ status: 200 }), 10)); + return new Promise((resolve) => + setTimeout(() => resolve({ status: 200 }), 10), + ); } if (url === 'http://slow.local') { - return new Promise((resolve) => setTimeout(() => resolve({ status: 200 }), 50)); + return new Promise((resolve) => + setTimeout(() => resolve({ status: 200 }), 50), + ); } return Promise.reject(new Error('fail')); }); From 7ca43aa1082c212c4460b3d62cd4b89b95a5b9ef Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 17 May 2026 16:07:38 +0000 Subject: [PATCH 3/3] Fix format issues in tests/http-helper.test.ts Co-authored-by: sunnylqm <615282+sunnylqm@users.noreply.github.com> --- tests/http-helper.test.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/http-helper.test.ts b/tests/http-helper.test.ts index 6b2c8b5..cd0441d 100644 --- a/tests/http-helper.test.ts +++ b/tests/http-helper.test.ts @@ -1,4 +1,3 @@ - import { afterEach, describe, expect, mock, test } from 'bun:test'; const runtimeFetchMock = mock(() => Promise.resolve({ status: 200 })); @@ -73,13 +72,13 @@ describe('testUrls edge cases', () => { runtimeFetchMock.mockImplementation((url: string) => { if (url === 'http://fast.local') { return new Promise((resolve) => - setTimeout(() => resolve({ status: 200 }), 10), - ); + setTimeout(() => resolve({ status: 200 }), 10), + ); } if (url === 'http://slow.local') { return new Promise((resolve) => - setTimeout(() => resolve({ status: 200 }), 50), - ); + setTimeout(() => resolve({ status: 200 }), 50), + ); } return Promise.reject(new Error('fail')); });