agentmux_srv_core/backend/rpc/
actor.rs1use crate::backend::rpc_types::RpcContext;
14
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub enum Actor {
17 Agent(String),
19 Operator,
21 Person(String),
24 Unproven,
26 Srv,
28}
29
30impl Actor {
31 pub fn is_operator(&self) -> bool {
33 matches!(self, Actor::Operator | Actor::Person(_))
34 }
35
36 pub fn label(&self) -> String {
39 match self {
40 Actor::Agent(id) => format!("agent:{id}"),
41 Actor::Operator => "operator".to_string(),
42 Actor::Person(id) => format!("person:{id} (AgentMux Web)"),
43 Actor::Unproven => "unproven".to_string(),
44 Actor::Srv => "agentmux".to_string(),
45 }
46 }
47}
48
49pub fn actor_of(ctx: &RpcContext) -> Actor {
52 if !ctx.agent_id.is_empty() {
53 Actor::Agent(ctx.agent_id.clone())
54 } else if ctx.operator_proven_now() {
55 match &ctx.operator_person {
56 Some(person) => Actor::Person(person.clone()),
57 None => Actor::Operator,
58 }
59 } else {
60 Actor::Unproven
61 }
62}
63
64#[cfg(test)]
65mod tests {
66 use super::*;
67
68 #[test]
69 fn an_agents_connection_is_that_agent() {
70 let ctx = RpcContext { agent_id: "opaz".into(), ..Default::default() };
71 assert_eq!(actor_of(&ctx), Actor::Agent("opaz".into()));
72 assert_eq!(actor_of(&ctx).label(), "agent:opaz");
73 }
74
75 #[test]
76 fn the_operator_needs_the_hosts_proof() {
77 assert_eq!(actor_of(&RpcContext::operator_for_tests()), Actor::Operator);
78 assert_eq!(actor_of(&RpcContext::default()), Actor::Unproven);
80 assert_eq!(actor_of(&RpcContext { blockid: "b".into(), tabid: "t".into(), conn: "c".into(), ..Default::default() }), Actor::Unproven);
81 }
82
83 #[test]
84 fn an_agent_is_never_the_operator_even_with_the_proof_flag_set() {
85 let ctx = RpcContext { agent_id: "opaz".into(), operator_proven: true, ..Default::default() };
86 assert_eq!(actor_of(&ctx), Actor::Agent("opaz".into()));
87 }
88
89 #[test]
90 fn no_agent_id_can_read_as_the_operator_or_unproven() {
91 assert_eq!(Actor::Agent("operator".into()).label(), "agent:operator");
92 assert_eq!(Actor::Agent("unproven".into()).label(), "agent:unproven");
93 assert_ne!(Actor::Agent("operator".into()).label(), Actor::Operator.label());
94 }
95}