Files
huskies/server/src/chat/commands/show.rs
T

174 lines
5.8 KiB
Rust

//! Handler for the `show` command.
use super::CommandContext;
/// Display the full markdown text of a work item identified by its numeric ID.
///
/// Lookup priority: CRDT → content store → filesystem (Story 512).
/// Returns a friendly message when no match is found.
pub(super) fn handle_show(ctx: &CommandContext) -> Option<String> {
let num_str = ctx.args.trim();
if num_str.is_empty() {
return Some(format!(
"Usage: `{} show <number>`\n\nDisplays the full text of a story, bug, or spike.",
ctx.bot_name
));
}
if !num_str.chars().all(|c| c.is_ascii_digit()) {
return Some(format!(
"Invalid story number: `{num_str}`. Usage: `{} show <number>`",
ctx.bot_name
));
}
// Find the story by numeric prefix: CRDT → content store → filesystem.
let (story_id, _stage_dir, path, content) =
match crate::chat::lookup::find_story_by_number(ctx.project_root, num_str) {
Some(found) => found,
None => {
return Some(format!(
"No story, bug, or spike with number **{num_str}** found."
));
}
};
// `content` is populated from the content store (CRDT/DB path) or read
// from disk during the filesystem fallback. If it is None (story found in
// CRDT but no content-store entry yet), attempt a direct disk read.
Some(
content
.or_else(|| std::fs::read_to_string(&path).ok())
.unwrap_or_else(|| {
format!("Story {story_id} found in pipeline but its content is unavailable.")
}),
)
}
#[cfg(test)]
mod tests {
use crate::agents::AgentPool;
use std::collections::HashSet;
use std::sync::{Arc, Mutex};
use super::super::{CommandDispatch, try_handle_command};
fn show_cmd_with_root(root: &std::path::Path, args: &str) -> Option<String> {
let agents = Arc::new(AgentPool::new_test(3000));
let ambient_rooms = Arc::new(Mutex::new(HashSet::new()));
let room_id = "!test:example.com".to_string();
let dispatch = CommandDispatch {
bot_name: "Timmy",
bot_user_id: "@timmy:homeserver.local",
project_root: root,
agents: &agents,
ambient_rooms: &ambient_rooms,
room_id: &room_id,
};
try_handle_command(&dispatch, &format!("@timmy show {args}"))
}
use crate::chat::test_helpers::write_story_file;
#[test]
fn show_command_is_registered() {
use super::super::commands;
let found = commands().iter().any(|c| c.name == "show");
assert!(found, "show command must be in the registry");
}
#[test]
fn show_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("show"), "help should list show command: {output}");
}
#[test]
fn show_command_no_args_returns_usage() {
let tmp = tempfile::TempDir::new().unwrap();
let output = show_cmd_with_root(tmp.path(), "").unwrap();
assert!(
output.contains("Usage"),
"no args should show usage hint: {output}"
);
}
#[test]
fn show_command_non_numeric_args_returns_error() {
let tmp = tempfile::TempDir::new().unwrap();
let output = show_cmd_with_root(tmp.path(), "abc").unwrap();
assert!(
output.contains("Invalid"),
"non-numeric arg should return error message: {output}"
);
}
#[test]
fn show_command_not_found_returns_friendly_message() {
let tmp = tempfile::TempDir::new().unwrap();
let output = show_cmd_with_root(tmp.path(), "999").unwrap();
assert!(
output.contains("999"),
"not-found message should include the queried number: {output}"
);
assert!(
output.contains("found"),
"not-found message should say not found: {output}"
);
}
#[test]
fn show_command_finds_story_in_backlog() {
let tmp = tempfile::TempDir::new().unwrap();
write_story_file(
tmp.path(),
"1_backlog",
"305_story_show_command.md",
"---\nname: Show command\n---\n\n# Story 305\n\nFull story text here.",
);
let output = show_cmd_with_root(tmp.path(), "305").unwrap();
assert!(
output.contains("Full story text here."),
"show should return full story content: {output}"
);
}
#[test]
fn show_command_finds_story_in_current() {
let tmp = tempfile::TempDir::new().unwrap();
write_story_file(
tmp.path(),
"2_current",
"42_story_do_something.md",
"---\nname: Do something\n---\n\n# Story 42\n\nIn progress.",
);
let output = show_cmd_with_root(tmp.path(), "42").unwrap();
assert!(
output.contains("In progress."),
"show should return story from current stage: {output}"
);
}
#[test]
fn show_command_finds_bug() {
let tmp = tempfile::TempDir::new().unwrap();
write_story_file(
tmp.path(),
"1_backlog",
"7_bug_crash_on_login.md",
"---\nname: Crash on login\n---\n\n## Symptom\n\nCrashes.",
);
let output = show_cmd_with_root(tmp.path(), "7").unwrap();
assert!(
output.contains("Symptom"),
"show should return bug content: {output}"
);
}
#[test]
fn show_command_case_insensitive() {
let result = super::super::tests::try_cmd_addressed("Timmy", "@timmy:homeserver.local", "@timmy SHOW 1");
assert!(result.is_some(), "SHOW should match case-insensitively");
}
}