Files
NODEDC_MISSION_CORE/tests/test_closed_evidence_compaction.py
T

61 lines
2.2 KiB
Python

import importlib.util
from pathlib import Path
from types import SimpleNamespace
import pytest
spec = importlib.util.spec_from_file_location(
"compact", Path(__file__).parents[1] / "scripts/compact_closed_evidence.py"
)
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
def fixture(tmp_path, monkeypatch):
root = tmp_path / "closed"
root.mkdir()
store = tmp_path / "cas"
for name in ["first.npz", "second.npz"]:
(root / name).write_bytes(b"abcd" * 20000)
(root / "original.ply").write_bytes(b"abcd" * 20000)
monkeypatch.setattr(m.shutil, "disk_usage", lambda p: SimpleNamespace(free=1000 * 1024**3))
return root, store
def test_aliases_preserve_bytes_and_restore_independent_copy(tmp_path, monkeypatch):
root, store = fixture(tmp_path, monkeypatch)
plan = m.plan([root], store)
assert plan["duplicate_bytes"] == 80000
m.apply(plan)
first, second = root / "first.npz", root / "second.npz"
assert first.samefile(second) and first.read_bytes() == b"abcd" * 20000
assert not (root / "original.ply").samefile(first)
row = next(x for x in plan["groups"][0] if x["path"] == str(first))
blob = next(store.glob("*.blob"))
restored = m.restore_alias(row, blob, tmp_path / "restored.npz")
assert not restored.samefile(second)
assert first.samefile(second)
restored.write_bytes(b"changed")
assert second.read_bytes() == b"abcd" * 20000
def test_changed_candidate_refuses_group_before_replacement(tmp_path, monkeypatch):
root, store = fixture(tmp_path, monkeypatch)
plan = m.plan([root], store)
(root / "second.npz").write_bytes(b"changed")
with pytest.raises(ValueError, match="changed"):
m.apply(plan)
assert (root / "first.npz").stat().st_nlink == 1
assert (root / "second.npz").read_bytes() == b"changed"
def test_restore_refuses_permissions_drift(tmp_path, monkeypatch):
root, store = fixture(tmp_path, monkeypatch)
document = m.plan([root], store)
m.apply(document)
blob = next(store.glob("*.blob"))
blob.chmod(0o600)
with pytest.raises(ValueError, match="matches"):
m.restore_alias(document["groups"][0][0], blob, tmp_path / "restored")
assert not (tmp_path / "restored").exists()