use bkmr::cli::args::{Cli, Commands};
use bkmr::domain::tag::Tag;
use bkmr::util::argument_processor::ArgumentProcessor;
use bkmr::util::testing::{init_test_env, EnvGuard};
#[test]
fn given_tag_prefix_options_when_search_then_combines_tag_sets() {
let _env = init_test_env();
let _guard = EnvGuard::new();
let _ = Cli {
name: None,
config: None,
db: None,
debug: 0,
no_color: false,
generate_config: false,
command: Some(Commands::Search {
fts_query: None,
tags_exact: Some("tag1".to_string()),
tags_exact_prefix: Some("prefix1".to_string()),
tags_all: Some("tag2".to_string()),
tags_all_prefix: Some("prefix2".to_string()),
tags_all_not: Some("tag3".to_string()),
tags_all_not_prefix: Some("prefix3".to_string()),
tags_any: Some("tag4".to_string()),
tags_any_prefix: Some("prefix4".to_string()),
tags_any_not: Some("tag5".to_string()),
tags_any_not_prefix: Some("prefix5".to_string()),
order_desc: false,
order_asc: false,
sort_field: None,
non_interactive: true,
is_fuzzy: false,
fzf_style: None,
is_json: true, limit: None,
interpolate: false,
shell_stubs: false,
stdout: false,
embeddable: false,
}),
};
let exact_tags = ArgumentProcessor::apply_prefix_tags(
ArgumentProcessor::parse_tag_string(&Some("tag1".to_string())),
ArgumentProcessor::parse_tag_string(&Some("prefix1".to_string())),
);
assert!(exact_tags.is_some());
let exact_tags_set = exact_tags.unwrap();
assert_eq!(exact_tags_set.len(), 2);
assert!(exact_tags_set.contains(&Tag::new("tag1").unwrap()));
assert!(exact_tags_set.contains(&Tag::new("prefix1").unwrap()));
}
#[test]
fn given_search_command_with_prefixes_when_executed_then_performs_search() {
let _env = init_test_env();
let _guard = EnvGuard::new();
let _ = Cli {
name: None,
config: None,
db: None,
debug: 0,
no_color: false,
generate_config: false,
command: Some(Commands::Search {
fts_query: None,
tags_exact: Some("tag1".to_string()),
tags_exact_prefix: Some("prefix1".to_string()),
tags_all: None,
tags_all_prefix: None,
tags_all_not: None,
tags_all_not_prefix: None,
tags_any: None,
tags_any_prefix: None,
tags_any_not: None,
tags_any_not_prefix: None,
order_desc: false,
order_asc: false,
sort_field: None,
non_interactive: true,
is_fuzzy: false,
fzf_style: None,
is_json: true,
limit: None,
interpolate: false,
shell_stubs: false,
stdout: false,
embeddable: false,
}),
};
}
#[test]
fn test_search_interpolate_flag_parsing() {
let _env = init_test_env();
let _guard = EnvGuard::new();
use clap::Parser;
let args = vec![
"bkmr",
"search",
"--json",
"--interpolate",
"-t",
"_snip_",
"test query",
];
let cli = Cli::try_parse_from(args).unwrap();
if let Some(Commands::Search { interpolate, .. }) = cli.command {
assert!(interpolate, "interpolate flag should be true");
} else {
panic!("Expected Search command");
}
let args = vec!["bkmr", "search", "--json", "-t", "_snip_", "test query"];
let cli = Cli::try_parse_from(args).unwrap();
if let Some(Commands::Search { interpolate, .. }) = cli.command {
assert!(!interpolate, "interpolate flag should default to false");
} else {
panic!("Expected Search command");
}
}
#[test]
fn test_search_command_structure_with_interpolate() {
let _env = init_test_env();
let _guard = EnvGuard::new();
let cli = Cli {
name: None,
config: None,
db: None,
debug: 0,
no_color: false,
generate_config: false,
command: Some(Commands::Search {
fts_query: Some("test query".to_string()),
tags_exact: None,
tags_exact_prefix: None,
tags_all: Some("_snip_".to_string()),
tags_all_prefix: None,
tags_all_not: None,
tags_all_not_prefix: None,
tags_any: None,
tags_any_prefix: None,
tags_any_not: None,
tags_any_not_prefix: None,
order_desc: false,
order_asc: false,
sort_field: None,
non_interactive: true,
is_fuzzy: false,
fzf_style: None,
is_json: true,
limit: Some(50),
interpolate: true, shell_stubs: false,
stdout: false,
embeddable: false,
}),
};
if let Some(Commands::Search {
interpolate,
tags_all,
is_json,
limit,
..
}) = cli.command
{
assert!(interpolate, "interpolate should be true");
assert!(is_json, "is_json should be true");
assert_eq!(tags_all, Some("_snip_".to_string()));
assert_eq!(limit, Some(50));
} else {
panic!("Expected Search command");
}
}
#[test]
fn test_interpolation_conditions() {
let test_cases = vec![
("regular url", false),
("{{ 'pwd' | shell }}", true),
("{% set var = 'value' %}", true),
("no templates here", false),
("{{ current_date | strftime('%Y-%m-%d') }}", true),
(
"mixed content {{ 'whoami' | shell }} and regular text",
true,
),
("{%- comment -%}test{%- endcomment -%}", true),
];
for (content, should_interpolate) in test_cases {
let has_template = content.contains("{{") || content.contains("{%");
assert_eq!(
has_template,
should_interpolate,
"Content '{}' should {} interpolation",
content,
if should_interpolate {
"trigger"
} else {
"not trigger"
}
);
}
}