|
| 1 | +"""Tests for --dry-run on apc collect (#25) and apc sync (#24). |
| 2 | +
|
| 3 | +apc collect --dry-run: previews what would be collected without writing. |
| 4 | +apc sync --dry-run: previews file paths per tool without writing. |
| 5 | +""" |
| 6 | + |
| 7 | +import sys |
| 8 | +import unittest |
| 9 | +from pathlib import Path |
| 10 | +from unittest.mock import MagicMock, patch |
| 11 | + |
| 12 | +sys.path.insert(0, str(Path(__file__).parent.parent / "src")) |
| 13 | + |
| 14 | +from click.testing import CliRunner |
| 15 | + |
| 16 | +# --------------------------------------------------------------------------- |
| 17 | +# Helpers |
| 18 | +# --------------------------------------------------------------------------- |
| 19 | + |
| 20 | + |
| 21 | +def _cli(): |
| 22 | + from main import cli |
| 23 | + |
| 24 | + return cli |
| 25 | + |
| 26 | + |
| 27 | +def _runner(): |
| 28 | + return CliRunner() |
| 29 | + |
| 30 | + |
| 31 | +def _mock_extractor(skills=None, mcp=None, memory=None): |
| 32 | + """Return a MagicMock extractor with canned data.""" |
| 33 | + ext = MagicMock() |
| 34 | + ext.extract_skills.return_value = skills or [] |
| 35 | + ext.extract_mcp_servers.return_value = mcp or [] |
| 36 | + ext.extract_memory.return_value = memory or [] |
| 37 | + return ext |
| 38 | + |
| 39 | + |
| 40 | +# --------------------------------------------------------------------------- |
| 41 | +# apc collect --dry-run (#25) |
| 42 | +# --------------------------------------------------------------------------- |
| 43 | + |
| 44 | + |
| 45 | +class TestCollectDryRun(unittest.TestCase): |
| 46 | + """collect --dry-run must preview without touching the cache.""" |
| 47 | + |
| 48 | + def _invoke(self, skills=None, mcp=None, memory=None, extra_args=None): |
| 49 | + import tempfile |
| 50 | + |
| 51 | + with tempfile.TemporaryDirectory() as td: |
| 52 | + td = Path(td) |
| 53 | + cache_dir = td / ".apc" / "cache" |
| 54 | + extractor = _mock_extractor( |
| 55 | + skills=skills or [{"name": "pdf", "source_tool": "claude-code", "body": "# PDF"}], |
| 56 | + mcp=mcp |
| 57 | + or [{"name": "test-mcp", "source_tool": "cursor", "command": "npx", "args": []}], |
| 58 | + memory=memory or [], |
| 59 | + ) |
| 60 | + args = ["collect", "--dry-run", "--yes"] + (extra_args or []) |
| 61 | + with ( |
| 62 | + patch("collect.detect_installed_tools", return_value=["claude-code"]), |
| 63 | + patch("collect.get_extractor", return_value=extractor), |
| 64 | + patch("cache.get_cache_dir", return_value=cache_dir), |
| 65 | + ): |
| 66 | + result = _runner().invoke(_cli(), args) |
| 67 | + return result, cache_dir |
| 68 | + |
| 69 | + def test_dry_run_flag_accepted(self): |
| 70 | + result, _ = self._invoke() |
| 71 | + assert result.exit_code == 0, result.output |
| 72 | + |
| 73 | + def test_dry_run_shows_cache_paths(self): |
| 74 | + result, _ = self._invoke() |
| 75 | + # Paths may wrap across lines in rich output; check for filename substrings |
| 76 | + flat = result.output.replace("\n", " ") |
| 77 | + assert "skills.j" in flat # skills.json (may wrap) |
| 78 | + assert "mcp.json" in flat |
| 79 | + assert "memory.j" in flat # memory.json (may wrap) |
| 80 | + |
| 81 | + def test_dry_run_shows_skill_count(self): |
| 82 | + result, _ = self._invoke( |
| 83 | + skills=[ |
| 84 | + {"name": "pdf", "source_tool": "claude-code", "body": "# PDF"}, |
| 85 | + {"name": "sk", "source_tool": "claude-code", "body": "# SK"}, |
| 86 | + ] |
| 87 | + ) |
| 88 | + assert "2 skills" in result.output |
| 89 | + |
| 90 | + def test_dry_run_shows_mcp_count(self): |
| 91 | + result, _ = self._invoke( |
| 92 | + mcp=[ |
| 93 | + {"name": "mcp-a", "source_tool": "cursor", "command": "npx", "args": []}, |
| 94 | + {"name": "mcp-b", "source_tool": "cursor", "command": "npx", "args": []}, |
| 95 | + {"name": "mcp-c", "source_tool": "cursor", "command": "npx", "args": []}, |
| 96 | + ] |
| 97 | + ) |
| 98 | + assert "3 MCP" in result.output |
| 99 | + |
| 100 | + def test_dry_run_lists_skill_names(self): |
| 101 | + result, _ = self._invoke( |
| 102 | + skills=[{"name": "pdf", "source_tool": "claude-code", "body": "# PDF"}] |
| 103 | + ) |
| 104 | + assert "pdf" in result.output |
| 105 | + |
| 106 | + def test_dry_run_does_not_write_cache(self): |
| 107 | + """Cache files must NOT be created when --dry-run is used.""" |
| 108 | + result, cache_dir = self._invoke() |
| 109 | + assert result.exit_code == 0, result.output |
| 110 | + assert not (cache_dir / "skills.json").exists(), "skills.json written in dry-run" |
| 111 | + assert not (cache_dir / "mcp.json").exists(), "mcp.json written in dry-run" |
| 112 | + assert not (cache_dir / "memory.json").exists(), "memory.json written in dry-run" |
| 113 | + |
| 114 | + def test_dry_run_no_files_written_message(self): |
| 115 | + result, _ = self._invoke(skills=[], mcp=[], memory=[]) |
| 116 | + assert "No files written" in result.output or "dry-run" in result.output.lower() |
| 117 | + |
| 118 | + def test_dry_run_memory_entries_listed(self): |
| 119 | + mem = [{"source_tool": "claude-code", "source_file": "CLAUDE.md", "content": "Some rule"}] |
| 120 | + result, _ = self._invoke(memory=mem) |
| 121 | + assert "claude-code" in result.output or "CLAUDE.md" in result.output |
| 122 | + |
| 123 | + |
| 124 | +# --------------------------------------------------------------------------- |
| 125 | +# apc sync --dry-run (#24) |
| 126 | +# --------------------------------------------------------------------------- |
| 127 | + |
| 128 | + |
| 129 | +class TestSyncDryRun(unittest.TestCase): |
| 130 | + """sync --dry-run must preview file paths per tool without writing.""" |
| 131 | + |
| 132 | + def _invoke_sync_dry(self, tools="cursor"): |
| 133 | + mock_bundle = { |
| 134 | + "skills": [{"name": "pdf", "source_tool": "claude-code", "body": "# PDF"}], |
| 135 | + "mcp_servers": [{"name": "test-mcp", "command": "npx", "args": []}], |
| 136 | + "memory": [], |
| 137 | + } |
| 138 | + with ( |
| 139 | + patch("main.load_local_bundle", return_value=mock_bundle), |
| 140 | + patch("main.count_installed_skills", return_value=1), |
| 141 | + patch("main.resolve_target_tools", return_value=[tools]), |
| 142 | + ): |
| 143 | + return _runner().invoke(_cli(), ["sync", "--dry-run", "--yes"]) |
| 144 | + |
| 145 | + def test_dry_run_flag_accepted(self): |
| 146 | + result = self._invoke_sync_dry() |
| 147 | + assert result.exit_code == 0, result.output |
| 148 | + |
| 149 | + def test_dry_run_shows_no_files_written(self): |
| 150 | + result = self._invoke_sync_dry() |
| 151 | + assert "No files written" in result.output or "dry-run" in result.output.lower() |
| 152 | + |
| 153 | + def test_dry_run_shows_tool_name(self): |
| 154 | + result = self._invoke_sync_dry(tools="cursor") |
| 155 | + assert "cursor" in result.output |
| 156 | + |
| 157 | + def test_dry_run_does_not_call_sync_all(self): |
| 158 | + """sync_all must not be called in dry-run mode.""" |
| 159 | + mock_bundle = { |
| 160 | + "skills": [{"name": "pdf", "source_tool": "claude-code", "body": "# PDF"}], |
| 161 | + "mcp_servers": [], |
| 162 | + "memory": [], |
| 163 | + } |
| 164 | + with ( |
| 165 | + patch("main.load_local_bundle", return_value=mock_bundle), |
| 166 | + patch("main.count_installed_skills", return_value=1), |
| 167 | + patch("main.resolve_target_tools", return_value=["cursor"]), |
| 168 | + patch("main.sync_all") as mock_sync, |
| 169 | + ): |
| 170 | + _runner().invoke(_cli(), ["sync", "--dry-run", "--yes"]) |
| 171 | + |
| 172 | + mock_sync.assert_not_called() |
0 commit comments