from __future__ import annotations

from typing import Any

from tests.harness.test_runner import TestCase, TestContext


def assert_success(resp: dict[str, Any]) -> None:
    assert isinstance(resp, dict), "response must be dict"
    assert resp.get("success") is True, f"expected success=true, got: {resp}"


def turns_tool_names(resp: dict[str, Any]) -> list[str]:
    names: list[str] = []
    for turn in resp.get("turns", []) or []:
        for tc in turn.get("tool_calls", []) or []:
            name = tc.get("name")
            if isinstance(name, str):
                names.append(name)
    return names


def simple_prompt_test(suite: str, name: str, description: str, prompt: str, expected_tool: str | None = None) -> TestCase:
    def _run(ctx: TestContext):
        resp = ctx.client.prompt(prompt, max_turns=6)
        assert_success(resp)
        final_response = str(resp.get("final_response", "")).strip()
        assert final_response, "final_response is empty"
        tools = turns_tool_names(resp)
        if expected_tool:
            assert expected_tool in tools, f"expected tool {expected_tool}, got {tools}"
        return True, "ok", {"tool_calls": tools, "final_response": final_response[:200]}

    return TestCase(suite=suite, name=name, description=description, fn=_run)
