@@ -33,6 +33,90 @@ def test_client_config_from_env_rejects_control_character_api_key(monkeypatch):
3333 ClientConfig .from_env ()
3434
3535
36+ def test_client_config_from_env_wraps_api_key_env_read_runtime_errors (
37+ monkeypatch : pytest .MonkeyPatch ,
38+ ):
39+ original_get = config_module .os .environ .get
40+
41+ def _broken_get (env_name : str , default = None ):
42+ if env_name == "HYPERBROWSER_API_KEY" :
43+ raise RuntimeError ("api key env read exploded" )
44+ return original_get (env_name , default )
45+
46+ monkeypatch .setattr (config_module .os .environ , "get" , _broken_get )
47+
48+ with pytest .raises (
49+ HyperbrowserError ,
50+ match = "api key env read exploded" ,
51+ ) as exc_info :
52+ ClientConfig .from_env ()
53+
54+ assert isinstance (exc_info .value .original_error , RuntimeError )
55+
56+
57+ def test_client_config_from_env_wraps_base_url_env_read_runtime_errors (
58+ monkeypatch : pytest .MonkeyPatch ,
59+ ):
60+ original_get = config_module .os .environ .get
61+ monkeypatch .setenv ("HYPERBROWSER_API_KEY" , "test-key" )
62+
63+ def _broken_get (env_name : str , default = None ):
64+ if env_name == "HYPERBROWSER_BASE_URL" :
65+ raise RuntimeError ("base url env read exploded" )
66+ return original_get (env_name , default )
67+
68+ monkeypatch .setattr (config_module .os .environ , "get" , _broken_get )
69+
70+ with pytest .raises (
71+ HyperbrowserError ,
72+ match = "base url env read exploded" ,
73+ ) as exc_info :
74+ ClientConfig .from_env ()
75+
76+ assert isinstance (exc_info .value .original_error , RuntimeError )
77+
78+
79+ def test_client_config_from_env_wraps_headers_env_read_runtime_errors (
80+ monkeypatch : pytest .MonkeyPatch ,
81+ ):
82+ original_get = config_module .os .environ .get
83+ monkeypatch .setenv ("HYPERBROWSER_API_KEY" , "test-key" )
84+ monkeypatch .setenv ("HYPERBROWSER_BASE_URL" , "https://api.hyperbrowser.ai" )
85+
86+ def _broken_get (env_name : str , default = None ):
87+ if env_name == "HYPERBROWSER_HEADERS" :
88+ raise RuntimeError ("headers env read exploded" )
89+ return original_get (env_name , default )
90+
91+ monkeypatch .setattr (config_module .os .environ , "get" , _broken_get )
92+
93+ with pytest .raises (
94+ HyperbrowserError ,
95+ match = "headers env read exploded" ,
96+ ) as exc_info :
97+ ClientConfig .from_env ()
98+
99+ assert isinstance (exc_info .value .original_error , RuntimeError )
100+
101+
102+ def test_client_config_from_env_preserves_hyperbrowser_env_read_errors (
103+ monkeypatch : pytest .MonkeyPatch ,
104+ ):
105+ def _broken_read_env (env_name : str ):
106+ if env_name == "HYPERBROWSER_API_KEY" :
107+ raise HyperbrowserError ("custom env read failure" )
108+ return None
109+
110+ monkeypatch .setattr (
111+ ClientConfig , "_read_env_value" , staticmethod (_broken_read_env )
112+ )
113+
114+ with pytest .raises (HyperbrowserError , match = "custom env read failure" ) as exc_info :
115+ ClientConfig .from_env ()
116+
117+ assert exc_info .value .original_error is None
118+
119+
36120def test_client_config_from_env_reads_api_key_and_base_url (monkeypatch ):
37121 monkeypatch .setenv ("HYPERBROWSER_API_KEY" , "test-key" )
38122 monkeypatch .setenv ("HYPERBROWSER_BASE_URL" , "https://example.local" )
0 commit comments