@@ -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
0 commit comments