"""Tests for marketing_intelligence.storage.checkpoint.""" from pathlib import Path from typing import Any from unittest.mock import patch from marketing_intelligence.storage import checkpoint as cp def _patch_dir(tmp_path: Path) -> Any: return patch.object(cp, "_LOCAL_DIR", tmp_path / "checkpoints") class TestLoadCheckpoint: def test_returns_defaults_when_missing(self, tmp_path: Path) -> None: with _patch_dir(tmp_path): data = cp.load_checkpoint("camp_a") assert data == {"completed": [], "failed": []} def test_returns_saved_data(self, tmp_path: Path) -> None: with _patch_dir(tmp_path): cp.save_checkpoint("camp_a", {"completed": ["v1"], "failed": []}) data = cp.load_checkpoint("camp_a") assert data["completed"] == ["v1"] class TestSaveCheckpoint: def test_creates_file(self, tmp_path: Path) -> None: with _patch_dir(tmp_path): cp.save_checkpoint("camp_a", {"completed": ["v1"], "failed": []}) p = tmp_path / "checkpoints" / "camp_a.json" assert p.exists() def test_overwrites_existing(self, tmp_path: Path) -> None: with _patch_dir(tmp_path): cp.save_checkpoint("camp_a", {"completed": ["v1"], "failed": []}) cp.save_checkpoint("camp_a", {"completed": ["v1", "v2"], "failed": []}) data = cp.load_checkpoint("camp_a") assert len(data["completed"]) == 2 class TestClearCheckpoint: def test_deletes_file(self, tmp_path: Path) -> None: with _patch_dir(tmp_path): cp.save_checkpoint("camp_a", {"completed": [], "failed": []}) cp.clear_checkpoint("camp_a") p = tmp_path / "checkpoints" / "camp_a.json" assert not p.exists() def test_no_error_when_file_missing(self, tmp_path: Path) -> None: with _patch_dir(tmp_path): cp.clear_checkpoint("no_such_campaign")