"""Unit tests for the disk-backed durable queue used by result delivery.""" import json from durable_queue import DiskQueue def test_put_contains_remove(tmp_path): q = DiskQueue(str(tmp_path / "q")) assert not q.contains("a") q.put("a", {"hello": 1}) assert q.contains("a") q.remove("a") assert not q.contains("a") q.remove("a") # idempotent - no error on missing def test_put_overwrites_and_roundtrips_payload(tmp_path): q = DiskQueue(str(tmp_path / "q")) q.put("uuid-1", {"uuid-1": {"10.1/x": {"Title": ["P"], "type": "article"}}}) q.put("uuid-1", {"uuid-1": {"changed": True}}) items = q.items() assert len(items) == 1 key, payload, _ts = items[0] assert key == "uuid-1" assert payload == {"uuid-1": {"changed": True}} def test_items_sorted_oldest_first(tmp_path, monkeypatch): q = DiskQueue(str(tmp_path / "q")) import durable_queue times = iter([100.0, 200.0, 300.0]) monkeypatch.setattr(durable_queue.time, "time", lambda: next(times)) q.put("c", {}) q.put("a", {}) q.put("b", {}) assert [k for k, _p, _ts in q.items()] == ["c", "a", "b"] def test_corrupt_file_is_skipped_not_fatal(tmp_path): directory = tmp_path / "q" q = DiskQueue(str(directory)) q.put("good", {"ok": 1}) (directory / "broken.json").write_text("{ this is not json", encoding="utf-8") keys = q.keys() assert keys == ["good"] # broken file skipped, good one survives def test_prune_keeps_newest(tmp_path, monkeypatch): q = DiskQueue(str(tmp_path / "q")) import durable_queue times = iter([1.0, 2.0, 3.0, 4.0, 5.0]) monkeypatch.setattr(durable_queue.time, "time", lambda: next(times)) for key in ("k1", "k2", "k3", "k4", "k5"): q.put(key, {}) dropped = q.prune(2) assert dropped == 3 assert set(q.keys()) == {"k4", "k5"} def test_atomic_write_leaves_no_tmp_files(tmp_path): directory = tmp_path / "q" q = DiskQueue(str(directory)) q.put("a", {"x": 1}) leftover = [p.name for p in directory.iterdir() if p.suffix == ".tmp"] assert leftover == [] def test_key_with_slashes_is_sanitised(tmp_path): q = DiskQueue(str(tmp_path / "q")) q.put("../../etc/passwd", {"evil": 1}) # Stays inside the directory (no traversal), and round-trips by key. files = list((tmp_path / "q").iterdir()) assert all(f.parent == tmp_path / "q" for f in files) assert q.items()[0][1] == {"evil": 1}