mirror of
https://github.com/vercel-labs/agent-browser.git
synced 2026-11-23 10:34:19 +08:00
* fix(cli): reject unknown screenshot flags Treat unknown flags as errors instead of using them as selectors or output paths. Keep dash-prefixed values available after the end-of-options marker. Co-Authored-By: on101094 <333183542+on101094@users.noreply.github.com> * fix(cli): reject unknown command flags Parse command options consistently while preserving literal arguments and MCP parity. * fix(cli): preserve option boundaries Keep literal values and batch arguments consistent across the CLI and MCP. * Fix batch flag guidance and close validation --------- Co-authored-by: on101094 <333183542+on101094@users.noreply.github.com>
95 lines
3.3 KiB
Rust
95 lines
3.3 KiB
Rust
use serde_json::Value;
|
|
use std::process::{Command, Output};
|
|
use tempfile::TempDir;
|
|
|
|
const BIN: &str = env!("CARGO_BIN_EXE_agent-browser");
|
|
|
|
fn run(args: &[&str]) -> (TempDir, Output) {
|
|
let temp = tempfile::tempdir().unwrap();
|
|
let mut command = Command::new(BIN);
|
|
for (key, _) in std::env::vars_os() {
|
|
if key.to_string_lossy().starts_with("AGENT_BROWSER_") {
|
|
command.env_remove(key);
|
|
}
|
|
}
|
|
let socket_dir = temp.path().join("sockets");
|
|
let output = command
|
|
.args(args)
|
|
.current_dir(temp.path())
|
|
.env("HOME", temp.path())
|
|
.env("USERPROFILE", temp.path())
|
|
.env("AGENT_BROWSER_SOCKET_DIR", socket_dir)
|
|
.env("NO_COLOR", "1")
|
|
.output()
|
|
.unwrap();
|
|
(temp, output)
|
|
}
|
|
|
|
#[test]
|
|
fn help_only_succeeds_without_a_command_or_for_known_commands() {
|
|
let cases: &[(&[&str], bool, &str)] = &[
|
|
(&["unknown-command", "--help"], false, "Unknown command"),
|
|
(&["--help", "unknown-command"], false, "Unknown command"),
|
|
(&["unknown-command", "-h"], false, "Unknown command"),
|
|
(
|
|
&["--json", "unknown-command", "--help"],
|
|
false,
|
|
"unknown_command",
|
|
),
|
|
(&["--unknown", "--help"], true, "Usage:"),
|
|
(&["--help", "open"], true, "agent-browser open"),
|
|
(&["open", "--help"], true, "agent-browser open"),
|
|
(&["--json", "--help"], true, "Usage:"),
|
|
];
|
|
|
|
for (args, succeeds, expected) in cases {
|
|
let (temp, output) = run(args);
|
|
assert_eq!(output.status.success(), *succeeds, "{args:?}");
|
|
if args.contains(&"--json") && !succeeds {
|
|
let result: Value = serde_json::from_slice(&output.stdout).unwrap();
|
|
assert_eq!(result["success"], false, "{args:?}: {result}");
|
|
assert_eq!(result["type"], *expected, "{args:?}: {result}");
|
|
} else {
|
|
let text = format!(
|
|
"{}{}",
|
|
String::from_utf8_lossy(&output.stdout),
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
assert!(text.contains(expected), "{args:?}: {text}");
|
|
}
|
|
assert!(!temp.path().join("sockets").exists(), "{args:?}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn cookies_rejects_unknown_operations_without_starting_a_daemon() {
|
|
let (temp, output) = run(&["--json", "cookies", "typo"]);
|
|
assert!(!output.status.success());
|
|
let result: Value = serde_json::from_slice(&output.stdout).unwrap();
|
|
assert_eq!(result["type"], "unknown_subcommand");
|
|
assert!(result["error"]
|
|
.as_str()
|
|
.unwrap()
|
|
.contains("Valid options: get, set, clear"));
|
|
assert!(!temp.path().join("sockets").exists());
|
|
}
|
|
|
|
#[test]
|
|
fn close_all_rejects_unknown_flags_before_closing_sessions() {
|
|
for args in [
|
|
&["close", "--all", "--bogus"][..],
|
|
&["quit", "--all", "--bogus"][..],
|
|
&["exit", "--all", "--bogus"][..],
|
|
] {
|
|
let (temp, output) = run(args);
|
|
assert!(!output.status.success(), "{args:?}");
|
|
let text = format!(
|
|
"{}{}",
|
|
String::from_utf8_lossy(&output.stdout),
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
assert!(text.contains("unknown flag '--bogus'"), "{args:?}: {text}");
|
|
assert!(!temp.path().join("sockets").exists(), "{args:?}");
|
|
}
|
|
}
|