import json

from app.harness.steps_json_traces import steps_json_to_trace_rows


def test_parses_tool_call_into_name_and_args():
    # 真实 infer_adhoc steps_json 形态：tool_call={"tool":..., "args":...}（非 "name"）
    steps = [
        {
            "thought": "看根节点",
            "tool_call": {"tool": "view_node", "args": {"node_id": "v_L1_000"}},
            "tool_output": "o0",
        },
        {
            "thought": "搜索",
            "tool_call": {"tool": "search_similar", "args": {"query": "gadget"}},
            "tool_output": "hit",
        },
    ]
    rows = steps_json_to_trace_rows("vid1", "q1", json.dumps(steps))
    assert [r["step"] for r in rows] == [0, 1]
    assert rows[0]["tool_name"] == "view_node"
    assert rows[0]["tool_args"] == {"node_id": "v_L1_000"}
    assert rows[0]["video_id"] == "vid1" and rows[0]["question_id"] == "q1"
    assert rows[1]["tool_output"] == "hit"


def test_empty_or_blank_steps_json_returns_empty():
    assert steps_json_to_trace_rows("v", "q", "") == []
    assert steps_json_to_trace_rows("v", "q", "[]") == []
