huskies: merge 547_story_run_tests_bot_command_accepts_optional_story_number_to_run_tests_in_a_worktree

This commit is contained in:
dave
2026-04-12 13:22:13 +00:00
parent b7f077197d
commit 76765e15d2
+149 -4
View File
@@ -1,16 +1,65 @@
//! Handler for the `test` bot command — run the project's test suite. //! Handler for the `test` bot command — run the project's test suite.
//! //!
//! Executes `script/test` from the project root and returns a formatted //! Executes `script/test` from the project root (or an optional story worktree)
//! pass/fail summary with output (truncated for failures). //! and returns a formatted pass/fail summary with output (truncated for failures).
//!
//! Usage:
//! `run_tests` — run from project root
//! `run_tests <N>` — run from the worktree for story number N
use super::CommandContext; use super::CommandContext;
use std::path::PathBuf;
const TEST_SCRIPT: &str = "script/test"; const TEST_SCRIPT: &str = "script/test";
/// Maximum number of output lines to include in the response. /// Maximum number of output lines to include in the response.
const MAX_OUTPUT_LINES: usize = 80; const MAX_OUTPUT_LINES: usize = 80;
/// Resolve the working directory for the test run.
///
/// If `args` is empty, returns the project root. If it is a story number,
/// looks for a matching worktree under `.huskies/worktrees/`. Returns
/// `Err(message)` when the number is given but no worktree is found.
fn resolve_run_dir(ctx: &CommandContext) -> Result<PathBuf, String> {
let number = ctx.args.trim();
if number.is_empty() {
return Ok(ctx.project_root.to_path_buf());
}
// Validate: must be all digits.
if !number.chars().all(|c| c.is_ascii_digit()) {
return Err(format!(
"**Test**\n\nInvalid argument `{number}`: expected a story number (digits only)."
));
}
let worktrees_dir = ctx.project_root.join(".huskies/worktrees");
let prefix = format!("{number}_");
match std::fs::read_dir(&worktrees_dir) {
Ok(entries) => {
for entry in entries.flatten() {
let name = entry.file_name();
if name.to_string_lossy().starts_with(&prefix) && entry.path().is_dir() {
return Ok(entry.path());
}
}
Err(format!(
"**Test**\n\nNo worktree found for story `{number}`. \
Use `run_tests` (no arg) to run tests from the project root."
))
}
Err(e) => Err(format!(
"**Test**\n\nCould not read worktrees directory: {e}"
)),
}
}
pub(super) fn handle_test(ctx: &CommandContext) -> Option<String> { pub(super) fn handle_test(ctx: &CommandContext) -> Option<String> {
let script_path = ctx.project_root.join(TEST_SCRIPT); let run_dir = match resolve_run_dir(ctx) {
Ok(d) => d,
Err(msg) => return Some(msg),
};
let script_path = run_dir.join(TEST_SCRIPT);
if !script_path.exists() { if !script_path.exists() {
return Some(format!( return Some(format!(
@@ -20,7 +69,7 @@ pub(super) fn handle_test(ctx: &CommandContext) -> Option<String> {
let output = std::process::Command::new("bash") let output = std::process::Command::new("bash")
.arg(&script_path) .arg(&script_path)
.current_dir(ctx.project_root) .current_dir(&run_dir)
.output(); .output();
match output { match output {
@@ -239,4 +288,100 @@ mod tests {
assert_eq!(p, 8); assert_eq!(p, 8);
assert_eq!(f, 1); assert_eq!(f, 1);
} }
// -- worktree tests -------------------------------------------------------
/// Create a fake worktree directory under `project_root/.huskies/worktrees/`
/// named `{number}_fake_story` and optionally write `script/test` into it.
fn create_worktree(project_root: &std::path::Path, number: u32, with_script: bool) {
let wt = project_root
.join(".huskies/worktrees")
.join(format!("{number}_fake_story"));
std::fs::create_dir_all(&wt).unwrap();
if with_script {
write_script(
&wt,
"#!/usr/bin/env bash\necho 'test result: ok. 2 passed; 0 failed'\nexit 0\n",
);
}
}
#[test]
fn run_tests_with_no_args_uses_project_root() {
let dir = tempfile::tempdir().unwrap();
write_script(
dir.path(),
"#!/usr/bin/env bash\necho 'test result: ok. 7 passed; 0 failed'\nexit 0\n",
);
let agents = test_agents();
let ambient = test_ambient();
let ctx = make_ctx(&agents, &ambient, dir.path(), "");
let output = handle_test(&ctx).unwrap();
assert!(output.contains("PASS"), "no-arg should use project root: {output}");
assert!(output.contains('7'), "should show count from project root script: {output}");
}
#[test]
fn run_tests_with_story_number_uses_worktree() {
let dir = tempfile::tempdir().unwrap();
create_worktree(dir.path(), 541, true);
let agents = test_agents();
let ambient = test_ambient();
let ctx = make_ctx(&agents, &ambient, dir.path(), "541");
let output = handle_test(&ctx).unwrap();
assert!(output.contains("PASS"), "should run tests in worktree: {output}");
assert!(output.contains('2'), "should show count from worktree script: {output}");
}
#[test]
fn run_tests_with_unknown_story_number_returns_error() {
let dir = tempfile::tempdir().unwrap();
// Create the worktrees dir but no matching entry
std::fs::create_dir_all(dir.path().join(".huskies/worktrees")).unwrap();
let agents = test_agents();
let ambient = test_ambient();
let ctx = make_ctx(&agents, &ambient, dir.path(), "999");
let output = handle_test(&ctx).unwrap();
assert!(
output.contains("No worktree found") || output.contains("999"),
"unknown story number should return error: {output}"
);
}
#[test]
fn run_tests_with_invalid_arg_returns_error() {
let dir = tempfile::tempdir().unwrap();
let agents = test_agents();
let ambient = test_ambient();
let ctx = make_ctx(&agents, &ambient, dir.path(), "notanumber");
let output = handle_test(&ctx).unwrap();
assert!(
output.contains("Invalid argument") || output.contains("notanumber"),
"non-numeric arg should return error: {output}"
);
}
#[test]
fn run_tests_with_story_number_via_dispatch() {
let dir = tempfile::tempdir().unwrap();
create_worktree(dir.path(), 541, true);
let agents = test_agents();
let ambient = test_ambient();
let room_id = "!test:example.com".to_string();
let dispatch = super::super::CommandDispatch {
bot_name: "Timmy",
bot_user_id: "@timmy:homeserver.local",
project_root: dir.path(),
agents: &agents,
ambient_rooms: &ambient,
room_id: &room_id,
};
let result = super::super::try_handle_command(&dispatch, "@timmy run_tests 541");
assert!(
result.is_some(),
"run_tests with story number must respond via dispatch"
);
let output = result.unwrap();
assert!(output.contains("PASS"), "should PASS for valid worktree: {output}");
}
} }