Skip to main content

kanade_shared/
subject.rs

1pub const COMMANDS_ALL: &str = "commands.all";
2
3pub fn commands_group(name: &str) -> String {
4    format!("commands.group.{name}")
5}
6
7pub fn commands_pc(pc_id: &str) -> String {
8    format!("commands.pc.{pc_id}")
9}
10
11pub fn commands_deploy(job_id: &str) -> String {
12    format!("commands.deploy.{job_id}")
13}
14
15pub fn results(request_id: &str) -> String {
16    format!("results.{request_id}")
17}
18
19pub fn heartbeat(pc_id: &str) -> String {
20    format!("heartbeat.{pc_id}")
21}
22
23pub fn kill(job_id: &str) -> String {
24    format!("kill.{job_id}")
25}
26
27pub fn inventory(pc_id: &str, category: &str) -> String {
28    format!("inventory.{pc_id}.{category}")
29}
30
31pub const INVENTORY_HW: &str = "hw";
32pub const INVENTORY_SW: &str = "sw";
33pub const INVENTORY_NET: &str = "net";
34
35/// `logs.fetch.<pc_id>` — request/reply: operator (or backend) sends
36/// a `LogsRequest`; the addressed agent replies with the tail of its
37/// local log file. On-demand only, no stream.
38pub fn logs_fetch(pc_id: &str) -> String {
39    format!("logs.fetch.{pc_id}")
40}
41
42/// `request.inventory.<pc_id>` — request/reply: operator asks the
43/// addressed agent to collect WMI inventory NOW (out of band of its
44/// configured cadence) and publish it to `inventory.<pc_id>.hw`.
45/// The reply body is `"ok"` on success or `"error: <reason>"` on
46/// failure — useful for diagnosing WMI repository / permission
47/// issues without waiting for the next scheduled cycle.
48///
49/// Namespaced under `request.*` deliberately: the INVENTORY stream's
50/// subject filter is `inventory.>`, so a request subject under
51/// `inventory.request.*` was getting captured by the stream too —
52/// the JetStream publish-ack hit the operator's reply inbox before
53/// the agent's actual `"ok"` and confused `kanade inventory`. The
54/// `request.*` top-level is intentionally not captured by any
55/// stream and is reserved for future operator → agent on-demand
56/// request/reply patterns.
57pub fn inventory_request(pc_id: &str) -> String {
58    format!("request.inventory.{pc_id}")
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn commands_all_constant() {
67        assert_eq!(COMMANDS_ALL, "commands.all");
68    }
69
70    #[test]
71    fn commands_group_formats_name() {
72        assert_eq!(commands_group("canary"), "commands.group.canary");
73        assert_eq!(commands_group("wave1"), "commands.group.wave1");
74    }
75
76    #[test]
77    fn commands_pc_formats_id() {
78        assert_eq!(commands_pc("minipc"), "commands.pc.minipc");
79        assert_eq!(commands_pc("PC1234"), "commands.pc.PC1234");
80    }
81
82    #[test]
83    fn commands_deploy_formats_job_id() {
84        let job = "3c3c56b3-c83e-4c27-9fa9-4a75e1f5da6f";
85        assert_eq!(
86            commands_deploy(job),
87            "commands.deploy.3c3c56b3-c83e-4c27-9fa9-4a75e1f5da6f"
88        );
89    }
90
91    #[test]
92    fn results_formats_request_id() {
93        assert_eq!(results("req-1"), "results.req-1");
94    }
95
96    #[test]
97    fn heartbeat_formats_pc_id() {
98        assert_eq!(heartbeat("minipc"), "heartbeat.minipc");
99    }
100
101    #[test]
102    fn kill_formats_job_id() {
103        assert_eq!(kill("testjob1"), "kill.testjob1");
104    }
105
106    #[test]
107    fn logs_fetch_formats_pc_id() {
108        assert_eq!(logs_fetch("minipc"), "logs.fetch.minipc");
109    }
110
111    #[test]
112    fn inventory_request_lives_outside_inventory_namespace() {
113        // The INVENTORY JetStream stream filter is `inventory.>` —
114        // this subject MUST stay outside that prefix or the server
115        // ack-replies to the request reply inbox and clobbers the
116        // agent's actual response.
117        let subj = inventory_request("minipc");
118        assert_eq!(subj, "request.inventory.minipc");
119        assert!(!subj.starts_with("inventory."));
120    }
121
122    #[test]
123    fn inventory_formats_pc_id_and_category() {
124        assert_eq!(inventory("minipc", "hw"), "inventory.minipc.hw");
125        assert_eq!(inventory("minipc", INVENTORY_HW), "inventory.minipc.hw");
126        assert_eq!(inventory("minipc", INVENTORY_SW), "inventory.minipc.sw");
127        assert_eq!(inventory("minipc", INVENTORY_NET), "inventory.minipc.net");
128    }
129}