gabriel / muse public

test_commit_record_schema.py file-level

at sha256:1 · View file ↗ · Intel ↗

History
1 files
1 commits
0 hotspots
0 🧊 dead
0 💥 blast risk
sha256:4 Merge branch 'dev' into main · gabriel · Jun 17, 2026
1 """Tests for CommitRecord and CommitDict schema contract.
2
3 Canonical field names:
4 - ``branch`` — the branch the commit was made on (not ``created_on_branch``)
5 - ``format_version`` — removed entirely; not stored, not serialised
6 """
7
8 from __future__ import annotations
9
10 import datetime
11
12 import pytest
13
14 from muse.core.commits import (
15 CommitDict,
16 CommitRecord,
17 )
18 from muse.core.types import MsgpackDict, long_id
19
20
21 # ---------------------------------------------------------------------------
22 # Helpers
23 # ---------------------------------------------------------------------------
24
25 _NOW = datetime.datetime(2025, 1, 1, 0, 0, 0, tzinfo=datetime.timezone.utc)
26 _TS = _NOW.isoformat()
27 _CID = long_id("a" * 64)
28 _SID = long_id("b" * 64)
29
30
31 def _minimal_dict(branch_key: str = "branch") -> MsgpackDict:
32 """Minimal raw dict for CommitRecord.from_dict."""
33 return {
34 "commit_id": _CID,
35 "repo_id": "test-repo",
36 branch_key: "main",
37 "snapshot_id": _SID,
38 "message": "test commit",
39 "committed_at": _TS,
40 }
41
42
43 # ---------------------------------------------------------------------------
44 # CommitRecord field existence
45 # ---------------------------------------------------------------------------
46
47
48 class TestCommitRecordFields:
49 def test_has_branch_field(self) -> None:
50 rec = CommitRecord(
51 commit_id=_CID,
52 branch="main",
53 snapshot_id=_SID,
54 message="m",
55 committed_at=_NOW,
56 )
57 assert rec.branch == "main"
58
59 def test_has_no_created_on_branch_field(self) -> None:
60 rec = CommitRecord(
61 commit_id=_CID,
62 branch="main",
63 snapshot_id=_SID,
64 message="m",
65 committed_at=_NOW,
66 )
67 assert not hasattr(rec, "created_on_branch"), (
68 "CommitRecord must not have a 'created_on_branch' field"
69 )
70
71 def test_has_no_format_version_field(self) -> None:
72 rec = CommitRecord(
73 commit_id=_CID,
74 branch="main",
75 snapshot_id=_SID,
76 message="m",
77 committed_at=_NOW,
78 )
79 assert not hasattr(rec, "format_version"), (
80 "CommitRecord must not have a 'format_version' field"
81 )
82
83
84 # ---------------------------------------------------------------------------
85 # to_dict serialisation
86 # ---------------------------------------------------------------------------
87
88
89 class TestCommitRecordToDict:
90 def _make(self) -> CommitRecord:
91 return CommitRecord(
92 commit_id=_CID,
93 branch="dev",
94 snapshot_id=_SID,
95 message="m",
96 committed_at=_NOW,
97 )
98
99 def test_to_dict_has_branch_key(self) -> None:
100 d = self._make().to_dict()
101 assert d["branch"] == "dev"
102
103 def test_to_dict_has_no_created_on_branch_key(self) -> None:
104 d = self._make().to_dict()
105 assert "created_on_branch" not in d, (
106 "to_dict() must not emit 'created_on_branch' key"
107 )
108
109 def test_to_dict_has_no_format_version_key(self) -> None:
110 d = self._make().to_dict()
111 assert "format_version" not in d, (
112 "to_dict() must not emit 'format_version' key"
113 )
114
115
116 # ---------------------------------------------------------------------------
117 # from_dict deserialisation
118 # ---------------------------------------------------------------------------
119
120
121 class TestCommitRecordFromDict:
122 def test_reads_branch_key(self) -> None:
123 raw = _minimal_dict("branch")
124 rec = CommitRecord.from_dict(raw)
125 assert rec.branch == "main"
126
127 def test_falls_back_to_created_on_branch_for_old_records(self) -> None:
128 """Old stored files with 'created_on_branch' key must still deserialise.
129
130 This shim supports repos not yet migrated via 'muse code migrate'.
131 New commits are always written with 'branch'.
132 """
133 raw = _minimal_dict("created_on_branch")
134 rec = CommitRecord.from_dict(raw)
135 assert rec.branch == "main"
136
137 def test_raises_when_neither_key_present(self) -> None:
138 raw = _minimal_dict("branch")
139 del raw["branch"]
140 with pytest.raises(TypeError, match="branch"):
141 CommitRecord.from_dict(raw)
142
143 def test_from_dict_ignores_format_version_key(self) -> None:
144 """Old records with format_version in the dict must still deserialise
145 without storing the field on the returned record."""
146 raw = _minimal_dict("branch")
147 raw["format_version"] = 8
148 rec = CommitRecord.from_dict(raw)
149 assert not hasattr(rec, "format_version")
150
151
152 # ---------------------------------------------------------------------------
153 # CommitDict TypedDict
154 # ---------------------------------------------------------------------------
155
156
157 class TestCommitDict:
158 def test_commit_dict_has_branch_key(self) -> None:
159 """CommitDict must declare 'branch', not 'created_on_branch'."""
160 annotations = CommitDict.__annotations__
161 assert "branch" in annotations, (
162 "CommitDict must have 'branch' key"
163 )
164 assert "created_on_branch" not in annotations, (
165 "CommitDict must not have 'created_on_branch' key"
166 )
167
168 def test_commit_dict_has_no_format_version_key(self) -> None:
169 annotations = CommitDict.__annotations__
170 assert "format_version" not in annotations, (
171 "CommitDict must not have 'format_version' key"
172 )