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
|
use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use std::time::Duration;
use tempfile::TempDir;
#[test]
fn test_help_output() {
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.args(&["--help"])
.assert()
.success()
.stdout(predicate::str::contains("daemon process management"))
.stdout(predicate::str::contains("run"))
.stdout(predicate::str::contains("stop"))
.stdout(predicate::str::contains("tail"))
.stdout(predicate::str::contains("cat"))
.stdout(predicate::str::contains("list"))
.stdout(predicate::str::contains("status"))
.stdout(predicate::str::contains("clean"));
}
#[test]
fn test_version_output() {
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.args(&["--version"])
.assert()
.success()
.stdout(predicate::str::contains("demon 0.1.0"));
}
#[test]
fn test_run_missing_command() {
let temp_dir = TempDir::new().unwrap();
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.current_dir(temp_dir.path())
.args(&["run", "--id", "test"])
.assert()
.failure()
.stderr(predicate::str::contains("Command cannot be empty"));
}
#[test]
fn test_run_creates_files() {
let temp_dir = TempDir::new().unwrap();
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.current_dir(temp_dir.path())
.args(&["run", "--id", "test", "echo", "hello"])
.assert()
.success()
.stdout(predicate::str::contains("Started daemon 'test'"));
// Verify files were created
assert!(temp_dir.path().join("test.pid").exists());
assert!(temp_dir.path().join("test.stdout").exists());
assert!(temp_dir.path().join("test.stderr").exists());
// Check that stdout contains our output
let stdout_content = fs::read_to_string(temp_dir.path().join("test.stdout")).unwrap();
assert_eq!(stdout_content.trim(), "hello");
}
#[test]
fn test_run_duplicate_process() {
let temp_dir = TempDir::new().unwrap();
// Start a long-running process
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.current_dir(temp_dir.path())
.args(&["run", "--id", "long", "sleep", "30"])
.assert()
.success();
// Try to start another with the same ID
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.current_dir(temp_dir.path())
.args(&["run", "--id", "long", "sleep", "5"])
.assert()
.failure()
.stderr(predicate::str::contains("already running"));
// Clean up the running process
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.current_dir(temp_dir.path())
.args(&["stop", "--id", "long"])
.assert()
.success();
}
#[test]
fn test_list_empty() {
let temp_dir = TempDir::new().unwrap();
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.current_dir(temp_dir.path())
.args(&["list"])
.assert()
.success()
.stdout(predicate::str::contains("ID"))
.stdout(predicate::str::contains("PID"))
.stdout(predicate::str::contains("STATUS"))
.stdout(predicate::str::contains("No daemon processes found"));
}
#[test]
fn test_list_with_processes() {
let temp_dir = TempDir::new().unwrap();
// Start a process
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.current_dir(temp_dir.path())
.args(&["run", "--id", "test", "echo", "done"])
.assert()
.success();
// List processes
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.current_dir(temp_dir.path())
.args(&["list"])
.assert()
.success()
.stdout(predicate::str::contains("test"))
.stdout(predicate::str::contains("DEAD")); // Process should be finished by now
}
#[test]
fn test_cat_output() {
let temp_dir = TempDir::new().unwrap();
// Create a process with output
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.current_dir(temp_dir.path())
.args(&[
"run",
"--id",
"test",
"--",
"sh",
"-c",
"echo 'stdout line'; echo 'stderr line' >&2",
])
.assert()
.success();
// Cat the output
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.current_dir(temp_dir.path())
.args(&["cat", "--id", "test"])
.assert()
.success()
.stdout(predicate::str::contains("stdout line"))
.stdout(predicate::str::contains("stderr line"));
}
#[test]
fn test_cat_stdout_only() {
let temp_dir = TempDir::new().unwrap();
// Create a process with output
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.current_dir(temp_dir.path())
.args(&[
"run",
"--id",
"test",
"--",
"sh",
"-c",
"echo 'stdout line'; echo 'stderr line' >&2",
])
.assert()
.success();
// Cat only stdout
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.current_dir(temp_dir.path())
.args(&["cat", "--id", "test", "--stdout"])
.assert()
.success()
.stdout(predicate::str::contains("stdout line"))
.stdout(predicate::str::contains("stderr line").not());
}
#[test]
fn test_status_nonexistent() {
let temp_dir = TempDir::new().unwrap();
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.current_dir(temp_dir.path())
.args(&["status", "--id", "nonexistent"])
.assert()
.success()
.stdout(predicate::str::contains("NOT FOUND"));
}
#[test]
fn test_status_dead_process() {
let temp_dir = TempDir::new().unwrap();
// Create a short-lived process
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.current_dir(temp_dir.path())
.args(&["run", "--id", "dead", "echo", "hello"])
.assert()
.success();
// Check its status (should be dead)
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.current_dir(temp_dir.path())
.args(&["status", "--id", "dead"])
.assert()
.success()
.stdout(predicate::str::contains("DEAD"));
}
#[test]
fn test_stop_nonexistent() {
let temp_dir = TempDir::new().unwrap();
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.current_dir(temp_dir.path())
.args(&["stop", "--id", "nonexistent"])
.assert()
.success()
.stdout(predicate::str::contains("not running"));
}
#[test]
fn test_stop_process() {
let temp_dir = TempDir::new().unwrap();
// Start a long-running process
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.current_dir(temp_dir.path())
.args(&["run", "--id", "long", "sleep", "10"])
.assert()
.success();
// Stop it
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.current_dir(temp_dir.path())
.args(&["stop", "--id", "long"])
.assert()
.success()
.stdout(predicate::str::contains("terminated gracefully"));
// Verify PID file is gone
assert!(!temp_dir.path().join("long.pid").exists());
}
#[test]
fn test_clean_no_orphans() {
let temp_dir = TempDir::new().unwrap();
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.current_dir(temp_dir.path())
.args(&["clean"])
.assert()
.success()
.stdout(predicate::str::contains("No orphaned files found"));
}
#[test]
fn test_clean_with_orphans() {
let temp_dir = TempDir::new().unwrap();
// Create a dead process
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.current_dir(temp_dir.path())
.args(&["run", "--id", "dead", "echo", "hello"])
.assert()
.success();
// Clean up orphaned files
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.current_dir(temp_dir.path())
.args(&["clean"])
.assert()
.success()
.stdout(predicate::str::contains("Cleaned up"))
.stdout(predicate::str::contains("orphaned"));
// Verify files are gone
assert!(!temp_dir.path().join("dead.pid").exists());
assert!(!temp_dir.path().join("dead.stdout").exists());
assert!(!temp_dir.path().join("dead.stderr").exists());
}
#[test]
fn test_run_with_complex_command() {
let temp_dir = TempDir::new().unwrap();
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.current_dir(temp_dir.path())
.args(&[
"run",
"--id",
"complex",
"--",
"sh",
"-c",
"for i in 1 2 3; do echo \"line $i\"; done",
])
.assert()
.success();
// Give the process a moment to complete
std::thread::sleep(Duration::from_millis(100));
// Check the output contains all lines
let stdout_content = fs::read_to_string(temp_dir.path().join("complex.stdout")).unwrap();
assert!(stdout_content.contains("line 1"));
assert!(stdout_content.contains("line 2"));
assert!(stdout_content.contains("line 3"));
}
#[test]
fn test_timeout_configuration() {
let temp_dir = TempDir::new().unwrap();
// Start a process
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.current_dir(temp_dir.path())
.args(&["run", "--id", "timeout-test", "sleep", "5"])
.assert()
.success();
// Stop with custom timeout (should work normally since sleep responds to SIGTERM)
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.current_dir(temp_dir.path())
.args(&["stop", "--id", "timeout-test", "--timeout", "2"])
.assert()
.success()
.stdout(predicate::str::contains("terminated gracefully"));
}
#[test]
fn test_invalid_process_id() {
let temp_dir = TempDir::new().unwrap();
// Create an invalid PID file
fs::write(temp_dir.path().join("invalid.pid"), "not-a-number").unwrap();
// Status should handle it gracefully
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.current_dir(temp_dir.path())
.args(&["status", "--id", "invalid"])
.assert()
.success()
.stdout(predicate::str::contains("ERROR"));
// Clean should remove it
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.current_dir(temp_dir.path())
.args(&["clean"])
.assert()
.success()
.stdout(predicate::str::contains("invalid PID file"));
}
#[test]
fn test_list_quiet_mode() {
let temp_dir = TempDir::new().unwrap();
// Test quiet mode with no processes
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.current_dir(temp_dir.path())
.args(&["list", "--quiet"])
.assert()
.success()
.stdout(predicate::str::is_empty());
// Create a process
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.current_dir(temp_dir.path())
.args(&["run", "--id", "quiet-test", "echo", "done"])
.assert()
.success();
// Test quiet mode with process - should output colon-separated format
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.current_dir(temp_dir.path())
.args(&["list", "-q"])
.assert()
.success()
.stdout(predicate::str::contains("quiet-test:"))
.stdout(predicate::str::contains(":DEAD"))
// Should not contain headers
.stdout(predicate::str::contains("ID").not())
.stdout(predicate::str::contains("PID").not())
.stdout(predicate::str::contains("STATUS").not());
}
#[test]
fn test_llm_command() {
let mut cmd = Command::cargo_bin("demon").unwrap();
cmd.args(&["llm"])
.assert()
.success()
.stdout(predicate::str::contains(
"# Demon - Daemon Process Management CLI",
))
.stdout(predicate::str::contains("## Available Commands"))
.stdout(predicate::str::contains("demon run"))
.stdout(predicate::str::contains("demon stop"))
.stdout(predicate::str::contains("demon list"))
.stdout(predicate::str::contains("demon tail"))
.stdout(predicate::str::contains("demon cat"))
.stdout(predicate::str::contains("demon status"))
.stdout(predicate::str::contains("demon clean"))
.stdout(predicate::str::contains("Common Workflows"))
.stdout(predicate::str::contains("Best Practices"))
.stdout(predicate::str::contains("Integration Tips"));
}
|