unity-cli 0.15.1

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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
use std::fs;
use std::path::PathBuf;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use anyhow::{anyhow, Context, Result};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;
use tokio::time::timeout;

const MAX_HEALTH_RESPONSE_BYTES: i32 = 1024 * 1024;

#[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 {
    bridge_ping(host, port, timeout_duration)
        .await
        .unwrap_or(false)
}

async fn bridge_ping(host: &str, port: u16, timeout_duration: Duration) -> Result<bool> {
    let mut stream = timeout(timeout_duration, TcpStream::connect((host, port))).await??;
    let request = json!({
        "id": "health",
        "type": "ping",
        "params": {},
    });
    let payload = serde_json::to_vec(&request)?;
    let payload_len = i32::try_from(payload.len()).context("Health check payload too large")?;

    let mut frame = Vec::with_capacity(4 + payload.len());
    frame.extend_from_slice(&payload_len.to_be_bytes());
    frame.extend_from_slice(&payload);
    timeout(timeout_duration, stream.write_all(&frame)).await??;

    let mut header = [0_u8; 4];
    timeout(timeout_duration, stream.read_exact(&mut header)).await??;
    let response_len = i32::from_be_bytes(header);
    if !(1..=MAX_HEALTH_RESPONSE_BYTES).contains(&response_len) {
        return Ok(false);
    }

    let mut response_payload = vec![0_u8; response_len as usize];
    timeout(timeout_duration, stream.read_exact(&mut response_payload)).await??;
    let response: Value = serde_json::from_slice(&response_payload)?;
    Ok(is_successful_bridge_response(&response))
}

fn is_successful_bridge_response(response: &Value) -> bool {
    if response.get("error").is_some()
        || matches!(response.get("success"), Some(Value::Bool(false)))
    {
        return false;
    }

    let status = response
        .get("status")
        .and_then(Value::as_str)
        .map(str::to_ascii_lowercase);

    matches!(status.as_deref(), Some("success" | "ok"))
        || matches!(response.get("success"), Some(Value::Bool(true)))
        || response.get("result").is_some()
        || response.get("data").is_some()
}

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 serde_json::json;
    use std::path::PathBuf;
    use std::sync::Mutex;
    use std::time::{Duration, SystemTime, UNIX_EPOCH};
    use tokio::io::{AsyncReadExt, AsyncWriteExt};
    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
    }

    async fn spawn_mock_bridge(accept_count: usize) -> (u16, tokio::task::JoinHandle<()>) {
        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 task = tokio::spawn(async move {
            for _ in 0..accept_count {
                let (mut socket, _) = listener.accept().await.expect("accept should succeed");
                let mut len_buf = [0_u8; 4];
                socket
                    .read_exact(&mut len_buf)
                    .await
                    .expect("request header should be readable");
                let payload_len = i32::from_be_bytes(len_buf);
                let mut payload = vec![0_u8; payload_len as usize];
                socket
                    .read_exact(&mut payload)
                    .await
                    .expect("request payload should be readable");

                let response = json!({
                    "status": "success",
                    "result": {"pong": true}
                });
                let response_bytes =
                    serde_json::to_vec(&response).expect("response should serialize");
                socket
                    .write_all(&(response_bytes.len() as i32).to_be_bytes())
                    .await
                    .expect("response header should write");
                socket
                    .write_all(&response_bytes)
                    .await
                    .expect("response payload should write");
            }
        });

        (port, task)
    }

    #[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 (port, accept_task) = spawn_mock_bridge(1).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");

        tokio::time::timeout(Duration::from_secs(1), accept_task)
            .await
            .expect("bridge task should finish")
            .expect("bridge task should succeed");
        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_down_for_tcp_listener_that_is_not_unity_bridge() {
        let _guard = env_lock()
            .lock()
            .unwrap_or_else(|poison| poison.into_inner());
        let registry_path = temp_registry_path("instances-zombie");
        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, "down");

        tokio::time::timeout(Duration::from_secs(1), accept_task)
            .await
            .expect("listener should observe the health check connection")
            .expect("listener task should succeed");
        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 (port, accept_task) = spawn_mock_bridge(2).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()));

        tokio::time::timeout(Duration::from_secs(1), accept_task)
            .await
            .expect("bridge task should finish")
            .expect("bridge task should succeed");
        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");
    }
}