1use serde_json::Value;
8
9use super::AppState;
10use crate::backend::message_flow::MessageFlowEnd;
11use crate::backend::task_flow::{self, Seen, TaskFlowKind};
12
13fn end_of(name: &str, here: &str) -> Option<MessageFlowEnd> {
16 let name = name.trim();
17 if name.is_empty() || ["operator", "agentmux", "system"].iter().any(|n| name.eq_ignore_ascii_case(n)) {
18 return None;
19 }
20 Some(match name.split_once('@') {
21 Some((agent, host)) if !agent.is_empty() && !host.is_empty() => MessageFlowEnd { agent: agent.to_string(), host: Some(host.to_lowercase()) },
22 _ => MessageFlowEnd { agent: name.to_string(), host: Some(here.to_string()) },
23 })
24}
25
26pub(crate) fn note(state: &AppState, work_id: &str, kind: TaskFlowKind, by: &str, to: &str) {
28 let Ok(Some((board, number))) = state.work_store.work_task_board(work_id) else { return };
29 let here = crate::backend::reactive::registry::local_host_label();
30 task_flow::note(&Seen { number, board: board.slug, board_id: board.id, host: here.clone(), by: end_of(by, &here), to: end_of(to, &here), kind });
31}
32
33fn remote_filed(slug: &str, here: &str, peer_host: &str, target: &str, answer: &Value) -> Vec<Seen> {
37 let peer = peer_host.trim().to_lowercase();
38 let board = answer.get("board").and_then(Value::as_str).unwrap_or_default();
39 let to = Some(target.trim()).filter(|t| !t.is_empty()).map(|t| MessageFlowEnd { agent: t.to_string(), host: Some(peer.clone()) });
40 ["num", "build_num"]
41 .iter()
42 .filter_map(|key| answer.get(*key).and_then(Value::as_i64))
43 .map(|number| Seen {
44 number,
45 board: board.to_string(),
46 board_id: format!("remote:{peer}:{board}"),
47 host: peer.clone(),
48 by: Some(MessageFlowEnd { agent: slug.to_string(), host: Some(here.to_string()) }),
49 to: to.clone(),
50 kind: TaskFlowKind::Filed,
51 })
52 .collect()
53}
54
55pub(crate) fn note_remote_filed(slug: &str, peer_host: &str, target: &str, answer: &Value) {
58 let here = crate::backend::reactive::registry::local_host_label();
59 remote_filed(slug, &here, peer_host, target, answer).iter().for_each(task_flow::note);
60}
61
62#[cfg(test)]
63mod tests {
64 use super::*;
65
66 #[test]
67 fn an_agent_of_this_computer_or_another_has_an_end_and_the_operator_has_none() {
68 let end = |n| end_of(n, "charlie").map(|e| (e.agent, e.host));
69 assert_eq!(end("maricon-2"), Some(("maricon-2".into(), Some("charlie".into()))));
70 assert_eq!(end("agenty@Narko"), Some(("agenty".into(), Some("narko".into()))));
71 assert_eq!((end("operator"), end("Operator"), end("agentmux"), end(" "), end("")), (None, None, None, None, None));
72 }
73
74 #[test]
75 fn a_task_filed_on_another_board_is_reported_by_that_boards_host_and_number() {
76 let answer = serde_json::json!({ "id": "x", "num": 12, "board": "work", "build_num": 13 });
77 let lines = remote_filed("maricon-2", "charlie", "Narko ", "agenty", &answer);
78 let seen: Vec<_> = lines.iter().map(|l| (l.number, l.host.as_str(), l.board.as_str(), l.kind)).collect();
79 assert_eq!(seen, [(12, "narko", "work", TaskFlowKind::Filed), (13, "narko", "work", TaskFlowKind::Filed)]);
80 let by = lines[0].by.as_ref().unwrap();
81 assert_eq!((by.agent.as_str(), by.host.as_deref()), ("maricon-2", Some("charlie")));
82 let to = lines[0].to.as_ref().unwrap();
83 assert_eq!((to.agent.as_str(), to.host.as_deref()), ("agenty", Some("narko")));
84 }
85
86 #[test]
87 fn an_answer_with_no_number_reports_nothing_and_a_task_for_anyone_has_no_receiver() {
88 assert!(remote_filed("a", "x", "narko", "b", &serde_json::json!({ "error": "no" })).is_empty());
89 assert!(remote_filed("a", "x", "narko", " ", &serde_json::json!({ "num": 1, "board": "w" }))[0].to.is_none());
90 }
91}