|
| 1 | +"""Worktree parser and consumer contracts. Source: ADR-1079.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from mcp_server import doctor |
| 8 | +from mcp_server.handlers import check_setup |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.parametrize("output", [None, "", "HEAD abc\0\0"]) |
| 12 | +def test_unavailable_or_empty_listing_contract(monkeypatch, output): |
| 13 | + monkeypatch.setattr(doctor, "run_with_hard_timeout", lambda *a, **k: output) |
| 14 | + check = doctor._worktree_locations() |
| 15 | + assert check.optional |
| 16 | + if output is None: |
| 17 | + assert doctor._worktree_list() is None |
| 18 | + assert check.ok is False |
| 19 | + assert "Unable to inspect worktree locations" in check.detail |
| 20 | + assert "not a Git checkout, Git unavailable, or command failed" in check.detail |
| 21 | + assert "git worktree list --porcelain -z" in check.fix |
| 22 | + else: |
| 23 | + assert doctor._worktree_list() == [] |
| 24 | + assert check.ok |
| 25 | + assert check.detail == "no registered worktrees" |
| 26 | + assert check.fix == "" |
| 27 | + |
| 28 | + |
| 29 | +def test_nul_parser_preserves_records_markers_and_unicode(monkeypatch, tmp_path): |
| 30 | + main = str(tmp_path / "répo") |
| 31 | + linked = str(tmp_path / "deleted") |
| 32 | + output = f"worktree {main}\0bare\0\0worktree {linked}\0prunable gone\0\0" |
| 33 | + calls = [] |
| 34 | + |
| 35 | + def execute(argv, **kwargs): |
| 36 | + calls.append(argv) |
| 37 | + return output |
| 38 | + |
| 39 | + monkeypatch.setattr(doctor, "run_with_hard_timeout", execute) |
| 40 | + assert doctor._worktree_list() == [ |
| 41 | + {"path": main, "bare": True, "prunable": False}, |
| 42 | + {"path": linked, "bare": False, "prunable": True}, |
| 43 | + ] |
| 44 | + assert calls == [["git", "worktree", "list", "--porcelain", "-z"]] |
| 45 | + |
| 46 | + |
| 47 | +def test_orphan_markers_are_ignored(monkeypatch, tmp_path): |
| 48 | + output = f"bare\0prunable gone\0worktree {tmp_path}\0HEAD abc\0\0" |
| 49 | + monkeypatch.setattr(doctor, "run_with_hard_timeout", lambda *a, **k: output) |
| 50 | + assert doctor._worktree_list() == [ |
| 51 | + {"path": str(tmp_path), "bare": False, "prunable": False} |
| 52 | + ] |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.parametrize("outside", [False, True]) |
| 56 | +def test_real_worktree_check_cli_and_mcp_contract( |
| 57 | + monkeypatch, tmp_path, capsys, outside |
| 58 | +): |
| 59 | + location = tmp_path / ("outside" if outside else ".Codex/worktrees/good") |
| 60 | + entries = [{"path": str(tmp_path)}, {"path": str(location)}] |
| 61 | + monkeypatch.setattr(doctor, "_worktree_list", lambda: entries) |
| 62 | + monkeypatch.setattr(doctor, "active_checks", lambda: [doctor._worktree_locations]) |
| 63 | + monkeypatch.setattr( |
| 64 | + check_setup, "active_checks", lambda: [doctor._worktree_locations] |
| 65 | + ) |
| 66 | + monkeypatch.setattr(doctor.sys, "argv", ["doctor"]) |
| 67 | + assert doctor.run() == 0 |
| 68 | + output = capsys.readouterr().out |
| 69 | + check = doctor._worktree_locations() |
| 70 | + assert check.ok is not outside |
| 71 | + assert ("[WARN]" in output) is outside |
| 72 | + assert "[FAIL]" not in output |
| 73 | + if outside: |
| 74 | + assert str(location.resolve()) in output |
| 75 | + assert check.fix in output |
| 76 | + assert ".Codex/worktrees/<name>/" in check.fix |
| 77 | + assert ".claude/worktrees/<name>/" in check.fix |
| 78 | + else: |
| 79 | + assert check.fix == "" |
| 80 | + assert "All checks passed" in output |
| 81 | + result = asyncio.run(check_setup.handler()) |
| 82 | + assert result["ready"] is True and result["fixes_needed"] == 0 |
| 83 | + assert result["checks"] == [ |
| 84 | + { |
| 85 | + "name": check.name, |
| 86 | + "ok": check.ok, |
| 87 | + "optional": True, |
| 88 | + "detail": check.detail, |
| 89 | + "fix_command": check.fix, |
| 90 | + } |
| 91 | + ] |
| 92 | + |
| 93 | + |
| 94 | +def test_registries_include_optional_worktree_check(): |
| 95 | + assert doctor._worktree_locations in doctor.CHECKS |
| 96 | + assert doctor._worktree_locations in doctor.SQLITE_CHECKS |
| 97 | + |
| 98 | + |
| 99 | +def test_bare_and_exact_root_details(tmp_path): |
| 100 | + ok, detail = doctor._worktree_classification( |
| 101 | + [{"path": str(tmp_path), "bare": True}] |
| 102 | + ) |
| 103 | + assert ok and detail == "bare repository — rule not applicable" |
| 104 | + root = (tmp_path / ".Codex/worktrees").resolve() |
| 105 | + assert doctor._under(root, root) |
| 106 | + ok, detail = doctor._worktree_classification( |
| 107 | + [ |
| 108 | + {"path": str(tmp_path)}, |
| 109 | + {"path": str(root)}, |
| 110 | + {"path": str(root)}, |
| 111 | + ] |
| 112 | + ) |
| 113 | + assert ok and str(root) in detail |
| 114 | + assert str((tmp_path / ".claude/worktrees").resolve()) in detail |
| 115 | + |
| 116 | + |
| 117 | +def test_unavailable_listing_warns_without_blocking_consumers(monkeypatch, capsys): |
| 118 | + monkeypatch.setattr(doctor, "run_with_hard_timeout", lambda *a, **k: None) |
| 119 | + monkeypatch.setattr(doctor, "active_checks", lambda: [doctor._worktree_locations]) |
| 120 | + monkeypatch.setattr( |
| 121 | + check_setup, "active_checks", lambda: [doctor._worktree_locations] |
| 122 | + ) |
| 123 | + monkeypatch.setattr(doctor.sys, "argv", ["doctor"]) |
| 124 | + assert doctor.run() == 0 |
| 125 | + output = capsys.readouterr().out |
| 126 | + assert "[WARN]" in output and "[FAIL]" not in output |
| 127 | + assert "Unable to inspect worktree locations" in output |
| 128 | + assert "git worktree list --porcelain -z" in output |
| 129 | + result = asyncio.run(check_setup.handler()) |
| 130 | + assert result["ready"] and result["fixes_needed"] == 0 |
| 131 | + check = result["checks"][0] |
| 132 | + assert check["optional"] and not check["ok"] |
| 133 | + assert "Unable to inspect worktree locations" in check["detail"] |
| 134 | + assert "git worktree list --porcelain -z" in check["fix_command"] |
0 commit comments