use crate::tests::parse_ts as parse_source;
#[test]
fn captures_bare_identifier_call() {
let info = parse_source("import { execSync } from 'node:child_process';\nexecSync('ls');");
assert!(
info.callee_uses.iter().any(|u| u.callee_path == "execSync"),
"bare identifier call should be captured"
);
}
#[test]
fn captures_static_member_chain_as_dotted_path() {
let info = parse_source("import * as cp from 'child_process';\ncp.exec('ls');");
assert!(
info.callee_uses.iter().any(|u| u.callee_path == "cp.exec"),
"static member call should flatten to a dotted path"
);
}
#[test]
fn captures_global_callee_without_import() {
let info = parse_source("console.log('hi');\nconsole.table([]);");
assert!(
info.callee_uses
.iter()
.any(|u| u.callee_path == "console.log")
);
assert!(
info.callee_uses
.iter()
.any(|u| u.callee_path == "console.table")
);
}
#[test]
fn keeps_every_site_of_a_repeated_callee_under_one_entry() {
let source = "console.log('a');\nconsole.log('b');\nconsole.log('c');";
let info = parse_source(source);
let uses: Vec<_> = info
.callee_uses
.iter()
.filter(|u| u.callee_path == "console.log")
.collect();
assert_eq!(uses.len(), 1, "one entry per path and form");
assert_eq!(uses[0].span_starts, vec![0, 18, 36], "one start per site");
}
#[test]
fn unwraps_parenthesized_callee() {
let info = parse_source("(console.log)('hi');");
assert!(
info.callee_uses
.iter()
.any(|u| u.callee_path == "console.log"),
"parenthesized callee should unwrap"
);
}
#[test]
fn skips_computed_member_callee() {
let info = parse_source("const m = 'log';\nconsole[m]('hi');");
assert!(
!info
.callee_uses
.iter()
.any(|u| u.callee_path.starts_with("console")),
"computed member callee is not statically flattenable"
);
}
#[test]
fn skips_dynamic_dispatch_callee() {
let info = parse_source("factory()();");
assert_eq!(info.callee_uses.len(), 1);
assert_eq!(info.callee_uses[0].callee_path, "factory");
}
#[test]
fn coexists_with_security_sink_capture() {
let info = parse_source(
"import { exec } from 'node:child_process';\nconst cmd = userInput();\nexec(cmd);",
);
assert!(
info.callee_uses.iter().any(|u| u.callee_path == "exec"),
"callee-use capture should coexist with sink capture"
);
assert!(
info.security_sinks
.iter()
.any(|s| s.callee_path == "node:child_process.exec"),
"security sink capture should trace the named import"
);
}
#[test]
fn captures_optional_chaining_callee() {
let info = parse_source("import * as cp from 'child_process';\ncp?.exec('ls');");
assert!(
info.callee_uses.iter().any(|u| u.callee_path == "cp.exec"),
"optional-chaining callee should flatten like its non-optional form, got {:?}",
info.callee_uses
);
}
#[test]
fn zero_arg_and_fully_literal_calls_are_captured() {
let info = parse_source(
"import { execSync } from 'child_process';\nexecSync('ls');\nconsole.table();",
);
assert!(info.callee_uses.iter().any(|u| u.callee_path == "execSync"));
assert!(
info.callee_uses
.iter()
.any(|u| u.callee_path == "console.table")
);
}
fn forms(source: &str) -> Vec<(String, bool, u32)> {
parse_source(source)
.callee_uses
.iter()
.flat_map(|u| {
u.span_starts
.iter()
.map(|&start| (u.callee_path.clone(), u.is_new, start))
})
.collect()
}
#[test]
fn captures_new_expression_as_new_form() {
assert_eq!(forms("new Date();"), vec![("Date".to_owned(), true, 0)]);
}
#[test]
fn captures_new_without_parentheses_and_parenthesized_callee() {
assert_eq!(forms("new Mesh;"), vec![("Mesh".to_owned(), true, 0)]);
assert_eq!(
forms("new (T.Mesh)();"),
vec![("T.Mesh".to_owned(), true, 0)]
);
}
#[test]
fn captures_new_static_member_chain() {
assert_eq!(
forms("new T.Group.Inner();"),
vec![("T.Group.Inner".to_owned(), true, 0)]
);
}
#[test]
fn groups_sites_per_form_and_keeps_every_site() {
let source = "new Date();Date();new Date();Date();";
assert_eq!(
forms(source),
vec![
("Date".to_owned(), true, 0),
("Date".to_owned(), true, 18),
("Date".to_owned(), false, 11),
("Date".to_owned(), false, 29),
]
);
}
#[test]
fn nested_new_and_call_on_one_line_give_two_entries() {
assert_eq!(
forms("new Date(Date());"),
vec![("Date".to_owned(), true, 0), ("Date".to_owned(), false, 9)]
);
}
#[test]
fn skips_dynamic_new_callee() {
assert_eq!(
forms("const m = 'x';\nnew ns[m]();\nnew (make())();"),
vec![("make".to_owned(), false, 33)]
);
}
#[test]
fn call_form_entry_is_not_marked_new() {
assert_eq!(forms("Date();"), vec![("Date".to_owned(), false, 0)]);
}