Files
leleka/desktop-core/tests/native_tools_sdet/declarations.rs
T

213 lines
7.7 KiB
Rust

//! Tool declarations, choices, schemas and the prompt template.
use crate::harness::{call, function, request};
use serde_json::{Value, json};
use strata_desktop_core::runtime_tools::{ValidatedSegment, prepare};
#[test]
fn no_tool_prompt_retains_literal_template_bytes() {
let plan = prepare(&json!({"model":"native_fixture","input":" hello "})).unwrap();
assert_eq!(plan.max_output_tokens, 256);
assert_eq!(
plan.prompt,
"<|im_start|>user\nhello<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n"
);
let segments = plan.policy.parse_output("plain text").unwrap();
assert!(matches!(&segments[0], ValidatedSegment::Text(text) if text == "plain text"));
}
fn assert_invalid_top_level_fields_refused() {
for (field, value) in [
("previous_response_id", json!("resp_old")),
("store", json!(true)),
// Sampling controls are forwarded since cce4333, but only inside the Original's ranges.
("temperature", json!(2.5)),
("unexpected_field", json!(true)),
] {
let mut invalid = request(vec![function("read")]);
invalid[field] = value;
assert!(prepare(&invalid).is_err(), "field {field}");
}
}
#[test]
fn declarations_are_bounded_unique_and_closed_before_render() {
let valid = prepare(&request(vec![function("read")])).unwrap();
assert_eq!(valid.max_output_tokens, 64);
assert!(valid.prompt.contains("read"));
let grammar = json!({"type":"grammar","syntax":"lark","definition":"start: WORD"});
for tools in [
vec![function("read"), function("read")],
vec![json!({"type":"web_search"})],
vec![json!({"type":"custom","name":"run","format":grammar})],
vec![
json!({"type":"function","name":"read","parameters":{"type":"object"},"surprise":true}),
],
(0..129).map(|i| function(&format!("read_{i}"))).collect(),
] {
assert!(prepare(&request(tools)).is_err());
}
assert_invalid_top_level_fields_refused();
let mut sampled = request(vec![function("read")]);
sampled["temperature"] = json!(0.4);
assert!(
prepare(&sampled).is_ok(),
"an in-range temperature is accepted"
);
}
#[test]
fn choice_requires_declared_identity_and_controls_call_presence() {
let raw = call("read", &[("path", "fixture")]);
for choice in [
json!("auto"),
json!("required"),
json!({"type":"function","name":"read"}),
] {
let mut req = request(vec![function("read")]);
req["tool_choice"] = choice;
let plan = prepare(&req).unwrap();
assert!(plan.policy.parse_output(&raw).is_ok());
if req["tool_choice"] != "auto" {
assert!(plan.policy.parse_output("text alone").is_err());
}
}
let mut none = request(vec![function("read")]);
none["tool_choice"] = json!("none");
let plan = prepare(&none).unwrap();
assert!(plan.policy.parse_output("text alone").is_ok());
assert!(plan.policy.parse_output(&raw).is_err());
for choice in [
json!("unknown"),
json!({"type":"function","name":"absent"}),
json!({"type":"function","name":"read","extra":1}),
] {
let mut req = request(vec![function("read")]);
req["tool_choice"] = choice;
assert!(prepare(&req).is_err());
}
}
#[test]
fn allowed_subset_and_parallel_false_are_enforced_after_whole_parse() {
let mut req = request(vec![function("read"), function("write")]);
req["tool_choice"] = json!({
"type": "allowed_tools", "mode": "required", "tools": [{"type": "function", "name": "read"}],
});
let plan = prepare(&req).unwrap();
assert!(
plan.policy
.parse_output(&call("read", &[("path", "a")]))
.is_ok()
);
assert!(
plan.policy
.parse_output(&call("write", &[("path", "a")]))
.is_err()
);
assert!(plan.policy.parse_output("text alone").is_err());
req["tool_choice"]["tools"] = json!([{"type":"function","name":"absent"}]);
assert!(prepare(&req).is_err());
let mut req = request(vec![function("read")]);
req["parallel_tool_calls"] = json!(false);
let plan = prepare(&req).unwrap();
let one = call("read", &[("path", "a")]);
assert!(plan.policy.parse_output(&one).is_ok());
assert!(plan.policy.parse_output(&format!("{one}\n{one}")).is_err());
}
fn assert_nullable_path_values(plan: &strata_desktop_core::runtime_tools::ToolPlan) {
for (raw, expected) in [("null", Value::Null), ("123", json!("123"))] {
let parsed = plan
.policy
.parse_output(&call("read", &[("path", raw)]))
.unwrap();
let ValidatedSegment::Call(item) = &parsed[0] else {
panic!("expected call");
};
let arguments: Value = serde_json::from_str(item["arguments"].as_str().unwrap()).unwrap();
assert_eq!(arguments, json!({"path":expected}));
assert_eq!(item["name"], "read");
assert_eq!(item["type"], "function_call");
assert!(item.get("id").is_none());
assert!(item.get("call_id").is_none());
assert!(item.get("status").is_none());
}
}
fn assert_integer_minimum_is_enforced() {
let count = json!({"type":"function","name":"count","parameters":{
"type":"object","properties":{"n":{"type":"integer","minimum":1}},
"required":["n"],"additionalProperties":false}});
let typed = prepare(&request(vec![count])).unwrap();
for bad in ["0", "1.5", "null", "oops"] {
assert!(
typed
.policy
.parse_output(&call("count", &[("n", bad)]))
.is_err()
);
}
assert!(
typed
.policy
.parse_output(&call("count", &[("n", "2")]))
.is_ok()
);
}
#[test]
fn schema_required_additional_and_nullable_values_are_independent() {
let tool = json!({"type":"function","name":"read","parameters":{
"type":"object","properties":{"path":{"type":["string","null"]}},
"required":["path"],"additionalProperties":false}});
let plan = prepare(&request(vec![tool])).unwrap();
assert_nullable_path_values(&plan);
assert!(plan.policy.parse_output(&call("read", &[])).is_err());
assert!(
plan.policy
.parse_output(&call("read", &[("path", "a"), ("extra", "1")]))
.is_err()
);
assert_integer_minimum_is_enforced();
}
fn assert_reference_refused(key: &str, reference: &str) {
let mut schema = json!({"type":"object","properties":{"deep":{"allOf":[{}]}}});
schema["properties"]["deep"]["allOf"][0][key] = json!(reference);
let tool = json!({"type":"function","name":"read","parameters":schema});
assert!(prepare(&request(vec![tool])).is_err(), "{key} {reference}");
}
fn assert_external_reference_forms_refuse() {
for key in ["$ref", "$dynamicRef", "$recursiveRef"] {
for reference in [
"https://example.invalid/schema",
"file:///etc/private",
"../schema.json",
] {
assert_reference_refused(key, reference);
}
}
}
#[test]
fn schema_local_refs_work_and_all_external_reference_forms_refuse() {
let schema = json!({"type":"object","$defs":{"path":{"type":"string"}},
"properties":{"path":{"$ref":"#/$defs/path"}},"required":["path"],"additionalProperties":false});
let plan = prepare(&request(vec![
json!({"type":"function","name":"read","parameters":schema}),
]))
.unwrap();
assert!(
plan.policy
.parse_output(&call("read", &[("path", "fixture")]))
.is_ok()
);
assert_external_reference_forms_refuse();
let bad = request(vec![
json!({"type":"function","name":"read","parameters":{"type":"bogus"}}),
]);
assert!(prepare(&bad).is_err());
}