nightshift/tests/test_agents.py

100 lines
3.5 KiB
Python

from pathlib import Path
import tempfile
import unittest
from nightshift.agents import AgentExecutor, build_prompt_bundle, parse_review_output
from nightshift.artifacts import ArtifactStore
from nightshift.config import AgentConfig, StageConfig
from nightshift.tasks import parse_tasks
TASK_MD = """# Tasks
- [ ] TASK-001: Add fake agent coverage
Description:
Exercise fake command agents.
Acceptance Criteria:
- Prompt includes task details
- Agent output is stored
"""
class AgentExecutorTests(unittest.TestCase):
def test_build_prompt_bundle_includes_task_and_acceptance_criteria(self) -> None:
task = parse_tasks(TASK_MD)[0]
prompt = build_prompt_bundle(
system_prompt="System rules",
stage=StageConfig(id="plan", type="agent", agent="planner"),
task=task,
project_context="Project context",
previous_outputs={"prior": "Earlier output"},
retry_notes=["Retry note"],
)
self.assertIn("System rules", prompt)
self.assertIn("TASK-001", prompt)
self.assertIn("- Prompt includes task details", prompt)
self.assertIn("Earlier output", prompt)
self.assertIn("Retry note", prompt)
def test_build_prompt_bundle_includes_task_context(self) -> None:
task = parse_tasks(TASK_MD)[0]
prompt = build_prompt_bundle(
system_prompt="System rules",
stage=StageConfig(id="plan", type="agent", agent="planner"),
task=task,
project_context="Project context",
task_context="Task context body",
previous_outputs={},
retry_notes=[],
retry_context="- No retries",
)
self.assertIn("## Task Context", prompt)
self.assertIn("Task context body", prompt)
self.assertIn("- No retries", prompt)
def test_command_agent_writes_output_and_returns_pass(self) -> None:
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
prompt_path = root / "planner.md"
prompt_path.write_text("Plan carefully.", encoding="utf-8")
artifacts = ArtifactStore(root, ".nightshift", run_id="test-run")
executor = AgentExecutor(
root,
{
"planner": AgentConfig(
id="planner",
backend="command",
command='python -c "import sys; print(sys.stdin.read())"',
system_prompt=Path("planner.md"),
)
},
artifacts,
)
task = parse_tasks(TASK_MD)[0]
stage = StageConfig(id="plan", type="agent", agent="planner", output="plan.md")
result = executor.run_stage(stage, task)
self.assertEqual(result.status, "pass")
output = (root / result.output_path).read_text(encoding="utf-8")
self.assertIn("TASK-001", output)
self.assertIn("Plan carefully.", output)
def test_review_output_parser_accepts_structured_status(self) -> None:
status, reason, next_stage, context_update = parse_review_output(
"status: retry\nreason: Needs changes\nnext_stage: implement\ncontext_update: Fix tests\n"
)
self.assertEqual(status, "retry")
self.assertEqual(reason, "Needs changes")
self.assertEqual(next_stage, "implement")
self.assertEqual(context_update, "Fix tests")
if __name__ == "__main__":
unittest.main()