#[cfg(test)]
mod kg_protocol_resolution_test {
use ahash::AHashMap;
use std::sync::Arc;
use terraphim_config::{
Config, ConfigId, ConfigState, KnowledgeGraph, KnowledgeGraphLocal, Role,
};
use terraphim_service::TerraphimService;
use terraphim_types::{KnowledgeGraphInputType, RelevanceFunction, RoleName};
use tokio::sync::Mutex;
#[tokio::test]
async fn test_kg_protocol_resolves_to_definition_documents() {
println!("๐งช Testing KG protocol resolution to definition documents...");
let test_role = Role {
shortname: Some("test-engineer".to_string()),
name: "Test Engineer".to_string().into(),
relevance_function: RelevanceFunction::TerraphimGraph,
terraphim_it: true, theme: "superhero".to_string(),
kg: Some(KnowledgeGraph {
automata_path: None, knowledge_graph_local: Some(KnowledgeGraphLocal {
input_type: KnowledgeGraphInputType::Markdown,
path: std::env::current_dir().unwrap().join("docs/src/kg"),
}),
public: false,
publish: false,
}),
haystacks: vec![],
llm_enabled: false,
llm_api_key: None,
llm_model: None,
llm_auto_summarize: false,
llm_chat_enabled: false,
llm_chat_system_prompt: None,
llm_chat_model: None,
llm_context_window: None,
extra: AHashMap::new(),
llm_router_enabled: false,
llm_router_config: None,
};
let role_name = RoleName::new("Test Engineer");
let mut roles = AHashMap::new();
roles.insert(role_name.clone(), test_role);
let config = Config {
id: ConfigId::Server,
global_shortcut: "Cmd+Space".to_string(),
roles,
default_role: role_name.clone(),
selected_role: role_name.clone(),
};
let config_state = ConfigState {
config: Arc::new(Mutex::new(config)),
roles: AHashMap::new(),
};
let mut terraphim_service = TerraphimService::new(config_state);
let test_cases = vec![
("terraphim-graph", "./docs/src/kg/terraphim-graph.md"),
("graph", "./docs/src/kg/terraphim-graph.md"), ("graph embeddings", "./docs/src/kg/terraphim-graph.md"), (
"knowledge graph based embeddings",
"./docs/src/kg/terraphim-graph.md",
), ];
println!("๐ Testing {} KG term resolution cases", test_cases.len());
for (kg_term, expected_doc_path) in test_cases {
println!(
" ๐ Testing KG term: '{}' โ should resolve to '{}'",
kg_term, expected_doc_path
);
let documents = terraphim_service
.find_documents_for_kg_term(&role_name, kg_term)
.await
.unwrap_or_else(|_| panic!("Failed to find documents for KG term '{}'", kg_term));
println!(
" ๐ Found {} documents for term '{}'",
documents.len(),
kg_term
);
assert!(
!documents.is_empty(),
"No documents found for KG term '{}' - should find definition document at '{}'",
kg_term,
expected_doc_path
);
let found_definition_doc = documents.iter().find(|doc| {
doc.url.contains("terraphim-graph.md")
|| doc.url.ends_with("terraphim-graph.md")
|| doc.id.contains("terraphim-graph")
});
assert!(
found_definition_doc.is_some(),
"KG term '{}' should resolve to definition document '{}', but found documents: {:?}",
kg_term,
expected_doc_path,
documents.iter().map(|d| &d.url).collect::<Vec<_>>()
);
let definition_doc = found_definition_doc.unwrap();
println!(
" โ
Found definition document: '{}' (url: '{}')",
definition_doc.title, definition_doc.url
);
assert!(
!definition_doc.body.is_empty(),
"Definition document body should not be empty for term '{}'",
kg_term
);
assert!(
definition_doc.body.contains("synonyms::")
|| definition_doc.body.contains("graph embeddings")
|| definition_doc.body.contains("knowledge graph"),
"Definition document for '{}' should contain KG content with synonyms, found: '{}'",
kg_term,
definition_doc.body.chars().take(200).collect::<String>()
);
println!(" โ
Definition document contains expected KG content");
}
println!("๐ All KG protocol resolution tests passed!");
println!(" โ KG terms resolve to correct definition documents");
println!(" โ Synonyms resolve to the same definition document");
println!(" โ Definition documents contain expected KG content");
}
#[tokio::test]
async fn test_kg_synonyms_resolve_to_same_definition() {
println!("๐งช Testing that KG synonyms resolve to the same definition document...");
let test_role = Role {
shortname: Some("synonym-test".to_string()),
name: "Synonym Test".to_string().into(),
relevance_function: RelevanceFunction::TerraphimGraph,
terraphim_it: true,
theme: "lumen".to_string(),
kg: Some(KnowledgeGraph {
automata_path: None,
knowledge_graph_local: Some(KnowledgeGraphLocal {
input_type: KnowledgeGraphInputType::Markdown,
path: std::env::current_dir().unwrap().join("docs/src/kg"),
}),
public: false,
publish: false,
}),
haystacks: vec![],
llm_enabled: false,
llm_api_key: None,
llm_model: None,
llm_auto_summarize: false,
llm_chat_enabled: false,
llm_chat_system_prompt: None,
llm_chat_model: None,
llm_context_window: None,
extra: AHashMap::new(),
llm_router_enabled: false,
llm_router_config: None,
};
let role_name = RoleName::new("Synonym Test");
let mut roles = AHashMap::new();
roles.insert(role_name.clone(), test_role);
let config = Config {
id: ConfigId::Server,
global_shortcut: "Cmd+Space".to_string(),
roles,
default_role: role_name.clone(),
selected_role: role_name.clone(),
};
let config_state = ConfigState {
config: Arc::new(Mutex::new(config)),
roles: AHashMap::new(),
};
let mut terraphim_service = TerraphimService::new(config_state);
let synonyms = vec![
"graph embeddings",
"graph",
"knowledge graph based embeddings",
];
println!(
"๐ Testing {} synonyms resolve to same definition",
synonyms.len()
);
let mut resolved_docs = Vec::new();
for synonym in &synonyms {
println!(" ๐ Resolving synonym: '{}'", synonym);
let documents = terraphim_service
.find_documents_for_kg_term(&role_name, synonym)
.await
.unwrap_or_else(|_| panic!("Failed to find documents for synonym '{}'", synonym));
let definition_doc = documents.iter().find(|doc| {
doc.url.contains("terraphim-graph") || doc.id.contains("terraphim-graph")
});
assert!(
definition_doc.is_some(),
"Synonym '{}' should resolve to terraphim-graph definition document",
synonym
);
let doc = definition_doc.unwrap();
resolved_docs.push((synonym, doc.url.clone(), doc.id.clone()));
println!(" โ
Resolved to: '{}' (id: '{}')", doc.url, doc.id);
}
let first_url = &resolved_docs[0].1;
let first_id = &resolved_docs[0].2;
for (synonym, url, id) in &resolved_docs {
assert_eq!(
url, first_url,
"All synonyms should resolve to the same URL. '{}' resolved to '{}' but expected '{}'",
synonym, url, first_url
);
assert!(
id.contains("terraphim-graph") && first_id.contains("terraphim-graph"),
"All synonyms should resolve to documents with 'terraphim-graph' in ID. '{}' resolved to '{}' but expected similar to '{}'",
synonym,
id,
first_id
);
}
println!("๐ Synonym resolution test passed!");
println!(
" โ All {} synonyms resolve to the same definition document",
synonyms.len()
);
println!(" โ Definition document: '{}'", first_url);
}
}