Skip to main content

mecha10_cli_core/utils/
node_identity.rs

1//! Node identity helpers shared across the CLI and its sibling binaries.
2//!
3//! A node has two name forms: scoped (`@mecha10/teleop`) and short (`teleop`).
4
5/// Strip a `@scope/name` node identifier down to its bare short name. Plain (already-short)
6/// identifiers pass through unchanged.
7pub fn strip_node_scope(identifier: &str) -> &str {
8    identifier.rsplit('/').next().unwrap_or(identifier)
9}
10
11#[cfg(test)]
12mod tests {
13    use super::*;
14
15    #[test]
16    fn strips_scope_prefix() {
17        assert_eq!(strip_node_scope("@mecha10/simulator"), "simulator");
18        assert_eq!(strip_node_scope("@mecha10/video-streamer"), "video-streamer");
19        assert_eq!(strip_node_scope("@mecha10/teleop"), "teleop");
20    }
21
22    #[test]
23    fn leaves_plain_name_untouched() {
24        assert_eq!(strip_node_scope("simulator"), "simulator");
25        assert_eq!(strip_node_scope("teleop"), "teleop");
26    }
27
28    #[test]
29    fn is_consistent_across_scoped_and_short_pairs() {
30        let pairs = [
31            ("@mecha10/object-detector", "object-detector"),
32            ("@mecha10/image-classifier", "image-classifier"),
33            ("@mecha10/llm-command", "llm-command"),
34            ("@mecha10/behavior-executor", "behavior-executor"),
35            ("@mecha10/diagnostics", "diagnostics"),
36            ("@mecha10/imu", "imu"),
37            ("@mecha10/listener", "listener"),
38            ("@mecha10/motor", "motor"),
39            ("@mecha10/speaker", "speaker"),
40            ("@mecha10/teleop", "teleop"),
41            ("@mecha10/websocket-bridge", "websocket-bridge"),
42            ("teleop", "teleop"),
43            ("simulator", "simulator"),
44        ];
45
46        for (scoped, short) in pairs {
47            assert_eq!(
48                strip_node_scope(scoped),
49                short,
50                "scoped form should strip to short form"
51            );
52            assert_eq!(
53                strip_node_scope(short),
54                short,
55                "short form should pass through unchanged"
56            );
57        }
58    }
59}