gabriel / muse public
test_commit_record_schema.py python
173 lines 5.5 KB
Raw
sha256:51ce277f663e01a43eaffbe77509b1de7ac2d4251b55d23306304bcdeb92c90d feat(pack): delta-encode snapshots in MPackBundle wire format Sonnet 4.6 minor ⚠ breaking 121 days ago
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.store import CommitRecord, CommitDict
15 from muse.core.types import MsgpackDict, long_id
16
17
18 # ---------------------------------------------------------------------------
19 # Helpers
20 # ---------------------------------------------------------------------------
21
22 _NOW = datetime.datetime(2025, 1, 1, 0, 0, 0, tzinfo=datetime.timezone.utc)
23 _TS = _NOW.isoformat()
24 _CID = long_id("a" * 64)
25 _SID = long_id("b" * 64)
26
27
28 def _minimal_msgpack(branch_key: str = "branch") -> MsgpackDict:
29 """Minimal raw dict for CommitRecord.from_msgpack."""
30 return {
31 "commit_id": _CID,
32 "repo_id": "test-repo",
33 branch_key: "main",
34 "snapshot_id": _SID,
35 "message": "test commit",
36 "committed_at": _TS,
37 }
38
39
40 # ---------------------------------------------------------------------------
41 # CommitRecord field existence
42 # ---------------------------------------------------------------------------
43
44
45 class TestCommitRecordFields:
46 def test_has_branch_field(self) -> None:
47 rec = CommitRecord(
48 commit_id=_CID,
49 repo_id="r",
50 branch="main",
51 snapshot_id=_SID,
52 message="m",
53 committed_at=_NOW,
54 )
55 assert rec.branch == "main"
56
57 def test_has_no_created_on_branch_field(self) -> None:
58 rec = CommitRecord(
59 commit_id=_CID,
60 repo_id="r",
61 branch="main",
62 snapshot_id=_SID,
63 message="m",
64 committed_at=_NOW,
65 )
66 assert not hasattr(rec, "created_on_branch"), (
67 "CommitRecord must not have a 'created_on_branch' field"
68 )
69
70 def test_has_no_format_version_field(self) -> None:
71 rec = CommitRecord(
72 commit_id=_CID,
73 repo_id="r",
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 repo_id="r",
94 branch="dev",
95 snapshot_id=_SID,
96 message="m",
97 committed_at=_NOW,
98 )
99
100 def test_to_dict_has_branch_key(self) -> None:
101 d = self._make().to_dict()
102 assert d["branch"] == "dev"
103
104 def test_to_dict_has_no_created_on_branch_key(self) -> None:
105 d = self._make().to_dict()
106 assert "created_on_branch" not in d, (
107 "to_dict() must not emit 'created_on_branch' key"
108 )
109
110 def test_to_dict_has_no_format_version_key(self) -> None:
111 d = self._make().to_dict()
112 assert "format_version" not in d, (
113 "to_dict() must not emit 'format_version' key"
114 )
115
116
117 # ---------------------------------------------------------------------------
118 # from_msgpack deserialisation
119 # ---------------------------------------------------------------------------
120
121
122 class TestCommitRecordFromMsgpack:
123 def test_reads_branch_key(self) -> None:
124 raw = _minimal_msgpack("branch")
125 rec = CommitRecord.from_msgpack(raw)
126 assert rec.branch == "main"
127
128 def test_falls_back_to_created_on_branch_for_old_records(self) -> None:
129 """Old stored msgpack files with 'created_on_branch' key must still deserialise.
130
131 This shim supports repos not yet migrated via 'muse code migrate'.
132 New commits are always written with 'branch'.
133 """
134 raw = _minimal_msgpack("created_on_branch")
135 rec = CommitRecord.from_msgpack(raw)
136 assert rec.branch == "main"
137
138 def test_raises_when_neither_key_present(self) -> None:
139 raw = _minimal_msgpack("branch")
140 del raw["branch"]
141 with pytest.raises(TypeError, match="branch"):
142 CommitRecord.from_msgpack(raw)
143
144 def test_from_msgpack_ignores_format_version_key(self) -> None:
145 """Old records with format_version in the dict must still deserialise
146 without storing the field on the returned record."""
147 raw = _minimal_msgpack("branch")
148 raw["format_version"] = 8
149 rec = CommitRecord.from_msgpack(raw)
150 assert not hasattr(rec, "format_version")
151
152
153 # ---------------------------------------------------------------------------
154 # CommitDict TypedDict
155 # ---------------------------------------------------------------------------
156
157
158 class TestCommitDict:
159 def test_commit_dict_has_branch_key(self) -> None:
160 """CommitDict must declare 'branch', not 'created_on_branch'."""
161 annotations = CommitDict.__annotations__
162 assert "branch" in annotations, (
163 "CommitDict must have 'branch' key"
164 )
165 assert "created_on_branch" not in annotations, (
166 "CommitDict must not have 'created_on_branch' key"
167 )
168
169 def test_commit_dict_has_no_format_version_key(self) -> None:
170 annotations = CommitDict.__annotations__
171 assert "format_version" not in annotations, (
172 "CommitDict must not have 'format_version' key"
173 )
File History 1 commit
sha256:51ce277f663e01a43eaffbe77509b1de7ac2d4251b55d23306304bcdeb92c90d feat(pack): delta-encode snapshots in MPackBundle wire format Sonnet 4.6 minor 121 days ago