test_workflow_manager.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import json
  4. import logging
  5. import sys
  6. from pathlib import Path
  7. def _load_module():
  8. scripts_dir = Path(__file__).resolve().parents[2]
  9. if str(scripts_dir) not in sys.path:
  10. sys.path.insert(0, str(scripts_dir))
  11. import workflow_manager
  12. return workflow_manager
  13. def test_workflow_lifecycle_and_trace(tmp_path, monkeypatch):
  14. module = _load_module()
  15. monkeypatch.setattr(module, "find_project_root", lambda: tmp_path)
  16. webnovel_dir = tmp_path / ".webnovel"
  17. webnovel_dir.mkdir(parents=True, exist_ok=True)
  18. module.start_task("webnovel-write", {"chapter_num": 7})
  19. module.start_step("Step 1", "Context")
  20. module.complete_step("Step 1", json.dumps({"state_json_modified": True}, ensure_ascii=False))
  21. module.complete_task(json.dumps({"review_completed": True}, ensure_ascii=False))
  22. state = module.load_state()
  23. assert state["current_task"] is None
  24. assert state["history"][-1]["status"] == module.TASK_STATUS_COMPLETED
  25. assert state["last_stable_state"]["artifacts"]["review_completed"] is True
  26. trace_path = module.get_call_trace_path()
  27. assert trace_path.exists()
  28. lines = trace_path.read_text(encoding="utf-8").strip().splitlines()
  29. events = [json.loads(line)["event"] for line in lines if line.strip()]
  30. assert "task_started" in events
  31. assert "step_started" in events
  32. assert "step_completed" in events
  33. assert "task_completed" in events
  34. def test_start_task_reentry_increments_retry(tmp_path, monkeypatch):
  35. module = _load_module()
  36. monkeypatch.setattr(module, "find_project_root", lambda: tmp_path)
  37. webnovel_dir = tmp_path / ".webnovel"
  38. webnovel_dir.mkdir(parents=True, exist_ok=True)
  39. module.start_task("webnovel-write", {"chapter_num": 8})
  40. module.start_task("webnovel-write", {"chapter_num": 8})
  41. state = module.load_state()
  42. task = state["current_task"]
  43. assert task is not None
  44. assert task["status"] == module.TASK_STATUS_RUNNING
  45. assert int(task.get("retry_count", 0)) >= 1
  46. def test_complete_step_rejects_mismatch_step_id(tmp_path, monkeypatch):
  47. module = _load_module()
  48. monkeypatch.setattr(module, "find_project_root", lambda: tmp_path)
  49. webnovel_dir = tmp_path / ".webnovel"
  50. webnovel_dir.mkdir(parents=True, exist_ok=True)
  51. module.start_task("webnovel-write", {"chapter_num": 9})
  52. module.start_step("Step 2A", "Draft")
  53. module.complete_step("Step 2B")
  54. state = module.load_state()
  55. current_step = state["current_task"]["current_step"]
  56. assert current_step is not None
  57. assert current_step["id"] == "Step 2A"
  58. assert current_step["status"] == module.STEP_STATUS_RUNNING
  59. def test_workflow_step_owner_and_order_violation_trace(tmp_path, monkeypatch):
  60. module = _load_module()
  61. monkeypatch.setattr(module, "find_project_root", lambda: tmp_path)
  62. webnovel_dir = tmp_path / ".webnovel"
  63. webnovel_dir.mkdir(parents=True, exist_ok=True)
  64. assert module.expected_step_owner("webnovel-write", "Step 1") == "context-agent"
  65. assert module.expected_step_owner("webnovel-write", "Step 5") == "data-agent"
  66. module.start_task("webnovel-write", {"chapter_num": 12})
  67. module.start_step("Step 3", "Review")
  68. trace_path = module.get_call_trace_path()
  69. lines = [json.loads(line) for line in trace_path.read_text(encoding="utf-8").splitlines() if line.strip()]
  70. events = [row.get("event") for row in lines]
  71. assert "step_order_violation" in events
  72. step_started = [row for row in lines if row.get("event") == "step_started"]
  73. assert step_started
  74. assert step_started[-1].get("payload", {}).get("expected_owner") == "review-agents"
  75. def test_safe_append_call_trace_logs_failure(monkeypatch, caplog):
  76. module = _load_module()
  77. def _raise_trace_error(event, payload=None):
  78. raise RuntimeError("trace failure")
  79. monkeypatch.setattr(module, "append_call_trace", _raise_trace_error)
  80. with caplog.at_level(logging.WARNING):
  81. module.safe_append_call_trace("unit_test_event", {"ok": True})
  82. message_text = "\n".join(record.getMessage() for record in caplog.records)
  83. assert "failed to append call trace" in message_text
  84. assert "unit_test_event" in message_text
  85. def test_workflow_reentry_does_not_duplicate_history(tmp_path, monkeypatch):
  86. module = _load_module()
  87. monkeypatch.setattr(module, "find_project_root", lambda: tmp_path)
  88. webnovel_dir = tmp_path / ".webnovel"
  89. webnovel_dir.mkdir(parents=True, exist_ok=True)
  90. module.start_task("webnovel-write", {"chapter_num": 20})
  91. module.start_task("webnovel-write", {"chapter_num": 20})
  92. module.start_task("webnovel-write", {"chapter_num": 20})
  93. state = module.load_state()
  94. assert isinstance(state.get("history"), list)
  95. assert len(state.get("history")) == 0
  96. task = state.get("current_task") or {}
  97. assert int(task.get("retry_count", 0)) >= 2