|
| 1 | +"""Snapshot tests for brew-hop-search CLI output.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import re |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from tests.snap import snap # noqa: F401 (fixture) |
| 11 | + |
| 12 | + |
| 13 | +def run(*args: str) -> str: |
| 14 | + """Run brew-hop-search and return stdout, stripping ANSI codes.""" |
| 15 | + result = subprocess.run( |
| 16 | + [sys.executable, "-m", "brew_hop_search.cli", *args], |
| 17 | + capture_output=True, text=True, timeout=30, |
| 18 | + ) |
| 19 | + # Strip ANSI escape codes for stable snapshots |
| 20 | + clean = re.sub(r"\033\[[0-9;]*m", "", result.stdout + result.stderr) |
| 21 | + return clean |
| 22 | + |
| 23 | + |
| 24 | +def test_help(snap): |
| 25 | + snap.assert_match(run("--help")) |
| 26 | + |
| 27 | + |
| 28 | +def test_version_importable(): |
| 29 | + from brew_hop_search import __version__ |
| 30 | + assert re.match(r"\d+\.\d+\.\d+", __version__) |
| 31 | + |
| 32 | + |
| 33 | +def test_cache_status_json(): |
| 34 | + """Cache status JSON should be valid and have expected keys.""" |
| 35 | + import json |
| 36 | + output = run("-C", "--json") |
| 37 | + data = json.loads(output) |
| 38 | + assert "cache_dir" in data |
| 39 | + assert "db_path" in data |
| 40 | + assert "sources" in data |
| 41 | + |
| 42 | + |
| 43 | +def test_duration_parsing(): |
| 44 | + from brew_hop_search.cli import parse_duration |
| 45 | + assert parse_duration("30m") == 1800 |
| 46 | + assert parse_duration("6h") == 21600 |
| 47 | + assert parse_duration("1d") == 86400 |
| 48 | + assert parse_duration("1h30m") == 5400 |
| 49 | + assert parse_duration("90") == 90 |
| 50 | + |
| 51 | + |
| 52 | +def test_duration_formatting(): |
| 53 | + from brew_hop_search.display import fmt_duration |
| 54 | + assert fmt_duration(30) == "30s" |
| 55 | + assert fmt_duration(90) == "1m30s" |
| 56 | + assert fmt_duration(3600) == "1h" |
| 57 | + assert fmt_duration(3660) == "1h1m" |
| 58 | + assert fmt_duration(86400) == "1d" |
| 59 | + assert fmt_duration(float("inf")) == "never" |
| 60 | + |
| 61 | + |
| 62 | +def test_scoring(): |
| 63 | + from brew_hop_search.search import score |
| 64 | + # Exact match |
| 65 | + assert score("python", "", ["python"]) == 100 |
| 66 | + # Prefix match |
| 67 | + assert score("python3", "", ["python"]) == 60 |
| 68 | + # Substring match |
| 69 | + assert score("cpython", "", ["python"]) == 30 |
| 70 | + # Description match |
| 71 | + assert score("foo", "uses python", ["python"]) == 10 |
| 72 | + # No match |
| 73 | + assert score("foo", "bar", ["python"]) == 0 |
| 74 | + # All terms must match |
| 75 | + assert score("python", "language", ["python", "java"]) == 0 |
| 76 | + |
| 77 | + |
| 78 | +def test_fts_query(): |
| 79 | + from brew_hop_search.search import fts_query |
| 80 | + assert fts_query(["python"]) == '"python"*' |
| 81 | + assert fts_query(["python", "build"]) == '"python"* AND "build"*' |
| 82 | + |
| 83 | + |
| 84 | +def test_rb_parser(): |
| 85 | + """Test the lightweight Ruby formula parser.""" |
| 86 | + import tempfile |
| 87 | + from pathlib import Path |
| 88 | + from brew_hop_search.sources.taps import parse_rb |
| 89 | + |
| 90 | + rb_content = ''' |
| 91 | +cask "test-app" do |
| 92 | + version "1.2.3" |
| 93 | + desc "A test application" |
| 94 | + homepage "https://example.com" |
| 95 | + url "https://example.com/download/v1.2.3/test.dmg" |
| 96 | +end |
| 97 | +''' |
| 98 | + with tempfile.NamedTemporaryFile(suffix=".rb", mode="w", delete=False) as f: |
| 99 | + f.write(rb_content) |
| 100 | + f.flush() |
| 101 | + result = parse_rb(Path(f.name), "test/tap") |
| 102 | + |
| 103 | + assert result is not None |
| 104 | + assert result["name"] == Path(f.name).stem |
| 105 | + assert result["version"] == "1.2.3" |
| 106 | + assert result["desc"] == "A test application" |
| 107 | + assert result["homepage"] == "https://example.com" |
| 108 | + assert result["tap"] == "test/tap" |
0 commit comments