use super::{run::ShowMode, selection::selection_file, *};
use clap::error::ErrorKind;
#[test]
fn all_run_request_and_reuse_key_arguments_retain_valid_wire_strings() {
let id = "run-legacy_1.request";
let key = "a".repeat(64);
let commands = [
vec!["run", "show", id],
vec!["request", "list", "--run", id],
vec!["request", "show", id],
vec!["request", "claim", id],
vec!["request", "unclaim", id],
vec!["request", "tool", id, "inspect"],
vec!["request", "submit", id, "--verdict", "GREEN"],
vec!["review", id],
vec!["cache", "show", key.as_str()],
vec!["cache", "rm", key.as_str()],
vec!["server", "rm", key.as_str()],
];
for command in commands {
Cli::try_parse_from(std::iter::once("artifactize").chain(command)).unwrap();
}
let Some(CommandArgs::General(Command::Run {
command: RunCommand::Show { run_id, mode },
})) = Cli::try_parse_from(["artifactize", "run", "show", id])
.unwrap()
.command
else {
panic!("Run show");
};
assert_eq!(run_id.as_str(), id);
assert_eq!(mode, ShowMode::Snapshot);
let Some(CommandArgs::General(Command::Cache {
command: CacheCommand::Show { key: parsed, .. },
})) = Cli::try_parse_from(["artifactize", "cache", "show", key.as_str()])
.unwrap()
.command
else {
panic!("Cache show");
};
assert_eq!(parsed.as_str(), key);
let legacy = "remote-".to_owned() + &"x".repeat(200);
assert!(
legacy
.parse::<artifactize_core::types::ExecutionId>()
.is_ok()
);
assert!(Cli::try_parse_from(["artifactize", "request", "show", legacy.as_str()]).is_err());
}
#[test]
fn malformed_identity_arguments_are_clap_usage_errors() {
for id in [
"",
"..",
"../escape",
"a/b",
"spaces here",
"한글",
&"x".repeat(201),
] {
for command in [
vec!["run", "show", id],
vec!["request", "list", "--run", id],
vec!["request", "show", id],
vec!["request", "claim", id],
vec!["request", "unclaim", id],
vec!["request", "tool", id, "inspect"],
vec!["request", "submit", id, "--verdict", "GREEN"],
vec!["review", id],
] {
let error =
Cli::try_parse_from(std::iter::once("artifactize").chain(command)).unwrap_err();
assert_eq!(error.kind(), ErrorKind::ValueValidation);
}
}
for key in [
"missing",
&"a".repeat(63),
&"A".repeat(64),
&"g".repeat(64),
&"0".repeat(65),
] {
for command in [
["cache", "show", key],
["cache", "rm", key],
["server", "rm", key],
] {
assert_eq!(
Cli::try_parse_from(std::iter::once("artifactize").chain(command))
.unwrap_err()
.kind(),
ErrorKind::ValueValidation
);
}
}
}
#[test]
fn selection_duration_and_verdict_are_parsed_at_the_command_boundary() {
for args in [
vec!["verify", "app/check"],
vec!["verify", "--evals", "app/check,lib/test"],
vec!["verify", "--artifacts", "app,lib"],
vec!["config", "graph", "app"],
] {
Cli::try_parse_from(std::iter::once("artifactize").chain(args)).unwrap();
}
for args in [
vec!["verify", "../escape"],
vec!["verify", "--eval", "app"],
vec!["verify", "--evals", "app/check,invalid"],
vec!["verify", "--artifacts", "app,invalid/name"],
vec!["config", "graph", "app/check"],
vec!["request", "submit", "r1", "--verdict", "green"],
] {
assert!(Cli::try_parse_from(std::iter::once("artifactize").chain(args)).is_err());
}
let cli = Cli::try_parse_from([
"artifactize",
"request",
"submit",
"r1",
"--verdict",
"GREEN",
])
.unwrap();
let Some(CommandArgs::General(Command::Request {
command: RequestCommand::Submit { verdict, .. },
})) = cli.command
else {
panic!("submit")
};
assert_eq!(verdict, artifactize_core::runtime::Verdict::Green);
}
#[test]
fn verify_and_status_parse_exclusive_selectors() {
let selected = |args: &[&str]| {
let cli = Cli::try_parse_from(["artifactize"].into_iter().chain(args.iter().copied()))?;
match cli.command {
Some(CommandArgs::General(
Command::Verify { selection, .. } | Command::Status { selection, .. },
)) => Ok::<_, clap::Error>(selection),
_ => panic!("expected verify or status"),
}
};
assert!(matches!(selected(&["status"]).unwrap(), SelectionArgs::All));
assert!(matches!(
selected(&["verify", "--all"]).unwrap(),
SelectionArgs::All
));
assert!(
matches!(selected(&["verify", "app"]).unwrap(), SelectionArgs::Artifact(name) if name.as_str() == "app")
);
for args in [
&["verify", "app/check"][..],
&["verify", "--eval", "app/check"],
] {
assert!(
matches!(selected(args).unwrap(), SelectionArgs::Eval(id) if id.as_str() == "app/check")
);
}
assert!(
matches!(selected(&["verify", "--evals-file", "evals.txt"]).unwrap(), SelectionArgs::EvalsFile(path) if path == std::path::Path::new("evals.txt"))
);
assert_eq!(
selected(&["verify"]).unwrap_err().kind(),
ErrorKind::MissingRequiredArgument
);
assert_eq!(
selected(&["status", "app", "--all"]).unwrap_err().kind(),
ErrorKind::ArgumentConflict
);
}
#[test]
fn run_wait_duration_validates_milliseconds_on_the_command_that_accepts_it() {
let parse = |millis| {
Cli::try_parse_from([
"artifactize",
"run",
"show",
"run-valid",
"--wait",
"--timeout-ms",
millis,
])
};
for millis in ["0", "2147483648"] {
assert_eq!(
parse(millis).unwrap_err().kind(),
ErrorKind::ValueValidation
);
}
for (millis, expected) in [("1", 1), ("2147483647", 2_147_483_647)] {
let Some(CommandArgs::General(Command::Run {
command: RunCommand::Show { mode, .. },
})) = parse(millis).unwrap().command
else {
panic!("run show command");
};
assert_eq!(
mode,
ShowMode::Wait {
timeout: std::time::Duration::from_millis(expected)
}
);
}
}
#[test]
fn selection_files_validate_ids_before_entering_the_project() {
let root = crate::test_os::tempdir();
let path = root.path().join("selection.json");
std::fs::write(&path, r#"["app/check","../escape"]"#).unwrap();
assert!(selection_file::<artifactize_core::types::EvalId>(&path).is_err());
std::fs::write(&path, r#"["app","lib"]"#).unwrap();
assert_eq!(
selection_file::<artifactize_core::types::ArtifactName>(&path).unwrap(),
["app", "lib"]
);
}
#[test]
fn human_tool_names_validate_at_the_cli_boundary() {
for name in ["", "../escape", "bad/name", "spaces here", "한글"] {
let error = Cli::try_parse_from(["artifactize", "request", "tool", "request-valid", name])
.unwrap_err();
assert_eq!(error.kind(), ErrorKind::ValueValidation);
}
let Some(CommandArgs::General(Command::Request {
command: RequestCommand::Tool { tool, .. },
})) = Cli::try_parse_from([
"artifactize",
"request",
"tool",
"request-valid",
"inspect_owner",
])
.unwrap()
.command
else {
panic!("request tool");
};
assert_eq!(tool.as_str(), "inspect_owner");
}