Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /tests /unit /test_checkpoints.py
| import json | |
| import os | |
| import tempfile | |
| import pytest | |
| from orchestrator.checkpoints import Checkpoint, CheckpointManager | |
| from orchestrator.state import StateManager, TaskState | |
| def state_manager(): | |
| return StateManager() | |
| def db_path(): | |
| with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f: | |
| path = f.name | |
| yield path | |
| if os.path.exists(path): | |
| os.unlink(path) | |
| def cp_manager(state_manager, db_path): | |
| mgr = CheckpointManager(state_manager=state_manager, db_path=db_path) | |
| yield mgr | |
| mgr.close() | |
| async def test_init_creates_database(cp_manager, db_path): | |
| assert os.path.exists(db_path) | |
| assert os.path.getsize(db_path) > 0 | |
| async def test_save_checkpoint(cp_manager): | |
| wf_id = "test-wf" | |
| state = {"task": "recon", "phase": 1} | |
| metadata = {"step": 1} | |
| cp = cp_manager.save_checkpoint(wf_id, state, metadata) | |
| assert isinstance(cp, Checkpoint) | |
| assert cp.workflow_id == wf_id | |
| assert cp.state == state | |
| assert cp.metadata == metadata | |
| assert cp.checksum is not None | |
| assert cp.checkpoint_id is not None | |
| async def test_load_checkpoint_returns_latest(cp_manager): | |
| wf_id = "load-test" | |
| cp_manager.save_checkpoint(wf_id, {"step": 1}, {"ver": 1}) | |
| cp_manager.save_checkpoint(wf_id, {"step": 2}, {"ver": 2}) | |
| loaded = cp_manager.load_checkpoint(wf_id) | |
| assert loaded is not None | |
| assert loaded.state["step"] == 2 | |
| assert loaded.metadata["ver"] == 2 | |
| async def test_load_checkpoint_returns_none_for_missing(cp_manager): | |
| loaded = cp_manager.load_checkpoint("nonexistent") | |
| assert loaded is None | |
| async def test_has_checkpoint(cp_manager): | |
| wf_id = "has-test" | |
| assert cp_manager.has_checkpoint(wf_id) is False | |
| cp_manager.save_checkpoint(wf_id, {"a": 1}, {}) | |
| assert cp_manager.has_checkpoint(wf_id) is True | |
| async def test_list_checkpoints(cp_manager): | |
| wf_id = "list-test" | |
| cp_manager.save_checkpoint(wf_id, {"s": 1}, {}) | |
| cp_manager.save_checkpoint(wf_id, {"s": 2}, {}) | |
| checkpoints = cp_manager.list_checkpoints(wf_id) | |
| assert len(checkpoints) == 2 | |
| assert checkpoints[0].state["s"] == 2 | |
| async def test_checkpoint_integrity_verification(cp_manager): | |
| wf_id = "integrity-test" | |
| cp = cp_manager.save_checkpoint(wf_id, {"data": "secret"}, {"tag": "a"}) | |
| assert cp_manager.verify_checkpoint(cp.checkpoint_id) is True | |
| cp.state["data"] = "tampered" | |
| with pytest.raises(ValueError, match="Checksum mismatch"): | |
| cp_manager._verify_checksum(cp) | |
| async def test_delete_workflow_checkpoints(cp_manager): | |
| wf_id = "delete-test" | |
| cp_manager.save_checkpoint(wf_id, {"x": 1}, {}) | |
| cp_manager.save_checkpoint(wf_id, {"x": 2}, {}) | |
| assert cp_manager.count_checkpoints(wf_id) == 2 | |
| deleted = cp_manager.delete_workflow_checkpoints(wf_id) | |
| assert deleted == 2 | |
| assert cp_manager.count_checkpoints(wf_id) == 0 | |
| async def test_prune_old_checkpoints(cp_manager): | |
| wf_id = "prune-test" | |
| for i in range(25): | |
| cp_manager.save_checkpoint(wf_id, {"i": i}, {}) | |
| assert cp_manager.count_checkpoints(wf_id) == 25 | |
| pruned = cp_manager.prune_old_checkpoints(max_age_days=365, max_per_workflow=10) | |
| assert pruned == 15 | |
| assert cp_manager.count_checkpoints(wf_id) == 10 | |
| async def test_create_checkpoint_snapshot(cp_manager, state_manager): | |
| await state_manager.set_task_state("task-1", TaskState.RUNNING) | |
| await state_manager.set_task_state("task-2", TaskState.COMPLETED) | |
| cp = await cp_manager.create_checkpoint("snap-test", {"phase": "recon"}) | |
| assert cp.workflow_id == "snap-test" | |
| assert cp.metadata["phase"] == "recon" | |
| assert cp.metadata["task_count"] == 2 | |
| assert cp.state["system_state"] in ("initializing", "active") | |
| async def test_restore_checkpoint(cp_manager, state_manager): | |
| await state_manager.set_task_state("task-a", TaskState.RUNNING) | |
| await cp_manager.create_checkpoint("restore-test", {"phase": "scan"}) | |
| await state_manager.set_task_state("task-a", TaskState.COMPLETED) | |
| await state_manager.set_task_state("task-b", TaskState.FAILED) | |
| snapshot = await cp_manager.restore_checkpoint("restore-test") | |
| assert snapshot is not None | |
| assert snapshot.system_state in ("initializing", "active") | |
| states = await state_manager.get_all_task_states() | |
| assert "task-a" in states | |
| assert states["task-a"] == TaskState.RUNNING | |
| assert "task-b" not in states | |
| async def test_restore_checkpoint_missing_workflow(cp_manager): | |
| snapshot = await cp_manager.restore_checkpoint("ghost-wf") | |
| assert snapshot is None | |
| async def test_corrupted_checkpoint_raises_error(cp_manager): | |
| wf_id = "corrupt-test" | |
| cp = cp_manager.save_checkpoint(wf_id, {"good": "data"}, {}) | |
| conn = cp_manager._get_connection() | |
| conn.execute( | |
| "UPDATE checkpoints SET state_json = ? WHERE id = ?", | |
| (json.dumps({"bad": "data"}), cp.checkpoint_id), | |
| ) | |
| conn.commit() | |
| with pytest.raises(ValueError, match="Checksum mismatch"): | |
| cp_manager.load_checkpoint(wf_id) | |
| async def test_load_checkpoint_by_id(cp_manager): | |
| wf_id = "byid-test" | |
| cp = cp_manager.save_checkpoint(wf_id, {"val": 42}, {}) | |
| loaded = cp_manager.load_checkpoint_by_id(cp.checkpoint_id) | |
| assert loaded is not None | |
| assert loaded.checkpoint_id == cp.checkpoint_id | |
| assert loaded.state["val"] == 42 | |
| async def test_load_checkpoint_by_id_missing(cp_manager): | |
| loaded = cp_manager.load_checkpoint_by_id("nonexistent-id") | |
| assert loaded is None | |
| async def test_get_workflow_status(cp_manager): | |
| wf_id = "status-test" | |
| assert cp_manager.get_workflow_status(wf_id) is None | |
| cp_manager.save_checkpoint(wf_id, {}, {"status": "completed"}) | |
| assert cp_manager.get_workflow_status(wf_id) == "completed" | |
| async def test_count_checkpoints(cp_manager): | |
| assert cp_manager.count_checkpoints() == 0 | |
| cp_manager.save_checkpoint("wf-a", {}, {}) | |
| cp_manager.save_checkpoint("wf-b", {}, {}) | |
| assert cp_manager.count_checkpoints() == 2 | |
| assert cp_manager.count_checkpoints("wf-a") == 1 | |
| async def test_checksum_computation(cp_manager): | |
| state = {"key": "value"} | |
| meta = {"tag": "test"} | |
| cs1 = cp_manager._compute_checksum(state, meta) | |
| cs2 = cp_manager._compute_checksum(dict(state), dict(meta)) | |
| assert cs1 == cs2 | |
| cs3 = cp_manager._compute_checksum({"key": "CHANGED"}, meta) | |
| assert cs1 != cs3 | |
| async def test_context_manager(cp_manager, state_manager, db_path): | |
| with CheckpointManager(state_manager=state_manager, db_path=db_path) as mgr: | |
| cp = mgr.save_checkpoint("ctx-test", {"val": 1}, {}) | |
| assert cp.checkpoint_id is not None | |
| assert mgr._connection is None or mgr._connection.closed | |
| async def test_save_checkpoint_with_parent_id(cp_manager): | |
| parent = cp_manager.save_checkpoint("parent-test", {"step": 1}, {}) | |
| child = cp_manager.save_checkpoint( | |
| "parent-test", | |
| {"step": 2}, | |
| {}, | |
| parent_id=parent.checkpoint_id, | |
| ) | |
| assert child.parent_id == parent.checkpoint_id | |
| async def test_default_db_path(): | |
| path = CheckpointManager._default_db_path() | |
| assert str(path).endswith("checkpoints.db") | |
| assert ".zen-ai" in str(path) | |
Xet Storage Details
- Size:
- 8.22 kB
- Xet hash:
- 32f203bc2dd28b4b7e42cb2aff66e24ab964d31bb9fcc92036e3dd65c6cfb917
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.