pub use kaish_tool_api::GlobalFlags;
#[cfg(test)]
mod tests {
use super::*;
use crate::interpreter::OutputFormat;
use crate::tools::{ExecContext, ToolArgs};
use crate::vfs::{MemoryFs, VfsRouter};
use std::sync::Arc;
fn make_ctx() -> ExecContext {
let mut vfs = VfsRouter::new();
vfs.mount("/", MemoryFs::new());
ExecContext::new(Arc::new(vfs))
}
#[test]
fn apply_from_args_sets_json_when_flag_present() {
let mut ctx = make_ctx();
let mut args = ToolArgs::new();
args.flags.insert("json".to_string());
GlobalFlags::apply_from_args(&args, false, &mut ctx);
assert!(matches!(ctx.output_format, Some(OutputFormat::Json)));
}
#[test]
fn apply_from_args_leaves_format_alone_when_absent() {
let mut ctx = make_ctx();
let args = ToolArgs::new();
GlobalFlags::apply_from_args(&args, false, &mut ctx);
assert!(ctx.output_format.is_none());
}
#[test]
fn apply_from_args_idempotent_with_apply() {
let mut ctx = make_ctx();
let mut args = ToolArgs::new();
args.flags.insert("json".to_string());
GlobalFlags::apply_from_args(&args, false, &mut ctx);
let gf = GlobalFlags { json: true };
gf.apply(&mut ctx);
assert!(matches!(ctx.output_format, Some(OutputFormat::Json)));
}
#[test]
fn apply_from_args_sets_json_from_raw_argv_positional() {
use crate::ast::Value;
let mut ctx = make_ctx();
let mut args = ToolArgs::new();
args.positional.push(Value::String("--json".to_string()));
GlobalFlags::apply_from_args(&args, true, &mut ctx);
assert!(matches!(ctx.output_format, Some(OutputFormat::Json)));
}
#[test]
fn apply_from_args_sets_json_from_raw_argv_positional_truthy_value() {
use crate::ast::Value;
let mut ctx = make_ctx();
let mut args = ToolArgs::new();
args.positional.push(Value::String("--json=yes".to_string()));
GlobalFlags::apply_from_args(&args, true, &mut ctx);
assert!(matches!(ctx.output_format, Some(OutputFormat::Json)));
}
#[test]
fn apply_from_args_leaves_format_alone_for_raw_argv_positional_falsy_value() {
use crate::ast::Value;
let mut ctx = make_ctx();
let mut args = ToolArgs::new();
args.positional.push(Value::String("--json=false".to_string()));
GlobalFlags::apply_from_args(&args, true, &mut ctx);
assert!(ctx.output_format.is_none());
}
#[test]
fn apply_from_args_ignores_json_positional_after_double_dash() {
use crate::ast::Value;
let mut ctx = make_ctx();
let mut args = ToolArgs::new();
args.positional.push(Value::String("--".to_string()));
args.positional.push(Value::String("--json".to_string()));
GlobalFlags::apply_from_args(&args, true, &mut ctx);
assert!(ctx.output_format.is_none());
}
#[test]
fn apply_from_args_ignores_a_typed_tools_positional_json() {
use crate::ast::Value;
let mut ctx = make_ctx();
let mut args = ToolArgs::new();
args.positional.push(Value::String("--json".to_string()));
GlobalFlags::apply_from_args(&args, false, &mut ctx);
assert!(ctx.output_format.is_none());
}
}