forked from ogulcancelik/herdr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto_detect.rs
More file actions
783 lines (671 loc) · 25.2 KB
/
Copy pathauto_detect.rs
File metadata and controls
783 lines (671 loc) · 25.2 KB
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
//! Integration tests for auto-detect launch behavior.
#![cfg(not(target_os = "macos"))]
mod support;
use std::fs;
use std::io::{BufRead, BufReader, Write};
use std::os::unix::net::UnixStream;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::{Mutex, MutexGuard, OnceLock};
use std::thread;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use portable_pty::{native_pty_system, Child, CommandBuilder, MasterPty, PtySize};
use serde_json::Value;
use support::{
cleanup_test_base, register_runtime_dir, register_spawned_omni_pid, unregister_spawned_omni_pid,
};
fn unique_test_dir() -> PathBuf {
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0);
PathBuf::from(format!(
"/tmp/omni-autodetect-test-{}-{nanos}",
std::process::id()
))
}
struct SpawnedOmni {
_master: Box<dyn MasterPty + Send>,
child: Box<dyn Child + Send + Sync>,
}
impl Drop for SpawnedOmni {
fn drop(&mut self) {
let pid = self.child.process_id();
let _ = self.child.kill();
if let Some(pid) = pid {
let deadline = Instant::now() + Duration::from_secs(2);
while Instant::now() < deadline {
let mut status = 0;
let result =
unsafe { libc::waitpid(pid as libc::pid_t, &mut status, libc::WNOHANG) };
if result == pid as libc::pid_t || result == -1 {
break;
}
thread::sleep(Duration::from_millis(20));
}
unregister_spawned_omni_pid(Some(pid));
}
}
}
fn cleanup_spawned_omni(spawned: SpawnedOmni, base: PathBuf) {
drop(spawned);
cleanup_test_base(&base);
}
fn wait_for_socket(path: &Path, timeout: Duration) {
let deadline = Instant::now() + timeout;
while Instant::now() < deadline {
if path.exists() && UnixStream::connect(path).is_ok() {
return;
}
thread::sleep(Duration::from_millis(25));
}
panic!("socket did not appear at {}", path.display());
}
fn test_lock() -> MutexGuard<'static, ()> {
static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
LOCK.get_or_init(|| Mutex::new(()))
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
}
fn spawn_server(
config_home: &Path,
runtime_dir: &Path,
api_socket_path: &Path,
_client_socket_path: &Path,
) -> SpawnedOmni {
fs::create_dir_all(config_home.join("omni")).unwrap();
fs::create_dir_all(runtime_dir).unwrap();
register_runtime_dir(runtime_dir);
fs::write(config_home.join("omni/config.toml"), "onboarding = false\n").unwrap();
let pair = native_pty_system()
.openpty(PtySize {
rows: 24,
cols: 80,
pixel_width: 0,
pixel_height: 0,
})
.unwrap();
let mut cmd = CommandBuilder::new(env!("CARGO_BIN_EXE_omni"));
cmd.arg("server");
cmd.env("XDG_CONFIG_HOME", config_home);
cmd.env("XDG_RUNTIME_DIR", runtime_dir);
cmd.env("OMNI_SOCKET_PATH", api_socket_path);
cmd.env_remove("OMNI_CLIENT_SOCKET_PATH");
cmd.env("SHELL", "/bin/sh");
cmd.env_remove("OMNI_ENV");
let child = pair.slave.spawn_command(cmd).unwrap();
register_spawned_omni_pid(child.process_id());
drop(pair.slave);
SpawnedOmni {
_master: pair.master,
child,
}
}
/// Spawn `omni` (no subcommand) — the auto-detect launch path.
fn spawn_omni_auto(
config_home: &Path,
runtime_dir: &Path,
api_socket_path: &Path,
_client_socket_path: &Path,
) -> SpawnedOmni {
fs::create_dir_all(config_home.join("omni")).unwrap();
fs::create_dir_all(runtime_dir).unwrap();
register_runtime_dir(runtime_dir);
fs::write(config_home.join("omni/config.toml"), "onboarding = false\n").unwrap();
let pair = native_pty_system()
.openpty(PtySize {
rows: 24,
cols: 80,
pixel_width: 0,
pixel_height: 0,
})
.unwrap();
let mut cmd = CommandBuilder::new(env!("CARGO_BIN_EXE_omni"));
// No subcommand, no --no-session → auto-detect launch
cmd.env("XDG_CONFIG_HOME", config_home);
cmd.env("XDG_RUNTIME_DIR", runtime_dir);
cmd.env("OMNI_SOCKET_PATH", api_socket_path);
cmd.env_remove("OMNI_CLIENT_SOCKET_PATH");
cmd.env("SHELL", "/bin/sh");
cmd.env_remove("OMNI_ENV");
let child = pair.slave.spawn_command(cmd).unwrap();
register_spawned_omni_pid(child.process_id());
drop(pair.slave);
SpawnedOmni {
_master: pair.master,
child,
}
}
/// Spawn `omni --no-session` — the monolithic escape hatch.
fn spawn_omni_no_session(
config_home: &Path,
runtime_dir: &Path,
api_socket_path: &Path,
) -> SpawnedOmni {
fs::create_dir_all(config_home.join("omni")).unwrap();
fs::create_dir_all(runtime_dir).unwrap();
register_runtime_dir(runtime_dir);
fs::write(config_home.join("omni/config.toml"), "onboarding = false\n").unwrap();
let pair = native_pty_system()
.openpty(PtySize {
rows: 24,
cols: 80,
pixel_width: 0,
pixel_height: 0,
})
.unwrap();
let mut cmd = CommandBuilder::new(env!("CARGO_BIN_EXE_omni"));
cmd.arg("--no-session");
cmd.env("XDG_CONFIG_HOME", config_home);
cmd.env("XDG_RUNTIME_DIR", runtime_dir);
cmd.env("OMNI_SOCKET_PATH", api_socket_path);
cmd.env("SHELL", "/bin/sh");
cmd.env_remove("OMNI_ENV");
let child = pair.slave.spawn_command(cmd).unwrap();
register_spawned_omni_pid(child.process_id());
drop(pair.slave);
SpawnedOmni {
_master: pair.master,
child,
}
}
fn ping_socket(socket_path: &Path) -> String {
let mut stream = UnixStream::connect(socket_path).expect("should connect to API socket");
let request = r#"{"id":"1","method":"ping","params":{}}"#;
writeln!(stream, "{}", request).unwrap();
let mut reader = BufReader::new(stream);
let mut response = String::new();
reader.read_line(&mut response).unwrap();
response.trim().to_string()
}
fn wait_for_log_contains(path: &Path, needle: &str, timeout: Duration) {
let deadline = Instant::now() + timeout;
while Instant::now() < deadline {
if let Ok(content) = fs::read_to_string(path) {
if content.contains(needle) {
return;
}
}
thread::sleep(Duration::from_millis(25));
}
let content = fs::read_to_string(path).unwrap_or_default();
panic!(
"log {} did not contain {:?}. content:\n{}",
path.display(),
needle,
content
);
}
fn run_cli(socket_path: &Path, args: &[&str]) -> std::process::Output {
let mut command = Command::new(env!("CARGO_BIN_EXE_omni"));
command.args(args);
command.env("OMNI_SOCKET_PATH", socket_path);
command.output().unwrap()
}
fn process_exists(pid: u32) -> bool {
let result = unsafe { libc::kill(pid as i32, 0) };
if result == 0 {
true
} else {
std::io::Error::last_os_error().raw_os_error() == Some(libc::EPERM)
}
}
fn read_json_line(stream: UnixStream) -> Value {
let mut reader = BufReader::new(stream);
let mut response = String::new();
reader.read_line(&mut response).unwrap();
serde_json::from_str(&response).unwrap()
}
fn wait_for_pid_exit(pid: u32, timeout: Duration) -> bool {
let deadline = Instant::now() + timeout;
while Instant::now() < deadline {
if !process_exists(pid) {
return true;
}
thread::sleep(Duration::from_millis(20));
}
false
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
/// Running `omni` with no server present starts a server
/// and attaches as client.
#[test]
fn auto_detect_no_server_spawns_server_and_attaches() {
let _lock = test_lock();
let base = unique_test_dir();
let config_home = base.join("config");
let runtime_dir = base.join("runtime");
let api_socket = runtime_dir.join("omni.sock");
let client_socket = runtime_dir.join("omni-client.sock");
// Ensure no server is running initially.
assert!(
!api_socket.exists(),
"api socket should not exist initially"
);
assert!(
!client_socket.exists(),
"client socket should not exist initially"
);
// Run `omni` (no subcommand) — should auto-detect, spawn server, attach as client.
let omni = spawn_omni_auto(&config_home, &runtime_dir, &api_socket, &client_socket);
// Wait for both sockets to appear (server was spawned).
wait_for_socket(&api_socket, Duration::from_secs(10));
wait_for_socket(&client_socket, Duration::from_secs(10));
// Verify the API socket responds to ping (server is running).
let response = ping_socket(&api_socket);
assert!(
response.contains("pong"),
"API socket should respond to ping: {response}"
);
// Verify the client socket accepts connections (server is listening on it).
let _stream = UnixStream::connect(&client_socket)
.expect("should connect to client socket (server is listening)");
// Verify the client process is running.
let client_pid = omni.child.process_id().expect("client should have PID");
assert!(
process_exists(client_pid),
"client process should be running"
);
cleanup_spawned_omni(omni, base);
}
/// Running `omni` with a server already running attaches
/// as client directly (no second server).
#[test]
fn auto_detect_server_running_attaches_directly() {
let _lock = test_lock();
let base = unique_test_dir();
let config_home = base.join("config");
let runtime_dir = base.join("runtime");
let api_socket = runtime_dir.join("omni.sock");
let client_socket = runtime_dir.join("omni-client.sock");
// Start a server explicitly.
let server = spawn_server(&config_home, &runtime_dir, &api_socket, &client_socket);
wait_for_socket(&api_socket, Duration::from_secs(10));
wait_for_socket(&client_socket, Duration::from_secs(10));
let server_pid = server.child.process_id().expect("server should have PID");
// Verify server is running.
assert!(process_exists(server_pid), "server should be running");
// Run `omni` (no subcommand) — should detect the running server and attach.
let client = spawn_omni_auto(&config_home, &runtime_dir, &api_socket, &client_socket);
// Wait a moment for the client to attach.
thread::sleep(Duration::from_millis(500));
// Verify the client is running.
let client_pid = client.child.process_id().expect("client should have PID");
assert!(
process_exists(client_pid),
"client process should be running"
);
// Verify the server is still the same one (no second server spawned).
assert!(
process_exists(server_pid),
"original server should still be running"
);
// Verify API still responds.
let response = ping_socket(&api_socket);
assert!(
response.contains("pong"),
"API should still respond to ping: {response}"
);
cleanup_spawned_omni(client, PathBuf::from("/nonexistent"));
cleanup_spawned_omni(server, base);
}
/// Socket path resolution is consistent between server and client.
/// Both derive the client socket from the `OMNI_SOCKET_PATH` override,
/// so overriding the API socket keeps both endpoints aligned.
#[test]
fn auto_detect_socket_path_consistency() {
let _lock = test_lock();
let base = unique_test_dir();
let config_home = base.join("config");
let runtime_dir = base.join("runtime");
let api_socket = runtime_dir.join("omni.sock");
let client_socket = runtime_dir.join("omni-client.sock");
// Run `omni` with custom socket paths.
let omni = spawn_omni_auto(&config_home, &runtime_dir, &api_socket, &client_socket);
// Wait for both sockets to appear at the custom paths.
wait_for_socket(&api_socket, Duration::from_secs(10));
wait_for_socket(&client_socket, Duration::from_secs(10));
// Verify sockets exist at the specified paths.
assert!(
api_socket.exists(),
"API socket should exist at custom path"
);
assert!(
client_socket.exists(),
"client socket should exist at custom path"
);
// Verify API responds (server is using the custom API socket path).
let response = ping_socket(&api_socket);
assert!(
response.contains("pong"),
"API should respond at custom path: {response}"
);
// Verify client socket accepts connections (server is using the custom
// client socket path).
let _stream = UnixStream::connect(&client_socket)
.expect("should connect to client socket at custom path");
cleanup_spawned_omni(omni, base);
}
/// `omni --no-session` bypasses server/client and runs
/// monolithically. No server process is spawned. No client socket is created.
#[test]
fn no_session_flag_runs_monolithically() {
let _lock = test_lock();
let base = unique_test_dir();
let config_home = base.join("config");
let runtime_dir = base.join("runtime");
let api_socket = runtime_dir.join("omni.sock");
let client_socket = runtime_dir.join("omni-client.sock");
// Run `omni --no-session` — monolithic mode, no server/client.
let omni = spawn_omni_no_session(&config_home, &runtime_dir, &api_socket);
// Wait for the API socket (monolithic mode creates it).
wait_for_socket(&api_socket, Duration::from_secs(10));
// Verify the API socket exists and responds.
let response = ping_socket(&api_socket);
assert!(
response.contains("pong"),
"monolithic API should respond: {response}"
);
// Verify NO client socket was created — this is the key distinction
// between monolithic mode and server/client mode.
assert!(
!client_socket.exists(),
"no client socket should exist in monolithic mode"
);
// Verify the API socket is served by the monolithic process itself,
// not by a separate server. We can check this by verifying the client
// PID matches what would be serving the socket — in monolithic mode,
// there is only one omni process.
let client_pid = omni.child.process_id().expect("should have PID");
assert!(
process_exists(client_pid),
"monolithic process should be running"
);
cleanup_spawned_omni(omni, base);
}
/// CLI subcommands work through the server's JSON API socket.
#[test]
fn cli_subcommands_work_through_server() {
let _lock = test_lock();
let base = unique_test_dir();
let config_home = base.join("config");
let runtime_dir = base.join("runtime");
let api_socket = runtime_dir.join("omni.sock");
let client_socket = runtime_dir.join("omni-client.sock");
// Start a server.
let server = spawn_server(&config_home, &runtime_dir, &api_socket, &client_socket);
wait_for_socket(&api_socket, Duration::from_secs(10));
wait_for_socket(&client_socket, Duration::from_secs(10));
// Test `omni workspace list` through the server's API socket.
let output = run_cli(&api_socket, &["workspace", "list"]);
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
output.status.success(),
"workspace list should succeed: stderr={}",
String::from_utf8_lossy(&output.stderr)
);
// The response should be valid JSON with a "result" field.
assert!(
stdout.contains("result"),
"workspace list output should contain 'result': {stdout}"
);
// Test `omni pane list` through the server's API socket.
let output = run_cli(&api_socket, &["pane", "list"]);
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
output.status.success(),
"pane list should succeed: stderr={}",
String::from_utf8_lossy(&output.stderr)
);
assert!(
stdout.contains("result"),
"pane list output should contain 'result': {stdout}"
);
cleanup_spawned_omni(server, base);
}
/// Verify that the server spawned by auto-detect
/// persists after the client exits, and a new `omni` can reattach.
#[test]
fn auto_detect_server_persists_and_reattaches() {
let _lock = test_lock();
let base = unique_test_dir();
let config_home = base.join("config");
let runtime_dir = base.join("runtime");
let api_socket = runtime_dir.join("omni.sock");
let client_socket = runtime_dir.join("omni-client.sock");
// Run `omni` — auto-detect spawns server + attaches client.
let mut client1 = spawn_omni_auto(&config_home, &runtime_dir, &api_socket, &client_socket);
wait_for_socket(&api_socket, Duration::from_secs(10));
wait_for_socket(&client_socket, Duration::from_secs(10));
// Verify API responds.
let response = ping_socket(&api_socket);
assert!(
response.contains("pong"),
"API should respond before client exit: {response}"
);
// Kill the first client.
let client1_pid = client1.child.process_id().expect("client1 should have PID");
let _ = client1.child.kill();
let _ = wait_for_pid_exit(client1_pid, Duration::from_secs(2));
drop(client1);
// Wait a moment for the server to process the disconnect.
thread::sleep(Duration::from_millis(500));
// Verify server is still running after client exit — the API should
// still respond because the server is a separate daemon process.
let response = ping_socket(&api_socket);
assert!(
response.contains("pong"),
"API should still respond after client exit (server persists): {response}"
);
// The client socket should still exist (server is still listening).
assert!(
client_socket.exists(),
"client socket should still exist after client exit"
);
// Run `omni` again — should detect the running server and reattach.
let client2 = spawn_omni_auto(&config_home, &runtime_dir, &api_socket, &client_socket);
thread::sleep(Duration::from_millis(500));
// Verify the new client is running.
let client2_pid = client2.child.process_id().expect("client2 should have PID");
assert!(
process_exists(client2_pid),
"second client should be running"
);
// Verify API still responds (same server).
let response = ping_socket(&api_socket);
assert!(
response.contains("pong"),
"API should still respond after reattach: {response}"
);
cleanup_spawned_omni(client2, PathBuf::from("/nonexistent"));
cleanup_test_base(&base);
}
/// Verify that the default API and client
/// sockets live in the app config directory when no env override is set.
#[test]
fn auto_detect_default_socket_path_from_config_dir() {
let _lock = test_lock();
let base = unique_test_dir();
let config_home = base.join("config");
let runtime_dir = base.join("runtime");
// Don't set OMNI_SOCKET_PATH or OMNI_CLIENT_SOCKET_PATH.
// The default paths should come from the app config directory, not XDG_RUNTIME_DIR.
let app_dir_name = if cfg!(debug_assertions) {
"omni-dev"
} else {
"omni"
};
let api_socket = config_home.join(app_dir_name).join("omni.sock");
let client_socket = config_home.join(app_dir_name).join("omni-client.sock");
// Spawn server with XDG_RUNTIME_DIR set to a different directory to prove it is ignored.
fs::create_dir_all(config_home.join(app_dir_name)).unwrap();
fs::create_dir_all(&runtime_dir).unwrap();
register_runtime_dir(&runtime_dir);
fs::write(
config_home.join(app_dir_name).join("config.toml"),
"onboarding = false\n",
)
.unwrap();
let pair = native_pty_system()
.openpty(PtySize {
rows: 24,
cols: 80,
pixel_width: 0,
pixel_height: 0,
})
.unwrap();
let mut cmd = CommandBuilder::new(env!("CARGO_BIN_EXE_omni"));
cmd.arg("server");
cmd.env("XDG_CONFIG_HOME", &config_home);
cmd.env("XDG_RUNTIME_DIR", &runtime_dir);
cmd.env("SHELL", "/bin/sh");
cmd.env_remove("OMNI_ENV");
// Explicitly remove socket overrides to test default path resolution.
cmd.env_remove("OMNI_SOCKET_PATH");
cmd.env_remove("OMNI_CLIENT_SOCKET_PATH");
let child = pair.slave.spawn_command(cmd).unwrap();
register_spawned_omni_pid(child.process_id());
drop(pair.slave);
let server = SpawnedOmni {
_master: pair.master,
child,
};
// Wait for sockets to appear at the default config-dir paths.
wait_for_socket(&api_socket, Duration::from_secs(10));
wait_for_socket(&client_socket, Duration::from_secs(10));
// Verify both sockets exist.
assert!(api_socket.exists(), "API socket should exist in config dir");
assert!(
client_socket.exists(),
"client socket should exist in config dir"
);
// Verify API responds.
let response = ping_socket(&api_socket);
assert!(
response.contains("pong"),
"API should respond at config-dir path: {response}"
);
cleanup_spawned_omni(server, base);
}
#[test]
fn auto_detect_writes_client_and_server_logs_to_separate_files() {
let _lock = test_lock();
let base = unique_test_dir();
let config_home = base.join("config");
let runtime_dir = base.join("runtime");
let api_socket = runtime_dir.join("omni.sock");
let client_socket = runtime_dir.join("omni-client.sock");
let spawned = spawn_omni_auto(&config_home, &runtime_dir, &api_socket, &client_socket);
wait_for_socket(&api_socket, Duration::from_secs(10));
wait_for_socket(&client_socket, Duration::from_secs(10));
let app_dir_name = if cfg!(debug_assertions) {
"omni-dev"
} else {
"omni"
};
let log_dir = config_home.join(app_dir_name);
let client_log = log_dir.join("omni-client.log");
let server_log = log_dir.join("omni-server.log");
let monolith_log = log_dir.join("omni.log");
wait_for_log_contains(
&client_log,
"event=\"app.startup\" subsystem=\"client\"",
Duration::from_secs(10),
);
wait_for_log_contains(
&server_log,
"event=\"app.startup\" subsystem=\"server\"",
Duration::from_secs(10),
);
let monolith_content = fs::read_to_string(&monolith_log).unwrap_or_default();
assert!(
!monolith_content.contains("subsystem=\"client\""),
"persistent client logs should not land in omni.log: {monolith_content}"
);
cleanup_spawned_omni(spawned, base);
}
#[test]
fn no_session_writes_startup_logs_to_monolith_file() {
let _lock = test_lock();
let base = unique_test_dir();
let config_home = base.join("config");
let runtime_dir = base.join("runtime");
let api_socket = runtime_dir.join("omni.sock");
let spawned = spawn_omni_no_session(&config_home, &runtime_dir, &api_socket);
wait_for_socket(&api_socket, Duration::from_secs(10));
let app_dir_name = if cfg!(debug_assertions) {
"omni-dev"
} else {
"omni"
};
let log_dir = config_home.join(app_dir_name);
let monolith_log = log_dir.join("omni.log");
wait_for_log_contains(
&monolith_log,
"event=\"app.startup\" subsystem=\"app\"",
Duration::from_secs(10),
);
cleanup_spawned_omni(spawned, base);
}
#[test]
fn auto_detect_respects_nested_guard_before_auto_attach() {
let _lock = test_lock();
let base = unique_test_dir();
let config_home = base.join("config");
let runtime_dir = base.join("runtime");
let api_socket = runtime_dir.join("omni.sock");
let client_socket = runtime_dir.join("omni-client.sock");
let server = spawn_server(&config_home, &runtime_dir, &api_socket, &client_socket);
wait_for_socket(&api_socket, Duration::from_secs(10));
wait_for_socket(&client_socket, Duration::from_secs(10));
let baseline = read_json_line({
let mut stream = UnixStream::connect(&api_socket).unwrap();
writeln!(
stream,
r#"{{"id":"ws_before","method":"workspace.list","params":{{}}}}"#
)
.unwrap();
stream
});
let baseline_count = baseline["result"]["workspaces"]
.as_array()
.map(|workspaces| workspaces.len())
.unwrap_or(0);
let output = Command::new(env!("CARGO_BIN_EXE_omni"))
.env("XDG_CONFIG_HOME", &config_home)
.env("XDG_RUNTIME_DIR", &runtime_dir)
.env("OMNI_SOCKET_PATH", &api_socket)
.env_remove("OMNI_CLIENT_SOCKET_PATH")
.env("OMNI_ENV", "1")
.output()
.unwrap();
assert!(
!output.status.success(),
"nested launch should fail before auto-attach"
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("nested omni is disabled by default"),
"stderr should mention nested-launch guard: {stderr}"
);
let after = read_json_line({
let mut stream = UnixStream::connect(&api_socket).unwrap();
writeln!(
stream,
r#"{{"id":"ws_after","method":"workspace.list","params":{{}}}}"#
)
.unwrap();
stream
});
let after_count = after["result"]["workspaces"]
.as_array()
.map(|workspaces| workspaces.len())
.unwrap_or(0);
assert_eq!(
after_count, baseline_count,
"nested launch should not auto-attach or mutate server state"
);
cleanup_spawned_omni(server, base);
}