use serde_json::json;
use sgclaw::agent::planner::{plan_instruction, PlannerError};
use sgclaw::pipe::Action;

#[test]
fn planner_converts_baidu_search_instruction_into_three_steps() {
    let plan = plan_instruction("打开百度搜索天气").unwrap();

    assert_eq!(plan.summary, "已在百度搜索天气");
    assert_eq!(plan.steps.len(), 3);
    assert_eq!(plan.steps[0].action, Action::Navigate);
    assert_eq!(
        plan.steps[0].params,
        json!({ "url": "https://www.baidu.com" })
    );
    assert_eq!(plan.steps[1].action, Action::Type);
    assert_eq!(
        plan.steps[1].params,
        json!({ "selector": "#kw", "text": "天气", "clear_first": true })
    );
    assert_eq!(plan.steps[2].action, Action::Click);
    assert_eq!(plan.steps[2].params, json!({ "selector": "#su" }));
}

#[test]
fn planner_supports_baidu_search_variant_with_conjunction() {
    let plan = plan_instruction("打开百度并搜索电网调度").unwrap();

    assert_eq!(plan.summary, "已在百度搜索电网调度");
    assert_eq!(plan.steps[1].params["text"], "电网调度");
}

#[test]
fn planner_supports_zhihu_search_instruction_with_direct_search_url() {
    let plan = plan_instruction("打开知乎搜索天气").unwrap();

    assert_eq!(plan.summary, "已在知乎搜索天气");
    assert_eq!(plan.steps.len(), 1);
    assert_eq!(plan.steps[0].action, Action::Navigate);
    assert_eq!(
        plan.steps[0].params,
        json!({ "url": "https://www.zhihu.com/search?type=content&q=%E5%A4%A9%E6%B0%94" })
    );
    assert_eq!(plan.steps[0].expected_domain, "www.zhihu.com");
    assert_eq!(
        plan.steps[0].log_message,
        "navigate https://www.zhihu.com/search?type=content&q=%E5%A4%A9%E6%B0%94"
    );
}

#[test]
fn planner_rejects_unrelated_instruction() {
    let err = plan_instruction("打开谷歌搜索天气").unwrap_err();

    assert_eq!(
        err,
        PlannerError::UnsupportedInstruction("打开谷歌搜索天气".to_string())
    );
}
