unity-cli 0.8.0

Rust CLI for Unity Editor automation over the Unity TCP protocol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
use std::fs;
use std::path::PathBuf;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use anyhow::{anyhow, Context, Result};
use serde::{Deserialize, Serialize};
use tokio::net::TcpStream;
use tokio::time::timeout;

#[derive(Debug, Clone, Serialize, Deserialize)]
struct InstanceRecord {
    id: String,
    host: String,
    port: u16,
}

#[derive(Debug, Default, Serialize, Deserialize)]
struct Registry {
    #[serde(default)]
    active_id: Option<String>,
    #[serde(default)]
    entries: Vec<InstanceRecord>,
}

#[derive(Debug, Serialize)]
pub struct InstanceStatus {
    pub id: String,
    pub host: String,
    pub port: u16,
    pub status: String,
    pub last_checked_at: String,
    pub active: bool,
}

#[derive(Debug, Serialize)]
pub struct SetActiveResult {
    pub active_id: String,
    pub previous_id: Option<String>,
}

pub fn active_endpoint() -> Result<Option<(String, u16)>> {
    let registry = load_registry()?;
    let Some(active_id) = registry.active_id else {
        return Ok(None);
    };

    if let Some(entry) = registry.entries.iter().find(|entry| entry.id == active_id) {
        return Ok(Some((entry.host.clone(), entry.port)));
    }

    let (host, port) = parse_id(&active_id)?;
    Ok(Some((host, port)))
}

pub async fn list_instances(
    host: &str,
    ports: &[u16],
    timeout_ms: u64,
) -> Result<Vec<InstanceStatus>> {
    let mut registry = load_registry()?;

    for port in ports {
        let id = format!("{host}:{port}");
        if registry.entries.iter().all(|entry| entry.id != id) {
            registry.entries.push(InstanceRecord {
                id,
                host: host.to_string(),
                port: *port,
            });
        }
    }

    if registry.entries.is_empty() {
        registry.entries.push(InstanceRecord {
            id: format!("{}:{}", host, 6400),
            host: host.to_string(),
            port: 6400,
        });
    }

    registry.entries.sort_by(|a, b| a.id.cmp(&b.id));

    let mut statuses = Vec::with_capacity(registry.entries.len());
    let timeout = Duration::from_millis(timeout_ms);
    let checked_at = unix_timestamp();

    for entry in &registry.entries {
        let up = can_connect(&entry.host, entry.port, timeout).await;
        statuses.push(InstanceStatus {
            id: entry.id.clone(),
            host: entry.host.clone(),
            port: entry.port,
            status: if up {
                "up".to_string()
            } else {
                "down".to_string()
            },
            last_checked_at: checked_at.clone(),
            active: registry.active_id.as_deref() == Some(&entry.id),
        });
    }

    save_registry(&registry)?;
    Ok(statuses)
}

pub async fn set_active_instance(id: &str, timeout_ms: u64) -> Result<SetActiveResult> {
    let mut registry = load_registry()?;

    if registry.entries.iter().all(|entry| entry.id != id) {
        let (host, port) = parse_id(id)?;
        registry.entries.push(InstanceRecord {
            id: id.to_string(),
            host,
            port,
        });
    }

    let target = registry
        .entries
        .iter()
        .find(|entry| entry.id == id)
        .ok_or_else(|| anyhow!("Instance not found: {id}"))?;

    let ok = can_connect(&target.host, target.port, Duration::from_millis(timeout_ms)).await;
    if !ok {
        return Err(anyhow!("Instance unreachable: {id}"));
    }

    let previous_id = registry.active_id.clone();
    registry.active_id = Some(id.to_string());
    save_registry(&registry)?;

    Ok(SetActiveResult {
        active_id: id.to_string(),
        previous_id,
    })
}

fn parse_id(id: &str) -> Result<(String, u16)> {
    let (host, port_str) = id
        .split_once(':')
        .ok_or_else(|| anyhow!("Invalid instance id: {id}. Expected host:port"))?;
    let port = port_str
        .parse::<u16>()
        .with_context(|| format!("Invalid port in instance id: {id}"))?;
    if host.trim().is_empty() {
        return Err(anyhow!("Invalid host in instance id: {id}"));
    }
    Ok((host.to_string(), port))
}

async fn can_connect(host: &str, port: u16, timeout_duration: Duration) -> bool {
    matches!(
        timeout(timeout_duration, TcpStream::connect((host, port))).await,
        Ok(Ok(_))
    )
}

fn registry_path() -> Result<PathBuf> {
    if let Ok(raw_path) = std::env::var("UNITY_CLI_REGISTRY_PATH") {
        let trimmed = raw_path.trim();
        if !trimmed.is_empty() {
            let path = PathBuf::from(trimmed);
            if let Some(parent) = path.parent() {
                fs::create_dir_all(parent).with_context(|| {
                    format!("Failed to create registry parent dir: {}", parent.display())
                })?;
            }
            return Ok(path);
        }
    }

    let base_dir = dirs::config_dir()
        .or_else(|| dirs::home_dir().map(|home| home.join(".config")))
        .ok_or_else(|| anyhow!("Unable to resolve config directory"))?;

    let dir = base_dir.join("unity-cli");
    fs::create_dir_all(&dir)
        .with_context(|| format!("Failed to create config dir: {}", dir.display()))?;
    Ok(dir.join("instances.json"))
}

fn load_registry() -> Result<Registry> {
    let path = registry_path()?;
    if !path.exists() {
        return Ok(Registry::default());
    }

    let raw = fs::read_to_string(&path)
        .with_context(|| format!("Failed to read registry file: {}", path.display()))?;

    let mut registry: Registry = serde_json::from_str(&raw)
        .with_context(|| format!("Failed to parse registry file: {}", path.display()))?;

    for entry in &mut registry.entries {
        if entry.id.trim().is_empty() {
            entry.id = format!("{}:{}", entry.host, entry.port);
        }
    }

    Ok(registry)
}

fn save_registry(registry: &Registry) -> Result<()> {
    let path = registry_path()?;
    let content = serde_json::to_string_pretty(registry)?;
    fs::write(&path, content)
        .with_context(|| format!("Failed to write registry file: {}", path.display()))
}

fn unix_timestamp() -> String {
    match SystemTime::now().duration_since(UNIX_EPOCH) {
        Ok(duration) => duration.as_secs().to_string(),
        Err(_) => "0".to_string(),
    }
}

#[cfg(test)]
mod tests {
    use super::{
        list_instances, load_registry, parse_id, registry_path, set_active_instance,
        InstanceRecord, Registry,
    };
    use std::path::PathBuf;
    use std::sync::Mutex;
    use std::time::{Duration, SystemTime, UNIX_EPOCH};
    use tokio::net::TcpListener;

    fn env_lock() -> &'static Mutex<()> {
        crate::test_env::env_lock()
    }

    fn temp_registry_path(label: &str) -> PathBuf {
        let nanos = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or(Duration::from_secs(0))
            .as_nanos();
        let mut path = std::env::temp_dir();
        path.push(format!("unity-cli-{label}-{nanos}.json"));
        path
    }

    #[test]
    fn parse_id_validates_shape() {
        let (host, port) = parse_id("localhost:6400").expect("host:port should parse");
        assert_eq!(host, "localhost");
        assert_eq!(port, 6400);

        let err = parse_id("invalid").expect_err("missing separator should fail");
        assert!(format!("{err:#}").contains("Expected host:port"));

        let err = parse_id(":6400").expect_err("empty host should fail");
        assert!(format!("{err:#}").contains("Invalid host"));

        let err = parse_id("localhost:not-a-port").expect_err("invalid port should fail");
        assert!(format!("{err:#}").contains("Invalid port"));
    }

    #[test]
    fn registry_path_uses_env_and_creates_parent_directory() {
        let _guard = env_lock()
            .lock()
            .unwrap_or_else(|poison| poison.into_inner());
        let registry_file =
            temp_registry_path("instances-registry-path").with_file_name("nested/instances.json");
        std::env::set_var("UNITY_CLI_REGISTRY_PATH", &registry_file);

        let resolved = registry_path().expect("registry path should resolve");
        assert_eq!(resolved, registry_file);
        assert!(resolved
            .parent()
            .expect("registry file should have parent")
            .exists());

        std::env::remove_var("UNITY_CLI_REGISTRY_PATH");
        if let Some(parent) = resolved.parent() {
            let _ = std::fs::remove_dir_all(parent);
        }
    }

    #[test]
    fn load_registry_reconstructs_missing_ids() {
        let _guard = env_lock()
            .lock()
            .unwrap_or_else(|poison| poison.into_inner());
        let registry_path = temp_registry_path("instances-load");
        std::env::set_var("UNITY_CLI_REGISTRY_PATH", &registry_path);
        std::fs::write(
            &registry_path,
            r#"{"active_id":null,"entries":[{"id":"","host":"127.0.0.1","port":6400}]}"#,
        )
        .expect("fixture write should succeed");

        let loaded = load_registry().expect("registry should load");
        assert_eq!(loaded.entries.len(), 1);
        assert_eq!(loaded.entries[0].id, "127.0.0.1:6400");

        std::env::remove_var("UNITY_CLI_REGISTRY_PATH");
        let _ = std::fs::remove_file(&registry_path);
    }

    #[tokio::test]
    #[allow(clippy::await_holding_lock)]
    async fn list_instances_reports_up_for_reachable_port() {
        let _guard = env_lock()
            .lock()
            .unwrap_or_else(|poison| poison.into_inner());
        let registry_path = temp_registry_path("instances-up");
        std::env::set_var("UNITY_CLI_REGISTRY_PATH", &registry_path);

        let listener = TcpListener::bind(("127.0.0.1", 0))
            .await
            .expect("listener should bind");
        let port = listener
            .local_addr()
            .expect("listener should have local addr")
            .port();
        let accept_task = tokio::spawn(async move {
            let _ = listener.accept().await;
        });

        let statuses = list_instances("127.0.0.1", &[port], 300)
            .await
            .expect("list should succeed");
        assert_eq!(statuses.len(), 1);
        assert_eq!(statuses[0].status, "up");

        accept_task.abort();
        std::env::remove_var("UNITY_CLI_REGISTRY_PATH");
        let _ = std::fs::remove_file(&registry_path);
    }

    #[tokio::test]
    #[allow(clippy::await_holding_lock)]
    async fn list_instances_adds_default_port_when_no_entries_exist() {
        let _guard = env_lock()
            .lock()
            .unwrap_or_else(|poison| poison.into_inner());
        let registry_path = temp_registry_path("instances-default");
        std::env::set_var("UNITY_CLI_REGISTRY_PATH", &registry_path);

        let statuses = list_instances("127.0.0.1", &[], 50)
            .await
            .expect("list should succeed");
        assert_eq!(statuses.len(), 1);
        assert_eq!(statuses[0].id, "127.0.0.1:6400");
        assert_eq!(statuses[0].port, 6400);

        std::env::remove_var("UNITY_CLI_REGISTRY_PATH");
        let _ = std::fs::remove_file(&registry_path);
    }

    #[tokio::test]
    #[allow(clippy::await_holding_lock)]
    async fn set_active_fails_for_unreachable_instance() {
        let _guard = env_lock()
            .lock()
            .unwrap_or_else(|poison| poison.into_inner());
        let registry_path = temp_registry_path("instances-down");
        std::env::set_var("UNITY_CLI_REGISTRY_PATH", &registry_path);

        let listener = TcpListener::bind(("127.0.0.1", 0))
            .await
            .expect("listener should bind");
        let port = listener
            .local_addr()
            .expect("listener should have local addr")
            .port();
        drop(listener);

        let id = format!("127.0.0.1:{port}");
        let err = set_active_instance(&id, 100)
            .await
            .expect_err("set-active should fail for down port");
        assert!(format!("{err:#}").contains("unreachable"));

        std::env::remove_var("UNITY_CLI_REGISTRY_PATH");
        let _ = std::fs::remove_file(&registry_path);
    }

    #[tokio::test]
    #[allow(clippy::await_holding_lock)]
    async fn set_active_succeeds_for_reachable_instance_and_tracks_previous() {
        let _guard = env_lock()
            .lock()
            .unwrap_or_else(|poison| poison.into_inner());
        let registry_path = temp_registry_path("instances-active");
        std::env::set_var("UNITY_CLI_REGISTRY_PATH", &registry_path);

        let listener = TcpListener::bind(("127.0.0.1", 0))
            .await
            .expect("listener should bind");
        let port = listener
            .local_addr()
            .expect("listener should expose local addr")
            .port();
        let accept_task = tokio::spawn(async move {
            for _ in 0..2 {
                let _ = listener.accept().await;
            }
        });

        let id = format!("127.0.0.1:{port}");
        let first = set_active_instance(&id, 300)
            .await
            .expect("first set-active should succeed");
        assert_eq!(first.active_id, id);
        assert!(first.previous_id.is_none());

        let second = set_active_instance(&id, 300)
            .await
            .expect("second set-active should succeed");
        assert_eq!(second.previous_id.as_deref(), Some(id.as_str()));

        accept_task.abort();
        std::env::remove_var("UNITY_CLI_REGISTRY_PATH");
        let _ = std::fs::remove_file(&registry_path);
    }

    #[test]
    fn registry_round_trip_serialization_shape_is_supported() {
        let registry = Registry {
            active_id: Some("127.0.0.1:6400".to_string()),
            entries: vec![InstanceRecord {
                id: "127.0.0.1:6400".to_string(),
                host: "127.0.0.1".to_string(),
                port: 6400,
            }],
        };
        let raw = serde_json::to_string(&registry).expect("registry should serialize");
        let parsed: Registry = serde_json::from_str(&raw).expect("registry should deserialize");
        assert_eq!(parsed.active_id.as_deref(), Some("127.0.0.1:6400"));
        assert_eq!(parsed.entries[0].host, "127.0.0.1");
    }
}