Skip to content

Commit 95cca5e

Browse files
committed
fix(stores,tests): align loopback URL resolution and update settings store expectations
1 parent 315b38e commit 95cca5e

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

src/stores/settings_store.test.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,45 @@ describe('settings_store', () => {
6060
await use_settings_store.persist.rehydrate();
6161

6262
const settings = get_settings();
63-
expect(settings.tadpole_os_url).toBe(import.meta.env.VITE_TADPOLE_OS_URL || 'http://127.0.0.1:8000');
63+
const expected_url = import.meta.env.VITE_TADPOLE_OS_URL || (typeof window !== 'undefined' && window.location?.hostname === '127.0.0.1' ? 'http://127.0.0.1:8000' : 'http://localhost:8000');
64+
expect(settings.tadpole_os_url).toBe(expected_url);
6465
expect(settings.tadpole_os_api_key).toBe(import.meta.env.VITE_NEURAL_TOKEN || '');
6566
});
6667

68+
it('defaults to loopback based on window origin when VITE_TADPOLE_OS_URL is unset', async () => {
69+
vi.stubEnv('VITE_TADPOLE_OS_URL', '');
70+
const { get_settings, use_settings_store } = await import('./settings_store');
71+
await use_settings_store.persist.rehydrate();
72+
73+
const settings = get_settings();
74+
const expected_url = typeof window !== 'undefined' && window.location?.hostname === '127.0.0.1' ? 'http://127.0.0.1:8000' : 'http://localhost:8000';
75+
expect(settings.tadpole_os_url).toBe(expected_url);
76+
vi.unstubAllEnvs();
77+
});
78+
79+
it('aligns loopback URL to 127.0.0.1 when running on 127.0.0.1 origin and no env override', async () => {
80+
vi.stubEnv('VITE_TADPOLE_OS_URL', '');
81+
const original_location = window.location;
82+
try {
83+
Object.defineProperty(window, 'location', {
84+
value: { ...original_location, hostname: '127.0.0.1' },
85+
writable: true,
86+
configurable: true,
87+
});
88+
const { get_settings, use_settings_store } = await import('./settings_store');
89+
await use_settings_store.persist.rehydrate();
90+
const settings = get_settings();
91+
expect(settings.tadpole_os_url).toBe('http://127.0.0.1:8000');
92+
} finally {
93+
Object.defineProperty(window, 'location', {
94+
value: original_location,
95+
writable: true,
96+
configurable: true,
97+
});
98+
vi.unstubAllEnvs();
99+
}
100+
});
101+
67102
it('rehydrates from localStorage correctly', async () => {
68103
const test_key = 'custom-key';
69104

src/stores/settings_store.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,17 @@ interface Settings_State {
6161
}
6262

6363
const get_base_url = (): string => {
64+
if (import.meta.env.VITE_TADPOLE_OS_URL) {
65+
return import.meta.env.VITE_TADPOLE_OS_URL;
66+
}
6467
// Dynamically align loopback URL with current window origin if available
6568
if (typeof window !== 'undefined' && window.location?.hostname) {
6669
const host = window.location.hostname;
6770
if (host === 'localhost' || host === '127.0.0.1' || host === '0.0.0.0') {
6871
return `http://${host}:8000`;
6972
}
7073
}
71-
return import.meta.env.VITE_TADPOLE_OS_URL || 'http://localhost:8000';
74+
return 'http://localhost:8000';
7275
};
7376

7477
const normalize_loopback_url = (url: string | undefined): string => {

0 commit comments

Comments
 (0)