//! Agent-task command provider/contract metadata export tests.

use super::support::*;

#[test]
fn providers_output_includes_core_capability_contract() {
    with_isolated_home(|_| {
        let (value, status) = review::providers(ProvidersArgs {
            backend: None,
            selector: None,
            secret_env: Vec::new(),
            validate_readiness: false,
            refresh: false,
        })
        .expect("providers output");

        assert_eq!(status, 0);
        assert_eq!(
            value["capability_contract"]["schema"],
            AGENT_TASK_PROVIDER_CAPABILITY_CONTRACT_SCHEMA
        );
        assert_eq!(
            value["capability_contract"]["provider_schema"],
            AGENT_TASK_EXECUTOR_PROVIDER_SCHEMA
        );
        assert_eq!(
            value["capability_contract"]["request_schema"],
            AGENT_TASK_REQUEST_SCHEMA
        );
        assert_eq!(
            value["capability_contract"]["outcome_schema"],
            AGENT_TASK_OUTCOME_SCHEMA
        );
    });
}

#[test]
fn contract_output_exports_core_agent_task_metadata() {
    let (value, status) = contract::contract(ContractArgs {
        format: ContractFormat::Json,
    })
    .expect("contract output");

    assert_eq!(status, 0);
    assert_eq!(value["schema"], "homeboy/agent-task-core-contract/v1");
    assert_eq!(value["schemas"]["request"], AGENT_TASK_REQUEST_SCHEMA);
    assert_eq!(
        value["schemas"]["artifact_declaration"],
        "homeboy/agent-task-artifact-declaration/v1"
    );
    assert_eq!(
        value["schemas"]["artifact_handoff"],
        "homeboy/agent-task-artifact-handoff/v1"
    );
    assert_eq!(
        value["schemas"]["evidence_ref"],
        "homeboy/agent-task-evidence-ref/v1"
    );
    assert_eq!(
        value["schemas"]["loop_action"],
        "homeboy/agent-task-loop-action/v1"
    );
    assert_eq!(
        value["schemas"]["provider_outcome_contract"],
        "homeboy/agent-task-provider-outcome-contract/v1"
    );
    assert_eq!(
        value["schemas"]["diagnostic_ranking"],
        "homeboy/agent-task-diagnostic-ranking/v1"
    );
    assert_eq!(
        value["schemas"]["secret_env_requirement"],
        "homeboy/secret-env-requirement/v1"
    );
    assert_eq!(
        value["schemas"]["loop_definition"],
        "homeboy/agent-task-loop-definition/v1"
    );
    assert_eq!(
        value["schemas"]["provider"],
        AGENT_TASK_EXECUTOR_PROVIDER_SCHEMA
    );
    assert_eq!(
        value["provider_capability"]["schema"],
        AGENT_TASK_PROVIDER_CAPABILITY_CONTRACT_SCHEMA
    );
    assert_eq!(
        value["provider_capability"]["request_required_fields"],
        json!(["schema", "task_id", "executor.backend", "instructions"])
    );
    assert_eq!(
        value["provider_capability"]["redacted_metadata_keys"],
        json!(["secret_env_values", "secretEnvValues", "secrets"])
    );
    assert!(value["enums"]["outcome_status"]
        .as_array()
        .expect("outcome statuses")
        .contains(&json!("provider_error")));
    assert!(value["enums"]["failure_classification"]
        .as_array()
        .expect("failure classifications")
        .contains(&json!("capability_missing")));
    assert!(value["redaction_defaults"]["sensitive_keys"]
        .as_array()
        .expect("sensitive keys")
        .contains(&json!("refresh_token")));
}

#[test]
fn generic_loop_contract_fixtures_are_versioned_and_provider_neutral() {
    let fixtures = [
        include_str!("../../../../tests/fixtures/agent_task_contract/successful_required_artifact_handoff.json"),
        include_str!("../../../../tests/fixtures/agent_task_contract/nested_runtime_import_failure.json"),
        include_str!("../../../../tests/fixtures/agent_task_contract/local_file_evidence_refs.json"),
        include_str!("../../../../tests/fixtures/agent_task_contract/missing_required_artifact.json"),
    ];

    for raw in fixtures {
        assert!(!raw.contains("WordPress"));
        assert!(!raw.contains("WP Codebox"));
        assert!(!raw.contains("Data Machine"));
        assert!(!raw.contains("WPSG"));
        assert!(!raw.contains("wp-codebox"));
        let outcome: AgentTaskOutcome = serde_json::from_str(raw).expect("fixture outcome");
        assert_eq!(outcome.schema, AGENT_TASK_OUTCOME_SCHEMA);
    }
}
