generated from nhcarrigan/template
feat: add tests and assert coverage (#71)
### Explanation _No response_ ### Issue _No response_ ### Attestations - [ ] I have read and agree to the [Code of Conduct](https://docs.nhcarrigan.com/community/coc/) - [ ] I have read and agree to the [Community Guidelines](https://docs.nhcarrigan.com/community/guide/). - [ ] My contribution complies with the [Contributor Covenant](https://docs.nhcarrigan.com/dev/covenant/). ### Dependencies - [ ] I have pinned the dependencies to a specific patch version. ### Style - [ ] I have run the linter and resolved any errors. - [ ] My pull request uses an appropriate title, matching the conventional commit standards. - [ ] My scope of feat/fix/chore/etc. correctly matches the nature of changes in my pull request. ### Tests - [ ] My contribution adds new code, and I have added tests to cover it. - [ ] My contribution modifies existing code, and I have updated the tests to reflect these changes. - [ ] All new and existing tests pass locally with my changes. - [ ] Code coverage remains at or above the configured threshold. ### Documentation _No response_ ### Versioning _No response_ Co-authored-by: Hikari <hikari@nhcarrigan.com> Reviewed-on: #71 Co-authored-by: Naomi Carrigan <commits@nhcarrigan.com> Co-committed-by: Naomi Carrigan <commits@nhcarrigan.com>
This commit was merged in pull request #71.
This commit is contained in:
@@ -205,6 +205,19 @@ pub async fn reset_default_snippets(app: AppHandle) -> Result<(), String> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::collections::HashSet;
|
||||
|
||||
fn create_test_snippet(id: &str, name: &str, category: &str, is_default: bool) -> Snippet {
|
||||
Snippet {
|
||||
id: id.to_string(),
|
||||
name: name.to_string(),
|
||||
content: "Test content".to_string(),
|
||||
category: category.to_string(),
|
||||
is_default,
|
||||
created_at: Utc::now(),
|
||||
updated_at: Utc::now(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_default_snippets_exist() {
|
||||
@@ -223,4 +236,204 @@ mod tests {
|
||||
assert!(!snippet.category.is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_default_snippets_count() {
|
||||
let defaults = get_default_snippets();
|
||||
// Should have 8 default snippets
|
||||
assert_eq!(defaults.len(), 8);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_default_snippets_have_unique_ids() {
|
||||
let defaults = get_default_snippets();
|
||||
let ids: HashSet<&String> = defaults.iter().map(|s| &s.id).collect();
|
||||
assert_eq!(ids.len(), defaults.len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_default_snippets_ids_start_with_default() {
|
||||
let defaults = get_default_snippets();
|
||||
assert!(defaults.iter().all(|s| s.id.starts_with("default-")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_snippet_serialization() {
|
||||
let snippet = create_test_snippet("test-1", "Test Snippet", "Testing", false);
|
||||
let json = serde_json::to_string(&snippet).expect("Failed to serialize");
|
||||
let parsed: Snippet = serde_json::from_str(&json).expect("Failed to deserialize");
|
||||
|
||||
assert_eq!(parsed.id, snippet.id);
|
||||
assert_eq!(parsed.name, snippet.name);
|
||||
assert_eq!(parsed.content, snippet.content);
|
||||
assert_eq!(parsed.category, snippet.category);
|
||||
assert_eq!(parsed.is_default, snippet.is_default);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_snippet_clone() {
|
||||
let original = create_test_snippet("clone-test", "Clone Test", "Category", true);
|
||||
let cloned = original.clone();
|
||||
|
||||
assert_eq!(original.id, cloned.id);
|
||||
assert_eq!(original.name, cloned.name);
|
||||
assert_eq!(original.is_default, cloned.is_default);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[allow(clippy::useless_vec)]
|
||||
fn test_snippet_sorting_by_category_then_name() {
|
||||
let mut snippets = vec![
|
||||
create_test_snippet("s1", "Zebra", "B-Category", false),
|
||||
create_test_snippet("s2", "Apple", "A-Category", false),
|
||||
create_test_snippet("s3", "Banana", "B-Category", false),
|
||||
create_test_snippet("s4", "Alpha", "A-Category", false),
|
||||
];
|
||||
|
||||
// Sort by category, then by name (mimics list_snippets behavior)
|
||||
snippets.sort_by(|a, b| {
|
||||
let cat_cmp = a.category.cmp(&b.category);
|
||||
if cat_cmp == std::cmp::Ordering::Equal {
|
||||
a.name.cmp(&b.name)
|
||||
} else {
|
||||
cat_cmp
|
||||
}
|
||||
});
|
||||
|
||||
// A-Category should come first
|
||||
assert_eq!(snippets[0].category, "A-Category");
|
||||
assert_eq!(snippets[1].category, "A-Category");
|
||||
assert_eq!(snippets[2].category, "B-Category");
|
||||
assert_eq!(snippets[3].category, "B-Category");
|
||||
|
||||
// Within categories, alphabetically by name
|
||||
assert_eq!(snippets[0].name, "Alpha");
|
||||
assert_eq!(snippets[1].name, "Apple");
|
||||
assert_eq!(snippets[2].name, "Banana");
|
||||
assert_eq!(snippets[3].name, "Zebra");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_known_default_snippets() {
|
||||
let defaults = get_default_snippets();
|
||||
let ids: Vec<&str> = defaults.iter().map(|s| s.id.as_str()).collect();
|
||||
|
||||
assert!(ids.contains(&"default-explain-code"));
|
||||
assert!(ids.contains(&"default-fix-error"));
|
||||
assert!(ids.contains(&"default-write-tests"));
|
||||
assert!(ids.contains(&"default-refactor"));
|
||||
assert!(ids.contains(&"default-optimize"));
|
||||
assert!(ids.contains(&"default-review-pr"));
|
||||
assert!(ids.contains(&"default-add-comments"));
|
||||
assert!(ids.contains(&"default-security-review"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_default_snippet_categories() {
|
||||
let defaults = get_default_snippets();
|
||||
let categories: HashSet<&String> = defaults.iter().map(|s| &s.category).collect();
|
||||
|
||||
assert!(categories.contains(&"Code Review".to_string()));
|
||||
assert!(categories.contains(&"Debugging".to_string()));
|
||||
assert!(categories.contains(&"Testing".to_string()));
|
||||
assert!(categories.contains(&"Performance".to_string()));
|
||||
assert!(categories.contains(&"Documentation".to_string()));
|
||||
assert!(categories.contains(&"Security".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_snippet_content_not_empty() {
|
||||
let defaults = get_default_snippets();
|
||||
for snippet in defaults {
|
||||
assert!(
|
||||
snippet.content.len() > 10,
|
||||
"Content should be meaningful: {}",
|
||||
snippet.name
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_snippet_timestamps() {
|
||||
let snippet = create_test_snippet("time-test", "Time Test", "Cat", false);
|
||||
assert!(snippet.created_at <= snippet.updated_at);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_default_snippets_have_same_timestamps() {
|
||||
let defaults = get_default_snippets();
|
||||
// All defaults are created at the same instant
|
||||
let first_created = defaults[0].created_at;
|
||||
let first_updated = defaults[0].updated_at;
|
||||
|
||||
for snippet in &defaults {
|
||||
assert_eq!(snippet.created_at, first_created);
|
||||
assert_eq!(snippet.updated_at, first_updated);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_snippet_retain_non_default() {
|
||||
let mut snippets = vec![
|
||||
create_test_snippet("default-1", "Default 1", "Cat", true),
|
||||
create_test_snippet("custom-1", "Custom 1", "Cat", false),
|
||||
create_test_snippet("default-2", "Default 2", "Cat", true),
|
||||
create_test_snippet("custom-2", "Custom 2", "Cat", false),
|
||||
];
|
||||
|
||||
// Mimics reset_default_snippets behavior (retain non-defaults)
|
||||
snippets.retain(|s| !s.is_default);
|
||||
|
||||
assert_eq!(snippets.len(), 2);
|
||||
assert!(snippets.iter().all(|s| !s.is_default));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[allow(clippy::useless_vec)]
|
||||
fn test_snippet_find_by_id() {
|
||||
let snippets = vec![
|
||||
create_test_snippet("snippet-1", "First", "Cat", false),
|
||||
create_test_snippet("snippet-2", "Second", "Cat", false),
|
||||
create_test_snippet("snippet-3", "Third", "Cat", false),
|
||||
];
|
||||
|
||||
let found = snippets.iter().find(|s| s.id == "snippet-2");
|
||||
assert!(found.is_some());
|
||||
assert_eq!(found.unwrap().name, "Second");
|
||||
|
||||
let not_found = snippets.iter().find(|s| s.id == "snippet-999");
|
||||
assert!(not_found.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[allow(clippy::useless_vec)]
|
||||
fn test_extract_categories_sorted_and_deduped() {
|
||||
let snippets = vec![
|
||||
create_test_snippet("s1", "S1", "Zebra", false),
|
||||
create_test_snippet("s2", "S2", "Alpha", false),
|
||||
create_test_snippet("s3", "S3", "Beta", false),
|
||||
create_test_snippet("s4", "S4", "Alpha", false), // Duplicate
|
||||
];
|
||||
|
||||
let mut categories: Vec<String> = snippets.iter().map(|s| s.category.clone()).collect();
|
||||
categories.sort();
|
||||
categories.dedup();
|
||||
|
||||
assert_eq!(categories.len(), 3);
|
||||
assert_eq!(categories[0], "Alpha");
|
||||
assert_eq!(categories[1], "Beta");
|
||||
assert_eq!(categories[2], "Zebra");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_snippet_category_code_review_count() {
|
||||
let defaults = get_default_snippets();
|
||||
let code_review_count = defaults
|
||||
.iter()
|
||||
.filter(|s| s.category == "Code Review")
|
||||
.count();
|
||||
|
||||
// There should be multiple code review snippets
|
||||
assert!(code_review_count >= 2);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user