|
| 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 | + class << self |
| 11 | + attr_reader :mutex |
| 12 | + end |
| 13 | + |
| 14 | + def setup |
| 15 | + super |
| 16 | + @tmp_home = Dir.mktmpdir |
| 17 | + @old_home = Dir.home if ENV.key?("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 | + if @old_home |
| 30 | + ENV["HOME"] = @old_home |
| 31 | + else |
| 32 | + ENV.delete("HOME") |
| 33 | + end |
| 34 | + ENV["XDG_CACHE_HOME"] = @old_xdg |
| 35 | + ENV["LOCALAPPDATA"] = @old_local |
| 36 | + FileUtils.remove_entry(@tmp_home) if @tmp_home && File.directory?(@tmp_home) |
| 37 | + WebMock.reset! |
| 38 | + WebMock.allow_net_connect! |
| 39 | + super |
| 40 | + end |
| 41 | + |
| 42 | + def around |
| 43 | + self.class.mutex.synchronize { super } |
| 44 | + end |
| 45 | + |
| 46 | + def test_uses_cache_without_network |
| 47 | + cache_dir = Stagehand::Local::Binary.cache_dir |
| 48 | + filename = Stagehand::Local::Binary.binary_filename |
| 49 | + FileUtils.mkdir_p(cache_dir) |
| 50 | + cache_path = File.join(cache_dir, filename) |
| 51 | + File.binwrite(cache_path, "cached") |
| 52 | + |
| 53 | + path = Stagehand::Local::Binary.resolve_binary_path |
| 54 | + assert_equal(cache_path, path) |
| 55 | + end |
| 56 | + |
| 57 | + def test_downloads_latest_tag_when_missing |
| 58 | + filename = Stagehand::Local::Binary.binary_filename |
| 59 | + |
| 60 | + stub_request(:get, "https://api.github.com/repos/browserbase/stagehand/releases?per_page=15") |
| 61 | + .to_return(status: 200, body: [{tag_name: "stagehand-server/v9.9.9"}].to_json) |
| 62 | + |
| 63 | + stub_request(:get, "https://github.com/browserbase/stagehand/releases/download/stagehand-server/v9.9.9/#{filename}") |
| 64 | + .to_return(status: 200, body: "binary") |
| 65 | + |
| 66 | + path = Stagehand::Local::Binary.resolve_binary_path |
| 67 | + |
| 68 | + assert(File.exist?(path)) |
| 69 | + assert_equal("binary", File.binread(path)) |
| 70 | + end |
| 71 | + |
| 72 | + def test_download_error_includes_manual_hint |
| 73 | + filename = Stagehand::Local::Binary.binary_filename |
| 74 | + |
| 75 | + stub_request(:get, "https://api.github.com/repos/browserbase/stagehand/releases?per_page=15") |
| 76 | + .to_return(status: 200, body: [{tag_name: "stagehand-server/v1.2.3"}].to_json) |
| 77 | + |
| 78 | + stub_request(:get, "https://github.com/browserbase/stagehand/releases/download/stagehand-server/v1.2.3/#{filename}") |
| 79 | + .to_return(status: 404, body: "not found") |
| 80 | + |
| 81 | + err = assert_raises(RuntimeError) { Stagehand::Local::Binary.resolve_binary_path } |
| 82 | + assert_includes(err.message, "Failed to download Stagehand driver binary") |
| 83 | + assert_includes(err.message, filename) |
| 84 | + end |
| 85 | +end |
0 commit comments