test_archive_manager.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from pathlib import Path
  4. import pytest
  5. def _load_archive_module():
  6. import sys
  7. scripts_dir = Path(__file__).resolve().parents[2]
  8. if str(scripts_dir) not in sys.path:
  9. sys.path.insert(0, str(scripts_dir))
  10. import archive_manager
  11. return archive_manager
  12. @pytest.fixture
  13. def archive_env(tmp_path):
  14. webnovel = tmp_path / ".webnovel"
  15. webnovel.mkdir(parents=True, exist_ok=True)
  16. state_path = webnovel / "state.json"
  17. state_path.write_text(
  18. '{"progress":{"current_chapter":10},"plot_threads":{},"review_checkpoints":[]}',
  19. encoding="utf-8",
  20. )
  21. return tmp_path
  22. def test_archive_remove_from_state_missing_sections(archive_env):
  23. module = _load_archive_module()
  24. manager = module.ArchiveManager(project_root=archive_env)
  25. state = {
  26. "progress": {"current_chapter": 50},
  27. }
  28. updated = manager.remove_from_state(state, inactive_chars=[], resolved_threads=[], old_reviews=[])
  29. assert updated.get("progress", {}).get("current_chapter") == 50
  30. def test_archive_check_trigger_conditions_edges(archive_env):
  31. module = _load_archive_module()
  32. manager = module.ArchiveManager(project_root=archive_env)
  33. manager.config["chapter_trigger"] = 10
  34. manager.config["file_size_trigger_mb"] = 9999.0
  35. trigger = manager.check_trigger_conditions({"progress": {"current_chapter": 20}})
  36. assert trigger["chapter_trigger"] is True
  37. assert trigger["should_archive"] is True
  38. def test_archive_identify_old_reviews_handles_mixed_formats(archive_env):
  39. module = _load_archive_module()
  40. manager = module.ArchiveManager(project_root=archive_env)
  41. manager.config["review_old_threshold"] = 5
  42. state = {
  43. "progress": {"current_chapter": 30},
  44. "review_checkpoints": [
  45. {"chapters": "20-22", "report": "r1.md"},
  46. {"chapter_range": [10, 12], "date": "2026-01-01"},
  47. {"report": "Review_Ch5-6.md"},
  48. ],
  49. }
  50. results = manager.identify_old_reviews(state)
  51. assert len(results) == 3
  52. assert all(row["chapters_since_review"] >= 5 for row in results)