use std::collections::HashMap;
use crate::core::quality;
use crate::service::diagnostics_dispatch;
use crate::service::handlers::analysis::SmellItem;
use crate::types::CodeChunk;
fn make_chunk(id: &str, file: &str, content: &str) -> CodeChunk {
CodeChunk {
id: id.into(),
file: file.into(),
start_line: 1,
end_line: 10,
content: content.into(),
function_name: None,
score: 0.0,
compact_snippet: None,
match_reason: "test".into(),
}
}
#[test]
fn run_diagnostics_blocking_two_files_same_basename() {
let mut by_file = HashMap::new();
by_file.insert("src/a/main.rs".to_string(), "fn a() {}".to_string());
by_file.insert("src/b/main.rs".to_string(), "fn b() {}".to_string());
let _report = diagnostics_dispatch::run_diagnostics_blocking(by_file, None, None, None);
}
#[test]
fn smell_item_omit_content_default_strips_content() {
let chunk = make_chunk("id1", "src/main.rs", "fn main() {}");
let item = SmellItem::from_chunk(&chunk, false);
let json = serde_json::to_value(&item).unwrap();
assert!(
json.get("content").is_none(),
"content must be absent when omit_content=true"
);
assert_eq!(json["file"], "src/main.rs");
assert_eq!(json["id"], "id1");
}
#[test]
fn smell_item_include_content_restores_text() {
let chunk = make_chunk("id2", "src/lib.rs", "fn foo() { 42 }");
let item = SmellItem::from_chunk(&chunk, true);
let json = serde_json::to_value(&item).unwrap();
assert_eq!(json["content"], "fn foo() { 42 }");
}
#[test]
fn smells_pagination_slice_and_envelope() {
let chunks: Vec<CodeChunk> = (0..10)
.map(|i| {
let mut body = format!("fn big_{i}() {{\n");
for _ in 0..60 {
body.push_str(" let _ = 1;\n");
}
body.push_str("}\n");
make_chunk(&format!("c{i}"), "f.rs", &body)
})
.collect();
let smelly = quality::smelly_chunks(&chunks);
let total = smelly.len();
let offset = 3usize;
let limit = 4usize;
let page: Vec<SmellItem> = smelly
.iter()
.skip(offset)
.take(limit)
.map(|c| SmellItem::from_chunk(c, false))
.collect();
let returned = page.len();
let truncated = (offset + returned) < total;
assert_eq!(returned, 4);
assert!(
truncated,
"should be truncated: total={total} offset={offset} returned={returned}"
);
}
#[test]
fn smells_pagination_offset_beyond_total_returns_empty() {
let chunks: Vec<CodeChunk> = (0..3)
.map(|i| {
let mut body = format!("fn big_{i}() {{\n");
for _ in 0..60 {
body.push_str(" let _ = 1;\n");
}
body.push_str("}\n");
make_chunk(&format!("c{i}"), "f.rs", &body)
})
.collect();
let smelly = quality::smelly_chunks(&chunks);
let total = smelly.len();
let offset = 100usize;
let limit = 10usize;
let page: Vec<SmellItem> = smelly
.iter()
.skip(offset)
.take(limit)
.map(|c| SmellItem::from_chunk(c, false))
.collect();
let returned = page.len();
let truncated = (offset + returned) < total;
assert_eq!(returned, 0);
assert!(!truncated, "should not be truncated when page is empty");
}