gabriel / muse public
test_reflog_time_resolution.py python
163 lines 6.6 KB
Raw
sha256:2d36521a1c0daf8222df46cff206eb4d8a12b8f22bc71dfa5d7011c5e47ad169 feat: muse#54 item 1 — relative time syntax for @{...} refl… Sonnet 5 patch 17 hours ago
1 """muse#54 item 1 -- relative time syntax for @{...} reflog refs.
2
3 `@{1.hour.ago}`, `@{2.days.ago}`, `@{yesterday}` resolve to the most
4 recent reflog entry at or before that point in time, exercised both at
5 the `resolve_reflog_ref` core level (with fabricated timestamps so the
6 tests are deterministic) and through the CLI (`muse rev-parse`).
7 """
8 from __future__ import annotations
9
10 import datetime
11 import json
12 import pathlib
13
14 import pytest
15
16 from tests.cli_test_helper import CliRunner
17 from muse.core.types import fake_id, Manifest, NULL_COMMIT_ID
18 from muse.core.paths import muse_dir, logs_dir, reflog_heads_dir, reflog_branch_path
19 from muse.core.reflog import resolve_reflog_ref
20
21 cli = None
22 runner = CliRunner()
23
24
25 def _env(root: pathlib.Path) -> Manifest:
26 return {"MUSE_REPO_ROOT": str(root)}
27
28
29 def _run(root: pathlib.Path, *args: str):
30 return runner.invoke(cli, list(args), env=_env(root))
31
32
33 def _init_repo(tmp_path: pathlib.Path) -> tuple[pathlib.Path, str]:
34 dot_muse = muse_dir(tmp_path)
35 dot_muse.mkdir()
36 repo_id = fake_id("repo")
37 (dot_muse / "repo.json").write_text(json.dumps({
38 "repo_id": repo_id, "domain": "code", "default_branch": "main",
39 "created_at": "2025-01-01T00:00:00+00:00",
40 }), encoding="utf-8")
41 (dot_muse / "HEAD").write_text("ref: refs/heads/main\n", encoding="utf-8")
42 (dot_muse / "refs" / "heads").mkdir(parents=True)
43 (dot_muse / "snapshots").mkdir()
44 (dot_muse / "commits").mkdir()
45 (dot_muse / "objects").mkdir()
46 return tmp_path, repo_id
47
48
49 def _write_raw_entry(root: pathlib.Path, path: pathlib.Path, old_id: str, new_id: str, when: datetime.datetime, operation: str) -> None:
50 path.parent.mkdir(parents=True, exist_ok=True)
51 ts = int(when.timestamp())
52 line = f"{old_id} {new_id} gabriel {ts} +0000\t{operation}\n"
53 with path.open("a", encoding="utf-8") as f:
54 f.write(line)
55
56
57 def _fake_commit_id(n: int) -> str:
58 return f"{n:064x}"
59
60
61 class TestTimeAgoResolution:
62 def test_hour_ago_finds_entry_before_cutoff(self, tmp_path: pathlib.Path) -> None:
63 root, repo_id = _init_repo(tmp_path)
64 head_log = logs_dir(root) / "HEAD"
65 now = datetime.datetime.now(datetime.timezone.utc)
66
67 c1 = _fake_commit_id(1)
68 c2 = _fake_commit_id(2)
69 # c1 was current 3 hours ago; c2 became current 10 minutes ago.
70 _write_raw_entry(root, head_log, NULL_COMMIT_ID, c1, now - datetime.timedelta(hours=3), "commit: c1")
71 _write_raw_entry(root, head_log, c1, c2, now - datetime.timedelta(minutes=10), "commit: c2")
72
73 # "1 hour ago" -> most recent entry at or before (now - 1h) -> c1,
74 # since c2 only became current 10 minutes ago (after the cutoff).
75 result = resolve_reflog_ref("@{1.hour.ago}", root)
76 assert result == c1
77
78 def test_minutes_ago_finds_most_recent_entry(self, tmp_path: pathlib.Path) -> None:
79 root, repo_id = _init_repo(tmp_path)
80 head_log = logs_dir(root) / "HEAD"
81 now = datetime.datetime.now(datetime.timezone.utc)
82
83 c1 = _fake_commit_id(1)
84 c2 = _fake_commit_id(2)
85 _write_raw_entry(root, head_log, NULL_COMMIT_ID, c1, now - datetime.timedelta(hours=2), "commit: c1")
86 _write_raw_entry(root, head_log, c1, c2, now - datetime.timedelta(hours=1), "commit: c2")
87
88 # "30 minutes ago" is after both entries were written -> most recent
89 # entry at or before that cutoff is c2.
90 result = resolve_reflog_ref("@{30.minute.ago}", root)
91 assert result == c2
92
93 def test_yesterday_finds_entry_before_todays_midnight(self, tmp_path: pathlib.Path) -> None:
94 root, repo_id = _init_repo(tmp_path)
95 head_log = logs_dir(root) / "HEAD"
96 now = datetime.datetime.now(datetime.timezone.utc)
97 today_midnight = now.replace(hour=0, minute=0, second=0, microsecond=0)
98
99 c1 = _fake_commit_id(1)
100 c2 = _fake_commit_id(2)
101 _write_raw_entry(root, head_log, NULL_COMMIT_ID, c1, today_midnight - datetime.timedelta(days=1), "commit: c1")
102 _write_raw_entry(root, head_log, c1, c2, now, "commit: c2")
103
104 result = resolve_reflog_ref("@{yesterday}", root)
105 assert result == c1
106
107 def test_no_entry_old_enough_returns_none(self, tmp_path: pathlib.Path) -> None:
108 root, repo_id = _init_repo(tmp_path)
109 head_log = logs_dir(root) / "HEAD"
110 now = datetime.datetime.now(datetime.timezone.utc)
111 c1 = _fake_commit_id(1)
112 _write_raw_entry(root, head_log, NULL_COMMIT_ID, c1, now, "commit: c1")
113
114 result = resolve_reflog_ref("@{1.week.ago}", root)
115 assert result is None
116
117 def test_named_branch_time_spec(self, tmp_path: pathlib.Path) -> None:
118 root, repo_id = _init_repo(tmp_path)
119 branch_log = reflog_branch_path(root, "main")
120 now = datetime.datetime.now(datetime.timezone.utc)
121 c1 = _fake_commit_id(1)
122 _write_raw_entry(root, branch_log, NULL_COMMIT_ID, c1, now - datetime.timedelta(hours=5), "commit: c1")
123
124 result = resolve_reflog_ref("main@{1.hour.ago}", root)
125 assert result == c1
126
127 def test_unrecognized_spec_returns_none(self, tmp_path: pathlib.Path) -> None:
128 root, repo_id = _init_repo(tmp_path)
129 head_log = logs_dir(root) / "HEAD"
130 now = datetime.datetime.now(datetime.timezone.utc)
131 c1 = _fake_commit_id(1)
132 _write_raw_entry(root, head_log, NULL_COMMIT_ID, c1, now, "commit: c1")
133
134 result = resolve_reflog_ref("@{not.a.real.spec}", root)
135 assert result is None
136
137 def test_plain_index_still_works(self, tmp_path: pathlib.Path) -> None:
138 """Non-regression: the pre-existing @{N} integer form must still work
139 after broadening the regex to accept time specs."""
140 root, repo_id = _init_repo(tmp_path)
141 head_log = logs_dir(root) / "HEAD"
142 now = datetime.datetime.now(datetime.timezone.utc)
143 c1 = _fake_commit_id(1)
144 _write_raw_entry(root, head_log, NULL_COMMIT_ID, c1, now, "commit: c1")
145
146 result = resolve_reflog_ref("@{0}", root)
147 assert result == c1
148
149
150 class TestTimeAgoViaCli:
151 def test_rev_parse_accepts_time_spec(self, tmp_path: pathlib.Path) -> None:
152 root, repo_id = _init_repo(tmp_path)
153 (root / "f.txt").write_text("v1\n")
154 add = _run(root, "code", "add", "f.txt")
155 assert add.exit_code == 0, add.output
156 commit = _run(root, "commit", "-m", "c1", "--json")
157 assert commit.exit_code == 0, commit.output
158 c1 = json.loads(commit.output)["commit_id"]
159
160 result = _run(root, "rev-parse", "@{0.second.ago}", "--json")
161 assert result.exit_code == 0, result.output
162 data = json.loads(result.output)
163 assert data["commit_id"] == c1
File History 1 commit
sha256:2d36521a1c0daf8222df46cff206eb4d8a12b8f22bc71dfa5d7011c5e47ad169 feat: muse#54 item 1 — relative time syntax for @{...} refl… Sonnet 5 patch 17 hours ago