|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +from unittest import mock |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from compendiumscribe.research.agents_workflow.artifacts import ResearchRunState |
| 9 | +from compendiumscribe.research.agents_workflow.state import load_state, save_state |
| 10 | + |
| 11 | + |
| 12 | +def test_save_state_writes_loadable_sidecar(tmp_path: Path) -> None: |
| 13 | + state_path = tmp_path / "report.research.json" |
| 14 | + state = ResearchRunState( |
| 15 | + topic="Atomic Recovery", |
| 16 | + title="Atomic Recovery", |
| 17 | + output_formats=["md"], |
| 18 | + ) |
| 19 | + |
| 20 | + save_state(state_path, state) |
| 21 | + |
| 22 | + loaded = load_state(state_path) |
| 23 | + assert loaded.run_id == state.run_id |
| 24 | + assert loaded.topic == "Atomic Recovery" |
| 25 | + assert loaded.output_formats == ["md"] |
| 26 | + |
| 27 | + |
| 28 | +def test_save_state_keeps_existing_sidecar_when_replace_fails( |
| 29 | + tmp_path: Path, |
| 30 | +) -> None: |
| 31 | + state_path = tmp_path / "report.research.json" |
| 32 | + original = ResearchRunState(topic="Original", title="Original") |
| 33 | + updated = ResearchRunState(topic="Updated", title="Updated") |
| 34 | + save_state(state_path, original) |
| 35 | + original_payload = state_path.read_text(encoding="utf-8") |
| 36 | + |
| 37 | + with mock.patch( |
| 38 | + "compendiumscribe.research.agents_workflow.state.os.replace", |
| 39 | + side_effect=OSError("replace failed"), |
| 40 | + ): |
| 41 | + with pytest.raises(OSError, match="replace failed"): |
| 42 | + save_state(state_path, updated) |
| 43 | + |
| 44 | + assert state_path.read_text(encoding="utf-8") == original_payload |
| 45 | + assert load_state(state_path).topic == "Original" |
| 46 | + assert not list(tmp_path.glob(".report.research.json.*.tmp")) |
0 commit comments