"""Registry unit tests: atomic persistence, adoption, tombstones.""" from __future__ import annotations import base64 import json import os from pathlib import Path import pytest from hhstreamd.clock import ManualClock from hhstreamd.registry import ( Registry, Session, State, atomic_write_json, mint_id, ) def make_session(sid: str = "foot-aaaaaa", **overrides) -> Session: base = dict( id=sid, app="foot", argv=["/usr/bin/foot"], pid=4242, pgid=4242, sid=4242, pid_starttime=918342, cmdline_sha256="9f2c", home="/home/someone", runtime_dir="/run/hhstream/sessions/" + sid, created_at="2026-10-01T09:14:02Z", created_boottime=1000.0, last_activity_at="2026-10-01T09:14:02Z", last_activity_boottime=1000.0, idle_timeout_s=10800.0, state=State.READY, ) base.update(overrides) return Session(**base) @pytest.fixture def registry(tmp_path: Path) -> Registry: reg = Registry(tmp_path, ManualClock(boottime=1000.0)) reg.ensure_dirs() return reg # --------------------------------------------------------------------------- # atomic persistence # --------------------------------------------------------------------------- def test_atomic_write_leaves_no_temporary_files_behind(tmp_path: Path): target = tmp_path / "rec.json" atomic_write_json(target, {"hello": "world"}) assert json.loads(target.read_text()) == {"hello": "world"} assert [p.name for p in tmp_path.iterdir()] == ["rec.json"] def test_a_failed_write_leaves_the_previous_record_intact(tmp_path: Path, monkeypatch): """The point of tmp + os.replace: a reader never sees a half-written record.""" target = tmp_path / "rec.json" atomic_write_json(target, {"generation": 1}) def explode(*args, **kwargs): raise OSError("disk full") monkeypatch.setattr(json, "dump", explode) with pytest.raises(OSError): atomic_write_json(target, {"generation": 2}) assert json.loads(target.read_text()) == {"generation": 1} assert [p.name for p in tmp_path.iterdir()] == ["rec.json"] # temp cleaned up def test_the_temp_file_is_written_in_the_target_directory(tmp_path: Path, monkeypatch): """os.replace is only atomic within one filesystem, so the temp must be adjacent.""" seen: list[str] = [] real_replace = os.replace def spy(src, dst, *args, **kwargs): seen.append(str(src)) return real_replace(src, dst, *args, **kwargs) monkeypatch.setattr(os, "replace", spy) target = tmp_path / "deep" / "rec.json" atomic_write_json(target, {"a": 1}) assert Path(seen[0]).parent == target.parent def test_persist_writes_the_record_and_it_round_trips(registry: Registry): session = registry.add(make_session()) path = registry.record_path(session.id) assert path.exists() restored = Session.from_dict(json.loads(path.read_text())) assert restored == session def test_a_mirror_write_failure_never_fails_the_command(registry: Registry, monkeypatch, caplog): session = registry.add(make_session()) monkeypatch.setattr( "hhstreamd.registry.atomic_write_json", lambda *a, **k: (_ for _ in ()).throw(OSError("read-only filesystem")), ) registry.touch(session, force_flush=True) # must not raise assert session.command_count == 1 assert "mirror write failed" in caplog.text def test_activity_flushes_are_rate_limited_but_transitions_are_not(registry: Registry): clock: ManualClock = registry.clock # type: ignore[assignment] session = registry.add(make_session()) registry.touch(session) # within the 15 s window: memory only on_disk = Session.from_dict(json.loads(registry.record_path(session.id).read_text())) assert session.command_count == 1 assert on_disk.command_count == 0 clock.advance(16.0) registry.touch(session) on_disk = Session.from_dict(json.loads(registry.record_path(session.id).read_text())) assert on_disk.command_count == 2 # --------------------------------------------------------------------------- # handles # --------------------------------------------------------------------------- def test_a_colliding_draw_is_discarded_and_another_taken(): """Handles are never reused: a draw already in the set is refused.""" class TwoDraws: def __init__(self): self.calls = 0 def token_bytes(self, n): self.calls += 1 return b"\x00" * n if self.calls == 1 else b"\xff" * n rng = TwoDraws() collision = f"foot-{base64.b32encode(b'\x00' * 5).decode().rstrip('=').lower()[:6]}" minted = mint_id("foot", {collision}, rng=rng) assert rng.calls == 2 assert minted != collision assert minted.startswith("foot-") def test_a_tombstoned_id_still_blocks_reuse(registry: Registry): session = registry.add(make_session()) registry.terminate(session, State.KILLED) assert session.id in registry.taken_ids() assert registry.new_id("foot") != session.id def test_the_slug_is_sanitised_but_never_parsed(): assert mint_id("Chromium", set()).startswith("chromium-") assert mint_id("/bin/odd name", set()).startswith("bin-odd-name-") assert mint_id("!!!", set()).startswith("app-") # --------------------------------------------------------------------------- # tombstones # --------------------------------------------------------------------------- def test_terminate_writes_a_tombstone_with_an_expiry(registry: Registry): clock: ManualClock = registry.clock # type: ignore[assignment] session = registry.add(make_session()) registry.terminate(session, State.EXITED, exit_code=0, exit_detected_by="sigchld") assert not session.live assert session.exit_code == 0 assert session.tombstoned_boottime == clock.boottime() assert session.tombstone_expires() == clock.boottime() + 3600.0 assert session.deadline() is None # a tombstone can never be idle-reaped assert registry.tombstones() == [session] assert registry.live() == [] def test_an_adopted_exit_records_no_exit_code_rather_than_inventing_a_zero(registry: Registry): session = registry.add(make_session(adopted=True)) registry.terminate(session, State.EXITED, exit_detected_by="pidfd") assert session.exit_code is None assert session.exit_detected_by == "pidfd" def test_drop_removes_the_record_and_its_mirror(registry: Registry): session = registry.add(make_session()) path = registry.record_path(session.id) registry.drop(session.id) assert not path.exists() assert registry.get(session.id) is None # --------------------------------------------------------------------------- # loading / adoption # --------------------------------------------------------------------------- def test_load_records_reads_back_what_was_persisted(tmp_path: Path): writer = Registry(tmp_path, ManualClock()) writer.ensure_dirs() writer.add(make_session("foot-aaaaaa")) writer.add(make_session("foot-bbbbbb", pid=99)) reader = Registry(tmp_path, ManualClock()) records, quarantined = reader.load_records() assert quarantined == [] assert sorted(r.id for r in records) == ["foot-aaaaaa", "foot-bbbbbb"] def test_a_corrupt_record_is_quarantined_not_deleted_and_not_acted_on(tmp_path: Path, caplog): registry = Registry(tmp_path, ManualClock()) registry.ensure_dirs() bad = registry.sessions_dir / "foot-broken.json" bad.write_text("{this is not json") records, quarantined = registry.load_records() assert records == [] assert len(quarantined) == 1 assert not bad.exists() moved = list(registry.quarantine_dir.iterdir()) assert len(moved) == 1 assert moved[0].read_text() == "{this is not json" assert "quarantining unreadable record" in caplog.text def test_a_record_that_is_not_an_object_is_quarantined(tmp_path: Path): registry = Registry(tmp_path, ManualClock()) registry.ensure_dirs() (registry.sessions_dir / "x.json").write_text("[1, 2, 3]") records, quarantined = registry.load_records() assert records == [] and len(quarantined) == 1 def test_unknown_fields_from_a_newer_schema_are_dropped_not_fatal(caplog): data = make_session().to_dict() data["invented_by_a_later_version"] = True session = Session.from_dict(data) assert session.id == "foot-aaaaaa" assert "dropping unknown record fields" in caplog.text def test_a_record_from_a_previous_boot_is_recognised_as_stale(tmp_path: Path): """Pids restart from zero at boot, so /proc cannot be trusted about them.""" registry = Registry(tmp_path, ManualClock()) this_boot = make_session("foot-aaaaaa") this_boot.boot_id = registry._boot_id previous_boot = make_session("foot-bbbbbb") previous_boot.boot_id = "00000000-0000-0000-0000-000000000000" assert registry.stale_boot(this_boot) is False assert registry.stale_boot(previous_boot) is True def test_derived_fields_reported_to_callers(registry: Registry): session = registry.add(make_session()) public = session.public(now=1000.0 + 600.0) assert public["idle_s"] == 600.0 assert public["expires_in_s"] == 10800.0 - 600.0 assert public["live"] is True assert public["schema"] == 2