use sgclaw::pipe::{Action, AgentMessage, BrowserMessage, SecurityFields, Timing};

#[test]
fn browser_init_round_trip_uses_frozen_wire_format() {
    let raw = r#"{"type":"init","version":"1.0","hmac_seed":"0123456789abcdef","capabilities":["browser_action"]}"#;
    let message: BrowserMessage = serde_json::from_str(raw).unwrap();

    assert!(matches!(
        message,
        BrowserMessage::Init {
            ref version,
            ref hmac_seed,
            ref capabilities
        } if version == "1.0"
            && hmac_seed == "0123456789abcdef"
            && *capabilities == vec!["browser_action".to_string()]
    ));

    assert_eq!(serde_json::to_string(&message).unwrap(), raw);
}

#[test]
fn command_serializes_action_and_security_fields() {
    let message = AgentMessage::Command {
        seq: 1,
        action: Action::GetText,
        params: serde_json::json!({ "selector": "#submit" }),
        security: SecurityFields {
            expected_domain: "oa.example.com".to_string(),
            hmac: "abc123".to_string(),
        },
    };

    let raw = serde_json::to_string(&message).unwrap();

    assert!(raw.contains(r#""type":"command""#));
    assert!(raw.contains(r#""action":"getText""#));
    assert!(raw.contains(r#""expected_domain":"oa.example.com""#));
}

#[test]
fn response_deserializes_timing_and_payload() {
    let raw = r#"{"type":"response","seq":7,"success":true,"data":{"text":"提交成功"},"aom_snapshot":[],"timing":{"queue_ms":2,"exec_ms":38}}"#;
    let message: BrowserMessage = serde_json::from_str(raw).unwrap();

    assert_eq!(
        message,
        BrowserMessage::Response {
            seq: 7,
            success: true,
            data: serde_json::json!({"text": "提交成功"}),
            aom_snapshot: vec![],
            timing: Timing {
                queue_ms: 2,
                exec_ms: 38,
            },
        }
    );
}
