|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "test_helper" |
| 4 | + |
| 5 | +class Stagehand::LocalBinaryTest < Minitest::Test |
| 6 | + include WebMock::API |
| 7 | + |
| 8 | + @mutex = Mutex.new |
| 9 | + |
| 10 | + def self.mutex |
| 11 | + @mutex |
| 12 | + end |
| 13 | + |
| 14 | + def setup |
| 15 | + super |
| 16 | + @tmp_home = Dir.mktmpdir |
| 17 | + @old_home = ENV["HOME"] |
| 18 | + @old_xdg = ENV["XDG_CACHE_HOME"] |
| 19 | + @old_local = ENV["LOCALAPPDATA"] |
| 20 | + ENV["HOME"] = @tmp_home |
| 21 | + ENV.delete("XDG_CACHE_HOME") |
| 22 | + ENV.delete("LOCALAPPDATA") |
| 23 | + ENV.delete("STAGEHAND_SERVER_VERSION") |
| 24 | + WebMock.enable! |
| 25 | + WebMock.disable_net_connect!(allow_localhost: true) |
| 26 | + end |
| 27 | + |
| 28 | + def teardown |
| 29 | + ENV["HOME"] = @old_home |
| 30 | + ENV["XDG_CACHE_HOME"] = @old_xdg |
| 31 | + ENV["LOCALAPPDATA"] = @old_local |
| 32 | + FileUtils.remove_entry(@tmp_home) if @tmp_home && File.directory?(@tmp_home) |
| 33 | + WebMock.reset! |
| 34 | + WebMock.allow_net_connect! |
| 35 | + super |
| 36 | + end |
| 37 | + |
| 38 | + def around |
| 39 | + self.class.mutex.synchronize { super } |
| 40 | + end |
| 41 | + |
| 42 | + def test_uses_cache_without_network |
| 43 | + cache_dir = Stagehand::Local::Binary.cache_dir |
| 44 | + filename = Stagehand::Local::Binary.binary_filename |
| 45 | + FileUtils.mkdir_p(cache_dir) |
| 46 | + cache_path = File.join(cache_dir, filename) |
| 47 | + File.binwrite(cache_path, "cached") |
| 48 | + |
| 49 | + path = Stagehand::Local::Binary.resolve_binary_path |
| 50 | + assert_equal cache_path, path |
| 51 | + end |
| 52 | + |
| 53 | + def test_downloads_latest_tag_when_missing |
| 54 | + filename = Stagehand::Local::Binary.binary_filename |
| 55 | + |
| 56 | + stub_request(:get, "https://api.github.com/repos/browserbase/stagehand/releases?per_page=15") |
| 57 | + .to_return(status: 200, body: [{ tag_name: "stagehand-server/v9.9.9" }].to_json) |
| 58 | + |
| 59 | + stub_request(:get, "https://github.com/browserbase/stagehand/releases/download/stagehand-server/v9.9.9/#{filename}") |
| 60 | + .to_return(status: 200, body: "binary") |
| 61 | + |
| 62 | + path = Stagehand::Local::Binary.resolve_binary_path |
| 63 | + |
| 64 | + assert File.exist?(path) |
| 65 | + assert_equal "binary", File.binread(path) |
| 66 | + end |
| 67 | + |
| 68 | + def test_download_error_includes_manual_hint |
| 69 | + filename = Stagehand::Local::Binary.binary_filename |
| 70 | + |
| 71 | + stub_request(:get, "https://api.github.com/repos/browserbase/stagehand/releases?per_page=15") |
| 72 | + .to_return(status: 200, body: [{ tag_name: "stagehand-server/v1.2.3" }].to_json) |
| 73 | + |
| 74 | + stub_request(:get, "https://github.com/browserbase/stagehand/releases/download/stagehand-server/v1.2.3/#{filename}") |
| 75 | + .to_return(status: 404, body: "not found") |
| 76 | + |
| 77 | + err = assert_raises(RuntimeError) { Stagehand::Local::Binary.resolve_binary_path } |
| 78 | + assert_includes err.message, "Failed to download Stagehand driver binary" |
| 79 | + assert_includes err.message, filename |
| 80 | + end |
| 81 | +end |
0 commit comments