//! Tests for the HTTP agent endpoints. use super::*; use crate::agents::AgentStatus; use std::path; use tempfile::TempDir; fn make_work_dirs(tmp: &TempDir) -> path::PathBuf { let root = tmp.path().to_path_buf(); for stage in &["5_done", "6_archived"] { std::fs::create_dir_all(root.join(".huskies").join("work").join(stage)).unwrap(); } root } #[test] fn story_is_archived_false_when_file_absent() { let tmp = TempDir::new().unwrap(); let root = make_work_dirs(&tmp); assert!(!svc::is_archived(&root, "79_story_foo")); } #[test] fn story_is_archived_true_when_file_in_5_done() { let tmp = TempDir::new().unwrap(); let root = make_work_dirs(&tmp); std::fs::write( root.join(".huskies/work/5_done/79_story_foo.md"), "---\nname: test\n---\n", ) .unwrap(); assert!(svc::is_archived(&root, "79_story_foo")); } #[test] fn story_is_archived_true_when_file_in_6_archived() { let tmp = TempDir::new().unwrap(); let root = make_work_dirs(&tmp); std::fs::write( root.join(".huskies/work/6_archived/79_story_foo.md"), "---\nname: test\n---\n", ) .unwrap(); assert!(svc::is_archived(&root, "79_story_foo")); } fn make_project_toml(root: &path::Path, content: &str) { let sk_dir = root.join(".huskies"); std::fs::create_dir_all(&sk_dir).unwrap(); std::fs::write(sk_dir.join("project.toml"), content).unwrap(); } // --- get_agent_config tests --- #[tokio::test] async fn get_agent_config_returns_default_when_no_toml() { let tmp = TempDir::new().unwrap(); let ctx = AppContext::new_test(tmp.path().to_path_buf()); let api = AgentsApi { ctx: Arc::new(ctx) }; let result = api.get_agent_config().await.unwrap().0; // Default config has one agent named "default" assert_eq!(result.len(), 1); assert_eq!(result[0].name, "default"); } #[tokio::test] async fn get_agent_config_returns_configured_agents() { let tmp = TempDir::new().unwrap(); make_project_toml( tmp.path(), r#" [[agent]] name = "coder-1" role = "Full-stack engineer" model = "sonnet" max_turns = 30 max_budget_usd = 5.0 [[agent]] name = "qa" role = "QA reviewer" model = "haiku" "#, ); let ctx = AppContext::new_test(tmp.path().to_path_buf()); let api = AgentsApi { ctx: Arc::new(ctx) }; let result = api.get_agent_config().await.unwrap().0; assert_eq!(result.len(), 2); assert_eq!(result[0].name, "coder-1"); assert_eq!(result[0].role, "Full-stack engineer"); assert_eq!(result[0].model, Some("sonnet".to_string())); assert_eq!(result[0].max_turns, Some(30)); assert_eq!(result[0].max_budget_usd, Some(5.0)); assert_eq!(result[1].name, "qa"); assert_eq!(result[1].model, Some("haiku".to_string())); } #[tokio::test] async fn get_agent_config_returns_error_when_no_project_root() { let tmp = TempDir::new().unwrap(); let ctx = AppContext::new_test(tmp.path().to_path_buf()); *ctx.state.project_root.lock().unwrap() = None; let api = AgentsApi { ctx: Arc::new(ctx) }; let result = api.get_agent_config().await; assert!(result.is_err()); } // --- reload_config tests --- #[tokio::test] async fn reload_config_returns_default_when_no_toml() { let tmp = TempDir::new().unwrap(); let ctx = AppContext::new_test(tmp.path().to_path_buf()); let api = AgentsApi { ctx: Arc::new(ctx) }; let result = api.reload_config().await.unwrap().0; assert_eq!(result.len(), 1); assert_eq!(result[0].name, "default"); } #[tokio::test] async fn reload_config_returns_configured_agents() { let tmp = TempDir::new().unwrap(); make_project_toml( tmp.path(), r#" [[agent]] name = "supervisor" role = "Coordinator" model = "opus" allowed_tools = ["Read", "Bash"] "#, ); let ctx = AppContext::new_test(tmp.path().to_path_buf()); let api = AgentsApi { ctx: Arc::new(ctx) }; let result = api.reload_config().await.unwrap().0; assert_eq!(result.len(), 1); assert_eq!(result[0].name, "supervisor"); assert_eq!(result[0].role, "Coordinator"); assert_eq!(result[0].model, Some("opus".to_string())); assert_eq!( result[0].allowed_tools, Some(vec!["Read".to_string(), "Bash".to_string()]) ); } #[tokio::test] async fn reload_config_returns_error_when_no_project_root() { let tmp = TempDir::new().unwrap(); let ctx = AppContext::new_test(tmp.path().to_path_buf()); *ctx.state.project_root.lock().unwrap() = None; let api = AgentsApi { ctx: Arc::new(ctx) }; let result = api.reload_config().await; assert!(result.is_err()); } // --- list_worktrees tests --- #[tokio::test] async fn list_worktrees_returns_empty_when_no_worktree_dir() { let tmp = TempDir::new().unwrap(); let ctx = AppContext::new_test(tmp.path().to_path_buf()); let api = AgentsApi { ctx: Arc::new(ctx) }; let result = api.list_worktrees().await.unwrap().0; assert!(result.is_empty()); } #[tokio::test] async fn list_worktrees_returns_entries_from_dir() { let tmp = TempDir::new().unwrap(); let worktrees_dir = tmp.path().join(".huskies").join("worktrees"); std::fs::create_dir_all(worktrees_dir.join("42_story_foo")).unwrap(); std::fs::create_dir_all(worktrees_dir.join("43_story_bar")).unwrap(); let ctx = AppContext::new_test(tmp.path().to_path_buf()); let api = AgentsApi { ctx: Arc::new(ctx) }; let mut result = api.list_worktrees().await.unwrap().0; result.sort_by(|a, b| a.story_id.cmp(&b.story_id)); assert_eq!(result.len(), 2); assert_eq!(result[0].story_id, "42_story_foo"); assert_eq!(result[1].story_id, "43_story_bar"); } #[tokio::test] async fn list_worktrees_returns_error_when_no_project_root() { let tmp = TempDir::new().unwrap(); let ctx = AppContext::new_test(tmp.path().to_path_buf()); *ctx.state.project_root.lock().unwrap() = None; let api = AgentsApi { ctx: Arc::new(ctx) }; let result = api.list_worktrees().await; assert!(result.is_err()); } // --- stop_agent tests --- #[tokio::test] async fn stop_agent_returns_error_when_no_project_root() { let tmp = TempDir::new().unwrap(); let ctx = AppContext::new_test(tmp.path().to_path_buf()); *ctx.state.project_root.lock().unwrap() = None; let api = AgentsApi { ctx: Arc::new(ctx) }; let result = api .stop_agent(Json(StopAgentPayload { story_id: "42_story_foo".to_string(), agent_name: "coder-1".to_string(), })) .await; assert!(result.is_err()); } #[tokio::test] async fn stop_agent_returns_error_when_agent_not_found() { let tmp = TempDir::new().unwrap(); let ctx = AppContext::new_test(tmp.path().to_path_buf()); let api = AgentsApi { ctx: Arc::new(ctx) }; let result = api .stop_agent(Json(StopAgentPayload { story_id: "nonexistent_story".to_string(), agent_name: "coder-1".to_string(), })) .await; assert!(result.is_err()); } #[tokio::test] async fn stop_agent_succeeds_with_running_agent() { let tmp = TempDir::new().unwrap(); let ctx = AppContext::new_test(tmp.path().to_path_buf()); ctx.services .agents .inject_test_agent("42_story_foo", "coder-1", AgentStatus::Running); let api = AgentsApi { ctx: Arc::new(ctx) }; let result = api .stop_agent(Json(StopAgentPayload { story_id: "42_story_foo".to_string(), agent_name: "coder-1".to_string(), })) .await .unwrap() .0; assert!(result); } // --- start_agent error path --- #[tokio::test] async fn start_agent_returns_error_when_no_project_root() { let tmp = TempDir::new().unwrap(); let ctx = AppContext::new_test(tmp.path().to_path_buf()); *ctx.state.project_root.lock().unwrap() = None; let api = AgentsApi { ctx: Arc::new(ctx) }; let result = api .start_agent(Json(StartAgentPayload { story_id: "42_story_foo".to_string(), agent_name: None, })) .await; assert!(result.is_err()); } // --- get_work_item_content tests --- fn make_stage_dir(root: &path::Path, stage: &str) { std::fs::create_dir_all(root.join(".huskies").join("work").join(stage)).unwrap(); } #[tokio::test] async fn get_work_item_content_returns_content_from_backlog() { crate::crdt_state::init_for_test(); let tmp = TempDir::new().unwrap(); let root = tmp.path(); make_stage_dir(root, "1_backlog"); std::fs::write( root.join(".huskies/work/1_backlog/42_story_foo.md"), "---\nname: \"Foo Story\"\n---\n\n# Story 42: Foo Story\n\nSome content.", ) .unwrap(); // Story 929: name lives in the typed CRDT register, not in YAML on disk. crate::crdt_state::write_item( "42_story_foo", "1_backlog", Some("Foo Story"), None, None, None, None, None, None, None, ); let ctx = AppContext::new_test(root.to_path_buf()); let api = AgentsApi { ctx: Arc::new(ctx) }; let result = api .get_work_item_content(Path("42_story_foo".to_string())) .await .unwrap() .0; assert!(result.content.contains("Some content.")); assert_eq!(result.stage, "backlog"); assert_eq!(result.name, Some("Foo Story".to_string())); } #[tokio::test] async fn get_work_item_content_returns_content_from_current() { crate::crdt_state::init_for_test(); let tmp = TempDir::new().unwrap(); let root = tmp.path(); make_stage_dir(root, "2_current"); std::fs::write( root.join(".huskies/work/2_current/43_story_bar.md"), "---\nname: \"Bar Story\"\n---\n\nBar content.", ) .unwrap(); crate::crdt_state::write_item( "43_story_bar", "2_current", Some("Bar Story"), None, None, None, None, None, None, None, ); let ctx = AppContext::new_test(root.to_path_buf()); let api = AgentsApi { ctx: Arc::new(ctx) }; let result = api .get_work_item_content(Path("43_story_bar".to_string())) .await .unwrap() .0; assert_eq!(result.stage, "current"); assert_eq!(result.name, Some("Bar Story".to_string())); } #[tokio::test] async fn get_work_item_content_returns_not_found_when_absent() { let tmp = TempDir::new().unwrap(); let ctx = AppContext::new_test(tmp.path().to_path_buf()); let api = AgentsApi { ctx: Arc::new(ctx) }; let result = api .get_work_item_content(Path("99_story_nonexistent".to_string())) .await; assert!(result.is_err()); } #[tokio::test] async fn get_work_item_content_falls_back_to_crdt_when_no_file() { let tmp = TempDir::new().unwrap(); let root = tmp.path().to_path_buf(); // Seed content + CRDT with no .md file on disk. crate::db::write_item_with_content( "44_story_crdt_only", "1_backlog", "---\nname: \"CRDT Only\"\n---\n\nCRDT content.", crate::db::ItemMeta::from_yaml("---\nname: \"CRDT Only\"\n---\n\nCRDT content."), ); let ctx = AppContext::new_test(root); let api = AgentsApi { ctx: Arc::new(ctx) }; let result = api .get_work_item_content(Path("44_story_crdt_only".to_string())) .await .unwrap() .0; assert!(result.content.contains("CRDT content.")); assert_eq!(result.stage, "backlog"); assert_eq!(result.name, Some("CRDT Only".to_string())); } #[tokio::test] async fn get_work_item_content_crdt_fallback_with_current_stage() { let tmp = TempDir::new().unwrap(); let root = tmp.path().to_path_buf(); // Seed a CRDT-only story in the coding/current stage. crate::db::write_item_with_content( "45_story_crdt_current", "2_current", "---\nname: \"Current CRDT\"\n---\n\nIn progress.", crate::db::ItemMeta::from_yaml("---\nname: \"Current CRDT\"\n---\n\nIn progress."), ); let ctx = AppContext::new_test(root); let api = AgentsApi { ctx: Arc::new(ctx) }; let result = api .get_work_item_content(Path("45_story_crdt_current".to_string())) .await .unwrap() .0; assert!(result.content.contains("In progress.")); assert_eq!(result.stage, "current"); assert_eq!(result.name, Some("Current CRDT".to_string())); } #[tokio::test] async fn get_work_item_content_returns_error_when_no_project_root() { let tmp = TempDir::new().unwrap(); let ctx = AppContext::new_test(tmp.path().to_path_buf()); *ctx.state.project_root.lock().unwrap() = None; let api = AgentsApi { ctx: Arc::new(ctx) }; let result = api .get_work_item_content(Path("42_story_foo".to_string())) .await; assert!(result.is_err()); } // --- get_agent_output tests --- #[tokio::test] async fn get_agent_output_returns_empty_when_no_log_exists() { let tmp = TempDir::new().unwrap(); let ctx = AppContext::new_test(tmp.path().to_path_buf()); let api = AgentsApi { ctx: Arc::new(ctx) }; let result = api .get_agent_output( Path("42_story_foo".to_string()), Path("coder-1".to_string()), ) .await .unwrap() .0; assert_eq!(result.output, ""); } #[tokio::test] async fn get_agent_output_returns_concatenated_output_events() { use crate::agent_log::AgentLogWriter; use crate::agents::AgentEvent; let tmp = TempDir::new().unwrap(); let root = tmp.path(); let mut writer = AgentLogWriter::new(root, "42_story_foo", "coder-1", "sess-test").unwrap(); writer .write_event(&AgentEvent::Status { story_id: "42_story_foo".to_string(), agent_name: "coder-1".to_string(), status: "running".to_string(), }) .unwrap(); writer .write_event(&AgentEvent::Output { story_id: "42_story_foo".to_string(), agent_name: "coder-1".to_string(), text: "Hello ".to_string(), }) .unwrap(); writer .write_event(&AgentEvent::Output { story_id: "42_story_foo".to_string(), agent_name: "coder-1".to_string(), text: "world\n".to_string(), }) .unwrap(); writer .write_event(&AgentEvent::Done { story_id: "42_story_foo".to_string(), agent_name: "coder-1".to_string(), session_id: None, }) .unwrap(); let ctx = AppContext::new_test(root.to_path_buf()); let api = AgentsApi { ctx: Arc::new(ctx) }; let result = api .get_agent_output( Path("42_story_foo".to_string()), Path("coder-1".to_string()), ) .await .unwrap() .0; // Only output event texts should be concatenated; status and done are excluded. assert_eq!(result.output, "Hello world\n"); } #[tokio::test] async fn get_agent_output_returns_error_when_no_project_root() { let tmp = TempDir::new().unwrap(); let ctx = AppContext::new_test(tmp.path().to_path_buf()); *ctx.state.project_root.lock().unwrap() = None; let api = AgentsApi { ctx: Arc::new(ctx) }; let result = api .get_agent_output( Path("42_story_foo".to_string()), Path("coder-1".to_string()), ) .await; assert!(result.is_err()); } // --- create_worktree error path --- #[tokio::test] async fn create_worktree_returns_error_when_no_project_root() { let tmp = TempDir::new().unwrap(); let ctx = AppContext::new_test(tmp.path().to_path_buf()); *ctx.state.project_root.lock().unwrap() = None; let api = AgentsApi { ctx: Arc::new(ctx) }; let result = api .create_worktree(Json(CreateWorktreePayload { story_id: "42_story_foo".to_string(), })) .await; assert!(result.is_err()); } #[tokio::test] async fn create_worktree_returns_error_when_not_a_git_repo() { let tmp = TempDir::new().unwrap(); // project_root is set but has no git repo — git worktree add will fail let ctx = AppContext::new_test(tmp.path().to_path_buf()); let api = AgentsApi { ctx: Arc::new(ctx) }; let result = api .create_worktree(Json(CreateWorktreePayload { story_id: "42_story_foo".to_string(), })) .await; assert!(result.is_err()); } // --- remove_worktree error paths --- #[tokio::test] async fn remove_worktree_returns_error_when_no_project_root() { let tmp = TempDir::new().unwrap(); let ctx = AppContext::new_test(tmp.path().to_path_buf()); *ctx.state.project_root.lock().unwrap() = None; let api = AgentsApi { ctx: Arc::new(ctx) }; let result = api.remove_worktree(Path("42_story_foo".to_string())).await; assert!(result.is_err()); } #[tokio::test] async fn remove_worktree_returns_error_when_worktree_not_found() { let tmp = TempDir::new().unwrap(); // project_root is set but no worktree exists for this story_id let ctx = AppContext::new_test(tmp.path().to_path_buf()); let api = AgentsApi { ctx: Arc::new(ctx) }; let result = api .remove_worktree(Path("nonexistent_story".to_string())) .await; assert!(result.is_err()); } // --- get_test_results tests --- #[tokio::test] async fn get_test_results_returns_none_when_no_results() { let tmp = TempDir::new().unwrap(); let root = make_work_dirs(&tmp); let ctx = AppContext::new_test(root); let api = AgentsApi { ctx: Arc::new(ctx) }; let result = api .get_test_results(Path("42_story_foo".to_string())) .await .unwrap() .0; assert!(result.is_none()); } #[tokio::test] async fn get_test_results_returns_in_memory_results() { let tmp = TempDir::new().unwrap(); let root = make_work_dirs(&tmp); let ctx = AppContext::new_test(root); // Record test results in-memory. { let mut workflow = ctx.workflow.lock().unwrap(); workflow .record_test_results_validated( "42_story_foo".to_string(), vec![crate::workflow::TestCaseResult { name: "unit_test_1".to_string(), status: crate::workflow::TestStatus::Pass, details: None, }], vec![crate::workflow::TestCaseResult { name: "int_test_1".to_string(), status: crate::workflow::TestStatus::Fail, details: Some("assertion failed".to_string()), }], ) .unwrap(); } let api = AgentsApi { ctx: Arc::new(ctx) }; let result = api .get_test_results(Path("42_story_foo".to_string())) .await .unwrap() .0 .expect("should have test results"); assert_eq!(result.unit.len(), 1); assert_eq!(result.unit[0].name, "unit_test_1"); assert_eq!(result.unit[0].status, "pass"); assert!(result.unit[0].details.is_none()); assert_eq!(result.integration.len(), 1); assert_eq!(result.integration[0].name, "int_test_1"); assert_eq!(result.integration[0].status, "fail"); assert_eq!( result.integration[0].details.as_deref(), Some("assertion failed") ); } #[tokio::test] async fn get_test_results_falls_back_to_file_persisted_results() { let tmp = TempDir::new().unwrap(); let root = tmp.path().to_path_buf(); // Create work dirs including 2_current for the story file. for stage in &["1_backlog", "2_current", "5_done", "6_archived"] { std::fs::create_dir_all(root.join(".huskies").join("work").join(stage)).unwrap(); } // Use a unique high-numbered story ID to avoid collisions with the // "42_story_foo" entry used by get_test_results_returns_none_when_no_results. let story_content = r#"--- name: "Test story" --- # Test story ## Test Results "#; std::fs::write( root.join(".huskies/work/2_current/9906_story_persisted_results.md"), story_content, ) .unwrap(); // Also write to the content store so read_story_content returns this // test's content even when another test left a stale entry in the // global content store. crate::db::ensure_content_store(); crate::db::write_content("9906_story_persisted_results", story_content); let ctx = AppContext::new_test(root); let api = AgentsApi { ctx: Arc::new(ctx) }; let result = api .get_test_results(Path("9906_story_persisted_results".to_string())) .await .unwrap() .0 .expect("should fall back to file results"); assert_eq!(result.unit.len(), 1); assert_eq!(result.unit[0].name, "from_file"); assert_eq!(result.unit[0].status, "pass"); }