"""core/agent/types.py 单元测试。"""
from __future__ import annotations

from core.agent.types import LoopResult, Step


class TestStep:
    def test_creation(self) -> None:
        step = Step(
            thought="thinking...",
            reflect={"key": "value"},
            plan={"next": "do something"},
            tool_call={"tool": "search", "args": {"query": "test"}},
            tool_output="result text",
            raw_content='{"reflect": {}, "plan": {}, "action": {}}',
            call_id="uuid-1",
        )
        assert step.thought == "thinking..."
        assert step.tool_call["tool"] == "search"
        assert step.call_id == "uuid-1"


class TestLoopResult:
    def test_defaults(self) -> None:
        lr = LoopResult()
        assert lr.result is None
        assert lr.steps == []
        assert lr.steps_used == 0
        assert lr.token_usage == {"prompt_tokens": 0, "completion_tokens": 0}
        assert lr.stop_reason == "finished"

    def test_with_steps(self) -> None:
        step = Step(
            thought="t", reflect={}, plan={},
            tool_call={"tool": "t", "args": {}},
            tool_output="o", raw_content="r", call_id="c",
        )
        lr = LoopResult(
            result={"answer": "42"}, steps=[step], steps_used=1,
            token_usage={"prompt_tokens": 100, "completion_tokens": 50},
            stop_reason="finished",
        )
        assert lr.result == {"answer": "42"}
        assert len(lr.steps) == 1

    def test_separate_instances_have_independent_lists(self) -> None:
        lr1 = LoopResult()
        lr2 = LoopResult()
        lr1.steps.append(Step("t", {}, {}, {"tool": "x", "args": {}}, "o", "r", "c"))
        assert len(lr2.steps) == 0
