|
| 1 | +import json |
| 2 | +import re |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import click |
| 6 | +import pytest |
| 7 | + |
| 8 | +from repowise.cli import mcp_config |
| 9 | + |
| 10 | + |
| 11 | +def _repowise_entry(repo_path: Path) -> dict: |
| 12 | + return mcp_config.generate_mcp_config(repo_path)["mcpServers"] |
| 13 | + |
| 14 | + |
| 15 | +def test_save_root_mcp_config_creates_missing_file(tmp_path: Path) -> None: |
| 16 | + config_path = mcp_config.save_root_mcp_config(tmp_path) |
| 17 | + |
| 18 | + saved = json.loads(config_path.read_text(encoding="utf-8")) |
| 19 | + assert "repowise" in saved["mcpServers"] |
| 20 | + |
| 21 | + |
| 22 | +def test_save_root_mcp_config_merges_valid_existing_file(tmp_path: Path) -> None: |
| 23 | + config_path = tmp_path / ".mcp.json" |
| 24 | + config_path.write_text( |
| 25 | + json.dumps( |
| 26 | + { |
| 27 | + "mcpServers": {"other": {"command": "other"}}, |
| 28 | + "custom": {"preserved": True}, |
| 29 | + } |
| 30 | + ), |
| 31 | + encoding="utf-8", |
| 32 | + ) |
| 33 | + |
| 34 | + mcp_config.save_root_mcp_config(tmp_path) |
| 35 | + |
| 36 | + saved = json.loads(config_path.read_text(encoding="utf-8")) |
| 37 | + assert saved["mcpServers"]["other"] == {"command": "other"} |
| 38 | + assert "repowise" in saved["mcpServers"] |
| 39 | + assert saved["custom"] == {"preserved": True} |
| 40 | + |
| 41 | + |
| 42 | +def test_save_root_mcp_config_rejects_invalid_existing_file(tmp_path: Path) -> None: |
| 43 | + config_path = tmp_path / ".mcp.json" |
| 44 | + original = '{\n "mcpServers": {},\n}\n' |
| 45 | + config_path.write_text(original, encoding="utf-8") |
| 46 | + |
| 47 | + with pytest.raises(click.ClickException, match=re.escape(str(config_path))): |
| 48 | + mcp_config.save_root_mcp_config(tmp_path) |
| 49 | + |
| 50 | + assert config_path.read_text(encoding="utf-8") == original |
| 51 | + |
| 52 | + |
| 53 | +def test_merge_mcp_entry_creates_missing_file(tmp_path: Path) -> None: |
| 54 | + config_path = tmp_path / "settings.json" |
| 55 | + |
| 56 | + assert mcp_config._merge_mcp_entry(config_path, _repowise_entry(tmp_path)) |
| 57 | + |
| 58 | + saved = json.loads(config_path.read_text(encoding="utf-8")) |
| 59 | + assert "repowise" in saved["mcpServers"] |
| 60 | + |
| 61 | + |
| 62 | +def test_merge_mcp_entry_merges_valid_existing_file(tmp_path: Path) -> None: |
| 63 | + config_path = tmp_path / "settings.json" |
| 64 | + config_path.write_text( |
| 65 | + json.dumps( |
| 66 | + { |
| 67 | + "mcpServers": {"existing": {"command": "existing"}}, |
| 68 | + "permissions": {"allow": ["Bash(git status:*)"]}, |
| 69 | + } |
| 70 | + ), |
| 71 | + encoding="utf-8", |
| 72 | + ) |
| 73 | + |
| 74 | + assert mcp_config._merge_mcp_entry(config_path, _repowise_entry(tmp_path)) |
| 75 | + |
| 76 | + saved = json.loads(config_path.read_text(encoding="utf-8")) |
| 77 | + assert saved["mcpServers"]["existing"] == {"command": "existing"} |
| 78 | + assert "repowise" in saved["mcpServers"] |
| 79 | + assert saved["permissions"] == {"allow": ["Bash(git status:*)"]} |
| 80 | + |
| 81 | + |
| 82 | +def test_merge_mcp_entry_rejects_invalid_existing_file(tmp_path: Path) -> None: |
| 83 | + config_path = tmp_path / "settings.json" |
| 84 | + original = '{\n "permissions": {},\n}\n' |
| 85 | + config_path.write_text(original, encoding="utf-8") |
| 86 | + |
| 87 | + with pytest.raises(click.ClickException, match=re.escape(str(config_path))): |
| 88 | + mcp_config._merge_mcp_entry(config_path, _repowise_entry(tmp_path)) |
| 89 | + |
| 90 | + assert config_path.read_text(encoding="utf-8") == original |
| 91 | + |
| 92 | + |
| 93 | +def test_install_claude_code_hooks_creates_missing_file( |
| 94 | + tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 95 | +) -> None: |
| 96 | + monkeypatch.setattr(Path, "home", lambda: tmp_path) |
| 97 | + |
| 98 | + settings_path = mcp_config.install_claude_code_hooks() |
| 99 | + |
| 100 | + assert settings_path == tmp_path / ".claude" / "settings.json" |
| 101 | + saved = json.loads(settings_path.read_text(encoding="utf-8")) |
| 102 | + assert "PreToolUse" in saved["hooks"] |
| 103 | + assert "PostToolUse" in saved["hooks"] |
| 104 | + |
| 105 | + |
| 106 | +def test_install_claude_code_hooks_merges_valid_existing_file( |
| 107 | + tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 108 | +) -> None: |
| 109 | + monkeypatch.setattr(Path, "home", lambda: tmp_path) |
| 110 | + settings_path = tmp_path / ".claude" / "settings.json" |
| 111 | + settings_path.parent.mkdir(parents=True) |
| 112 | + settings_path.write_text( |
| 113 | + json.dumps( |
| 114 | + { |
| 115 | + "permissions": {"allow": ["Bash(git status:*)"]}, |
| 116 | + "hooks": { |
| 117 | + "PreToolUse": [ |
| 118 | + { |
| 119 | + "matcher": "Read", |
| 120 | + "hooks": [{"type": "command", "command": "echo read"}], |
| 121 | + } |
| 122 | + ] |
| 123 | + }, |
| 124 | + } |
| 125 | + ), |
| 126 | + encoding="utf-8", |
| 127 | + ) |
| 128 | + |
| 129 | + assert mcp_config.install_claude_code_hooks() == settings_path |
| 130 | + |
| 131 | + saved = json.loads(settings_path.read_text(encoding="utf-8")) |
| 132 | + assert saved["permissions"] == {"allow": ["Bash(git status:*)"]} |
| 133 | + assert saved["hooks"]["PreToolUse"][0]["matcher"] == "Read" |
| 134 | + assert any( |
| 135 | + hook["command"] == "repowise augment" |
| 136 | + for entry in saved["hooks"]["PreToolUse"] |
| 137 | + for hook in entry["hooks"] |
| 138 | + ) |
| 139 | + assert "PostToolUse" in saved["hooks"] |
| 140 | + |
| 141 | + |
| 142 | +def test_install_claude_code_hooks_rejects_invalid_existing_file( |
| 143 | + tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 144 | +) -> None: |
| 145 | + monkeypatch.setattr(Path, "home", lambda: tmp_path) |
| 146 | + settings_path = tmp_path / ".claude" / "settings.json" |
| 147 | + settings_path.parent.mkdir(parents=True) |
| 148 | + original = '{\n "permissions": {},\n}\n' |
| 149 | + settings_path.write_text(original, encoding="utf-8") |
| 150 | + |
| 151 | + with pytest.raises(click.ClickException, match=re.escape(str(settings_path))): |
| 152 | + mcp_config.install_claude_code_hooks() |
| 153 | + |
| 154 | + assert settings_path.read_text(encoding="utf-8") == original |
0 commit comments