| 1 | //! Command traits and registry support. |
| 2 | |
| 3 | use std::borrow::Cow; |
| 4 | use std::collections::HashMap; |
| 5 | |
| 6 | use codewhale_localization::{Locale, MessageId, tr}; |
| 7 | |
| 8 | use super::CommandResult; |
| 9 | |
| 10 | #[derive(Debug, Clone, Copy)] |
| 11 | pub struct CommandInfo { |
| 12 | pub name: &'static str, |
| 13 | pub aliases: &'static [&'static str], |
| 14 | pub usage: &'static str, |
| 15 | pub description_id: MessageId, |
| 16 | } |
| 17 | |
| 18 | use codewhale_command_contract::CommandDiscovery; |
| 19 | |
| 20 | pub(crate) const ADVANCED_DISCOVERY_COMMANDS: &[&str] = &[ |
| 21 | "anchor", |
| 22 | "balance", |
| 23 | "cache", |
| 24 | "change", |
| 25 | "context", |
| 26 | "diff", |
| 27 | "edit", |
| 28 | "hf", |
| 29 | "lsp", |
| 30 | "modeldb", |
| 31 | "models", |
| 32 | "network", |
| 33 | "preview-request", |
| 34 | "profile", |
| 35 | "purge", |
| 36 | "relay", |
| 37 | "rename", |
| 38 | "rlm", |
| 39 | "settings", |
| 40 | "share", |
| 41 | "workbar", |
| 42 | "status", |
| 43 | "system", |
| 44 | "theme", |
| 45 | "tools", |
| 46 | "trust", |
| 47 | "verbose", |
| 48 | ]; |
| 49 | |
| 50 | pub(crate) const COMPATIBILITY_DISCOVERY_COMMANDS: &[&str] = &[]; |
| 51 | |
| 52 | /// Commands that exist and run, but are not advertised anywhere the operator |
| 53 | /// browses: not in slash completion, not in `/help`, not in the palette. |
| 54 | /// |
| 55 | /// This is for surfaces that are real but not ready to be recommended. Typing |
| 56 | /// the command still works, the `codewhale <verb>` CLI is untouched, and the |
| 57 | /// hotbar still registers a binding for it — `codewhale-lane` resolves its |
| 58 | /// control-plane actions through that registry, so it is a substrate rather |
| 59 | /// than a place to browse. The entry here only stops the product from |
| 60 | /// *teaching* a route it is not standing behind yet. Founder, 2026-09-03: |
| 61 | /// "remove /lane". |
| 62 | pub(crate) const UNLISTED_COMMANDS: &[&str] = &["lane"]; |
| 63 | |
| 64 | pub(crate) const BARE_SLASH_DISCOVERY_COMMANDS: &[&str] = |
| 65 | &["help", "setup", "model", "settings", "resume", "rc"]; |
| 66 | |
| 67 | #[must_use] |
| 68 | pub(crate) fn bare_slash_discovery_rank(name: &str) -> Option<usize> { |
| 69 | BARE_SLASH_DISCOVERY_COMMANDS |
| 70 | .iter() |
| 71 | .position(|entry| *entry == name) |
| 72 | } |
| 73 | |
| 74 | /// Built-in commands that the palette pastes into the composer instead of |
| 75 | /// executing, even though they have no *required* argument. |
| 76 | /// |
| 77 | /// Prefer keeping this empty. Every name here must be a registered canonical |
| 78 | /// command name — see `palette_paste_only_names_are_registered` in the |
| 79 | /// command palette tests. |
| 80 | pub(crate) const PALETTE_PASTE_ONLY: &[&str] = &[]; |
| 81 | |
| 82 | /// Bare words in a usage line that stand for a value the operator supplies |
| 83 | /// rather than a literal token they type. `/workspace [path|worktrees]` reads |
| 84 | /// the same to a parser either way, so the metavariables are named once here |
| 85 | /// instead of being re-encoded as a second usage source next to the registry. |
| 86 | const USAGE_METAVARIABLES: &[&str] = &[ |
| 87 | "args", "command", "days", "dir", "key", "message", "model", "name", "path", "prompt", "query", |
| 88 | "text", "url", "value", |
| 89 | ]; |
| 90 | |
| 91 | /// Literal subcommands declared by a `usage` line, in declaration order. |
| 92 | /// |
| 93 | /// The registry's `usage` strings are the only place argument shapes are |
| 94 | /// written down, so this reads them instead of adding a parallel |
| 95 | /// `subcommands` field that would drift. It is deliberately conservative: |
| 96 | /// anything that does not look like a typed word — `<placeholder>`, `--flag`, |
| 97 | /// `snake_case` metavariables, `path/to/file` — is skipped, so an unfamiliar |
| 98 | /// usage shape yields no hint rather than a wrong one. |
| 99 | #[must_use] |
| 100 | pub fn usage_subcommands(usage: &str) -> Vec<&str> { |
| 101 | let Some((_, rest)) = usage.trim().split_once(char::is_whitespace) else { |
| 102 | return Vec::new(); |
| 103 | }; |
| 104 | // Only the first argument group names subcommands. A later group is a |
| 105 | // modifier on whichever one was chosen (`/workbar […] [--save]`). |
| 106 | let group = first_top_level_group(rest.trim_start()); |
| 107 | let mut subcommands: Vec<&str> = Vec::new(); |
| 108 | for alternative in split_top_level_alternatives(strip_one_bracket(group)) { |
| 109 | let Some(token) = leading_literal_token(alternative) else { |
| 110 | continue; |
| 111 | }; |
| 112 | if !subcommands.contains(&token) { |
| 113 | subcommands.push(token); |
| 114 | } |
| 115 | } |
| 116 | subcommands |
| 117 | } |
| 118 | |
| 119 | /// The first whitespace-separated chunk of `rest`, counting `[` and `<` so a |
| 120 | /// group containing spaces (`[open <id>|prune <days>]`) stays whole. |
| 121 | fn first_top_level_group(rest: &str) -> &str { |
| 122 | let mut depth = 0usize; |
| 123 | for (idx, ch) in rest.char_indices() { |
| 124 | match ch { |
| 125 | '[' | '<' => depth += 1, |
| 126 | ']' | '>' => depth = depth.saturating_sub(1), |
| 127 | _ if ch.is_whitespace() && depth == 0 => return &rest[..idx], |
| 128 | _ => {} |
| 129 | } |
| 130 | } |
| 131 | rest |
| 132 | } |
| 133 | |
| 134 | /// Remove one enclosing `[…]`, or one enclosing `<…>` that fences a choice. |
| 135 | /// |
| 136 | /// `<entry_id>` is a value and stays wrapped so it is skipped later; |
| 137 | /// `<turn <n>|plan>` is a required choice between literal verbs and is opened. |
| 138 | fn strip_one_bracket(group: &str) -> &str { |
| 139 | let (open, close) = match group.chars().next() { |
| 140 | Some('[') => ('[', ']'), |
| 141 | Some('<') => ('<', '>'), |
| 142 | _ => return group, |
| 143 | }; |
| 144 | if !group.ends_with(close) { |
| 145 | return group; |
| 146 | } |
| 147 | let inner = &group[open.len_utf8()..group.len() - close.len_utf8()]; |
| 148 | if !brackets_balanced(inner) { |
| 149 | return group; |
| 150 | } |
| 151 | if open == '<' && split_top_level_alternatives(inner).len() < 2 { |
| 152 | return group; |
| 153 | } |
| 154 | inner |
| 155 | } |
| 156 | |
| 157 | fn brackets_balanced(text: &str) -> bool { |
| 158 | let mut depth = 0isize; |
| 159 | for ch in text.chars() { |
| 160 | match ch { |
| 161 | '[' | '<' => depth += 1, |
| 162 | ']' | '>' => depth -= 1, |
| 163 | _ => {} |
| 164 | } |
| 165 | if depth < 0 { |
| 166 | return false; |
| 167 | } |
| 168 | } |
| 169 | depth == 0 |
| 170 | } |
| 171 | |
| 172 | /// Split on `|` that is not nested inside a `[…]` or `<…>`. |
| 173 | fn split_top_level_alternatives(group: &str) -> Vec<&str> { |
| 174 | let mut parts = Vec::new(); |
| 175 | let mut depth = 0usize; |
| 176 | let mut start = 0usize; |
| 177 | for (idx, ch) in group.char_indices() { |
| 178 | match ch { |
| 179 | '[' | '<' => depth += 1, |
| 180 | ']' | '>' => depth = depth.saturating_sub(1), |
| 181 | '|' if depth == 0 => { |
| 182 | parts.push(group[start..idx].trim()); |
| 183 | start = idx + ch.len_utf8(); |
| 184 | } |
| 185 | _ => {} |
| 186 | } |
| 187 | } |
| 188 | parts.push(group[start..].trim()); |
| 189 | parts.retain(|part| !part.is_empty()); |
| 190 | parts |
| 191 | } |
| 192 | |
| 193 | /// The literal verb an alternative starts with, or `None` when it starts with |
| 194 | /// a placeholder, a flag, or a metavariable. |
| 195 | fn leading_literal_token(alternative: &str) -> Option<&str> { |
| 196 | let end = alternative |
| 197 | .find(|ch: char| !ch.is_ascii_alphanumeric() && ch != '-') |
| 198 | .unwrap_or(alternative.len()); |
| 199 | let token = &alternative[..end]; |
| 200 | // A word only counts when it is spelled the way a typed verb is spelled: |
| 201 | // starting lowercase (or a digit, for `/mode [work|plan|1|2]`) and made |
| 202 | // of ASCII word characters. `N`, `entry_id`, `--force` and |
| 203 | // `path/to/export.json` are all values, not verbs. |
| 204 | let mut chars = token.chars(); |
| 205 | let first = chars.next()?; |
| 206 | if !(first.is_ascii_lowercase() || first.is_ascii_digit()) { |
| 207 | return None; |
| 208 | } |
| 209 | let trailing = &alternative[end..]; |
| 210 | if trailing |
| 211 | .chars() |
| 212 | .next() |
| 213 | .is_some_and(|ch| matches!(ch, '_' | '/' | '.')) |
| 214 | { |
| 215 | return None; |
| 216 | } |
| 217 | if USAGE_METAVARIABLES.contains(&token) && !trailing.trim_start().starts_with('<') { |
| 218 | return None; |
| 219 | } |
| 220 | Some(token) |
| 221 | } |
| 222 | |
| 223 | impl CommandInfo { |
| 224 | /// Literal subcommands this command's `usage` line declares. |
| 225 | /// |
| 226 | /// Used by the slash menu to list `/workspace worktrees` and friends |
| 227 | /// once the command name has been typed. |
| 228 | #[must_use] |
| 229 | pub fn subcommands(&self) -> Vec<&'static str> { |
| 230 | usage_subcommands(self.usage) |
| 231 | } |
| 232 | |
| 233 | pub fn requires_argument(&self) -> bool { |
| 234 | self.usage.contains('<') || self.usage.contains('[') |
| 235 | } |
| 236 | |
| 237 | pub fn requires_required_argument(&self) -> bool { |
| 238 | let mut optional_depth = 0usize; |
| 239 | for ch in self.usage.chars() { |
| 240 | match ch { |
| 241 | '[' => optional_depth += 1, |
| 242 | ']' => optional_depth = optional_depth.saturating_sub(1), |
| 243 | '<' if optional_depth == 0 => return true, |
| 244 | _ => {} |
| 245 | } |
| 246 | } |
| 247 | false |
| 248 | } |
| 249 | |
| 250 | /// Whether the slash menu / composer should leave a trailing space so the |
| 251 | /// user can type arguments immediately. `/change` is bare-useful (opens |
| 252 | /// the latest changelog) even though its usage documents an optional |
| 253 | /// version, so it is the only historical carve-out. |
| 254 | pub fn composer_wants_trailing_space(&self) -> bool { |
| 255 | self.name != "change" && self.requires_argument() |
| 256 | } |
| 257 | |
| 258 | /// Whether the command palette should run this command immediately when |
| 259 | /// selected, instead of pasting it into the composer. |
| 260 | /// |
| 261 | /// Default: run anything that does not require a mandatory positional |
| 262 | /// argument (including optional-arg commands that open a picker when bare). |
| 263 | /// [`PALETTE_PASTE_ONLY`] is the explicit opt-out for side-effectful or |
| 264 | /// multi-step no-arg commands that should still paste for confirmation. |
| 265 | pub fn palette_runs_directly(&self) -> bool { |
| 266 | if self.requires_required_argument() { |
| 267 | return false; |
| 268 | } |
| 269 | !PALETTE_PASTE_ONLY.contains(&self.name) |
| 270 | } |
| 271 | |
| 272 | pub fn palette_command(&self) -> String { |
| 273 | if self.requires_argument() { |
| 274 | format!("/{} ", self.name) |
| 275 | } else { |
| 276 | format!("/{}", self.name) |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | pub fn description_for(&self, locale: Locale) -> Cow<'static, str> { |
| 281 | tr(locale, self.description_id) |
| 282 | } |
| 283 | |
| 284 | pub fn palette_description_for(&self, locale: Locale) -> String { |
| 285 | let desc = self.description_for(locale); |
| 286 | if self.aliases.is_empty() { |
| 287 | desc.to_string() |
| 288 | } else { |
| 289 | format!( |
| 290 | "{} {}", |
| 291 | desc, |
| 292 | tr(locale, MessageId::PaletteAliasesSuffix).replace( |
| 293 | "{aliases}", |
| 294 | &self.aliases.join(&tr(locale, MessageId::ListSeparator)) |
| 295 | ) |
| 296 | ) |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | pub fn discovery(&self) -> CommandDiscovery { |
| 301 | if COMPATIBILITY_DISCOVERY_COMMANDS.contains(&self.name) { |
| 302 | CommandDiscovery::Compatibility |
| 303 | } else if ADVANCED_DISCOVERY_COMMANDS.contains(&self.name) { |
| 304 | CommandDiscovery::Advanced |
| 305 | } else { |
| 306 | CommandDiscovery::Primary |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | pub fn show_in_empty_discovery(&self) -> bool { |
| 311 | matches!(self.discovery(), CommandDiscovery::Primary) |
| 312 | } |
| 313 | |
| 314 | /// Whether this command may appear in slash completion at all. |
| 315 | /// |
| 316 | /// Always: the menu is how the command surface is discovered, so hiding |
| 317 | /// commands from it makes them unfindable. Founder live-test: "I like how |
| 318 | /// we prioritize the slash thing but it should still be able to find all |
| 319 | /// of them." A bare `/` used to return only |
| 320 | /// [`BARE_SLASH_DISCOVERY_COMMANDS`], which is a *ranking* concern — the |
| 321 | /// menu already sorts those six to the top and the popup scrolls around |
| 322 | /// the selection, so the short list is preserved as the head of a |
| 323 | /// complete one rather than as the whole of a truncated one. |
| 324 | pub fn show_in_slash_completion(&self, _prefix: &str) -> bool { |
| 325 | !self.is_unlisted() |
| 326 | } |
| 327 | |
| 328 | /// Whether this command is deliberately not advertised — see |
| 329 | /// [`UNLISTED_COMMANDS`]. It still runs when typed. |
| 330 | #[must_use] |
| 331 | pub fn is_unlisted(&self) -> bool { |
| 332 | UNLISTED_COMMANDS |
| 333 | .iter() |
| 334 | .any(|name| self.name == *name || self.aliases.contains(name)) |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | pub trait Command: Send + Sync { |
| 339 | fn info(&self) -> &'static CommandInfo; |
| 340 | |
| 341 | fn contextual_handler( |
| 342 | &self, |
| 343 | ) -> Option<codewhale_command_contract::handler::CommandHandler<CommandResult>>; |
| 344 | } |
| 345 | |
| 346 | pub trait CommandGroup: Send + Sync { |
| 347 | fn commands(&self) -> &'static [Box<dyn Command>]; |
| 348 | } |
| 349 | |
| 350 | pub(crate) struct ContextualCommand { |
| 351 | info: &'static CommandInfo, |
| 352 | handler: codewhale_command_contract::handler::CommandHandler<CommandResult>, |
| 353 | } |
| 354 | |
| 355 | impl ContextualCommand { |
| 356 | pub(crate) const fn contextual( |
| 357 | info: &'static CommandInfo, |
| 358 | handler: codewhale_command_contract::handler::CommandHandler<CommandResult>, |
| 359 | ) -> Self { |
| 360 | Self { info, handler } |
| 361 | } |
| 362 | |
| 363 | pub(crate) fn from_contract<C>() -> Result<Self, String> |
| 364 | where |
| 365 | C: codewhale_command_contract::metadata::RegisterCommand<CommandResult>, |
| 366 | { |
| 367 | let portable = C::info(); |
| 368 | let description_id = super::contract::key_to_message_id(portable.description_key) |
| 369 | .ok_or_else(|| { |
| 370 | format!( |
| 371 | "unknown command description key {:?} for /{}", |
| 372 | portable.description_key, portable.name |
| 373 | ) |
| 374 | })?; |
| 375 | let info = Box::leak(Box::new(CommandInfo { |
| 376 | name: portable.name, |
| 377 | aliases: portable.aliases, |
| 378 | usage: portable.usage, |
| 379 | description_id, |
| 380 | })); |
| 381 | Ok(Self::contextual(info, C::handler())) |
| 382 | } |
| 383 | } |
| 384 | impl Command for ContextualCommand { |
| 385 | fn info(&self) -> &'static CommandInfo { |
| 386 | self.info |
| 387 | } |
| 388 | |
| 389 | fn contextual_handler( |
| 390 | &self, |
| 391 | ) -> Option<codewhale_command_contract::handler::CommandHandler<CommandResult>> { |
| 392 | Some(self.handler.clone()) |
| 393 | } |
| 394 | } |
| 395 | pub struct CommandRegistry { |
| 396 | commands: Vec<&'static dyn Command>, |
| 397 | name_to_index: HashMap<&'static str, usize>, |
| 398 | } |
| 399 | |
| 400 | impl CommandRegistry { |
| 401 | pub fn empty() -> Self { |
| 402 | Self { |
| 403 | commands: Vec::new(), |
| 404 | name_to_index: HashMap::new(), |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | pub fn register(&mut self, command: &'static dyn Command) { |
| 409 | let index = self.commands.len(); |
| 410 | let info = command.info(); |
| 411 | self.name_to_index.insert(info.name, index); |
| 412 | for alias in info.aliases { |
| 413 | self.name_to_index.insert(alias, index); |
| 414 | } |
| 415 | self.commands.push(command); |
| 416 | } |
| 417 | |
| 418 | pub fn register_group(&mut self, group: &dyn CommandGroup) { |
| 419 | for command in group.commands() { |
| 420 | self.register(command.as_ref()); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | /// FEAT-015: register a test-only contextual command under `#[cfg(test)]`. |
| 425 | /// The production registry is untouched (D6); the fixture dispatches |
| 426 | /// through the public `execute()` to prove the seam. |
| 427 | #[cfg(test)] |
| 428 | pub(crate) fn register_test_only(&mut self, command: &'static dyn Command) { |
| 429 | self.register(command); |
| 430 | } |
| 431 | |
| 432 | pub fn get(&self, name: &str) -> Option<&dyn Command> { |
| 433 | let name = name.strip_prefix('/').unwrap_or(name); |
| 434 | self.name_to_index |
| 435 | .get(name) |
| 436 | .and_then(|index| self.commands.get(*index)) |
| 437 | .copied() |
| 438 | } |
| 439 | |
| 440 | pub fn get_info(&self, name: &str) -> Option<&'static CommandInfo> { |
| 441 | self.get(name).map(Command::info) |
| 442 | } |
| 443 | |
| 444 | /// FEAT-015: whether the named entry has a capability-scoped handler. |
| 445 | #[cfg(test)] |
| 446 | pub(crate) fn has_contextual_handler(&self, name: &str) -> bool { |
| 447 | self.get(name) |
| 448 | .is_some_and(|command| command.contextual_handler().is_some()) |
| 449 | } |
| 450 | |
| 451 | pub fn iter(&self) -> impl Iterator<Item = &dyn Command> { |
| 452 | self.commands.iter().copied() |
| 453 | } |
| 454 | |
| 455 | pub fn infos(&self) -> Vec<&'static CommandInfo> { |
| 456 | self.iter().map(Command::info).collect() |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | #[cfg(test)] |
| 461 | mod usage_subcommand_tests { |
| 462 | use super::*; |
| 463 | |
| 464 | use crate::commands::get_command_info; |
| 465 | |
| 466 | #[test] |
| 467 | fn workspace_usage_names_the_worktree_manager() { |
| 468 | let workspace = get_command_info("workspace").expect("built-in workspace command"); |
| 469 | assert_eq!(workspace.subcommands(), vec!["worktrees"]); |
| 470 | } |
| 471 | |
| 472 | /// The managers #5952 names: each one hid behind a single word, and each |
| 473 | /// one now has its verbs on the menu the moment the name is typed. |
| 474 | #[test] |
| 475 | fn the_managers_the_issue_names_declare_their_verbs() { |
| 476 | for (name, expected) in [ |
| 477 | ("workspace", vec!["worktrees"]), |
| 478 | ( |
| 479 | "fleet", |
| 480 | vec![ |
| 481 | "members", "setup", "teams", "workers", "context", "board", "help", |
| 482 | ], |
| 483 | ), |
| 484 | ( |
| 485 | "sessions", |
| 486 | vec!["show", "open", "archive", "unarchive", "prune"], |
| 487 | ), |
| 488 | ( |
| 489 | "automation", |
| 490 | vec![ |
| 491 | "list", "show", "print", "pause", "resume", "adopt", "delete", "run", |
| 492 | ], |
| 493 | ), |
| 494 | ] { |
| 495 | let info = get_command_info(name).expect("registered command"); |
| 496 | assert_eq!(info.subcommands(), expected, "/{name}"); |
| 497 | } |
| 498 | let mcp = get_command_info("mcp").expect("registered command"); |
| 499 | for verb in ["init", "import", "add", "doctor", "reload"] { |
| 500 | assert!( |
| 501 | mcp.subcommands().contains(&verb), |
| 502 | "/mcp must offer `{verb}`: {:?}", |
| 503 | mcp.subcommands() |
| 504 | ); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | #[test] |
| 509 | fn a_choice_group_yields_every_literal_verb_once() { |
| 510 | assert_eq!( |
| 511 | usage_subcommands("/queue [list|send <n>|edit <n>|drop <n>|clear]"), |
| 512 | vec!["list", "send", "edit", "drop", "clear"] |
| 513 | ); |
| 514 | // `/mcp` repeats `import` and `add` with different tails; the menu |
| 515 | // offers each verb once. |
| 516 | assert_eq!( |
| 517 | usage_subcommands( |
| 518 | "/mcp [init|import|import approve <name>|add stdio <name>|add http <name> <url>|doctor]" |
| 519 | ), |
| 520 | vec!["init", "import", "add", "doctor"] |
| 521 | ); |
| 522 | } |
| 523 | |
| 524 | #[test] |
| 525 | fn a_required_choice_between_verbs_is_opened_but_a_value_is_not() { |
| 526 | assert_eq!( |
| 527 | usage_subcommands( |
| 528 | "/structcopy <turn <n>|tool <call-id>|plan|workflow <run-id>> [stdout]" |
| 529 | ), |
| 530 | vec!["turn", "tool", "plan", "workflow"] |
| 531 | ); |
| 532 | assert!(usage_subcommands("/branch <entry_id>").is_empty()); |
| 533 | assert!(usage_subcommands("/rename <new title>").is_empty()); |
| 534 | } |
| 535 | |
| 536 | #[test] |
| 537 | fn a_bare_alternation_without_brackets_still_parses() { |
| 538 | assert_eq!( |
| 539 | usage_subcommands("/auth xai-device|chatgpt|chatgpt-revoke"), |
| 540 | vec!["xai-device", "chatgpt", "chatgpt-revoke"] |
| 541 | ); |
| 542 | assert_eq!(usage_subcommands("/turn inspect"), vec!["inspect"]); |
| 543 | } |
| 544 | |
| 545 | #[test] |
| 546 | fn values_flags_and_later_groups_are_not_offered_as_verbs() { |
| 547 | // Metavariables spelled bare, snake_case values, paths, flags, and a |
| 548 | // single uppercase placeholder are all values the operator supplies. |
| 549 | assert!(usage_subcommands("/help [command]").is_empty()); |
| 550 | assert!(usage_subcommands("/profile <name>").is_empty()); |
| 551 | assert!(usage_subcommands("/save [path]").is_empty()); |
| 552 | assert!(usage_subcommands("/new [--force]").is_empty()); |
| 553 | assert!(usage_subcommands("/agent [N] <task>").is_empty()); |
| 554 | assert!( |
| 555 | usage_subcommands("/resume [session_id|path/to/export.json]").is_empty(), |
| 556 | "snake_case and path-shaped alternatives are values" |
| 557 | ); |
| 558 | // Only the first group names subcommands; `[--save]` modifies whichever |
| 559 | // placement was chosen. |
| 560 | assert_eq!( |
| 561 | usage_subcommands("/workbar [bottom|top|off] [--save]"), |
| 562 | vec!["bottom", "top", "off"] |
| 563 | ); |
| 564 | } |
| 565 | |
| 566 | #[test] |
| 567 | fn a_command_without_arguments_declares_no_subcommands() { |
| 568 | assert!(usage_subcommands("/copy").is_empty()); |
| 569 | assert!(usage_subcommands("").is_empty()); |
| 570 | } |
| 571 | |
| 572 | #[test] |
| 573 | fn every_registered_usage_parses_without_panicking() { |
| 574 | // The parser reads strings maintained by hand across ~120 commands; |
| 575 | // an unfamiliar shape must yield no hint rather than a panic. |
| 576 | for info in crate::commands::command_infos() { |
| 577 | let subcommands = info.subcommands(); |
| 578 | for subcommand in subcommands { |
| 579 | assert!( |
| 580 | info.usage.contains(subcommand), |
| 581 | "/{}: `{subcommand}` is not in `{}`", |
| 582 | info.name, |
| 583 | info.usage |
| 584 | ); |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | } |
| 589 |