use crate::context::TaskSummary;
use crate::hooks::hook_context::HookContext;
#[derive(Debug, PartialEq, Eq)]
pub enum AutoBlockResult {
NoOp,
Blocked { task_id: String, reason: String },
}
pub fn check(ctx: &HookContext) -> AutoBlockResult {
if ctx.running.is_empty() {
return AutoBlockResult::NoOp;
}
let cmd = ctx.command();
let exit_code = ctx.exit_code();
let is_test = cmd.contains("nextest run") || cmd.contains("cargo test");
if !is_test || exit_code == 0 {
return AutoBlockResult::NoOp;
}
if cmd.contains("--auto-done") {
return AutoBlockResult::NoOp;
}
let target_task = match_task_to_crate(cmd, &ctx.running);
let task_id = match target_task {
Some(t) => t.id.clone(),
None => return AutoBlockResult::NoOp,
};
let failure_line = extract_failure_line(ctx.stdout());
let reason = format!("{failure_line} (exit {exit_code})");
AutoBlockResult::Blocked { task_id, reason }
}
pub fn format_result(result: &AutoBlockResult) -> Option<String> {
match result {
AutoBlockResult::NoOp => None,
AutoBlockResult::Blocked { task_id, reason } => {
Some(format!("[godmode] Auto-blocked task {task_id}: {reason}"))
}
}
}
fn match_task_to_crate<'a>(cmd: &str, running: &'a [TaskSummary]) -> Option<&'a TaskSummary> {
let crate_flag = extract_crate_flag(cmd);
if let Some(crate_name) = crate_flag {
let matched = running
.iter()
.find(|t| t.crate_name.as_deref() == Some(crate_name));
if matched.is_some() {
return matched;
}
}
running.first()
}
fn extract_crate_flag(cmd: &str) -> Option<&str> {
let parts: Vec<&str> = cmd.split_whitespace().collect();
for (i, part) in parts.iter().enumerate() {
if *part == "-p" {
return parts.get(i + 1).copied();
}
}
None
}
fn extract_failure_line(stdout: &str) -> &str {
stdout
.lines()
.find(|l| l.contains("FAIL") || l.contains("FAILED"))
.map(|l| l.trim())
.unwrap_or("test failure")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn extract_crate_flag_finds_p_arg() {
assert_eq!(
extract_crate_flag("cargo nextest run -p godmode-core"),
Some("godmode-core")
);
}
#[test]
fn extract_crate_flag_returns_none_without_p() {
assert_eq!(extract_crate_flag("cargo nextest run --workspace"), None);
}
#[test]
fn extract_failure_line_finds_fail() {
let stdout = "running 5 tests\ntest foo ... FAILED\ntest bar ... ok\n";
assert_eq!(extract_failure_line(stdout), "test foo ... FAILED");
}
#[test]
fn extract_failure_line_defaults_on_no_match() {
assert_eq!(extract_failure_line("all tests passed"), "test failure");
}
#[test]
fn match_task_picks_crate_match() {
let tasks = vec![
TaskSummary {
id: "t1".into(),
title: "A".into(),
crate_name: Some("godmode-core".into()),
},
TaskSummary {
id: "t2".into(),
title: "B".into(),
crate_name: Some("godmode-cli".into()),
},
];
let result = match_task_to_crate("cargo nextest run -p godmode-cli", &tasks);
assert_eq!(result.unwrap().id, "t2");
}
#[test]
fn match_task_falls_back_to_first() {
let tasks = vec![TaskSummary {
id: "t1".into(),
title: "A".into(),
crate_name: None,
}];
let result = match_task_to_crate("cargo test --workspace", &tasks);
assert_eq!(result.unwrap().id, "t1");
}
#[test]
fn check_noop_when_no_running_tasks() {
use std::path::PathBuf;
let ctx = HookContext {
input: serde_json::from_str(
r#"{"tool_input":{"command":"cargo test"},"tool_result":{"exit_code":1}}"#,
)
.unwrap(),
git_root: PathBuf::from("/tmp"),
project: "test".into(),
running: vec![],
pending_count: 0,
blocked: vec![],
recent_commits: vec![],
};
assert_eq!(check(&ctx), AutoBlockResult::NoOp);
}
#[test]
fn check_noop_when_exit_zero() {
use std::path::PathBuf;
let ctx = HookContext {
input: serde_json::from_str(
r#"{"tool_input":{"command":"cargo test"},"tool_result":{"exit_code":0}}"#,
)
.unwrap(),
git_root: PathBuf::from("/tmp"),
project: "test".into(),
running: vec![TaskSummary {
id: "t1".into(),
title: "A".into(),
crate_name: None,
}],
pending_count: 0,
blocked: vec![],
recent_commits: vec![],
};
assert_eq!(check(&ctx), AutoBlockResult::NoOp);
}
#[test]
fn check_blocks_on_test_failure() {
use std::path::PathBuf;
let ctx = HookContext {
input: serde_json::from_str(
r#"{"tool_input":{"command":"cargo nextest run -p foo"},"tool_result":{"exit_code":1,"stdout":"test bar ... FAILED\n"}}"#,
)
.unwrap(),
git_root: PathBuf::from("/tmp"),
project: "test".into(),
running: vec![TaskSummary {
id: "t1".into(),
title: "A".into(),
crate_name: Some("foo".into()),
}],
pending_count: 0,
blocked: vec![],
recent_commits: vec![],
};
let result = check(&ctx);
assert_eq!(
result,
AutoBlockResult::Blocked {
task_id: "t1".into(),
reason: "test bar ... FAILED (exit 1)".into(),
}
);
}
#[test]
fn check_skips_auto_done_commands() {
use std::path::PathBuf;
let ctx = HookContext {
input: serde_json::from_str(
r#"{"tool_input":{"command":"cargo nextest run --auto-done"},"tool_result":{"exit_code":1}}"#,
)
.unwrap(),
git_root: PathBuf::from("/tmp"),
project: "test".into(),
running: vec![TaskSummary {
id: "t1".into(),
title: "A".into(),
crate_name: None,
}],
pending_count: 0,
blocked: vec![],
recent_commits: vec![],
};
assert_eq!(check(&ctx), AutoBlockResult::NoOp);
}
}