|
| 1 | +"""Tests for utils/export_state_store.load_export_state_from_disk validation.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +from utils.export_state_store import load_export_state_from_disk |
| 9 | + |
| 10 | + |
| 11 | +def test_load_rejects_non_object_json(tmp_path: Path): |
| 12 | + p = tmp_path / "export_state.json" |
| 13 | + p.write_text(json.dumps([1, 2, 3]), encoding="utf-8") |
| 14 | + assert load_export_state_from_disk(str(p)) == {} |
| 15 | + |
| 16 | + |
| 17 | +def test_load_rejects_null_json(tmp_path: Path): |
| 18 | + p = tmp_path / "export_state.json" |
| 19 | + p.write_text("null", encoding="utf-8") |
| 20 | + assert load_export_state_from_disk(str(p)) == {} |
| 21 | + |
| 22 | + |
| 23 | +def test_load_sanitizes_non_dict_sessions(tmp_path: Path): |
| 24 | + p = tmp_path / "export_state.json" |
| 25 | + p.write_text( |
| 26 | + json.dumps( |
| 27 | + { |
| 28 | + "lastExportTime": "2026-01-01T00:00:00", |
| 29 | + "exportedCount": 1, |
| 30 | + "sessions": [], |
| 31 | + } |
| 32 | + ), |
| 33 | + encoding="utf-8", |
| 34 | + ) |
| 35 | + out = load_export_state_from_disk(str(p)) |
| 36 | + assert out["sessions"] == {} |
| 37 | + assert out["lastExportTime"] == "2026-01-01T00:00:00" |
| 38 | + assert out["exportedCount"] == 1 |
| 39 | + |
| 40 | + |
| 41 | +def test_load_adds_sessions_when_missing_but_has_last_export(tmp_path: Path): |
| 42 | + p = tmp_path / "export_state.json" |
| 43 | + p.write_text( |
| 44 | + json.dumps({"lastExportTime": "2026-01-01T00:00:00", "exportedCount": 0}), |
| 45 | + encoding="utf-8", |
| 46 | + ) |
| 47 | + out = load_export_state_from_disk(str(p)) |
| 48 | + assert out["sessions"] == {} |
| 49 | + assert out["lastExportTime"] == "2026-01-01T00:00:00" |
| 50 | + |
| 51 | + |
| 52 | +def test_load_legacy_flat_dict_unchanged_shape(tmp_path: Path): |
| 53 | + p = tmp_path / "export_state.json" |
| 54 | + legacy = {"uuid-one": 1740000000.0} |
| 55 | + p.write_text(json.dumps(legacy), encoding="utf-8") |
| 56 | + out = load_export_state_from_disk(str(p)) |
| 57 | + assert out == {"sessions": legacy} |
| 58 | + |
| 59 | + |
| 60 | +def test_export_state_lock_windows_branch_uses_msvcrt_when_no_fcntl( |
| 61 | + monkeypatch, tmp_path: Path |
| 62 | +): |
| 63 | + """When ``fcntl`` is absent, use ``msvcrt.locking`` (cross-process on Windows).""" |
| 64 | + import utils.export_state_store as mod |
| 65 | + |
| 66 | + monkeypatch.setattr(mod, "fcntl", None) |
| 67 | + calls: list[tuple[int, int]] = [] |
| 68 | + |
| 69 | + class FakeMsvcrt: |
| 70 | + LK_LOCK = 1 |
| 71 | + LK_UNLCK = 2 |
| 72 | + |
| 73 | + @staticmethod |
| 74 | + def locking(fd, mode, nbytes): |
| 75 | + calls.append((mode, nbytes)) |
| 76 | + |
| 77 | + monkeypatch.setattr(mod, "msvcrt", FakeMsvcrt) |
| 78 | + |
| 79 | + state_file = tmp_path / "export_state.json" |
| 80 | + state_file.write_text("{}", encoding="utf-8") |
| 81 | + with mod.export_state_lock(str(state_file)): |
| 82 | + assert (FakeMsvcrt.LK_LOCK, 1) in calls |
| 83 | + assert calls[-1] == (FakeMsvcrt.LK_UNLCK, 1) |
0 commit comments