test_store_branch_heads.py
python
| 1 | """Unit tests for get_all_branch_heads — store utility added with local transport.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import pathlib |
| 6 | |
| 7 | from muse.core.store import get_all_branch_heads |
| 8 | |
| 9 | |
| 10 | def _heads_dir(root: pathlib.Path) -> pathlib.Path: |
| 11 | d = root / ".muse" / "refs" / "heads" |
| 12 | d.mkdir(parents=True, exist_ok=True) |
| 13 | return d |
| 14 | |
| 15 | |
| 16 | class TestGetAllBranchHeads: |
| 17 | def test_empty_heads_dir_returns_empty_dict(self, tmp_path: pathlib.Path) -> None: |
| 18 | _heads_dir(tmp_path) |
| 19 | result = get_all_branch_heads(tmp_path) |
| 20 | assert result == {} |
| 21 | |
| 22 | def test_missing_heads_dir_returns_empty_dict(self, tmp_path: pathlib.Path) -> None: |
| 23 | # No .muse/ directory at all. |
| 24 | result = get_all_branch_heads(tmp_path) |
| 25 | assert result == {} |
| 26 | |
| 27 | def test_single_branch(self, tmp_path: pathlib.Path) -> None: |
| 28 | heads = _heads_dir(tmp_path) |
| 29 | (heads / "main").write_text("a" * 64) |
| 30 | result = get_all_branch_heads(tmp_path) |
| 31 | assert result == {"main": "a" * 64} |
| 32 | |
| 33 | def test_multiple_branches(self, tmp_path: pathlib.Path) -> None: |
| 34 | heads = _heads_dir(tmp_path) |
| 35 | (heads / "main").write_text("a" * 64) |
| 36 | (heads / "dev").write_text("b" * 64) |
| 37 | (heads / "feature").write_text("c" * 64) |
| 38 | result = get_all_branch_heads(tmp_path) |
| 39 | assert result == { |
| 40 | "main": "a" * 64, |
| 41 | "dev": "b" * 64, |
| 42 | "feature": "c" * 64, |
| 43 | } |
| 44 | |
| 45 | def test_whitespace_trimmed_from_ref_content(self, tmp_path: pathlib.Path) -> None: |
| 46 | heads = _heads_dir(tmp_path) |
| 47 | (heads / "main").write_text(" " + "a" * 64 + "\n") |
| 48 | result = get_all_branch_heads(tmp_path) |
| 49 | assert result["main"] == "a" * 64 |
| 50 | |
| 51 | def test_empty_ref_file_excluded(self, tmp_path: pathlib.Path) -> None: |
| 52 | heads = _heads_dir(tmp_path) |
| 53 | (heads / "main").write_text("") |
| 54 | result = get_all_branch_heads(tmp_path) |
| 55 | assert "main" not in result |
| 56 | |
| 57 | def test_subdirectory_in_heads_is_skipped(self, tmp_path: pathlib.Path) -> None: |
| 58 | """Namespaced branches (feature/foo) are represented as subdirs — only |
| 59 | files are branch heads; subdirs are namespace containers.""" |
| 60 | heads = _heads_dir(tmp_path) |
| 61 | (heads / "main").write_text("a" * 64) |
| 62 | sub = heads / "feature" |
| 63 | sub.mkdir() |
| 64 | (sub / "my-branch").write_text("b" * 64) |
| 65 | result = get_all_branch_heads(tmp_path) |
| 66 | # Only the flat file is returned (subdirs need recursive handling |
| 67 | # which is not the current contract — flat heads only). |
| 68 | assert "main" in result |
| 69 | assert "feature" not in result # the dir itself is not a head |
| 70 | |
| 71 | def test_returns_dict_type(self, tmp_path: pathlib.Path) -> None: |
| 72 | _heads_dir(tmp_path) |
| 73 | result = get_all_branch_heads(tmp_path) |
| 74 | assert isinstance(result, dict) |
| 75 | |
| 76 | def test_values_are_stripped_strings(self, tmp_path: pathlib.Path) -> None: |
| 77 | heads = _heads_dir(tmp_path) |
| 78 | cid = "f" * 64 |
| 79 | (heads / "release").write_text(cid + "\n") |
| 80 | result = get_all_branch_heads(tmp_path) |
| 81 | assert result["release"] == cid |
| 82 | assert isinstance(result["release"], str) |