"""TDD — granular unstage summary for muse code reset. RS-1 Unstage all: output counts dirs, files, and symbols separately. RS-2 Unstage specific dir: output says "1 directory", not "1 file(s)". RS-3 Unstage specific file: output says "1 file", not "1 file(s)". RS-4 Mixed unstage (dir + file + symbol): all three counts appear. RS-5 Dry-run unstage all: preview message uses same granular format. RS-6 Dry-run unstage specific: preview message uses same granular format. """ from __future__ import annotations import datetime import json import pathlib from collections.abc import Mapping import pytest from tests.cli_test_helper import CliRunner from muse.core.paths import muse_dir, ref_path from muse.core.object_store import write_object from muse.core.ids import hash_commit, hash_snapshot from muse.core.commits import CommitRecord, write_commit from muse.core.snapshots import SnapshotRecord, write_snapshot from muse.core.types import blob_id runner = CliRunner() # --------------------------------------------------------------------------- # Helpers # --------------------------------------------------------------------------- def _init_repo(path: pathlib.Path) -> pathlib.Path: dot = muse_dir(path) for d in ("commits", "snapshots", "objects", "refs/heads", "code"): (dot / d).mkdir(parents=True, exist_ok=True) (dot / "HEAD").write_text("ref: refs/heads/main", encoding="utf-8") (dot / "repo.json").write_text( json.dumps({"repo_id": "reset-test", "domain": "code"}), encoding="utf-8", ) return path def _make_commit(root: pathlib.Path, files: Mapping[str, bytes]) -> str: manifest: dict[str, str] = {} for rel, content in files.items(): oid = blob_id(content) write_object(root, oid, content) manifest[rel] = oid snap_id = hash_snapshot(manifest) write_snapshot(root, SnapshotRecord(snapshot_id=snap_id, manifest=manifest)) committed_at = datetime.datetime.now(datetime.timezone.utc) commit_id = hash_commit( parent_ids=[], snapshot_id=snap_id, message="init", committed_at_iso=committed_at.isoformat(), ) write_commit(root, CommitRecord( commit_id=commit_id, branch="main", snapshot_id=snap_id, message="init", committed_at=committed_at, )) ref_path(root, "main").write_text(commit_id, encoding="utf-8") return commit_id def _env(root: pathlib.Path) -> Mapping[str, str]: return {"MUSE_REPO_ROOT": str(root)} def _stage_dir(root: pathlib.Path, dirname: str) -> None: (root / dirname).mkdir(exist_ok=True) runner.invoke(None, ["code", "add", dirname + "/"], env=_env(root)) def _stage_file(root: pathlib.Path, filename: str, content: bytes = b"x") -> None: (root / filename).write_bytes(content) runner.invoke(None, ["code", "add", filename], env=_env(root)) # --------------------------------------------------------------------------- # RS-1 Unstage all: granular counts # --------------------------------------------------------------------------- class TestResetAllGranular: def test_unstage_all_dirs_says_directory( self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch ) -> None: """Unstage all when only a dir is staged: output must say 'directory'.""" root = _init_repo(tmp_path) _make_commit(root, {"readme.md": b"# hi\n"}) monkeypatch.chdir(root) _stage_dir(root, "mydir") result = runner.invoke(None, ["code", "reset"], env=_env(root)) assert result.exit_code == 0 assert "directory" in result.output assert "file" not in result.output, ( f"'file' must not appear when only a dir was staged:\n{result.output}" ) def test_unstage_all_files_says_file( self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch ) -> None: """Unstage all when only a file is staged: output must say 'file'.""" root = _init_repo(tmp_path) _make_commit(root, {"readme.md": b"# hi\n"}) monkeypatch.chdir(root) _stage_file(root, "new.py") result = runner.invoke(None, ["code", "reset"], env=_env(root)) assert result.exit_code == 0 assert "file" in result.output assert "directory" not in result.output def test_unstage_all_no_file_s_plural( self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch ) -> None: """The old 'file(s)' format must be gone.""" root = _init_repo(tmp_path) _make_commit(root, {"readme.md": b"# hi\n"}) monkeypatch.chdir(root) _stage_file(root, "new.py") result = runner.invoke(None, ["code", "reset"], env=_env(root)) assert "file(s)" not in result.output # --------------------------------------------------------------------------- # RS-2 Unstage specific dir: says 'directory' # --------------------------------------------------------------------------- class TestResetSpecificDir: def test_unstage_dir_says_directory( self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch ) -> None: root = _init_repo(tmp_path) _make_commit(root, {"readme.md": b"# hi\n"}) monkeypatch.chdir(root) _stage_dir(root, "mydir") result = runner.invoke(None, ["code", "reset", "mydir/"], env=_env(root)) assert result.exit_code == 0 assert "directory" in result.output assert "file" not in result.output # --------------------------------------------------------------------------- # RS-3 Unstage specific file: says 'file' (singular) # --------------------------------------------------------------------------- class TestResetSpecificFile: def test_unstage_one_file_says_singular( self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch ) -> None: root = _init_repo(tmp_path) _make_commit(root, {"readme.md": b"# hi\n"}) monkeypatch.chdir(root) _stage_file(root, "new.py") result = runner.invoke(None, ["code", "reset", "new.py"], env=_env(root)) assert result.exit_code == 0 assert "1 file" in result.output assert "file(s)" not in result.output # --------------------------------------------------------------------------- # RS-4 Mixed unstage: all three kinds appear # --------------------------------------------------------------------------- class TestResetMixed: def test_mixed_unstage_shows_dir_file_counts( self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch ) -> None: """Staging a dir + a file then resetting all must report both counts.""" root = _init_repo(tmp_path) _make_commit(root, {"readme.md": b"# hi\n"}) monkeypatch.chdir(root) _stage_dir(root, "mydir") _stage_file(root, "new.py") result = runner.invoke(None, ["code", "reset"], env=_env(root)) assert result.exit_code == 0 assert "directory" in result.output assert "file" in result.output # --------------------------------------------------------------------------- # RS-5 Dry-run unstage all: uses granular format # --------------------------------------------------------------------------- class TestResetDryRun: def test_dry_run_all_granular( self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch ) -> None: root = _init_repo(tmp_path) _make_commit(root, {"readme.md": b"# hi\n"}) monkeypatch.chdir(root) _stage_dir(root, "mydir") _stage_file(root, "new.py") result = runner.invoke(None, ["code", "reset", "-n"], env=_env(root)) assert result.exit_code == 0 assert "directory" in result.output assert "file" in result.output assert "file(s)" not in result.output