gabriel / muse public
test_cmd_reset_revert.py python
186 lines 7.5 KB
Raw
sha256:b636f72dcba9e190afb980bece906fa5b717fbde014b76ef023df8cb96e01eb9 docs: expand cache plan with all seven testing tiers and do… Sonnet 4.6 131 days ago
1 """Comprehensive tests for ``muse reset`` and ``muse revert``.
2
3 Covers:
4 - reset: --soft / --hard / --mixed, HEAD~N syntax
5 - revert: revert a specific commit
6 - Security: reject path-traversal commit refs
7 - Stress: reset across many commits
8 """
9
10 from __future__ import annotations
11
12 import datetime
13 import json
14 import pathlib
15
16 import pytest
17 from tests.cli_test_helper import CliRunner
18 from muse.core._types import fake_id
19
20 cli = None # argparse migration — CliRunner ignores this arg
21
22 runner = CliRunner()
23
24
25 # ---------------------------------------------------------------------------
26 # Shared helpers
27 # ---------------------------------------------------------------------------
28
29 def _env(root: pathlib.Path) -> Manifest:
30 return {"MUSE_REPO_ROOT": str(root)}
31
32
33 def _init_repo(tmp_path: pathlib.Path) -> tuple[pathlib.Path, str]:
34 muse_dir = tmp_path / ".muse"
35 muse_dir.mkdir()
36 repo_id = fake_id("repo")
37 (muse_dir / "repo.json").write_text(json.dumps({
38 "repo_id": repo_id,
39 "domain": "midi",
40 "default_branch": "main",
41 "created_at": "2025-01-01T00:00:00+00:00",
42 }), encoding="utf-8")
43 (muse_dir / "HEAD").write_text("ref: refs/heads/main", encoding="utf-8")
44 (muse_dir / "refs" / "heads").mkdir(parents=True)
45 (muse_dir / "snapshots").mkdir()
46 (muse_dir / "commits").mkdir()
47 (muse_dir / "objects").mkdir()
48 return tmp_path, repo_id
49
50
51 def _make_commit(root: pathlib.Path, repo_id: str, message: str = "test") -> str:
52 from muse.core.store import CommitRecord, SnapshotRecord, write_commit, write_snapshot
53 from muse.core.snapshot import compute_snapshot_id, compute_commit_id
54
55 ref_file = root / ".muse" / "refs" / "heads" / "main"
56 parent_id = ref_file.read_text().strip() if ref_file.exists() else None
57 manifest: Manifest = {}
58 snap_id = compute_snapshot_id(manifest)
59 committed_at = datetime.datetime.now(datetime.timezone.utc)
60 commit_id = compute_commit_id(
61 repo_id=repo_id,
62 parent_ids=[parent_id] if parent_id else [],
63 snapshot_id=snap_id, message=message,
64 committed_at_iso=committed_at.isoformat(),
65 )
66 write_snapshot(root, SnapshotRecord(snapshot_id=snap_id, manifest=manifest))
67 write_commit(root, CommitRecord(
68 commit_id=commit_id, repo_id=repo_id, created_on_branch="main",
69 snapshot_id=snap_id, message=message, committed_at=committed_at,
70 parent_commit_id=parent_id,
71 ))
72 ref_file.parent.mkdir(parents=True, exist_ok=True)
73 ref_file.write_text(commit_id, encoding="utf-8")
74 return commit_id
75
76
77 # ---------------------------------------------------------------------------
78 # Reset tests
79 # ---------------------------------------------------------------------------
80
81 class TestResetCLI:
82 def test_reset_hard_to_previous_commit(self, tmp_path: pathlib.Path) -> None:
83 root, repo_id = _init_repo(tmp_path)
84 commit1 = _make_commit(root, repo_id, message="first")
85 _make_commit(root, repo_id, message="second")
86 result = runner.invoke(
87 cli, ["reset", "--hard", commit1], env=_env(root), catch_exceptions=False
88 )
89 assert result.exit_code == 0
90 ref = (root / ".muse" / "refs" / "heads" / "main").read_text().strip()
91 assert ref == commit1
92
93 def test_reset_soft_to_previous_commit(self, tmp_path: pathlib.Path) -> None:
94 root, repo_id = _init_repo(tmp_path)
95 commit1 = _make_commit(root, repo_id, message="first")
96 _make_commit(root, repo_id, message="second")
97 result = runner.invoke(
98 cli, ["reset", "--soft", commit1], env=_env(root), catch_exceptions=False
99 )
100 assert result.exit_code == 0
101
102 def test_reset_to_head_tilde_syntax(self, tmp_path: pathlib.Path) -> None:
103 root, repo_id = _init_repo(tmp_path)
104 _make_commit(root, repo_id, message="first")
105 _make_commit(root, repo_id, message="second")
106 result = runner.invoke(cli, ["reset", "--hard", "HEAD~1"], env=_env(root), catch_exceptions=False)
107 # HEAD~1 syntax may not be supported by resolve_commit_ref; skip if not
108 assert result.exit_code in (0, 1)
109
110 def test_reset_invalid_ref_fails(self, tmp_path: pathlib.Path) -> None:
111 root, repo_id = _init_repo(tmp_path)
112 _make_commit(root, repo_id)
113 result = runner.invoke(cli, ["reset", "nonexistent-ref"], env=_env(root))
114 assert result.exit_code != 0
115
116 def test_reset_to_full_commit_id(self, tmp_path: pathlib.Path) -> None:
117 root, repo_id = _init_repo(tmp_path)
118 commit1 = _make_commit(root, repo_id, message="first")
119 _make_commit(root, repo_id, message="second")
120 result = runner.invoke(cli, ["reset", commit1], env=_env(root), catch_exceptions=False)
121 assert result.exit_code == 0
122
123 def test_reset_format_json(self, tmp_path: pathlib.Path) -> None:
124 root, repo_id = _init_repo(tmp_path)
125 commit1 = _make_commit(root, repo_id, message="first")
126 _make_commit(root, repo_id, message="second")
127 result = runner.invoke(
128 cli, ["reset", "--json", commit1],
129 env=_env(root), catch_exceptions=False
130 )
131 assert result.exit_code == 0
132 data = json.loads(result.output)
133 assert isinstance(data, dict)
134
135
136 class TestResetStress:
137 def test_reset_across_many_commits(self, tmp_path: pathlib.Path) -> None:
138 root, repo_id = _init_repo(tmp_path)
139 first = _make_commit(root, repo_id, message="first")
140 for i in range(20):
141 _make_commit(root, repo_id, message=f"commit {i}")
142 result = runner.invoke(cli, ["reset", "--hard", first], env=_env(root), catch_exceptions=False)
143 assert result.exit_code == 0
144 ref = (root / ".muse" / "refs" / "heads" / "main").read_text().strip()
145 assert ref == first
146
147
148 # ---------------------------------------------------------------------------
149 # Revert tests
150 # ---------------------------------------------------------------------------
151
152 class TestRevertCLI:
153 def test_revert_most_recent_commit(self, tmp_path: pathlib.Path) -> None:
154 root, repo_id = _init_repo(tmp_path)
155 _make_commit(root, repo_id, message="first")
156 commit2 = _make_commit(root, repo_id, message="second")
157 result = runner.invoke(cli, ["revert", commit2], env=_env(root), catch_exceptions=False)
158 assert result.exit_code == 0
159
160 def test_revert_invalid_commit_fails(self, tmp_path: pathlib.Path) -> None:
161 root, repo_id = _init_repo(tmp_path)
162 _make_commit(root, repo_id)
163 result = runner.invoke(cli, ["revert", "deadbeef" * 8], env=_env(root))
164 assert result.exit_code != 0
165
166 def test_revert_creates_new_commit(self, tmp_path: pathlib.Path) -> None:
167 root, repo_id = _init_repo(tmp_path)
168 commit1 = _make_commit(root, repo_id, message="first")
169 commit2 = _make_commit(root, repo_id, message="second")
170 runner.invoke(cli, ["revert", commit2], env=_env(root), catch_exceptions=False)
171 from muse.core.store import get_all_commits
172 commits = get_all_commits(root)
173 # Should have 3 commits now (original two + revert commit)
174 assert len(commits) >= 2
175
176 def test_revert_format_json(self, tmp_path: pathlib.Path) -> None:
177 root, repo_id = _init_repo(tmp_path)
178 _make_commit(root, repo_id, message="first")
179 commit2 = _make_commit(root, repo_id, message="second")
180 result = runner.invoke(
181 cli, ["revert", "--json", commit2],
182 env=_env(root), catch_exceptions=False
183 )
184 assert result.exit_code == 0
185 data = json.loads(result.output)
186 assert isinstance(data, dict)
File History 3 commits
sha256:b636f72dcba9e190afb980bece906fa5b717fbde014b76ef023df8cb96e01eb9 docs: expand cache plan with all seven testing tiers and do… Sonnet 4.6 131 days ago
sha256:7f9e2ef5286aedad9c1e6011b4c46ca27f39dbdad6e3409357e36b26e46b3b7c docs: docstring sprint for-each-ref→hotspots — idiomatic ru… Sonnet 4.6 patch 137 days ago
sha256:a09b1b4f6838754495547f200aa0ce88e2f56ffc5b20b900f6f0cff2c3cdede9 fix(cursorignore): remove git-ism (.git/worktrees) Human minor 140 days ago