Skip to content

The headless browser for AI agents and web scraping

fix(cli): make --quiet suppress JS console errors on stderr (#56) (#74)

Ben Younes84115ca06e5d
diff --git a/crates/obscura-cli/src/main.rs b/crates/obscura-cli/src/main.rs
index d19b34b5..357e4e87 100644
--- a/crates/obscura-cli/src/main.rs
+++ b/crates/obscura-cli/src/main.rs
@@ -119,11 +119,26 @@ fn print_banner(port: u16) {
"#, port);
}
+fn select_log_filter(verbose: bool, quiet: bool) -> &'static str {
+ if verbose {
+ "debug"
+ } else if quiet {
+ "off"
+ } else {
+ "warn"
+ }
+}
+
+fn is_quiet_command(cmd: &Option<Command>) -> bool {
+ matches!(cmd, Some(Command::Fetch { quiet: true, .. }))
+}
+
#[tokio::main(flavor = "current_thread")]
async fn main() -> anyhow::Result<()> {
let args = Args::parse();
- let filter = if args.verbose { "debug" } else { "warn" };
+ let quiet = is_quiet_command(&args.command);
+ let filter = select_log_filter(args.verbose, quiet);
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::try_from_default_env()
@@ -725,3 +740,89 @@ fn dump_links(page: &Page) {
}
});
}
+#[cfg(test)]
+mod tests {
+ use super::{is_quiet_command, select_log_filter, Args, Command};
+ use clap::Parser;
+
+ #[test]
+ fn default_filter_is_warn() {
+ assert_eq!(select_log_filter(false, false), "warn");
+ }
+
+ #[test]
+ fn verbose_filter_is_debug() {
+ assert_eq!(select_log_filter(true, false), "debug");
+ }
+
+ #[test]
+ fn quiet_filter_is_off() {
+ assert_eq!(select_log_filter(false, true), "off");
+ }
+
+ #[test]
+ fn verbose_wins_over_quiet() {
+ assert_eq!(select_log_filter(true, true), "debug");
+ }
+
+ #[test]
+ fn parsed_fetch_with_quiet_flag_is_detected() {
+ let args = Args::try_parse_from([
+ "obscura",
+ "fetch",
+ "--quiet",
+ "https://example.com",
+ ])
+ .expect("clap should accept --quiet on fetch");
+ assert!(is_quiet_command(&args.command));
+ }
+
+ #[test]
+ fn parsed_fetch_without_quiet_is_not_detected() {
+ let args = Args::try_parse_from(["obscura", "fetch", "https://example.com"])
+ .expect("clap should accept fetch without --quiet");
+ assert!(!is_quiet_command(&args.command));
+ }
+
+ #[test]
+ fn parsed_serve_command_is_not_quiet() {
+ let args = Args::try_parse_from(["obscura", "serve"])
+ .expect("clap should accept serve");
+ assert!(!is_quiet_command(&args.command));
+ }
+
+ #[test]
+ fn no_subcommand_is_not_quiet() {
+ assert!(!is_quiet_command(&None));
+ }
+
+ #[test]
+ fn parsed_fetch_quiet_resolves_to_off_filter() {
+ let args = Args::try_parse_from([
+ "obscura",
+ "fetch",
+ "--quiet",
+ "https://example.com",
+ ])
+ .unwrap();
+ let filter = select_log_filter(args.verbose, is_quiet_command(&args.command));
+ assert_eq!(filter, "off");
+ }
+
+ #[test]
+ fn matcher_still_uses_fetch_variant() {
+ let cmd = Some(Command::Fetch {
+ url: "https://x".to_string(),
+ dump: super::DumpFormat::Html,
+ selector: None,
+ wait: 5,
+ timeout: 30,
+ wait_until: "load".to_string(),
+ user_agent: None,
+ stealth: false,
+ eval: None,
+ quiet: true,
+ });
+ assert!(is_quiet_command(&cmd));
+ }
+}