|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import importlib.util |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +from stagehand.lib import sea_binary |
| 7 | +from stagehand._version import __version__ |
| 8 | + |
| 9 | + |
| 10 | +def _load_download_binary_module(): |
| 11 | + script_path = Path(__file__).resolve().parents[1] / "scripts" / "download-binary.py" |
| 12 | + spec = importlib.util.spec_from_file_location("download_binary_script", script_path) |
| 13 | + assert spec is not None |
| 14 | + assert spec.loader is not None |
| 15 | + |
| 16 | + module = importlib.util.module_from_spec(spec) |
| 17 | + spec.loader.exec_module(module) |
| 18 | + return module |
| 19 | + |
| 20 | + |
| 21 | +download_binary = _load_download_binary_module() |
| 22 | + |
| 23 | + |
| 24 | +def test_resolve_binary_path_defaults_cache_version_to_package_version( |
| 25 | + monkeypatch, |
| 26 | + tmp_path: Path, |
| 27 | +) -> None: |
| 28 | + resource_path = tmp_path / "stagehand-test" |
| 29 | + resource_path.write_bytes(b"binary") |
| 30 | + |
| 31 | + captured: dict[str, object] = {} |
| 32 | + |
| 33 | + monkeypatch.delenv("STAGEHAND_VERSION", raising=False) |
| 34 | + monkeypatch.setattr(sea_binary, "_resource_binary_path", lambda _filename: resource_path) |
| 35 | + |
| 36 | + def _fake_copy_to_cache(*, src: Path, filename: str, version: str) -> Path: |
| 37 | + captured["src"] = src |
| 38 | + captured["filename"] = filename |
| 39 | + captured["version"] = version |
| 40 | + return tmp_path / "cache" / filename |
| 41 | + |
| 42 | + monkeypatch.setattr(sea_binary, "_copy_to_cache", _fake_copy_to_cache) |
| 43 | + |
| 44 | + resolved = sea_binary.resolve_binary_path() |
| 45 | + |
| 46 | + assert resolved == tmp_path / "cache" / sea_binary.default_binary_filename() |
| 47 | + assert captured["src"] == resource_path |
| 48 | + assert captured["filename"] == sea_binary.default_binary_filename() |
| 49 | + assert captured["version"] == __version__ |
| 50 | + |
| 51 | + |
| 52 | +def test_parse_server_tag_rejects_prerelease_tags() -> None: |
| 53 | + assert download_binary._parse_server_tag("stagehand-server-v3/v3.20.0-dev") is None |
| 54 | + assert download_binary._parse_server_tag("stagehand-server-v3/v3.20.0+build.1") is None |
| 55 | + |
| 56 | + |
| 57 | +def test_resolve_latest_server_tag_ignores_dev_releases(monkeypatch) -> None: |
| 58 | + releases = [ |
| 59 | + {"tag_name": "stagehand-server-v3/v3.20.0-dev"}, |
| 60 | + {"tag_name": "stagehand-server-v3/v3.19.1"}, |
| 61 | + {"tag_name": "stagehand-server-v3/v3.19.0"}, |
| 62 | + ] |
| 63 | + |
| 64 | + monkeypatch.setattr(download_binary, "_http_get_json", lambda _url: releases) |
| 65 | + |
| 66 | + assert download_binary.resolve_latest_server_tag() == "stagehand-server-v3/v3.19.1" |
0 commit comments