|
| 1 | +#![cfg(feature = "unstable_tool_call_name")] |
| 2 | + |
| 3 | +#[cfg(feature = "unstable_protocol_v2")] |
| 4 | +use agent_client_protocol::schema::v1::ToolCallUpdate as V1ToolCallUpdate; |
| 5 | +use agent_client_protocol::schema::v1::{ToolCall, ToolCallUpdateFields}; |
| 6 | +use serde_json::json; |
| 7 | + |
| 8 | +#[test] |
| 9 | +fn v1_tool_call_name_serializes_and_updates() { |
| 10 | + let mut tool_call = ToolCall::new("call_1", "Read configuration").name("read_file"); |
| 11 | + |
| 12 | + assert_eq!( |
| 13 | + serde_json::to_value(&tool_call).unwrap()["name"], |
| 14 | + json!("read_file") |
| 15 | + ); |
| 16 | + |
| 17 | + tool_call.update(ToolCallUpdateFields::new().name("write_file")); |
| 18 | + assert_eq!(tool_call.name.as_deref(), Some("write_file")); |
| 19 | + |
| 20 | + tool_call.update(ToolCallUpdateFields::new()); |
| 21 | + assert_eq!(tool_call.name.as_deref(), Some("write_file")); |
| 22 | + |
| 23 | + let null_name: ToolCallUpdateFields = serde_json::from_value(json!({ "name": null })).unwrap(); |
| 24 | + tool_call.update(null_name); |
| 25 | + assert_eq!(tool_call.name.as_deref(), Some("write_file")); |
| 26 | +} |
| 27 | + |
| 28 | +#[cfg(feature = "unstable_protocol_v2")] |
| 29 | +#[test] |
| 30 | +fn tool_call_name_preserves_v2_patch_semantics() { |
| 31 | + use agent_client_protocol::schema::{ |
| 32 | + MaybeUndefined, |
| 33 | + v2::{ |
| 34 | + ToolCallUpdate, |
| 35 | + conversion::{try_v1_to_v2, try_v2_to_v1}, |
| 36 | + }, |
| 37 | + }; |
| 38 | + |
| 39 | + let named: ToolCallUpdate = |
| 40 | + try_v1_to_v2(ToolCall::new("call_1", "Read configuration").name("read_file")).unwrap(); |
| 41 | + assert_eq!(named.name, MaybeUndefined::Value("read_file".to_string())); |
| 42 | + |
| 43 | + let omitted: ToolCallUpdate = |
| 44 | + try_v1_to_v2(V1ToolCallUpdate::new("call_1", ToolCallUpdateFields::new())).unwrap(); |
| 45 | + assert_eq!(omitted.name, MaybeUndefined::Undefined); |
| 46 | + |
| 47 | + let cleared = ToolCallUpdate::new("call_1").name(None::<String>); |
| 48 | + assert_eq!(cleared.name, MaybeUndefined::Null); |
| 49 | + let error = try_v2_to_v1::<_, V1ToolCallUpdate>(cleared).unwrap_err(); |
| 50 | + assert_eq!( |
| 51 | + error.message(), |
| 52 | + "v2 ToolCallUpdate.name with null value cannot be represented in v1" |
| 53 | + ); |
| 54 | +} |
0 commit comments