use crate::cli::RunArgs;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Route {
Attended,
Resumable,
Attach,
}
pub(crate) fn route(args: &RunArgs, in_lan_task: bool) -> Route {
if in_lan_task {
return if args.await_result {
Route::Attach
} else {
Route::Resumable
};
}
if args.resumable {
return Route::Resumable;
}
if args.json && !args.await_result {
return Route::Attended;
}
Route::Attach
}
#[cfg(test)]
mod tests {
use super::*;
use clap::Parser;
use crate::cli::{Cli, Command};
fn args(flags: &[&str]) -> RunArgs {
let mut argv = vec!["basis", "spawn", "a prompt"];
argv.extend_from_slice(flags);
let cli = Cli::try_parse_from(argv).expect("flags parse");
let Some(Command::Spawn(args)) = cli.command else {
panic!("spawn parses");
};
args
}
#[test]
fn the_matrix_is_what_the_docs_say_it_is() {
let matrix = [
(false, vec![], Route::Attach),
(false, vec!["--json"], Route::Attended),
(false, vec!["--await"], Route::Attach),
(false, vec!["--json", "--await"], Route::Attach),
(false, vec!["--resumable"], Route::Resumable),
(false, vec!["--resumable", "--json"], Route::Resumable),
(false, vec!["--detached"], Route::Attach),
(true, vec![], Route::Resumable),
(true, vec!["--json"], Route::Resumable),
(true, vec!["--await"], Route::Attach),
(true, vec!["--json", "--await"], Route::Attach),
(true, vec!["--resumable"], Route::Resumable),
(true, vec!["--detached"], Route::Resumable),
];
for (in_lan_task, flags, expected) in matrix {
assert_eq!(
route(&args(&flags), in_lan_task),
expected,
"BASIS_TASK_ID {}, flags {flags:?}",
if in_lan_task { "set" } else { "unset" }
);
}
}
#[test]
fn a_bare_prompt_at_a_shell_is_driven_here() {
assert_eq!(route(&args(&[]), false), Route::Attach);
}
#[test]
fn json_never_changes_the_lifecycle_it_only_changes_the_rendering() {
for flags in [vec![], vec!["--await"], vec!["--resumable"]] {
let mut with_json = flags.clone();
with_json.push("--json");
for in_lan_task in [false, true] {
if flags.is_empty() && !in_lan_task {
continue;
}
assert_eq!(
route(&args(&flags), in_lan_task),
route(&args(&with_json), in_lan_task),
"flags {flags:?} in_lan_task {in_lan_task}"
);
}
}
}
#[test]
fn waiting_and_not_waiting_cannot_both_be_asked_for() {
let cli = Cli::try_parse_from(["basis", "spawn", "p", "--await", "--resumable"]);
assert!(cli.is_err(), "--await --resumable must not parse");
}
}