Skip to content

Commit 01c7d36

Browse files
haasonsaasclaude
andcommitted
TDD: fix index-out-of-bounds panic when selecting from empty comments list
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 4f279f2 commit 01c7d36

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/commands/misc.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,42 @@ fn select_discussion_comment(
351351
return Ok(selected);
352352
}
353353

354-
Ok(comments[0].clone())
354+
comments
355+
.first()
356+
.cloned()
357+
.ok_or_else(|| anyhow::anyhow!("No comments available"))
358+
}
359+
360+
#[cfg(test)]
361+
mod tests {
362+
use super::*;
363+
364+
#[test]
365+
fn test_select_discussion_comment_empty_comments() {
366+
// Should return an error, not panic
367+
let result = select_discussion_comment(&[], None, None);
368+
assert!(result.is_err());
369+
}
370+
371+
#[test]
372+
fn test_select_discussion_comment_defaults_to_first() {
373+
let comment = core::Comment {
374+
id: "cmt_1".to_string(),
375+
file_path: PathBuf::from("test.rs"),
376+
line_number: 1,
377+
content: "test".to_string(),
378+
rule_id: None,
379+
severity: core::comment::Severity::Info,
380+
category: core::comment::Category::BestPractice,
381+
suggestion: None,
382+
confidence: 0.8,
383+
code_suggestion: None,
384+
tags: vec![],
385+
fix_effort: core::comment::FixEffort::Low,
386+
};
387+
let result = select_discussion_comment(&[comment.clone()], None, None).unwrap();
388+
assert_eq!(result.id, "cmt_1");
389+
}
355390
}
356391

357392
fn load_discussion_thread(path: Option<&std::path::Path>, comment_id: &str) -> DiscussionThread {

0 commit comments

Comments
 (0)