use gadk_platform::Platform;
use tokio_util::sync::CancellationToken;
use crate::context::{ContextDelta, InvocationContext, ReadonlyContext};
#[test]
fn test_invocation_context_live_session_resumption_handle() {
let inv = InvocationContext::builder().build_invocation();
assert_eq!(
inv.live_session_resumption_handle(),
"",
"expected empty handle"
);
inv.set_live_session_resumption_handle("test-handle");
assert_eq!(inv.live_session_resumption_handle(), "test-handle");
}
#[test]
fn test_readonly_context() {
let inv = InvocationContext::builder().branch("b").build_invocation();
let r: ReadonlyContext = inv.as_readonly().clone();
assert_eq!(
(
r.invocation_id(),
r.agent_name(),
r.branch(),
r.user_id(),
r.app_name(),
r.session_id()
),
(
inv.invocation_id(),
inv.agent_name(),
inv.branch(),
inv.user_id(),
inv.app_name(),
inv.session_id()
)
);
}
#[test]
fn test_callback_context() {
let inv = InvocationContext::builder().build_invocation();
let cc = inv.clone().into_context().with_delta(ContextDelta {
actions: Some(Default::default()),
..Default::default()
});
let r: &ReadonlyContext = &cc;
assert_eq!(r.invocation_id(), inv.invocation_id());
}
#[test]
fn test_with_context() {
let inv = InvocationContext::builder()
.branch("test-branch")
.build_invocation();
let token = CancellationToken::new();
let got = inv.with_delta(ContextDelta {
cancellation_token: Some(token.clone()),
..Default::default()
});
token.cancel();
assert!(got.cancellation_token().is_cancelled());
assert!(!inv.cancellation_token().is_cancelled());
assert_eq!(
(
got.branch(),
got.invocation_id(),
got.agent_name(),
got.session_id(),
got.isolation_scope()
),
(
inv.branch(),
inv.invocation_id(),
inv.agent_name(),
inv.session_id(),
inv.isolation_scope()
)
);
}
#[test]
fn test_new_invocation_context_generates_id_with_provider() {
let inv = InvocationContext::builder()
.platform(Platform::default().with_uuid(|| "fixed".to_string()))
.build_invocation();
assert_eq!(inv.invocation_id(), "e-fixed");
}
#[test]
fn test_new_invocation_context_respects_explicit_id() {
let inv = InvocationContext::builder()
.platform(Platform::default().with_uuid(|| "fixed".to_string()))
.invocation_id("explicit")
.build_invocation();
assert_eq!(inv.invocation_id(), "explicit");
}
#[test]
fn test_new_invocation_context_default_id() {
let inv = InvocationContext::builder().build_invocation();
let id = inv.invocation_id();
let rest = id
.strip_prefix("e-")
.unwrap_or_else(|| panic!("InvocationID() = {id:?}, want \"e-\" prefix"));
let canonical = rest.len() == 36
&& rest.char_indices().all(|(i, c)| {
if matches!(i, 8 | 13 | 18 | 23) {
c == '-'
} else {
c.is_ascii_hexdigit()
}
});
assert!(
canonical,
"InvocationID() = {id:?}, suffix not a valid UUID"
);
}