//! Integration tests for kb-mcp

use kogral_mcp::{prompts, resources, tools};
use serde_json::json;

#[tokio::test]
async fn test_list_tools() {
    let tools = tools::list_tools();
    assert!(!tools.is_empty());
    assert!(tools.iter().any(|t| t.name == "kogral/search"));
    assert!(tools.iter().any(|t| t.name == "kogral/add_note"));
    assert!(tools.iter().any(|t| t.name == "kogral/add_decision"));
}

#[tokio::test]
async fn test_list_resources() {
    let resources = resources::list_resources().await.unwrap();
    assert!(!resources.is_empty());
    assert!(resources.iter().any(|r| r.uri == "kogral://project/notes"));
    assert!(resources
        .iter()
        .any(|r| r.uri == "kogral://shared/guidelines"));
}

#[test]
fn test_list_prompts() {
    let prompts = prompts::list_prompts();
    assert!(!prompts.is_empty());
    assert!(prompts.iter().any(|p| p.name == "kogral/summarize_project"));
    assert!(prompts.iter().any(|p| p.name == "kogral/find_related"));
}

#[tokio::test]
async fn test_execute_search_tool() {
    let args = json!({
        "query": "test",
        "limit": 5
    });

    // This test expects the tool to execute even if no graph exists
    // The tool should return an empty result or error gracefully
    let result = tools::execute_tool("kogral/search", args).await;

    // Tool execution should succeed (even with no results)
    // The actual search may fail if graph doesn't exist, but tool dispatch should
    // work
    match result {
        Ok(_) => assert!(true),
        Err(e) => {
            // If it errors, it should be due to missing graph, not a tool execution error
            assert!(e.to_string().contains("not found") || e.to_string().contains("Graph"));
        }
    }
}

#[tokio::test]
async fn test_execute_list_graphs_tool() {
    let result = tools::execute_tool("kogral/list_graphs", json!({})).await;
    assert!(result.is_ok());
}
