use crate::{
filter::Filter,
state::RustdocTools,
tools::{GetItem, ListCrates, Search, SetWorkingDirectory},
verbosity::Verbosity,
};
use mcplease::traits::Tool;
use std::path::PathBuf;
fn get_fixture_crate_path() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../tests/fixture-crate")
}
fn create_test_state() -> RustdocTools {
let mut state = RustdocTools::new(None)
.expect("Failed to create state")
.with_default_session_id("test");
SetWorkingDirectory {
path: get_fixture_crate_path().to_string_lossy().to_string(),
}
.execute(&mut state)
.unwrap();
state
}
#[test]
fn test_get_crate_root() {
let mut state = create_test_state();
let tool = GetItem {
name: "crate".to_string(),
..Default::default()
};
let result = tool.execute(&mut state).expect("Tool execution failed");
insta::assert_snapshot!(result);
}
#[test]
fn test_show_docs_vs_hide_docs_comparison() {
let mut state = create_test_state();
let tool_with_docs = GetItem {
name: "crate::TestStruct".to_string(),
..Default::default()
};
let result_with_docs = tool_with_docs
.execute(&mut state)
.expect("Tool execution failed");
let tool_no_docs = GetItem {
name: "crate::TestStruct".to_string(),
verbosity: Some(Verbosity::Minimal),
..Default::default()
};
let result_no_docs = tool_no_docs
.execute(&mut state)
.expect("Tool execution failed");
assert!(result_with_docs.len() > result_no_docs.len());
assert!(result_with_docs.contains("struct TestStruct"));
assert!(result_no_docs.contains("struct TestStruct"));
println!("=== WITH DOCS ({} chars) ===", result_with_docs.len());
println!("{result_with_docs}");
println!("\n=== WITHOUT DOCS ({} chars) ===", result_no_docs.len());
println!("{result_no_docs}");
}
#[test]
fn test_verbosity_minimal() {
let mut state = create_test_state();
let tool = GetItem {
name: "crate".to_string(),
verbosity: Some(Verbosity::Minimal),
..Default::default()
};
let result = tool.execute(&mut state).expect("Tool execution failed");
assert!(!result.contains("Documentation:"));
assert!(result.contains("Item: fixture_crate"));
assert!(
result.contains("Structs:") || result.contains("Enums:") || result.contains("Functions:")
);
insta::assert_snapshot!(result);
}
#[test]
fn test_fuzzy_matching_tool_execute() {
let mut state = create_test_state();
let tool = GetItem {
name: "crate::TestStruct::test_metod".to_string(), ..Default::default()
};
let result = tool.execute(&mut state).expect("Tool execution failed");
insta::assert_snapshot!(result);
}
#[test]
fn test_fuzzy_matching_trait_methods() {
let mut state = create_test_state();
let tool = GetItem {
name: "crate::TestStruct::cute".to_string(), ..Default::default()
};
let result = tool.execute(&mut state).expect("Tool execution failed");
assert!(result.contains("Did you mean"));
insta::assert_snapshot!(result);
}
#[test]
fn test_get_struct_details() {
let mut state = create_test_state();
let tool = GetItem {
name: "crate::TestStruct".to_string(),
..Default::default()
};
let result = tool.execute(&mut state).expect("Tool execution failed");
insta::assert_snapshot!(result);
}
#[test]
fn test_get_struct_with_source() {
let mut state = create_test_state();
let tool = GetItem {
name: "crate::TestStruct".to_string(),
include_source: Some(true),
..Default::default()
};
let result = tool.execute(&mut state).expect("Tool execution failed");
let fixture_crate_dir = state.get_context(None).unwrap().unwrap();
let normalized_result = result.replace(
&fixture_crate_dir.to_string_lossy().to_string(),
"/TEST_CRATE_ROOT",
);
insta::assert_snapshot!(normalized_result);
}
#[test]
fn test_get_function_details() {
let mut state = create_test_state();
let tool = GetItem {
name: "crate::test_function".to_string(),
include_source: Some(true),
..Default::default()
};
let result = tool.execute(&mut state).expect("Tool execution failed");
let fixture_crate_dir = state.get_context(None).unwrap().unwrap();
let normalized_result = result.replace(
&fixture_crate_dir.to_string_lossy().to_string(),
"/TEST_CRATE_ROOT",
);
insta::assert_snapshot!(normalized_result);
}
#[test]
fn test_get_submodule() {
let mut state = create_test_state();
let tool = GetItem {
name: "crate::submodule".to_string(),
include_source: None,
..Default::default()
};
let result = tool.execute(&mut state).expect("Tool execution failed");
insta::assert_snapshot!(result);
}
#[test]
fn test_get_enum_details() {
let mut state = create_test_state();
let tool = GetItem {
name: "crate::submodule::TestEnum".to_string(),
..Default::default()
};
let result = tool.execute(&mut state).expect("Tool execution failed");
insta::assert_snapshot!(result);
}
#[test]
fn test_get_generic_struct() {
let mut state = create_test_state();
let tool = GetItem {
name: "crate::GenericStruct".to_string(),
..Default::default()
};
let result = tool.execute(&mut state).expect("Tool execution failed");
insta::assert_snapshot!(result);
}
#[test]
fn test_get_generic_function() {
let mut state = create_test_state();
let tool = GetItem {
name: "crate::generic_function".to_string(),
..Default::default()
};
let result = tool.execute(&mut state).expect("Tool execution failed");
insta::assert_snapshot!(result);
}
#[test]
fn test_get_constants() {
let mut state = create_test_state();
let tool = GetItem {
name: "crate::TEST_CONSTANT".to_string(),
..Default::default()
};
let result = tool.execute(&mut state).expect("Tool execution failed");
insta::assert_snapshot!(result);
}
#[test]
fn test_get_struct_with_private_fields() {
let mut state = create_test_state();
let tool = GetItem {
name: "crate::GenericStruct".to_string(),
..Default::default()
};
let result = tool.execute(&mut state).expect("Tool execution failed");
insta::assert_snapshot!(result);
}
#[test]
fn test_fuzzy_matching_suggestions() {
let mut state = create_test_state();
let tool = GetItem {
name: "crate::TestStruct::incrementCount".to_string(), ..Default::default()
};
let result = tool.execute(&mut state).expect("Tool execution failed");
assert!(result.contains("Did you mean"));
assert!(result.contains("increment_count"));
insta::assert_snapshot!(result);
}
#[test]
fn test_nonexistent_item() {
let mut state = create_test_state();
let tool = GetItem {
name: "crate::DoesNotExist".to_string(),
include_source: None,
..Default::default()
};
let result = tool.execute(&mut state).expect("Tool execution failed");
insta::assert_snapshot!(result);
}
#[test]
fn test_get_unit_struct() {
let mut state = create_test_state();
let tool = GetItem {
name: "crate::UnitStruct".to_string(),
..Default::default()
};
let result = tool.execute(&mut state).expect("Tool execution failed");
insta::assert_snapshot!(result);
}
#[test]
fn test_get_tuple_struct() {
let mut state = create_test_state();
let tool = GetItem {
name: "crate::TupleStruct".to_string(),
..Default::default()
};
let result = tool.execute(&mut state).expect("Tool execution failed");
insta::assert_snapshot!(result);
}
#[test]
fn test_get_generic_enum() {
let mut state = create_test_state();
let tool = GetItem {
name: "crate::GenericEnum".to_string(),
..Default::default()
};
let result = tool.execute(&mut state).expect("Tool execution failed");
insta::assert_snapshot!(result);
}
#[test]
fn test_get_trait_details() {
let mut state = create_test_state();
let tool = GetItem {
name: "crate::TestTrait".to_string(),
..Default::default()
};
let result = tool.execute(&mut state).expect("Tool execution failed");
insta::assert_snapshot!(result);
}
#[test]
fn test_recursive_module_listing() {
let mut state = create_test_state();
let tool = GetItem {
name: "crate".to_string(),
recursive: Some(true),
..Default::default()
};
let result = tool.execute(&mut state).expect("Tool execution failed");
insta::assert_snapshot!(result);
}
#[test]
fn test_recursive_submodule_listing() {
let mut state = create_test_state();
let tool = GetItem {
name: "crate::submodule".to_string(),
recursive: Some(true),
..Default::default()
};
let result = tool.execute(&mut state).expect("Tool execution failed");
insta::assert_snapshot!(result);
}
#[test]
fn test_recursive_filtering() {
let mut state = create_test_state();
let tool = GetItem {
name: "crate".to_string(),
recursive: Some(true),
filter: Some(vec![Filter::Struct]),
..Default::default()
};
let result = tool.execute(&mut state).expect("Tool execution failed");
insta::assert_snapshot!(result);
}
#[test]
fn test_non_recursive_filtering() {
let mut state = create_test_state();
let tool = GetItem {
name: "crate".to_string(),
filter: Some(vec![Filter::Struct]),
..Default::default()
};
let result = tool.execute(&mut state).expect("Tool execution failed");
insta::assert_snapshot!(result);
}
#[test]
fn test_recursive_multiple_filters() {
let mut state = create_test_state();
let tool = GetItem {
name: "crate".to_string(),
recursive: Some(true),
filter: Some(vec![Filter::Function, Filter::Trait]),
..Default::default()
};
let result = tool.execute(&mut state).expect("Tool execution failed");
insta::assert_snapshot!(result);
}
#[test]
fn test_get_std_vec() {
let mut state = create_test_state();
let tool_std_root = GetItem {
name: "std".to_string(),
..Default::default()
};
let result_std_root = tool_std_root
.execute(&mut state)
.expect("Tool execution failed for std root");
insta::assert_snapshot!(result_std_root);
let tool_std_collections_hashmap = GetItem {
name: "std::collections::HashMap".to_string(),
..Default::default()
};
let result_std_collections_hashmap = tool_std_collections_hashmap
.execute(&mut state)
.expect("Tool execution failed for std::collections::HashMap");
insta::assert_snapshot!(result_std_collections_hashmap);
let tool_std_vec_vec = GetItem {
name: "std::vec::Vec".to_string(),
..Default::default()
};
let result_std_vec_vec = tool_std_vec_vec
.execute(&mut state)
.expect("Tool execution failed for std::vec::Vec");
insta::assert_snapshot!(result_std_vec_vec);
}
#[test]
fn test_get_item_with_normalized_crate_name() {
let mut state = create_test_state();
let tool = GetItem {
name: "fixture-crate::TestStruct".to_string(),
..Default::default()
};
let result = tool.execute(&mut state).expect("Tool execution failed");
insta::assert_snapshot!(result);
}
#[test]
fn test_get_complex_trait_details() {
let mut state = create_test_state();
let tool = GetItem {
name: "crate::ComplexTrait".to_string(),
..Default::default()
};
let result = tool.execute(&mut state).expect("Tool execution failed");
insta::assert_snapshot!(result);
}
#[test]
fn tools_doesnt_panic() {
use crate::tools::Tools;
use mcplease::traits::AsToolsList;
Tools::tools_list();
}
#[test]
fn list_crates() {
let mut state = create_test_state();
let result = ListCrates::default().execute(&mut state).unwrap();
insta::assert_snapshot!(result);
}
#[test]
fn search() {
let mut state = create_test_state();
let result = Search {
crate_name: "crate".into(),
query: "trigger line-based truncation".into(),
limit: None,
}
.execute(&mut state)
.unwrap();
insta::assert_snapshot!(result);
}
#[test]
fn search_2() {
let mut state = create_test_state();
let result = Search {
crate_name: "crate".into(),
query: "generic struct".into(),
limit: None,
}
.execute(&mut state)
.unwrap();
insta::assert_snapshot!(result);
}