gabriel / musehub public
test_object_store_invariant_phase6.py python
183 lines 7.7 KB
Raw
sha256:94ef169c149a452bff7c604ded8b280b19bd477c2dabcb56972780b0b784c7aa Merge 'fix/assignee-sigil-inline' into 'dev' — proposal: As… Human 1 day ago
1 """Phase 6 — Non-wire msgpack cleanup tests (issue #63).
2
3 Verifies:
4 - _snap_row_to_wire no longer reads delta_blob (dead code removed)
5 - _snap_row_to_wire fallback uses only manifest_blob
6 - _snap_row_to_wire_s3 emits a warning when falling back to DB
7 - _commit_to_wire_s3 emits a warning when falling back to DB
8 - Dead imports removed from musehub_wire and musehub_wire_push
9 """
10 from __future__ import annotations
11
12 import datetime
13 import logging
14 import msgpack
15 import pytest
16
17 from muse.core.types import fake_id
18 from musehub.db.musehub_repo_models import MusehubCommit, MusehubSnapshot
19
20
21 def _snap(
22 snapshot_id: str,
23 manifest: dict[str, str] | None = None,
24 delta_blob: bytes | None = None,
25 storage_uri: str | None = None,
26 ) -> MusehubSnapshot:
27 blob = msgpack.packb(manifest, use_bin_type=True) if manifest is not None else None
28 return MusehubSnapshot(
29 snapshot_id=snapshot_id,
30 manifest_blob=blob,
31 delta_blob=delta_blob,
32 entry_count=len(manifest) if manifest else 0,
33 directories=[],
34 storage_uri=storage_uri,
35 )
36
37
38 def _commit(commit_id: str, storage_uri: str | None = None) -> MusehubCommit:
39 return MusehubCommit(
40 commit_id=commit_id,
41 branch="main",
42 parent_ids=[],
43 message="test",
44 author="gabriel",
45 timestamp=datetime.datetime.now(tz=datetime.timezone.utc),
46 snapshot_id=None,
47 storage_uri=storage_uri,
48 )
49
50
51 # ---------------------------------------------------------------------------
52 # _snap_row_to_wire — delta_blob branch removed
53 # ---------------------------------------------------------------------------
54
55 class TestSnapRowToWireDeltaBlobRemoved:
56 def test_delta_blob_not_used_when_manifest_blob_present(self) -> None:
57 """_snap_row_to_wire must read from manifest_blob, never from delta_blob."""
58 from musehub.services.musehub_wire_shared import _snap_row_to_wire
59
60 manifest = {"a.py": fake_id("obj-a"), "b.py": fake_id("obj-b")}
61 # delta_blob has DIFFERENT content — if it were read, delta_upsert would differ
62 delta_encoded = {"add": {"WRONG.py": fake_id("wrong")}}
63 snap = _snap(
64 snapshot_id=fake_id("snap1"),
65 manifest=manifest,
66 delta_blob=msgpack.packb(delta_encoded, use_bin_type=True),
67 )
68
69 result = _snap_row_to_wire(snap)
70
71 assert "WRONG.py" not in result["delta_upsert"], \
72 "_snap_row_to_wire must NOT read delta_blob — that branch was removed in Phase 6"
73 assert "a.py" in result["delta_upsert"], \
74 "_snap_row_to_wire must read manifest_blob for delta_upsert"
75
76 def test_delta_blob_only_returns_empty_delta_upsert(self) -> None:
77 """Row with only delta_blob (no manifest_blob) returns empty delta_upsert.
78
79 Previously _snap_row_to_wire would decode delta_blob and return its content.
80 After Phase 6, the delta_blob branch is removed — the fallback is S3 via
81 _snap_row_to_wire_s3, not _snap_row_to_wire.
82 """
83 from musehub.services.musehub_wire_shared import _snap_row_to_wire
84
85 delta_encoded = {"add": {"c.py": fake_id("obj-c")}}
86 snap = _snap(
87 snapshot_id=fake_id("snap2"),
88 manifest=None,
89 delta_blob=msgpack.packb(delta_encoded, use_bin_type=True),
90 )
91
92 result = _snap_row_to_wire(snap)
93
94 assert result["delta_upsert"] == {}, \
95 "After Phase 6, _snap_row_to_wire must return empty delta_upsert " \
96 "when manifest_blob is absent — S3 path handles the real data"
97 assert result["delta_remove"] == []
98
99 def test_manifest_blob_present_returns_correct_manifest(self) -> None:
100 """_snap_row_to_wire still works correctly for manifest_blob fallback."""
101 from musehub.services.musehub_wire_shared import _snap_row_to_wire
102
103 manifest = {"src/main.py": fake_id("obj-main"), "README.md": fake_id("obj-readme")}
104 snap = _snap(snapshot_id=fake_id("snap3"), manifest=manifest)
105
106 result = _snap_row_to_wire(snap)
107
108 assert result["delta_upsert"] == manifest
109 assert result["delta_remove"] == []
110 assert result["snapshot_id"] == snap.snapshot_id
111
112
113 # ---------------------------------------------------------------------------
114 # _snap_row_to_wire_s3 — storage_uri=None is correct, no warning expected
115 # ---------------------------------------------------------------------------
116
117 class TestSnapRowToWireS3FallbackWarning:
118 async def test_no_warning_when_storage_uri_is_none(
119 self, caplog: pytest.LogCaptureFixture
120 ) -> None:
121 """_snap_row_to_wire_s3 must NOT warn when storage_uri=None — that is the correct state.
122
123 Snapshots are canonical in the DB. storage_uri=None means the object is
124 served from DB (the intended path), not a fallback condition.
125 """
126 from musehub.services.musehub_wire_shared import _snap_row_to_wire_s3
127 from musehub.storage.backends import get_backend
128
129 manifest = {"f.py": fake_id("obj-f")}
130 snap = _snap(snapshot_id=fake_id("snap-warn"), manifest=manifest, storage_uri=None)
131
132 with caplog.at_level(logging.WARNING, logger="musehub.services.musehub_wire_shared"):
133 result = await _snap_row_to_wire_s3(snap, get_backend())
134
135 assert result["delta_upsert"] == manifest
136 assert not any("fallback" in r.message.lower() or "storage_uri" in r.message.lower()
137 for r in caplog.records), \
138 "storage_uri=None is expected — must not produce a spurious warning"
139
140
141 # ---------------------------------------------------------------------------
142 # _commit_to_wire_s3 — storage_uri=None is correct, no warning expected
143 # ---------------------------------------------------------------------------
144
145 class TestCommitToWireS3FallbackWarning:
146 async def test_no_warning_when_storage_uri_is_none(
147 self, caplog: pytest.LogCaptureFixture
148 ) -> None:
149 """_commit_to_wire_s3 must NOT warn when storage_uri=None — that is the correct state.
150
151 Commits are canonical in the DB. storage_uri=None means the object is
152 served from DB (the intended path), not a fallback condition.
153 """
154 from musehub.services.musehub_wire_shared import _commit_to_wire_s3
155 from musehub.storage.backends import get_backend
156
157 commit = _commit(commit_id=fake_id("commit-warn"), storage_uri=None)
158
159 with caplog.at_level(logging.WARNING, logger="musehub.services.musehub_wire_shared"):
160 result = await _commit_to_wire_s3(commit, get_backend())
161
162 assert result.commit_id == commit.commit_id
163 assert not any("fallback" in r.message.lower() or "storage_uri" in r.message.lower()
164 for r in caplog.records), \
165 "storage_uri=None is expected — must not produce a spurious warning"
166
167
168 # ---------------------------------------------------------------------------
169 # Dead imports removed
170 # ---------------------------------------------------------------------------
171
172 class TestDeadImportsRemoved:
173 def test_snap_row_to_wire_not_in_wire_push_namespace(self) -> None:
174 """_snap_row_to_wire must not be imported at module level in musehub_wire_push."""
175 import musehub.services.musehub_wire_push as push_mod
176 assert not hasattr(push_mod, "_snap_row_to_wire"), \
177 "_snap_row_to_wire is unused in musehub_wire_push — import must be removed"
178
179 def test_snap_row_to_wire_not_in_wire_namespace(self) -> None:
180 """_snap_row_to_wire must not be imported at module level in musehub_wire."""
181 import musehub.services.musehub_wire as wire_mod
182 assert not hasattr(wire_mod, "_snap_row_to_wire"), \
183 "_snap_row_to_wire is unused in musehub_wire — import must be removed"
File History 1 commit
sha256:94ef169c149a452bff7c604ded8b280b19bd477c2dabcb56972780b0b784c7aa Merge 'fix/assignee-sigil-inline' into 'dev' — proposal: As… Human 1 day ago