use std::fs;
#[test]
fn test_persist_concepts_creates_markdown() {
let tmp = tempfile::TempDir::new().unwrap();
let kg_path = tmp.path().to_path_buf();
let concept_name = "Test Concept";
let synonyms = ["test".to_string(), "example".to_string()];
let relationships = ["related-concept".to_string()];
std::fs::create_dir_all(&kg_path).unwrap();
let slug = concept_name
.to_lowercase()
.replace(|c: char| !c.is_alphanumeric() && c != '-', "-");
let filename = format!("learned-{}.md", slug);
let filepath = kg_path.join(&filename);
let synonyms_line = format!("\nsynonyms:: {}", synonyms.join(", "));
let relationships_line = format!("\nrelated:: {}", relationships.join(", "));
let content = format!(
"# {}\n\nDiscovered during search: \"{}\"{}{}\n",
concept_name, "test query", synonyms_line, relationships_line
);
std::fs::write(&filepath, &content).unwrap();
assert!(filepath.exists(), "Expected markdown file to be created");
let read_content = fs::read_to_string(&filepath).unwrap();
assert!(
read_content.contains("# Test Concept"),
"Expected concept name in title"
);
assert!(
read_content.contains("synonyms:: test, example"),
"Expected synonyms in content"
);
assert!(
read_content.contains("related:: related-concept"),
"Expected relationships in content"
);
assert!(
read_content.contains("Discovered during search"),
"Expected source query attribution"
);
}
#[test]
fn test_persist_concepts_skips_existing() {
let tmp = tempfile::TempDir::new().unwrap();
let kg_path = tmp.path().to_path_buf();
let existing = kg_path.join("learned-existing-concept.md");
std::fs::create_dir_all(&kg_path).unwrap();
std::fs::write(&existing, "# existing content").unwrap();
assert!(existing.exists());
let content = fs::read_to_string(&existing).unwrap();
assert_eq!(
content, "# existing content",
"Existing file should not be overwritten"
);
}
#[test]
fn test_slug_generation() {
let concept_name = "Test Concept-With Special!Chars";
let slug = concept_name
.to_lowercase()
.replace(|c: char| !c.is_alphanumeric() && c != '-', "-");
assert_eq!(slug, "test-concept-with-special-chars");
}