"""muse#54 item 1 -- relative time syntax for @{...} reflog refs. `@{1.hour.ago}`, `@{2.days.ago}`, `@{yesterday}` resolve to the most recent reflog entry at or before that point in time, exercised both at the `resolve_reflog_ref` core level (with fabricated timestamps so the tests are deterministic) and through the CLI (`muse rev-parse`). """ from __future__ import annotations import datetime import json import pathlib import pytest from tests.cli_test_helper import CliRunner from muse.core.types import fake_id, Manifest, NULL_COMMIT_ID from muse.core.paths import muse_dir, logs_dir, reflog_heads_dir, reflog_branch_path from muse.core.reflog import resolve_reflog_ref cli = None runner = CliRunner() def _env(root: pathlib.Path) -> Manifest: return {"MUSE_REPO_ROOT": str(root)} def _run(root: pathlib.Path, *args: str): return runner.invoke(cli, list(args), env=_env(root)) def _init_repo(tmp_path: pathlib.Path) -> tuple[pathlib.Path, str]: dot_muse = muse_dir(tmp_path) dot_muse.mkdir() repo_id = fake_id("repo") (dot_muse / "repo.json").write_text(json.dumps({ "repo_id": repo_id, "domain": "code", "default_branch": "main", "created_at": "2025-01-01T00:00:00+00:00", }), encoding="utf-8") (dot_muse / "HEAD").write_text("ref: refs/heads/main\n", encoding="utf-8") (dot_muse / "refs" / "heads").mkdir(parents=True) (dot_muse / "snapshots").mkdir() (dot_muse / "commits").mkdir() (dot_muse / "objects").mkdir() return tmp_path, repo_id def _write_raw_entry(root: pathlib.Path, path: pathlib.Path, old_id: str, new_id: str, when: datetime.datetime, operation: str) -> None: path.parent.mkdir(parents=True, exist_ok=True) ts = int(when.timestamp()) line = f"{old_id} {new_id} gabriel {ts} +0000\t{operation}\n" with path.open("a", encoding="utf-8") as f: f.write(line) def _fake_commit_id(n: int) -> str: return f"{n:064x}" class TestTimeAgoResolution: def test_hour_ago_finds_entry_before_cutoff(self, tmp_path: pathlib.Path) -> None: root, repo_id = _init_repo(tmp_path) head_log = logs_dir(root) / "HEAD" now = datetime.datetime.now(datetime.timezone.utc) c1 = _fake_commit_id(1) c2 = _fake_commit_id(2) # c1 was current 3 hours ago; c2 became current 10 minutes ago. _write_raw_entry(root, head_log, NULL_COMMIT_ID, c1, now - datetime.timedelta(hours=3), "commit: c1") _write_raw_entry(root, head_log, c1, c2, now - datetime.timedelta(minutes=10), "commit: c2") # "1 hour ago" -> most recent entry at or before (now - 1h) -> c1, # since c2 only became current 10 minutes ago (after the cutoff). result = resolve_reflog_ref("@{1.hour.ago}", root) assert result == c1 def test_minutes_ago_finds_most_recent_entry(self, tmp_path: pathlib.Path) -> None: root, repo_id = _init_repo(tmp_path) head_log = logs_dir(root) / "HEAD" now = datetime.datetime.now(datetime.timezone.utc) c1 = _fake_commit_id(1) c2 = _fake_commit_id(2) _write_raw_entry(root, head_log, NULL_COMMIT_ID, c1, now - datetime.timedelta(hours=2), "commit: c1") _write_raw_entry(root, head_log, c1, c2, now - datetime.timedelta(hours=1), "commit: c2") # "30 minutes ago" is after both entries were written -> most recent # entry at or before that cutoff is c2. result = resolve_reflog_ref("@{30.minute.ago}", root) assert result == c2 def test_yesterday_finds_entry_before_todays_midnight(self, tmp_path: pathlib.Path) -> None: root, repo_id = _init_repo(tmp_path) head_log = logs_dir(root) / "HEAD" now = datetime.datetime.now(datetime.timezone.utc) today_midnight = now.replace(hour=0, minute=0, second=0, microsecond=0) c1 = _fake_commit_id(1) c2 = _fake_commit_id(2) _write_raw_entry(root, head_log, NULL_COMMIT_ID, c1, today_midnight - datetime.timedelta(days=1), "commit: c1") _write_raw_entry(root, head_log, c1, c2, now, "commit: c2") result = resolve_reflog_ref("@{yesterday}", root) assert result == c1 def test_no_entry_old_enough_returns_none(self, tmp_path: pathlib.Path) -> None: root, repo_id = _init_repo(tmp_path) head_log = logs_dir(root) / "HEAD" now = datetime.datetime.now(datetime.timezone.utc) c1 = _fake_commit_id(1) _write_raw_entry(root, head_log, NULL_COMMIT_ID, c1, now, "commit: c1") result = resolve_reflog_ref("@{1.week.ago}", root) assert result is None def test_named_branch_time_spec(self, tmp_path: pathlib.Path) -> None: root, repo_id = _init_repo(tmp_path) branch_log = reflog_branch_path(root, "main") now = datetime.datetime.now(datetime.timezone.utc) c1 = _fake_commit_id(1) _write_raw_entry(root, branch_log, NULL_COMMIT_ID, c1, now - datetime.timedelta(hours=5), "commit: c1") result = resolve_reflog_ref("main@{1.hour.ago}", root) assert result == c1 def test_unrecognized_spec_returns_none(self, tmp_path: pathlib.Path) -> None: root, repo_id = _init_repo(tmp_path) head_log = logs_dir(root) / "HEAD" now = datetime.datetime.now(datetime.timezone.utc) c1 = _fake_commit_id(1) _write_raw_entry(root, head_log, NULL_COMMIT_ID, c1, now, "commit: c1") result = resolve_reflog_ref("@{not.a.real.spec}", root) assert result is None def test_plain_index_still_works(self, tmp_path: pathlib.Path) -> None: """Non-regression: the pre-existing @{N} integer form must still work after broadening the regex to accept time specs.""" root, repo_id = _init_repo(tmp_path) head_log = logs_dir(root) / "HEAD" now = datetime.datetime.now(datetime.timezone.utc) c1 = _fake_commit_id(1) _write_raw_entry(root, head_log, NULL_COMMIT_ID, c1, now, "commit: c1") result = resolve_reflog_ref("@{0}", root) assert result == c1 class TestTimeAgoViaCli: def test_rev_parse_accepts_time_spec(self, tmp_path: pathlib.Path) -> None: root, repo_id = _init_repo(tmp_path) (root / "f.txt").write_text("v1\n") add = _run(root, "code", "add", "f.txt") assert add.exit_code == 0, add.output commit = _run(root, "commit", "-m", "c1", "--json") assert commit.exit_code == 0, commit.output c1 = json.loads(commit.output)["commit_id"] result = _run(root, "rev-parse", "@{0.second.ago}", "--json") assert result.exit_code == 0, result.output data = json.loads(result.output) assert data["commit_id"] == c1