//! End-to-end: load_extensions_manifest reads a TOML file and produces a
//! registry that drives the dispatcher.

#![cfg_attr(test, allow(clippy::expect_used, clippy::unwrap_used))]

use capo_agent::extensions::{diagnostic::DiagnosticSeverity, load_extensions_manifest};

#[tokio::test]
async fn missing_manifest_returns_empty_registry_no_error() {
    let path = std::env::temp_dir().join(format!("capo-v07-missing-{}.toml", std::process::id()));
    let (reg, diags) = load_extensions_manifest(&path).await;
    assert!(reg.extensions.is_empty());
    assert!(diags.is_empty());
}

#[tokio::test]
async fn malformed_manifest_returns_empty_with_diagnostic() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("extensions.toml");
    std::fs::write(&path, "this is = not valid = toml =").expect("write");
    let (reg, diags) = load_extensions_manifest(&path).await;
    assert!(reg.extensions.is_empty());
    assert_eq!(diags.len(), 1);
    assert_eq!(diags[0].severity, DiagnosticSeverity::Error);
    assert!(diags[0].message.contains("parse"));
}

#[tokio::test]
async fn valid_manifest_with_one_entry_loads() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("extensions.toml");
    std::fs::write(
        &path,
        r#"
            [[extensions]]
            name = "dirty"
            command = "/bin/true"
            hooks = ["session_before_switch"]
        "#,
    )
    .expect("write");
    let (reg, diags) = load_extensions_manifest(&path).await;
    assert_eq!(reg.extensions.len(), 1);
    assert_eq!(reg.extensions[0].entry.name, "dirty");
    assert!(diags.is_empty());
}
