@@ -256,6 +256,50 @@ def mock_system_calls
256256 end
257257 end
258258
259+ describe "browser auto-open readiness" do
260+ it "normalizes routes to request paths" do
261+ expect ( described_class . send ( :build_request_path , nil ) ) . to eq ( "/" )
262+ expect ( described_class . send ( :build_request_path , "/" ) ) . to eq ( "/" )
263+ expect ( described_class . send ( :build_request_path , "hello_world" ) ) . to eq ( "/hello_world" )
264+ expect ( described_class . send ( :build_request_path , "/hello_server" ) ) . to eq ( "/hello_server" )
265+ end
266+
267+ it "treats a successful response as ready" do
268+ response = Net ::HTTPOK . new ( "1.1" , "200" , "OK" )
269+ allow ( described_class ) . to receive ( :localhost_port_open? ) . with ( 3000 ) . and_return ( true )
270+ allow ( described_class ) . to receive ( :http_get_localhost ) . with ( 3000 , "/" ) . and_return ( response )
271+
272+ expect ( described_class . send ( :app_route_ready? , 3000 , "/" ) ) . to be true
273+ end
274+
275+ it "treats a redirect response as ready" do
276+ response = Net ::HTTPFound . new ( "1.1" , "302" , "Found" )
277+ allow ( described_class ) . to receive ( :localhost_port_open? ) . with ( 3000 ) . and_return ( true )
278+ allow ( described_class ) . to receive ( :http_get_localhost ) . with ( 3000 , "/" ) . and_return ( response )
279+
280+ expect ( described_class . send ( :app_route_ready? , 3000 , "/" ) ) . to be true
281+ end
282+
283+ it "does not treat a server error response as ready" do
284+ response = Net ::HTTPInternalServerError . new ( "1.1" , "500" , "Internal Server Error" )
285+ allow ( described_class ) . to receive ( :localhost_port_open? ) . with ( 3000 ) . and_return ( true )
286+ allow ( described_class ) . to receive ( :http_get_localhost ) . with ( 3000 , "/" ) . and_return ( response )
287+
288+ expect ( described_class . send ( :app_route_ready? , 3000 , "/" ) ) . to be false
289+ end
290+
291+ it "waits for the route to respond successfully before opening the browser" do
292+ allow ( described_class ) . to receive ( :browser_auto_open_allowed? ) . and_return ( true )
293+ allow ( File ) . to receive ( :exist? ) . with ( described_class ::OPEN_BROWSER_ONCE_MARKER ) . and_return ( false )
294+ allow ( Thread ) . to receive ( :new ) . and_yield
295+ allow ( described_class ) . to receive ( :wait_for_app_route ) . with ( 3000 , "/" ) . and_return ( true )
296+ expect ( described_class ) . to receive ( :open_browser ) . with ( "http://localhost:3000" ) . and_return ( true )
297+ expect ( described_class ) . to receive ( :mark_browser_opened_once )
298+
299+ described_class . send ( :schedule_browser_open , 3000 , route : "/" , once : true )
300+ end
301+ end
302+
259303 describe ".kill_processes" do
260304 before do
261305 allow_any_instance_of ( Kernel ) . to receive ( :` ) . and_return ( "" )
0 commit comments