Files
nextcraft/apps/ai-service/tests/agents/test_base.py
T
CIAgent 88a1dab810 docs(milestone): complete v0.2-ai-tutor-architecture
---ci---
phase: 7
milestone: v0.2
status: complete
requirements:
  covered: [REQ-2-001, REQ-2-002, REQ-2-003, REQ-2-004, REQ-2-005, REQ-2-006, REQ-2-007, REQ-2-008, REQ-2-009, REQ-2-010, REQ-2-011, REQ-2-012]
  partial: []
---/ci---

Milestone v0.2 (ai-tutor-architecture) merged to main.

Escalation record (audit remediation, durable): P1 executor
delegation failed twice (empty subagent results, zero files
created); auto-resolved at full autonomy to inline execution with
identical plan fidelity (commit 3271373, reflog-only after phase
branch squash-delete).
2026-09-11 17:34:50 +00:00

56 lines
1.7 KiB
Python

"""BaseAgent contract tests — stub agent + mock provider."""
from ai_service.agents.base import BaseAgent
from ai_service.config import Settings
from ai_service.corpus.learner_context import get_learner_context
from ai_service.llm.mock import MockProvider
from ai_service.llm.types import Message
class StubAgent(BaseAgent):
name = "stub"
def system_prompt(self, learner_context=None) -> str:
return "You are Stub. Answer briefly."
def make_agent() -> StubAgent:
return StubAgent(MockProvider(), Settings(provider="mock"))
async def test_build_messages_composition():
agent = make_agent()
history = [Message(role="user", content="earlier"), Message(role="assistant", content="reply")]
messages = agent.build_messages(history, "new question")
assert messages[0].role == "system"
assert messages[0].content == "You are Stub. Answer briefly."
assert [m.content for m in messages[1:]] == ["earlier", "reply", "new question"]
async def test_stream_reply_yields_deltas():
agent = make_agent()
tokens = [t async for t in agent.stream_reply(user_input="hello")]
assert len(tokens) >= 1
assert all(isinstance(t, str) for t in tokens)
async def test_stream_reply_with_history_and_context():
agent = make_agent()
ctx = get_learner_context()
history = [Message(role="user", content="earlier"), Message(role="assistant", content="ok")]
tokens = [t async for t in agent.stream_reply(history, "next", ctx)]
assert tokens
async def test_structured_reply_requires_schema():
import pytest
agent = make_agent()
with pytest.raises(ValueError):
await agent.structured_reply(user_input="x", schema=None)
async def test_name_defaults():
assert make_agent().name == "stub"