| 1 | use agents::SettingsProblem; |
| 2 | |
| 3 | pub fn placeholder_problem( |
| 4 | problems: &[SettingsProblem], |
| 5 | item_id: &str, |
| 6 | text: &str, |
| 7 | ) -> Option<String> { |
| 8 | problems.iter().find_map(|problem| match problem { |
| 9 | SettingsProblem::UnknownPlaceholder { |
| 10 | item_id: problem_item, |
| 11 | placeholder, |
| 12 | } if problem_item == item_id && text.contains(&format!("{{{placeholder}}}")) => { |
| 13 | Some(format!("Unknown placeholder {{{placeholder}}}")) |
| 14 | } |
| 15 | _ => None, |
| 16 | }) |
| 17 | } |
| 18 | |
| 19 | pub fn command_problem(problems: &[SettingsProblem], agent_id: &str, text: &str) -> Option<String> { |
| 20 | let empty = problems.iter().any( |
| 21 | |problem| matches!(problem, SettingsProblem::EmptyCommand { agent_id: id } if id == agent_id), |
| 22 | ); |
| 23 | if empty { |
| 24 | return Some("The command is empty".to_owned()); |
| 25 | } |
| 26 | placeholder_problem(problems, agent_id, text) |
| 27 | } |
| 28 | |
| 29 | pub fn agent_problem(problems: &[SettingsProblem], action_id: &str) -> Option<String> { |
| 30 | problems.iter().find_map(|problem| match problem { |
| 31 | SettingsProblem::UnknownAgent { |
| 32 | action_id: problem_action, |
| 33 | .. |
| 34 | } if problem_action == action_id => Some("This agent no longer exists".to_owned()), |
| 35 | _ => None, |
| 36 | }) |
| 37 | } |
| 38 | |
| 39 | #[cfg(test)] |
| 40 | mod tests { |
| 41 | use agents::{AgentSettings, SettingsProblem}; |
| 42 | |
| 43 | use super::{agent_problem, command_problem, placeholder_problem}; |
| 44 | |
| 45 | fn broken() -> Vec<SettingsProblem> { |
| 46 | let mut settings = AgentSettings::default(); |
| 47 | settings.agents[0].command = " {bogus}".to_owned(); |
| 48 | settings.agents[1].command.clear(); |
| 49 | settings.agents[0].open_session_command = "wt.exe {session_id}".to_owned(); |
| 50 | settings.actions[0].agent_id = "ghost".to_owned(); |
| 51 | settings.actions[1].extra_args = "--flag {nope}".to_owned(); |
| 52 | settings.validate() |
| 53 | } |
| 54 | |
| 55 | #[test] |
| 56 | fn unknown_placeholders_show_under_the_field_that_contains_them() { |
| 57 | let problems = broken(); |
| 58 | assert_eq!( |
| 59 | command_problem(&problems, "claude", " {bogus}").as_deref(), |
| 60 | Some("Unknown placeholder {bogus}") |
| 61 | ); |
| 62 | assert_eq!( |
| 63 | placeholder_problem(&problems, "claude", "wt.exe {session_id}"), |
| 64 | None |
| 65 | ); |
| 66 | assert_eq!( |
| 67 | placeholder_problem(&problems, "build-this", "--flag {nope}").as_deref(), |
| 68 | Some("Unknown placeholder {nope}") |
| 69 | ); |
| 70 | assert_eq!(placeholder_problem(&problems, "build-this", "--flag"), None); |
| 71 | } |
| 72 | |
| 73 | #[test] |
| 74 | fn an_empty_command_and_a_missing_agent_are_reported() { |
| 75 | let problems = broken(); |
| 76 | assert_eq!( |
| 77 | command_problem(&problems, "copilot", "").as_deref(), |
| 78 | Some("The command is empty") |
| 79 | ); |
| 80 | assert_eq!( |
| 81 | agent_problem(&problems, "answer-this").as_deref(), |
| 82 | Some("This agent no longer exists") |
| 83 | ); |
| 84 | assert_eq!(agent_problem(&problems, "build-this"), None); |
| 85 | } |
| 86 | |
| 87 | #[test] |
| 88 | fn valid_settings_show_nothing() { |
| 89 | let problems = AgentSettings::default().validate(); |
| 90 | assert_eq!(command_problem(&problems, "claude", "claude -p"), None); |
| 91 | assert_eq!(agent_problem(&problems, "answer-this"), None); |
| 92 | } |
| 93 | } |