use gadk_genai::{Content, FunctionCall, FunctionResponse, Part};
use gadk_platform::Platform;
use crate::tool::utils;
#[test]
fn test_generate_function_call_id_uses_provider() {
let platform = Platform::default().with_uuid(|| "fixed".to_string());
let got = utils::generate_function_call_id(&platform);
assert!(
got.starts_with("adk-"),
"GenerateFunctionCallID() = {got:?}, want \"adk-\" prefix"
);
assert!(
got.ends_with("fixed"),
"GenerateFunctionCallID() = {got:?}, want provider value"
);
}
#[test]
fn test_generate_function_call_id_default_is_unique() {
let platform = Platform::default();
let first = utils::generate_function_call_id(&platform);
let second = utils::generate_function_call_id(&platform);
assert_ne!(first, second);
}
#[test]
fn test_populate_client_function_call_id_uses_provider() {
let mut content = Content {
parts: vec![
Part {
function_call: Some(FunctionCall {
name: "needs_id".to_string(),
..Default::default()
}),
..Default::default()
},
Part {
function_call: Some(FunctionCall {
id: "keep".to_string(),
name: "has_id".to_string(),
..Default::default()
}),
..Default::default()
},
],
..Default::default()
};
let platform = Platform::default().with_uuid(|| "generated".to_string());
utils::populate_client_function_call_id(&platform, Some(&mut content));
let ids: Vec<&str> = content
.parts
.iter()
.map(|p| p.function_call.as_ref().unwrap().id.as_str())
.collect();
assert_eq!(ids[0], "adk-generated", "empty function call ID");
assert_eq!(
ids[1], "keep",
"preset function call ID must be left untouched"
);
}
#[test]
fn test_part_accessors_skip_nil_parts() {
let call = Part {
function_call: Some(FunctionCall {
name: "call".to_string(),
..Default::default()
}),
..Default::default()
};
let resp = Part {
function_response: Some(FunctionResponse {
name: "resp".to_string(),
..Default::default()
}),
..Default::default()
};
let text = Part::from_text("hi");
type Layout = fn(&Part) -> Vec<Part>;
let layouts: [(&str, Layout, usize); 4] = [
("leading", |keep| vec![Part::default(), keep.clone()], 1),
(
"middle",
|keep| vec![keep.clone(), Part::default(), keep.clone()],
2,
),
("trailing", |keep| vec![keep.clone(), Part::default()], 1),
("only", |_keep| vec![Part::default()], 0),
];
let content = |parts: Vec<Part>| Content {
parts,
..Default::default()
};
for (name, parts, want) in layouts {
assert_eq!(
utils::function_calls(Some(&content(parts(&call)))).len(),
want,
"{name}: FunctionCalls()"
);
assert_eq!(
utils::function_responses(Some(&content(parts(&resp)))).len(),
want,
"{name}: FunctionResponses()"
);
assert_eq!(
utils::text_parts(Some(&content(parts(&text)))).len(),
want,
"{name}: TextParts()"
);
}
assert!(utils::function_calls(None).is_empty());
assert!(utils::function_responses(None).is_empty());
assert!(utils::text_parts(None).is_empty());
}