rush-sh 0.8.0

A POSIX sh-compatible shell written in Rust
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
/// Tests for job management functionality

use crate::state::{Job, JobStatus, JobTable};

#[test]
fn test_job_status_to_string() {
    assert_eq!(JobStatus::Running.to_string(), "Running");
    assert_eq!(JobStatus::Stopped.to_string(), "Stopped");
    assert_eq!(JobStatus::Done(0).to_string(), "Done");
    assert_eq!(JobStatus::Done(1).to_string(), "Done(1)");
    assert_eq!(JobStatus::Done(127).to_string(), "Done(127)");
}

#[test]
fn test_job_creation() {
    let job = Job::new(1, Some(1234), "sleep 10 &".to_string(), vec![1234], false);
    
    assert_eq!(job.job_id, 1);
    assert_eq!(job.pgid, Some(1234));
    assert_eq!(job.command, "sleep 10 &");
    assert_eq!(job.pids, vec![1234]);
    assert_eq!(job.status, JobStatus::Running);
    assert_eq!(job.exit_code, None);
    assert!(!job.is_builtin);
}

#[test]
fn test_job_creation_builtin() {
    let job = Job::new(1, None, "sleep 10 &".to_string(), vec![], true);
    
    assert_eq!(job.job_id, 1);
    assert_eq!(job.pgid, None);
    assert!(job.is_builtin);
}

#[test]
fn test_job_update_status() {
    let mut job = Job::new(1, Some(1234), "sleep 10 &".to_string(), vec![1234], false);
    
    assert_eq!(job.status, JobStatus::Running);
    assert_eq!(job.exit_code, None);
    
    job.update_status(JobStatus::Stopped);
    assert_eq!(job.status, JobStatus::Stopped);
    assert_eq!(job.exit_code, None);
    
    job.update_status(JobStatus::Done(0));
    assert_eq!(job.status, JobStatus::Done(0));
    assert_eq!(job.exit_code, Some(0));
    
    job.update_status(JobStatus::Done(1));
    assert_eq!(job.status, JobStatus::Done(1));
    assert_eq!(job.exit_code, Some(1));
}

#[test]
fn test_job_is_active() {
    let mut job = Job::new(1, Some(1234), "sleep 10 &".to_string(), vec![1234], false);
    
    assert!(job.is_active());
    
    job.update_status(JobStatus::Stopped);
    assert!(job.is_active());
    
    job.update_status(JobStatus::Done(0));
    assert!(!job.is_active());
}

#[test]
fn test_job_table_creation() {
    let job_table = JobTable::new();
    
    assert_eq!(job_table.get_all_jobs().len(), 0);
    assert_eq!(job_table.get_current_job(), None);
    assert_eq!(job_table.get_previous_job(), None);
}

#[test]
fn test_job_table_allocate_job_id() {
    let mut job_table = JobTable::new();
    
    let id1 = job_table.allocate_job_id();
    let id2 = job_table.allocate_job_id();
    let id3 = job_table.allocate_job_id();
    
    assert_eq!(id1, 1);
    assert_eq!(id2, 2);
    assert_eq!(id3, 3);
}

#[test]
fn test_job_table_add_job() {
    let mut job_table = JobTable::new();
    
    let job = Job::new(1, Some(1234), "sleep 10 &".to_string(), vec![1234], false);
    job_table.add_job(job);
    
    assert_eq!(job_table.get_all_jobs().len(), 1);
    assert_eq!(job_table.get_current_job(), Some(1));
    assert_eq!(job_table.get_previous_job(), None);
}

#[test]
fn test_job_table_add_multiple_jobs() {
    let mut job_table = JobTable::new();
    
    let job1 = Job::new(1, Some(1234), "sleep 10 &".to_string(), vec![1234], false);
    let job2 = Job::new(2, Some(1235), "sleep 20 &".to_string(), vec![1235], false);
    let job3 = Job::new(3, Some(1236), "sleep 30 &".to_string(), vec![1236], false);
    
    job_table.add_job(job1);
    assert_eq!(job_table.get_current_job(), Some(1));
    assert_eq!(job_table.get_previous_job(), None);
    
    job_table.add_job(job2);
    assert_eq!(job_table.get_current_job(), Some(2));
    assert_eq!(job_table.get_previous_job(), Some(1));
    
    job_table.add_job(job3);
    assert_eq!(job_table.get_current_job(), Some(3));
    assert_eq!(job_table.get_previous_job(), Some(2));
    
    assert_eq!(job_table.get_all_jobs().len(), 3);
}

#[test]
fn test_job_table_get_job() {
    let mut job_table = JobTable::new();
    
    let job = Job::new(1, Some(1234), "sleep 10 &".to_string(), vec![1234], false);
    job_table.add_job(job);
    
    let retrieved = job_table.get_job(1);
    assert!(retrieved.is_some());
    assert_eq!(retrieved.unwrap().job_id, 1);
    assert_eq!(retrieved.unwrap().command, "sleep 10 &");
    
    let not_found = job_table.get_job(999);
    assert!(not_found.is_none());
}

#[test]
fn test_job_table_get_job_mut() {
    let mut job_table = JobTable::new();
    
    let job = Job::new(1, Some(1234), "sleep 10 &".to_string(), vec![1234], false);
    job_table.add_job(job);
    
    if let Some(job) = job_table.get_job_mut(1) {
        job.update_status(JobStatus::Done(0));
    }
    
    let retrieved = job_table.get_job(1);
    assert_eq!(retrieved.unwrap().status, JobStatus::Done(0));
}

#[test]
fn test_job_table_remove_job() {
    let mut job_table = JobTable::new();
    
    let job = Job::new(1, Some(1234), "sleep 10 &".to_string(), vec![1234], false);
    job_table.add_job(job);
    
    assert_eq!(job_table.get_all_jobs().len(), 1);
    
    let removed = job_table.remove_job(1);
    assert!(removed.is_some());
    assert_eq!(removed.unwrap().job_id, 1);
    
    assert_eq!(job_table.get_all_jobs().len(), 0);
    assert_eq!(job_table.get_current_job(), None);
}

#[test]
fn test_job_table_remove_job_updates_current() {
    let mut job_table = JobTable::new();
    
    let job1 = Job::new(1, Some(1234), "sleep 10 &".to_string(), vec![1234], false);
    let job2 = Job::new(2, Some(1235), "sleep 20 &".to_string(), vec![1235], false);
    let job3 = Job::new(3, Some(1236), "sleep 30 &".to_string(), vec![1236], false);
    
    job_table.add_job(job1);
    job_table.add_job(job2);
    job_table.add_job(job3);
    
    // Current is 3, previous is 2
    assert_eq!(job_table.get_current_job(), Some(3));
    assert_eq!(job_table.get_previous_job(), Some(2));
    
    // Remove current job (3)
    job_table.remove_job(3);
    
    // After removing job 3, jobs 1 and 2 remain
    // Current should be 2 (highest ID), previous should be 1 (second highest)
    assert_eq!(job_table.get_current_job(), Some(2));
    assert_eq!(job_table.get_previous_job(), Some(1));
}

#[test]
fn test_job_table_remove_previous_job() {
    let mut job_table = JobTable::new();
    
    let job1 = Job::new(1, Some(1234), "sleep 10 &".to_string(), vec![1234], false);
    let job2 = Job::new(2, Some(1235), "sleep 20 &".to_string(), vec![1235], false);
    
    job_table.add_job(job1);
    job_table.add_job(job2);
    
    assert_eq!(job_table.get_current_job(), Some(2));
    assert_eq!(job_table.get_previous_job(), Some(1));
    
    // Remove previous job (1)
    job_table.remove_job(1);
    
    assert_eq!(job_table.get_current_job(), Some(2));
    assert_eq!(job_table.get_previous_job(), None);
}

#[test]
fn test_job_table_find_job_by_pid() {
    let mut job_table = JobTable::new();
    
    let job1 = Job::new(1, Some(1234), "sleep 10 &".to_string(), vec![1234], false);
    let job2 = Job::new(2, Some(1235), "sleep 20 &".to_string(), vec![1235], false);
    
    job_table.add_job(job1);
    job_table.add_job(job2);
    
    let found = job_table.find_job_by_pid(1234);
    assert!(found.is_some());
    assert_eq!(found.unwrap().job_id, 1);
    
    let found = job_table.find_job_by_pid(1235);
    assert!(found.is_some());
    assert_eq!(found.unwrap().job_id, 2);
    
    let not_found = job_table.find_job_by_pid(9999);
    assert!(not_found.is_none());
}

#[test]
fn test_job_table_find_job_by_pid_pipeline() {
    let mut job_table = JobTable::new();
    
    // Pipeline with multiple PIDs
    let job = Job::new(1, Some(1234), "cat file | grep pattern &".to_string(), vec![1234, 1235], false);
    job_table.add_job(job);
    
    let found1 = job_table.find_job_by_pid(1234);
    assert!(found1.is_some());
    assert_eq!(found1.unwrap().job_id, 1);
    
    let found2 = job_table.find_job_by_pid(1235);
    assert!(found2.is_some());
    assert_eq!(found2.unwrap().job_id, 1);
}

#[test]
fn test_job_table_update_job_status() {
    let mut job_table = JobTable::new();
    
    let job = Job::new(1, Some(1234), "sleep 10 &".to_string(), vec![1234], false);
    job_table.add_job(job);
    
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Running);
    
    let updated = job_table.update_job_status(1234, JobStatus::Done(0));
    assert!(updated);
    
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Done(0));
    assert_eq!(job_table.get_job(1).unwrap().exit_code, Some(0));
}

#[test]
fn test_job_table_update_job_status_not_found() {
    let mut job_table = JobTable::new();
    
    let job = Job::new(1, Some(1234), "sleep 10 &".to_string(), vec![1234], false);
    job_table.add_job(job);
    
    let updated = job_table.update_job_status(9999, JobStatus::Done(0));
    assert!(!updated);
}

#[test]
fn test_job_table_get_all_jobs_sorted() {
    let mut job_table = JobTable::new();
    
    // Add jobs in non-sequential order
    let job3 = Job::new(3, Some(1236), "sleep 30 &".to_string(), vec![1236], false);
    let job1 = Job::new(1, Some(1234), "sleep 10 &".to_string(), vec![1234], false);
    let job2 = Job::new(2, Some(1235), "sleep 20 &".to_string(), vec![1235], false);
    
    job_table.add_job(job3);
    job_table.add_job(job1);
    job_table.add_job(job2);
    
    let jobs = job_table.get_all_jobs();
    assert_eq!(jobs.len(), 3);
    
    // Should be sorted by job ID
    assert_eq!(jobs[0].job_id, 1);
    assert_eq!(jobs[1].job_id, 2);
    assert_eq!(jobs[2].job_id, 3);
}

#[test]
fn test_job_table_multiple_status_transitions() {
    let mut job_table = JobTable::new();
    
    let job = Job::new(1, Some(1234), "sleep 10 &".to_string(), vec![1234], false);
    job_table.add_job(job);
    
    // Running -> Stopped
    job_table.update_job_status(1234, JobStatus::Stopped);
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Stopped);
    assert!(job_table.get_job(1).unwrap().is_active());
    
    // Stopped -> Running
    job_table.update_job_status(1234, JobStatus::Running);
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Running);
    assert!(job_table.get_job(1).unwrap().is_active());
    
    // Running -> Done
    job_table.update_job_status(1234, JobStatus::Done(0));
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Done(0));
    assert!(!job_table.get_job(1).unwrap().is_active());
}

#[test]
fn test_job_table_complex_scenario() {
    let mut job_table = JobTable::new();
    
    // Add three jobs
    let job1 = Job::new(1, Some(1234), "sleep 10 &".to_string(), vec![1234], false);
    let job2 = Job::new(2, Some(1235), "sleep 20 &".to_string(), vec![1235], false);
    let job3 = Job::new(3, Some(1236), "sleep 30 &".to_string(), vec![1236], false);
    
    job_table.add_job(job1);
    job_table.add_job(job2);
    job_table.add_job(job3);
    
    assert_eq!(job_table.get_all_jobs().len(), 3);
    assert_eq!(job_table.get_current_job(), Some(3));
    assert_eq!(job_table.get_previous_job(), Some(2));
    
    // Complete job 2
    job_table.update_job_status(1235, JobStatus::Done(0));
    assert_eq!(job_table.get_job(2).unwrap().status, JobStatus::Done(0));
    
    // Stop job 3
    job_table.update_job_status(1236, JobStatus::Stopped);
    assert_eq!(job_table.get_job(3).unwrap().status, JobStatus::Stopped);
    
    // Remove completed job 2
    job_table.remove_job(2);
    assert_eq!(job_table.get_all_jobs().len(), 2);
    
    // Current should still be 3, previous should be 1
    assert_eq!(job_table.get_current_job(), Some(3));
    
    // Add a new job
    let job4 = Job::new(4, Some(1237), "sleep 40 &".to_string(), vec![1237], false);
    job_table.add_job(job4);
    
    assert_eq!(job_table.get_current_job(), Some(4));
    assert_eq!(job_table.get_previous_job(), Some(3));
}

#[test]
fn test_job_table_empty_operations() {
    let mut job_table = JobTable::new();
    
    assert_eq!(job_table.get_all_jobs().len(), 0);
    assert!(job_table.get_job(1).is_none());
    assert!(job_table.get_job_mut(1).is_none());
    assert!(job_table.find_job_by_pid(1234).is_none());
    assert!(!job_table.update_job_status(1234, JobStatus::Done(0)));
    assert!(job_table.remove_job(1).is_none());
    assert_eq!(job_table.get_current_job(), None);
    assert_eq!(job_table.get_previous_job(), None);
}

#[test]
fn test_job_builtin_vs_external() {
    let mut job_table = JobTable::new();
    
    // External command
    let external_job = Job::new(1, Some(1234), "sleep 10 &".to_string(), vec![1234], false);
    assert!(!external_job.is_builtin);
    assert_eq!(external_job.pgid, Some(1234));
    assert!(!external_job.pids.is_empty());
    
    // Builtin command
    let builtin_job = Job::new(2, None, "sleep 10 &".to_string(), vec![], true);
    assert!(builtin_job.is_builtin);
    assert_eq!(builtin_job.pgid, None);
    assert!(builtin_job.pids.is_empty());
    
    job_table.add_job(external_job);
    job_table.add_job(builtin_job);
    
    assert_eq!(job_table.get_all_jobs().len(), 2);
}

// ============================================================================
// Pipeline Job Status Tracking Tests
// ============================================================================

#[test]
fn test_pipeline_job_single_process_backward_compatibility() {
    // Single process jobs should work exactly as before
    let mut job_table = JobTable::new();
    
    let job = Job::new(1, Some(1234), "sleep 10 &".to_string(), vec![1234], false);
    job_table.add_job(job);
    
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Running);
    
    // Update the single PID to Done
    job_table.update_job_status(1234, JobStatus::Done(0));
    
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Done(0));
    assert_eq!(job_table.get_job(1).unwrap().exit_code, Some(0));
}

#[test]
fn test_pipeline_job_two_processes_both_exit() {
    // Test a simple two-process pipeline where both processes exit
    let mut job_table = JobTable::new();
    
    let job = Job::new(1, Some(1234), "sleep 5 | sleep 10 &".to_string(), vec![1234, 1235], false);
    job_table.add_job(job);
    
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Running);
    
    // First process exits
    job_table.update_job_status(1234, JobStatus::Done(0));
    
    // Job should still be Running because second process hasn't exited
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Running);
    assert!(job_table.get_job(1).unwrap().is_active());
    
    // Second process exits
    job_table.update_job_status(1235, JobStatus::Done(0));
    
    // Now the job should be Done
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Done(0));
    assert!(!job_table.get_job(1).unwrap().is_active());
    assert_eq!(job_table.get_job(1).unwrap().exit_code, Some(0));
}

#[test]
fn test_pipeline_job_first_exits_before_last() {
    // Test pipeline where first process exits before last
    let mut job_table = JobTable::new();
    
    let job = Job::new(1, Some(1234), "cat file | grep pattern | wc -l &".to_string(),
                       vec![1234, 1235, 1236], false);
    job_table.add_job(job);
    
    // First process exits
    job_table.update_job_status(1234, JobStatus::Done(0));
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Running);
    
    // Second process exits
    job_table.update_job_status(1235, JobStatus::Done(0));
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Running);
    
    // Third process exits
    job_table.update_job_status(1236, JobStatus::Done(0));
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Done(0));
}

#[test]
fn test_pipeline_job_last_exits_before_first() {
    // Test pipeline where last process exits before first
    let mut job_table = JobTable::new();
    
    let job = Job::new(1, Some(1234), "sleep 10 | sleep 5 &".to_string(), vec![1234, 1235], false);
    job_table.add_job(job);
    
    // Last process exits first
    job_table.update_job_status(1235, JobStatus::Done(0));
    
    // Job should still be Running
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Running);
    assert!(job_table.get_job(1).unwrap().is_active());
    
    // First process exits
    job_table.update_job_status(1234, JobStatus::Done(0));
    
    // Now the job should be Done
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Done(0));
    assert!(!job_table.get_job(1).unwrap().is_active());
}

#[test]
fn test_pipeline_job_exit_code_from_last_process() {
    // POSIX behavior: pipeline exit code is the exit code of the last command
    let mut job_table = JobTable::new();
    
    let job = Job::new(1, Some(1234), "false | true &".to_string(), vec![1234, 1235], false);
    job_table.add_job(job);
    
    // First process exits with error
    job_table.update_job_status(1234, JobStatus::Done(1));
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Running);
    
    // Second process exits successfully
    job_table.update_job_status(1235, JobStatus::Done(0));
    
    // Job exit code should be from the last process (0, not 1)
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Done(0));
    assert_eq!(job_table.get_job(1).unwrap().exit_code, Some(0));
}

#[test]
fn test_pipeline_job_exit_code_last_process_fails() {
    // Test that last process exit code is used even when it fails
    let mut job_table = JobTable::new();
    
    let job = Job::new(1, Some(1234), "true | false &".to_string(), vec![1234, 1235], false);
    job_table.add_job(job);
    
    // First process exits successfully
    job_table.update_job_status(1234, JobStatus::Done(0));
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Running);
    
    // Second process exits with error
    job_table.update_job_status(1235, JobStatus::Done(1));
    
    // Job exit code should be from the last process (1, not 0)
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Done(1));
    assert_eq!(job_table.get_job(1).unwrap().exit_code, Some(1));
}

#[test]
fn test_pipeline_job_with_stopped_process() {
    // Test pipeline where one process is stopped but another is still running
    let mut job_table = JobTable::new();
    
    let job = Job::new(1, Some(1234), "sleep 10 | sleep 20 &".to_string(), vec![1234, 1235], false);
    job_table.add_job(job);
    
    // First process is stopped (SIGTSTP), but second is still running
    job_table.update_job_status(1234, JobStatus::Stopped);
    
    // Job should still be Running because second process is running
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Running);
    assert!(job_table.get_job(1).unwrap().is_active());
    
    // Now stop the second process too
    job_table.update_job_status(1235, JobStatus::Stopped);
    
    // Now the job should be Stopped (all processes stopped)
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Stopped);
    assert!(job_table.get_job(1).unwrap().is_active());
}

#[test]
fn test_pipeline_job_stopped_then_resumed() {
    // Test pipeline where a process is stopped and then resumed
    let mut job_table = JobTable::new();
    
    let job = Job::new(1, Some(1234), "sleep 10 | sleep 20 &".to_string(), vec![1234, 1235], false);
    job_table.add_job(job);
    
    // First process is stopped, but second is still running
    job_table.update_job_status(1234, JobStatus::Stopped);
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Running);
    
    // First process is resumed
    job_table.update_job_status(1234, JobStatus::Running);
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Running);
    
    // Both processes exit
    job_table.update_job_status(1234, JobStatus::Done(0));
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Running);
    
    job_table.update_job_status(1235, JobStatus::Done(0));
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Done(0));
}

#[test]
fn test_pipeline_job_mixed_stopped_and_done() {
    // Test pipeline where some processes are stopped and others are done
    let mut job_table = JobTable::new();
    
    let job = Job::new(1, Some(1234), "sleep 10 | sleep 20 | sleep 30 &".to_string(),
                       vec![1234, 1235, 1236], false);
    job_table.add_job(job);
    
    // First process exits
    job_table.update_job_status(1234, JobStatus::Done(0));
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Running);
    
    // Second process is stopped, but third is still running
    job_table.update_job_status(1235, JobStatus::Stopped);
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Running);
    
    // Third process exits
    job_table.update_job_status(1236, JobStatus::Done(0));
    
    // Now job should be Stopped (first is done, second is stopped, third is done)
    // Since not all are done and at least one is stopped, job is Stopped
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Stopped);
}

#[test]
fn test_pipeline_job_update_nonexistent_pid() {
    // Test that updating a PID not in the job returns false
    let mut job_table = JobTable::new();
    
    let job = Job::new(1, Some(1234), "sleep 10 | sleep 20 &".to_string(), vec![1234, 1235], false);
    job_table.add_job(job);
    
    // Try to update a PID that doesn't belong to this job
    let updated = job_table.update_job_status(9999, JobStatus::Done(0));
    assert!(!updated);
    
    // Job status should be unchanged
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Running);
}

#[test]
fn test_pipeline_job_large_pipeline() {
    // Test a larger pipeline with 5 processes
    let mut job_table = JobTable::new();
    
    let pids = vec![1234, 1235, 1236, 1237, 1238];
    let job = Job::new(1, Some(1234), "cmd1 | cmd2 | cmd3 | cmd4 | cmd5 &".to_string(),
                       pids.clone(), false);
    job_table.add_job(job);
    
    // Exit processes in random order
    job_table.update_job_status(1236, JobStatus::Done(0)); // 3rd
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Running);
    
    job_table.update_job_status(1234, JobStatus::Done(0)); // 1st
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Running);
    
    job_table.update_job_status(1238, JobStatus::Done(0)); // 5th (last)
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Running);
    
    job_table.update_job_status(1237, JobStatus::Done(0)); // 4th
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Running);
    
    job_table.update_job_status(1235, JobStatus::Done(0)); // 2nd (final)
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Done(0));
}

#[test]
fn test_pipeline_job_multiple_pipelines() {
    // Test multiple pipeline jobs running concurrently
    let mut job_table = JobTable::new();
    
    let job1 = Job::new(1, Some(1234), "sleep 5 | sleep 10 &".to_string(), vec![1234, 1235], false);
    let job2 = Job::new(2, Some(1236), "cat | grep | wc &".to_string(), vec![1236, 1237, 1238], false);
    
    job_table.add_job(job1);
    job_table.add_job(job2);
    
    // Complete first job
    job_table.update_job_status(1234, JobStatus::Done(0));
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Running);
    assert_eq!(job_table.get_job(2).unwrap().status, JobStatus::Running);
    
    job_table.update_job_status(1235, JobStatus::Done(0));
    assert_eq!(job_table.get_job(1).unwrap().status, JobStatus::Done(0));
    assert_eq!(job_table.get_job(2).unwrap().status, JobStatus::Running);
    
    // Complete second job
    job_table.update_job_status(1236, JobStatus::Done(0));
    job_table.update_job_status(1237, JobStatus::Done(0));
    assert_eq!(job_table.get_job(2).unwrap().status, JobStatus::Running);
    
    job_table.update_job_status(1238, JobStatus::Done(0));
    assert_eq!(job_table.get_job(2).unwrap().status, JobStatus::Done(0));
}

#[test]
fn test_pipeline_job_update_pid_status_directly() {
    // Test the update_pid_status method directly on a Job
    let mut job = Job::new(1, Some(1234), "sleep 5 | sleep 10 &".to_string(), vec![1234, 1235], false);
    
    assert_eq!(job.status, JobStatus::Running);
    
    // Update first PID
    assert!(job.update_pid_status(1234, JobStatus::Done(0)));
    assert_eq!(job.status, JobStatus::Running);
    
    // Update second PID
    assert!(job.update_pid_status(1235, JobStatus::Done(0)));
    assert_eq!(job.status, JobStatus::Done(0));
    
    // Try to update non-existent PID
    assert!(!job.update_pid_status(9999, JobStatus::Done(0)));
}

#[test]
fn test_pipeline_job_builtin_no_pids() {
    // Test that builtin jobs (no PIDs) still work correctly
    let mut job = Job::new(1, None, "sleep 10 &".to_string(), vec![], true);
    
    assert_eq!(job.status, JobStatus::Running);
    
    // Update status directly (not via PID)
    job.update_status(JobStatus::Done(0));
    assert_eq!(job.status, JobStatus::Done(0));
    assert_eq!(job.exit_code, Some(0));
}