huskies: merge 494_story_mcp_tool_to_run_project_test_suite

This commit is contained in:
dave
2026-04-07 14:39:47 +00:00
parent 1b8c391836
commit 19768c23d5
5 changed files with 503 additions and 1 deletions
+6
View File
@@ -15,6 +15,7 @@ mod help;
pub(crate) mod loc;
mod move_story;
mod overview;
mod run_tests;
mod setup;
mod show;
mod status;
@@ -130,6 +131,11 @@ pub fn commands() -> &'static [BotCommand] {
description: "Show test coverage: cached baseline by default, or `coverage run` to rerun the full suite",
handler: coverage::handle_coverage,
},
BotCommand {
name: "test",
description: "Run the project's test suite (`script/test`) and show pass/fail with output",
handler: run_tests::handle_test,
},
BotCommand {
name: "loc",
description: "Show top source files by line count: `loc` (top 10), `loc <N>`, or `loc <filepath>` for a specific file",
+242
View File
@@ -0,0 +1,242 @@
//! Handler for the `test` bot command — run the project's test suite.
//!
//! Executes `script/test` from the project root and returns a formatted
//! pass/fail summary with output (truncated for failures).
use super::CommandContext;
const TEST_SCRIPT: &str = "script/test";
/// Maximum number of output lines to include in the response.
const MAX_OUTPUT_LINES: usize = 80;
pub(super) fn handle_test(ctx: &CommandContext) -> Option<String> {
let script_path = ctx.project_root.join(TEST_SCRIPT);
if !script_path.exists() {
return Some(format!(
"**Test**\n\nTest script not found: `{TEST_SCRIPT}`\n\nEnsure `{TEST_SCRIPT}` exists in the project root."
));
}
let output = std::process::Command::new("bash")
.arg(&script_path)
.current_dir(ctx.project_root)
.output();
match output {
Err(e) => Some(format!("**Test**\n\nFailed to run test script: {e}")),
Ok(out) => {
let passed = out.status.success();
let stdout = String::from_utf8_lossy(&out.stdout).to_string();
let stderr = String::from_utf8_lossy(&out.stderr).to_string();
let combined = format!("{stdout}{stderr}");
let (tests_passed, tests_failed) = parse_test_counts(&combined);
let truncated = truncate_output(&combined, MAX_OUTPUT_LINES);
let status = if passed { "PASS" } else { "FAIL" };
let mut result = format!("**Test: {status}**\n\n");
if tests_passed > 0 || tests_failed > 0 {
result.push_str(&format!(
"{tests_passed} passed, {tests_failed} failed\n\n"
));
}
result.push_str(&format!("```\n{truncated}\n```"));
Some(result)
}
}
}
/// Truncate output to at most `max_lines` tail lines.
fn truncate_output(output: &str, max_lines: usize) -> String {
let lines: Vec<&str> = output.lines().collect();
if lines.len() <= max_lines {
return output.to_string();
}
let omitted = lines.len() - max_lines;
let tail = lines[lines.len() - max_lines..].join("\n");
format!("[... {omitted} lines omitted ...]\n{tail}")
}
/// Parse cumulative passed/failed counts from `cargo test` output lines.
fn parse_test_counts(output: &str) -> (u64, u64) {
let mut total_passed = 0u64;
let mut total_failed = 0u64;
for line in output.lines() {
if line.contains("test result:") {
if let Some(p) = extract_count(line, "passed") {
total_passed += p;
}
if let Some(f) = extract_count(line, "failed") {
total_failed += f;
}
}
}
(total_passed, total_failed)
}
fn extract_count(line: &str, label: &str) -> Option<u64> {
let pos = line.find(label)?;
let before = line[..pos].trim_end();
let num_str: String = before.chars().rev().take_while(|c| c.is_ascii_digit()).collect();
if num_str.is_empty() {
return None;
}
let num_str: String = num_str.chars().rev().collect();
num_str.parse().ok()
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
use crate::agents::AgentPool;
use std::collections::HashSet;
use std::sync::{Arc, Mutex};
fn make_ctx<'a>(
agents: &'a Arc<AgentPool>,
ambient_rooms: &'a Arc<Mutex<HashSet<String>>>,
project_root: &'a std::path::Path,
args: &'a str,
) -> super::super::CommandContext<'a> {
super::super::CommandContext {
bot_name: "Timmy",
args,
project_root,
agents,
ambient_rooms,
room_id: "!test:example.com",
}
}
fn test_agents() -> Arc<AgentPool> {
Arc::new(AgentPool::new_test(3000))
}
fn test_ambient() -> Arc<Mutex<HashSet<String>>> {
Arc::new(Mutex::new(HashSet::new()))
}
fn write_script(dir: &std::path::Path, content: &str) {
let script_dir = dir.join("script");
std::fs::create_dir_all(&script_dir).unwrap();
let path = script_dir.join("test");
std::fs::write(&path, content).unwrap();
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap();
}
}
#[test]
fn test_command_is_registered() {
use super::super::commands;
let found = commands().iter().any(|c| c.name == "test");
assert!(found, "test command must be in the registry");
}
#[test]
fn test_command_appears_in_help() {
let result = super::super::tests::try_cmd_addressed(
"Timmy",
"@timmy:homeserver.local",
"@timmy help",
);
let output = result.unwrap();
assert!(
output.contains("test"),
"help should list test command: {output}"
);
}
#[test]
fn test_command_missing_script_returns_error() {
let dir = tempfile::tempdir().unwrap();
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("not found") || output.contains("script"),
"missing script should produce a clear error: {output}"
);
}
#[test]
fn test_command_pass_when_script_exits_zero() {
let dir = tempfile::tempdir().unwrap();
write_script(
dir.path(),
"#!/usr/bin/env bash\necho 'test result: ok. 4 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"), "should show PASS: {output}");
assert!(output.contains('4'), "should show test count: {output}");
}
#[test]
fn test_command_fail_when_script_exits_nonzero() {
let dir = tempfile::tempdir().unwrap();
write_script(
dir.path(),
"#!/usr/bin/env bash\necho 'test result: FAILED. 1 passed; 2 failed'\nexit 1\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("FAIL"), "should show FAIL: {output}");
assert!(output.contains('2'), "should show failed count: {output}");
}
#[test]
fn test_command_works_via_dispatch() {
let dir = tempfile::tempdir().unwrap();
write_script(
dir.path(),
"#!/usr/bin/env bash\necho 'ok'\nexit 0\n",
);
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 test");
assert!(
result.is_some(),
"test command must respond via dispatch (not fall through to LLM)"
);
}
#[test]
fn truncate_output_keeps_tail() {
let lines: Vec<String> = (1..=150).map(|i| format!("line {i}")).collect();
let text = lines.join("\n");
let result = truncate_output(&text, 80);
assert!(result.contains("line 150"), "should keep last line");
assert!(result.contains("omitted"), "should note omitted lines");
}
#[test]
fn parse_test_counts_sums_multiple_results() {
let output = "test result: ok. 5 passed; 0 failed;\ntest result: ok. 3 passed; 1 failed;";
let (p, f) = parse_test_counts(output);
assert_eq!(p, 8);
assert_eq!(f, 1);
}
}