use crate::context::builder::test_support::{named_agent, session};
use crate::context::{ContextDelta, InvocationContext};
#[test]
fn test_identity_from_userless_session() {
let ic = InvocationContext::builder()
.session(session("app", "", "sid"))
.build_invocation();
let promoted = ic.clone().into_context();
let cases = [
("the invocation itself", ic.clone()),
("Promote", promoted.as_invocation().clone()),
(
"NewToolContext",
promoted
.with_delta(ContextDelta {
function_call_id: Some("fc".into()),
..Default::default()
})
.as_invocation()
.clone(),
),
(
"NewCallbackContext",
promoted
.with_delta(ContextDelta {
actions: Some(Default::default()),
..Default::default()
})
.as_invocation()
.clone(),
),
];
for (name, c) in cases {
assert_eq!(
(c.user_id(), c.app_name(), c.session_id()),
("", "app", "sid"),
"{name}"
);
}
}
#[test]
fn test_invocation_context_identity_is_owned() {
let ic = InvocationContext::builder()
.session(session("app-a", "user-a", "sess-a"))
.build_invocation();
let child = ic.with_delta(ContextDelta {
agent: Some(named_agent("child")),
..Default::default()
});
for c in [&ic, &child] {
assert_eq!(
(c.user_id(), c.app_name(), c.session_id()),
("user-a", "app-a", "sess-a")
);
}
}
#[test]
fn test_identity_through_nested_sessionless_contexts() {
let ic = InvocationContext::builder()
.session(session("app", "u", "s"))
.build_invocation();
let nested = ic
.into_context()
.with_delta(ContextDelta {
function_call_id: Some("fc-1".into()),
..Default::default()
})
.with_delta(ContextDelta {
function_call_id: Some("fc-2".into()),
..Default::default()
});
assert_eq!(
(nested.user_id(), nested.app_name(), nested.session_id()),
("u", "app", "s")
);
}
#[test]
fn test_nil_delta_keeps_the_context_usable() {
let ic = InvocationContext::builder()
.agent(named_agent("parent"))
.session(session("app", "u", "s"))
.build_invocation();
let tool_ctx = ic.into_context().with_delta(ContextDelta {
function_call_id: Some("fc".into()),
..Default::default()
});
let c = tool_ctx.with_delta(ContextDelta::default());
assert_eq!(c.agent_name(), "parent");
assert_eq!(c.user_id(), "u");
}