use uuid::Uuid;
pub const TEAMCTL_SESSION_NAMESPACE: Uuid = Uuid::from_bytes([
0x6d, 0xd6, 0xc8, 0xa3, 0x44, 0xb6, 0x4a, 0x18, 0x9b, 0x05, 0x91, 0xc1, 0xe2, 0x57, 0xfb, 0x3d,
]);
pub fn session_name(project: &str, agent: &str) -> String {
format!("teamctl:{project}:{agent}")
}
pub fn derive_session_id(project: &str, agent: &str) -> Uuid {
Uuid::new_v5(
&TEAMCTL_SESSION_NAMESPACE,
session_name(project, agent).as_bytes(),
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn same_inputs_yield_same_uuid() {
let a = derive_session_id("hello", "mgr");
let b = derive_session_id("hello", "mgr");
assert_eq!(a, b);
}
#[test]
fn different_agents_yield_different_uuids() {
let mgr = derive_session_id("hello", "mgr");
let dev = derive_session_id("hello", "dev");
assert_ne!(mgr, dev);
}
#[test]
fn different_projects_yield_different_uuids() {
let a = derive_session_id("alpha", "mgr");
let b = derive_session_id("beta", "mgr");
assert_ne!(a, b);
}
#[test]
fn namespace_constant_is_stable() {
assert_eq!(
TEAMCTL_SESSION_NAMESPACE.to_string(),
"6dd6c8a3-44b6-4a18-9b05-91c1e257fb3d"
);
}
#[test]
fn derived_uuid_is_v5_and_uses_namespace() {
let derived = derive_session_id("hello", "mgr");
let expected = Uuid::new_v5(&TEAMCTL_SESSION_NAMESPACE, b"teamctl:hello:mgr");
assert_eq!(derived, expected);
assert_eq!(derived.get_version_num(), 5);
}
#[test]
fn session_name_format_is_canonical() {
assert_eq!(session_name("hello", "mgr"), "teamctl:hello:mgr");
}
}