vessel-pty 0.17.1

PTY-based runtime for orchestrating interactive terminal processes over Unix sockets
Documentation
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
//! End-to-end CLI tests using assert_cmd.
//!
//! These tests run the actual vessel binary and verify stdout/stderr/exit codes.

use assert_cmd::Command;
use predicates::prelude::*;
use std::path::PathBuf;
use std::sync::atomic::{AtomicU32, Ordering};
use std::time::Duration;

static TEST_COUNTER: AtomicU32 = AtomicU32::new(0);

/// Generate a unique socket path for each test.
fn unique_socket_path() -> PathBuf {
    let id = TEST_COUNTER.fetch_add(1, Ordering::SeqCst);
    let pid = std::process::id();
    PathBuf::from(format!("/tmp/vessel-cli-test-{pid}-{id}.sock"))
}

/// Helper to clean up socket after test.
struct TestEnv {
    socket_path: PathBuf,
    server_process: Option<std::process::Child>,
}

impl TestEnv {
    fn new() -> Self {
        let socket_path = unique_socket_path();
        Self {
            socket_path,
            server_process: None,
        }
    }

    fn socket_arg(&self) -> String {
        format!("--socket={}", self.socket_path.display())
    }

    fn start_server(&mut self) {
        let child = std::process::Command::new(env!("CARGO_BIN_EXE_vessel"))
            .arg(&self.socket_arg())
            .arg("server")
            .spawn()
            .expect("failed to start server");
        self.server_process = Some(child);
        // Give server time to start
        std::thread::sleep(Duration::from_millis(200));
    }

    fn vessel(&self) -> Command {
        let mut cmd = Command::cargo_bin("vessel").unwrap();
        cmd.arg(&self.socket_arg());
        cmd
    }
}

impl Drop for TestEnv {
    fn drop(&mut self) {
        // Try to shut down the server gracefully
        if self.server_process.is_some() {
            let _ = std::process::Command::new(env!("CARGO_BIN_EXE_vessel"))
                .arg(&self.socket_arg())
                .arg("shutdown")
                .output();
        }

        // Kill server if still running
        if let Some(mut child) = self.server_process.take() {
            let _ = child.kill();
            let _ = child.wait();
        }

        // Clean up socket
        std::fs::remove_file(&self.socket_path).ok();
    }
}

#[test]
fn test_help() {
    Command::cargo_bin("vessel")
        .unwrap()
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("PTY-based runtime"))
        .stdout(predicate::str::contains("spawn"))
        .stdout(predicate::str::contains("list"))
        .stdout(predicate::str::contains("kill"));
}

#[test]
fn test_version() {
    Command::cargo_bin("vessel")
        .unwrap()
        .arg("--version")
        .assert()
        .success()
        .stdout(predicate::str::contains("vessel"));
}

#[test]
fn test_spawn_help() {
    Command::cargo_bin("vessel")
        .unwrap()
        .args(["spawn", "--help"])
        .assert()
        .success()
        .stdout(predicate::str::contains("Spawn a new agent"))
        .stdout(predicate::str::contains("--rows"))
        .stdout(predicate::str::contains("--cols"));
}

#[test]
fn test_spawn_list_kill_workflow() {
    let mut env = TestEnv::new();
    env.start_server();

    // Spawn an agent
    let output = env
        .vessel()
        .args(["spawn", "--", "sleep", "30"])
        .output()
        .expect("failed to run spawn");

    assert!(output.status.success(), "spawn should succeed");
    let agent_id = String::from_utf8_lossy(&output.stdout).trim().to_string();
    assert!(!agent_id.is_empty(), "should return agent ID");

    // List agents
    env.vessel()
        .arg("list")
        .assert()
        .success()
        .stdout(predicate::str::contains(&agent_id))
        .stdout(predicate::str::contains("sleep 30"))
        .stdout(predicate::str::contains("running"));

    // Kill the agent
    env.vessel()
        .args(["kill", &agent_id])
        .assert()
        .success()
        .stdout(predicate::str::contains("Signal sent"));

    // List should show exited (need --all to see exited agents)
    std::thread::sleep(Duration::from_millis(200));
    env.vessel()
        .args(["list", "--all"])
        .assert()
        .success()
        .stdout(predicate::str::contains("exited"));
}

#[test]
fn test_send_and_snapshot() {
    let mut env = TestEnv::new();
    env.start_server();

    // Spawn bash
    let output = env
        .vessel()
        .args(["spawn", "--", "bash"])
        .output()
        .expect("failed to run spawn");

    assert!(output.status.success());
    let agent_id = String::from_utf8_lossy(&output.stdout).trim().to_string();

    std::thread::sleep(Duration::from_millis(200));

    // Send a command
    env.vessel()
        .args(["send", &agent_id, "echo UNIQUE_TEST_STRING_12345", "--newline"])
        .assert()
        .success();

    std::thread::sleep(Duration::from_millis(300));

    // Snapshot should contain our output
    env.vessel()
        .args(["snapshot", &agent_id])
        .assert()
        .success()
        .stdout(predicate::str::contains("UNIQUE_TEST_STRING_12345"));

    // Clean up
    env.vessel().args(["kill", &agent_id]).assert().success();
}

#[test]
fn test_tail() {
    let mut env = TestEnv::new();
    env.start_server();

    // Spawn something that produces output
    let output = env
        .vessel()
        .args([
            "spawn",
            "--",
            "sh",
            "-c",
            "echo FIRST_LINE; echo SECOND_LINE; sleep 30",
        ])
        .output()
        .expect("failed to run spawn");

    assert!(output.status.success());
    let agent_id = String::from_utf8_lossy(&output.stdout).trim().to_string();

    std::thread::sleep(Duration::from_millis(300));

    // Tail should show the output
    env.vessel()
        .args(["tail", &agent_id])
        .assert()
        .success()
        .stdout(predicate::str::contains("FIRST_LINE"))
        .stdout(predicate::str::contains("SECOND_LINE"));

    // Clean up
    env.vessel().args(["kill", &agent_id]).assert().success();
}

#[test]
fn test_agent_not_found() {
    let mut env = TestEnv::new();
    env.start_server();

    // Try to snapshot a non-existent agent
    env.vessel()
        .args(["snapshot", "nonexistent-agent"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("not found"));
}

#[test]
fn test_spawn_requires_command() {
    Command::cargo_bin("vessel")
        .unwrap()
        .args(["spawn", "--"])
        .assert()
        .failure();
}

#[test]
fn test_send_bytes_hex() {
    let mut env = TestEnv::new();
    env.start_server();

    // Spawn bash
    let output = env
        .vessel()
        .args(["spawn", "--", "bash"])
        .output()
        .expect("failed to run spawn");

    assert!(output.status.success());
    let agent_id = String::from_utf8_lossy(&output.stdout).trim().to_string();

    std::thread::sleep(Duration::from_millis(200));

    // Send "hi\n" as hex (68 69 0a)
    env.vessel()
        .args(["send-bytes", &agent_id, "68690a"])
        .assert()
        .success();

    // Clean up
    env.vessel().args(["kill", &agent_id]).assert().success();
}

#[test]
fn test_shutdown() {
    let mut env = TestEnv::new();
    env.start_server();

    // Shutdown should succeed
    env.vessel()
        .arg("shutdown")
        .assert()
        .success()
        .stdout(predicate::str::contains("shutting down"));

    // Mark server as None so Drop doesn't try to shut it down again
    env.server_process = None;
}

#[test]
fn test_wait_for_content() {
    let mut env = TestEnv::new();
    env.start_server();

    // Spawn a program that outputs text after a delay
    let output = env
        .vessel()
        .args([
            "spawn",
            "--",
            "sh",
            "-c",
            "sleep 0.2; echo MARKER_READY; sleep 30",
        ])
        .output()
        .expect("failed to run spawn");

    assert!(output.status.success());
    let agent_id = String::from_utf8_lossy(&output.stdout).trim().to_string();

    // Wait should succeed when the content appears
    env.vessel()
        .args([
            "wait",
            &agent_id,
            "--contains",
            "MARKER_READY",
            "--timeout",
            "5",
            "--print",
        ])
        .assert()
        .success()
        .stdout(predicate::str::contains("MARKER_READY"));

    // Clean up
    env.vessel().args(["kill", &agent_id]).assert().success();
}

#[test]
fn test_wait_timeout() {
    let mut env = TestEnv::new();
    env.start_server();

    // Spawn a program that never outputs the expected content
    let output = env
        .vessel()
        .args(["spawn", "--", "sleep", "30"])
        .output()
        .expect("failed to run spawn");

    assert!(output.status.success());
    let agent_id = String::from_utf8_lossy(&output.stdout).trim().to_string();

    // Wait should fail after timeout
    env.vessel()
        .args([
            "wait",
            &agent_id,
            "--contains",
            "NEVER_APPEARS",
            "--timeout",
            "1",
        ])
        .assert()
        .failure()
        .stderr(predicate::str::contains("timeout"));

    // Clean up
    env.vessel().args(["kill", &agent_id]).assert().success();
}

#[test]
fn test_spawn_with_custom_name() {
    let mut env = TestEnv::new();
    env.start_server();

    // Spawn with custom name
    let output = env
        .vessel()
        .args(["spawn", "--name", "my-worker", "--", "sleep", "30"])
        .output()
        .expect("failed to run spawn");

    assert!(output.status.success(), "spawn should succeed");
    let agent_id = String::from_utf8_lossy(&output.stdout).trim().to_string();
    assert_eq!(agent_id, "my-worker", "should return the custom name");

    // List should show the custom name
    env.vessel()
        .arg("list")
        .assert()
        .success()
        .stdout(predicate::str::contains("my-worker"));

    // Clean up
    env.vessel().args(["kill", "my-worker"]).assert().success();
}

#[test]
fn test_spawn_duplicate_name_fails() {
    let mut env = TestEnv::new();
    env.start_server();

    // Spawn first agent with custom name
    let output = env
        .vessel()
        .args(["spawn", "--name", "unique-name", "--", "sleep", "30"])
        .output()
        .expect("failed to run spawn");

    assert!(output.status.success(), "first spawn should succeed");

    // Try to spawn second agent with same name - should fail
    env.vessel()
        .args(["spawn", "--name", "unique-name", "--", "sleep", "30"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("already in use"));

    // Clean up
    env.vessel().args(["kill", "unique-name"]).assert().success();
}

#[test]
fn test_exec_command() {
    let mut env = TestEnv::new();
    env.start_server();

    // Execute a simple command
    env.vessel()
        .args(["exec", "--timeout", "5", "--", "echo", "EXEC_TEST_OUTPUT"])
        .assert()
        .success()
        .stdout(predicate::str::contains("EXEC_TEST_OUTPUT"));
}

#[test]
fn test_exec_multiline_output() {
    let mut env = TestEnv::new();
    env.start_server();

    // Execute a command with multiple lines of output
    env.vessel()
        .args([
            "exec",
            "--timeout",
            "5",
            "--",
            "echo first; echo second; echo third",
        ])
        .assert()
        .success()
        .stdout(predicate::str::contains("first"))
        .stdout(predicate::str::contains("second"))
        .stdout(predicate::str::contains("third"));
}

#[test]
fn test_exec_exit_code_propagation() {
    let mut env = TestEnv::new();
    env.start_server();

    // Execute a failing command - should propagate exit code
    env.vessel()
        .args(["exec", "--timeout", "5", "--", "false"])
        .assert()
        .failure()
        .code(1);

    // Execute a command that fails with code 2
    env.vessel()
        .args(["exec", "--timeout", "5", "--", "ls /nonexistent_path_12345"])
        .assert()
        .failure()
        .code(2);
}

#[test]
fn test_kill_idempotent() {
    let mut env = TestEnv::new();
    env.start_server();

    // Killing a non-existent agent should succeed (like rm -f, pkill)
    env.vessel()
        .args(["kill", "nonexistent-agent"])
        .assert()
        .success();

    // Kill --all with no agents should also succeed
    env.vessel()
        .args(["kill", "--all"])
        .assert()
        .success();

    // Spawn an agent, kill it, then kill it again (should be idempotent)
    let output = env
        .vessel()
        .args(["spawn", "--", "sleep", "100"])
        .output()
        .expect("failed to run spawn");
    assert!(output.status.success());
    let agent_id = String::from_utf8_lossy(&output.stdout).trim().to_string();

    // First kill succeeds
    env.vessel().args(["kill", &agent_id]).assert().success();

    // Give it time to exit
    std::thread::sleep(Duration::from_millis(100));

    // Second kill should also succeed (idempotent)
    env.vessel()
        .args(["kill", &agent_id])
        .assert()
        .success();
}

#[test]
fn test_send_key() {
    let mut env = TestEnv::new();
    env.start_server();

    // Spawn bash
    let output = env
        .vessel()
        .args(["spawn", "--", "bash"])
        .output()
        .expect("failed to run spawn");
    assert!(output.status.success());
    let agent_id = String::from_utf8_lossy(&output.stdout).trim().to_string();

    std::thread::sleep(Duration::from_millis(200));

    // Send single keys - should all succeed
    env.vessel()
        .args(["send-keys", &agent_id, "up"])
        .assert()
        .success();

    env.vessel()
        .args(["send-keys", &agent_id, "down"])
        .assert()
        .success();

    env.vessel()
        .args(["send-keys", &agent_id, "enter"])
        .assert()
        .success();

    env.vessel()
        .args(["send-keys", &agent_id, "tab"])
        .assert()
        .success();

    env.vessel()
        .args(["send-keys", &agent_id, "ctrl-c"])
        .assert()
        .success();

    // Send multiple keys at once
    env.vessel()
        .args(["send-keys", &agent_id, "up", "down", "enter"])
        .assert()
        .success();

    // Invalid key name should fail
    env.vessel()
        .args(["send-keys", &agent_id, "invalid-key"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("unknown key"));

    // Clean up
    env.vessel().args(["kill", &agent_id]).assert().success();
}

#[test]
fn test_wait_combined_conditions() {
    let mut env = TestEnv::new();
    env.start_server();

    // Spawn bash
    let output = env
        .vessel()
        .args(["spawn", "--", "bash"])
        .output()
        .expect("failed to run spawn");
    assert!(output.status.success());
    let agent_id = String::from_utf8_lossy(&output.stdout).trim().to_string();

    std::thread::sleep(Duration::from_millis(300));

    // Test 1: Wait with --stable alone should work
    env.vessel()
        .args(["wait", &agent_id, "--stable", "100", "--timeout", "5"])
        .assert()
        .success();

    // Test 2: Send some output and wait for it with --contains alone
    env.vessel()
        .args(["send", &agent_id, "echo test123", "--newline"])
        .assert()
        .success();

    env.vessel()
        .args(["wait", &agent_id, "--contains", "test123", "--timeout", "5"])
        .assert()
        .success();

    // Test 3: Combined --stable AND --contains
    // Send a command and wait for both stable screen AND specific content
    env.vessel()
        .args(["send", &agent_id, "echo hello-combined", "--newline"])
        .assert()
        .success();

    env.vessel()
        .args([
            "wait",
            &agent_id,
            "--stable",
            "100",
            "--contains",
            "hello-combined",
            "--timeout",
            "5",
        ])
        .assert()
        .success();

    // Clean up
    env.vessel().args(["kill", &agent_id]).assert().success();
}

// ============================================================================
// wait --exited tests
// ============================================================================

#[test]
fn test_wait_exited() {
    let mut env = TestEnv::new();
    env.start_server();

    // Spawn a short-lived command that exits 0
    let output = env
        .vessel()
        .args(["spawn", "--name", "exit-ok", "--", "sh", "-c", "echo hello; exit 0"])
        .output()
        .expect("failed to run spawn");
    assert!(output.status.success());

    // Wait for it to exit
    env.vessel()
        .args(["wait", "--exited", "exit-ok", "--timeout", "10"])
        .assert()
        .success()
        .code(0);
}

#[test]
fn test_wait_exited_nonzero() {
    let mut env = TestEnv::new();
    env.start_server();

    // Spawn a command that exits with code 42
    let output = env
        .vessel()
        .args(["spawn", "--name", "exit-42", "--", "sh", "-c", "exit 42"])
        .output()
        .expect("failed to run spawn");
    assert!(output.status.success());

    // Wait for it to exit - should propagate exit code 42
    env.vessel()
        .args(["wait", "--exited", "exit-42", "--timeout", "10"])
        .assert()
        .failure()
        .code(42);
}

#[test]
fn test_wait_exited_already_exited() {
    let mut env = TestEnv::new();
    env.start_server();

    // Spawn a very short-lived command
    let output = env
        .vessel()
        .args(["spawn", "--name", "already-done", "--", "true"])
        .output()
        .expect("failed to run spawn");
    assert!(output.status.success());

    // Wait a bit so it definitely exits
    std::thread::sleep(Duration::from_millis(500));

    // wait --exited should return immediately since it already exited
    env.vessel()
        .args(["wait", "--exited", "already-done", "--timeout", "5"])
        .assert()
        .success()
        .code(0);
}

#[test]
fn test_wait_exited_timeout() {
    let mut env = TestEnv::new();
    env.start_server();

    // Spawn a long-running command
    let output = env
        .vessel()
        .args(["spawn", "--name", "long-run", "--", "sleep", "999"])
        .output()
        .expect("failed to run spawn");
    assert!(output.status.success());

    // Wait with a very short timeout - should fail
    env.vessel()
        .args(["wait", "--exited", "long-run", "--timeout", "1"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("timeout"));

    // Clean up
    env.vessel().args(["kill", "long-run"]).assert().success();
}

// ============================================================================
// Slash in agent name tests
// ============================================================================

#[test]
fn test_spawn_slash_name() {
    let mut env = TestEnv::new();
    env.start_server();

    // Spawn with a slash in the name
    let output = env
        .vessel()
        .args(["spawn", "--name", "parent/child", "--", "sleep", "30"])
        .output()
        .expect("failed to run spawn");

    assert!(output.status.success(), "spawn should succeed");
    let agent_id = String::from_utf8_lossy(&output.stdout).trim().to_string();
    assert_eq!(agent_id, "parent/child", "should return the slash name");

    // List should show the slash name
    env.vessel()
        .arg("list")
        .assert()
        .success()
        .stdout(predicate::str::contains("parent/child"));

    // Kill by slash name should work
    env.vessel()
        .args(["kill", "parent/child"])
        .assert()
        .success();
}

#[test]
fn test_spawn_slash_name_invalid() {
    let mut env = TestEnv::new();
    env.start_server();

    // Leading slash should be rejected
    env.vessel()
        .args(["spawn", "--name", "/leading", "--", "sleep", "30"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("must not start/end with '/'"));

    // Trailing slash should be rejected
    env.vessel()
        .args(["spawn", "--name", "trailing/", "--", "sleep", "30"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("must not start/end with '/'"));

    // Double slash should be rejected
    env.vessel()
        .args(["spawn", "--name", "a//b", "--", "sleep", "30"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("must not start/end with '/' or contain '//'"));
}

#[test]
fn test_spawn_multi_level_slash() {
    let mut env = TestEnv::new();
    env.start_server();

    // Multi-level slash names should work
    let output = env
        .vessel()
        .args(["spawn", "--name", "a/b/c", "--", "sleep", "30"])
        .output()
        .expect("failed to run spawn");

    assert!(output.status.success(), "spawn should succeed");
    let agent_id = String::from_utf8_lossy(&output.stdout).trim().to_string();
    assert_eq!(agent_id, "a/b/c");

    // Snapshot should work with slash names
    std::thread::sleep(Duration::from_millis(200));
    env.vessel()
        .args(["snapshot", "a/b/c"])
        .assert()
        .success();

    // Clean up
    env.vessel().args(["kill", "a/b/c"]).assert().success();
}