|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import importlib.util |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from stagehand.lib import sea_binary |
| 9 | +from stagehand._version import __version__ |
| 10 | + |
| 11 | + |
| 12 | +def _load_download_binary_module(): |
| 13 | + script_path = Path(__file__).resolve().parents[1] / "scripts" / "download-binary.py" |
| 14 | + spec = importlib.util.spec_from_file_location("download_binary_script", script_path) |
| 15 | + assert spec is not None |
| 16 | + assert spec.loader is not None |
| 17 | + |
| 18 | + module = importlib.util.module_from_spec(spec) |
| 19 | + spec.loader.exec_module(module) |
| 20 | + return module |
| 21 | + |
| 22 | + |
| 23 | +download_binary = _load_download_binary_module() |
| 24 | + |
| 25 | + |
| 26 | +def test_resolve_binary_path_defaults_cache_version_to_package_version( |
| 27 | + monkeypatch: pytest.MonkeyPatch, |
| 28 | + tmp_path: Path, |
| 29 | +) -> None: |
| 30 | + resource_path = tmp_path / "stagehand-test" |
| 31 | + resource_path.write_bytes(b"binary") |
| 32 | + |
| 33 | + captured: dict[str, object] = {} |
| 34 | + |
| 35 | + monkeypatch.delenv("STAGEHAND_VERSION", raising=False) |
| 36 | + def _fake_resource_binary_path(_filename: str) -> Path: |
| 37 | + return resource_path |
| 38 | + |
| 39 | + monkeypatch.setattr(sea_binary, "_resource_binary_path", _fake_resource_binary_path) |
| 40 | + |
| 41 | + def _fake_copy_to_cache(*, src: Path, filename: str, version: str) -> Path: |
| 42 | + captured["src"] = src |
| 43 | + captured["filename"] = filename |
| 44 | + captured["version"] = version |
| 45 | + return tmp_path / "cache" / filename |
| 46 | + |
| 47 | + monkeypatch.setattr(sea_binary, "_copy_to_cache", _fake_copy_to_cache) |
| 48 | + |
| 49 | + resolved = sea_binary.resolve_binary_path() |
| 50 | + |
| 51 | + assert resolved == tmp_path / "cache" / sea_binary.default_binary_filename() |
| 52 | + assert captured["src"] == resource_path |
| 53 | + assert captured["filename"] == sea_binary.default_binary_filename() |
| 54 | + assert captured["version"] == __version__ |
| 55 | + |
| 56 | + |
| 57 | +def test_parse_server_tag_rejects_prerelease_tags() -> None: |
| 58 | + assert download_binary._parse_server_tag("stagehand-server-v3/v3.20.0-dev") is None |
| 59 | + assert download_binary._parse_server_tag("stagehand-server-v3/v3.20.0+build.1") is None |
| 60 | + |
| 61 | + |
| 62 | +def test_normalize_server_tag_rejects_prerelease_input() -> None: |
| 63 | + try: |
| 64 | + download_binary.normalize_server_tag("v3.20.0-dev") |
| 65 | + except ValueError as exc: |
| 66 | + assert "stable tag" in str(exc) |
| 67 | + else: |
| 68 | + raise AssertionError("Expected prerelease version input to be rejected") |
| 69 | + |
| 70 | + |
| 71 | +def test_resolve_latest_server_tag_ignores_dev_releases( |
| 72 | + monkeypatch: pytest.MonkeyPatch, |
| 73 | +) -> None: |
| 74 | + releases = [ |
| 75 | + {"tag_name": "stagehand-server-v3/v3.20.0-dev"}, |
| 76 | + {"tag_name": "stagehand-server-v3/v3.19.1"}, |
| 77 | + {"tag_name": "stagehand-server-v3/v3.19.0"}, |
| 78 | + ] |
| 79 | + |
| 80 | + def _fake_http_get_json(_url: str) -> list[dict[str, str]]: |
| 81 | + return releases |
| 82 | + |
| 83 | + monkeypatch.setattr(download_binary, "_http_get_json", _fake_http_get_json) |
| 84 | + |
| 85 | + assert download_binary.resolve_latest_server_tag() == "stagehand-server-v3/v3.19.1" |
0 commit comments