Skip to main content

blyte_cli/
wait.rs

1use std::collections::HashSet;
2use std::io::Write;
3use std::path::Path;
4use std::time::{Duration, Instant};
5
6pub fn run(ids: &[String], any: bool, timeout_secs: u64) -> i32 {
7    let root = match crate::clean::find_repo_root() {
8        Ok(r) => r,
9        Err(e) => {
10            eprintln!("error: {e}");
11            return 2;
12        }
13    };
14    wait_in(
15        &root.join(".wt"),
16        ids,
17        any,
18        Duration::from_secs(timeout_secs),
19        Duration::from_secs(2),
20        Duration::from_secs(15),
21        &mut std::io::stdout(),
22        &mut std::io::stderr(),
23    )
24}
25
26fn try_read_summary(wt: &Path, id: &str) -> Option<serde_json::Value> {
27    let path = wt.join(format!("{id}.json"));
28    let s = std::fs::read_to_string(path).ok()?;
29    serde_json::from_str(&s).ok()
30}
31
32fn read_start_pid(path: &Path) -> Option<libc::pid_t> {
33    let s = std::fs::read_to_string(path).ok()?;
34    let val: serde_json::Value = serde_json::from_str(&s).ok()?;
35    val.get("pid")
36        .and_then(|v| v.as_i64())
37        .map(|v| v as libc::pid_t)
38}
39
40fn is_pid_alive(pid: libc::pid_t) -> bool {
41    if pid <= 0 {
42        return false;
43    }
44    let ret = unsafe { libc::kill(pid, 0) };
45    if ret == 0 {
46        true
47    } else {
48        std::io::Error::last_os_error().raw_os_error() == Some(libc::EPERM)
49    }
50}
51
52fn format_summary_line(id: &str, summary: &serde_json::Value) -> String {
53    let outcome = summary
54        .get("outcome")
55        .and_then(|v| v.as_str())
56        .unwrap_or("?");
57    let turns = summary
58        .get("turns")
59        .and_then(|v| {
60            v.as_i64()
61                .map(|t| t.to_string())
62                .or_else(|| v.as_u64().map(|t| t.to_string()))
63        })
64        .unwrap_or_else(|| "?".to_string());
65    let cost = match summary.get("cost_usd").and_then(|v| v.as_f64()) {
66        Some(c) => format!("${:.2}", c),
67        None => "-".to_string(),
68    };
69    format!("{id} {outcome} {turns} {cost}")
70}
71
72enum IdStatus {
73    Finished(String),
74    Running,
75    LooksDead,
76    Unknown,
77}
78
79fn check_id_status(wt: &Path, id: &str) -> IdStatus {
80    // 1. finished: wt/<id>.json exists and parses as JSON
81    if let Some(summary) = try_read_summary(wt, id) {
82        return IdStatus::Finished(format_summary_line(id, &summary));
83    }
84
85    // 2. running: wt/<id>.start.json exists, its pid is > 0 and alive
86    let start_path = wt.join(format!("{id}.start.json"));
87    if start_path.exists() {
88        if let Some(pid) = read_start_pid(&start_path) {
89            if is_pid_alive(pid) {
90                return IdStatus::Running;
91            }
92        }
93
94        // 3. dead: otherwise, if wt/<id>.start.json exists. Before declaring a run dead, check
95        // wt/<id>.json once more (the run may have written its summary and exited between the two checks);
96        // if it now exists the run is finished.
97        if let Some(summary) = try_read_summary(wt, id) {
98            return IdStatus::Finished(format_summary_line(id, &summary));
99        }
100        return IdStatus::LooksDead;
101    }
102
103    IdStatus::Unknown
104}
105
106fn is_known(wt: &Path, id: &str) -> bool {
107    let has_summary = try_read_summary(wt, id).is_some();
108    let has_start = wt.join(format!("{id}.start.json")).exists();
109    has_summary || has_start
110}
111
112#[allow(clippy::too_many_arguments)]
113pub fn wait_in(
114    wt: &Path,
115    ids: &[String],
116    any: bool,
117    timeout: Duration,
118    poll: Duration,
119    grace: Duration,
120    out: &mut impl Write,
121    err: &mut impl Write,
122) -> i32 {
123    let mut unique_ids: Vec<&str> = Vec::new();
124    let mut seen = HashSet::new();
125    for id in ids {
126        if seen.insert(id.as_str()) {
127            unique_ids.push(id.as_str());
128        }
129    }
130
131    if unique_ids.is_empty() {
132        return 0;
133    }
134
135    let start_time = Instant::now();
136    let mut printed = vec![false; unique_ids.len()];
137    let mut first_dead = vec![None; unique_ids.len()];
138    let mut printed_count = 0;
139
140    loop {
141        let mut printed_this_poll = false;
142        for (i, id) in unique_ids.iter().enumerate() {
143            if printed[i] {
144                continue;
145            }
146            match check_id_status(wt, id) {
147                IdStatus::Finished(line) => {
148                    let _ = writeln!(out, "{line}");
149                    let _ = out.flush();
150                    printed[i] = true;
151                    first_dead[i] = None;
152                    printed_count += 1;
153                    printed_this_poll = true;
154                }
155                IdStatus::Running => {
156                    first_dead[i] = None;
157                }
158                IdStatus::LooksDead => {
159                    let first_seen = *first_dead[i].get_or_insert_with(Instant::now);
160                    if grace.is_zero() || first_seen.elapsed() >= grace {
161                        let _ = writeln!(out, "{id} dead - -");
162                        let _ = out.flush();
163                        printed[i] = true;
164                        first_dead[i] = None;
165                        printed_count += 1;
166                        printed_this_poll = true;
167                    }
168                }
169                IdStatus::Unknown => {
170                    first_dead[i] = None;
171                }
172            }
173        }
174
175        if printed_count == unique_ids.len() {
176            return 0;
177        }
178
179        if any && printed_this_poll {
180            return 0;
181        }
182
183        let elapsed = start_time.elapsed();
184
185        if elapsed >= timeout && timeout < grace {
186            let unprinted: Vec<&str> = unique_ids
187                .iter()
188                .enumerate()
189                .filter(|(i, _)| !printed[*i])
190                .map(|(_, id)| *id)
191                .collect();
192            let _ = writeln!(err, "still running: {}", unprinted.join(" "));
193            let _ = err.flush();
194            return 2;
195        }
196
197        if elapsed >= grace {
198            let unknown_ids: Vec<&str> = unique_ids
199                .iter()
200                .enumerate()
201                .filter(|(i, id)| !printed[*i] && !is_known(wt, id))
202                .map(|(_, id)| *id)
203                .collect();
204            if !unknown_ids.is_empty() {
205                for id in unknown_ids {
206                    let _ = writeln!(err, "no dispatch {id} here");
207                }
208                let _ = err.flush();
209                return 1;
210            }
211        }
212
213        if elapsed >= timeout {
214            let unprinted: Vec<&str> = unique_ids
215                .iter()
216                .enumerate()
217                .filter(|(i, _)| !printed[*i])
218                .map(|(_, id)| *id)
219                .collect();
220            let _ = writeln!(err, "still running: {}", unprinted.join(" "));
221            let _ = err.flush();
222            return 2;
223        }
224
225        let remaining = timeout.saturating_sub(start_time.elapsed());
226        let sleep_dur = poll.min(remaining);
227        std::thread::sleep(sleep_dur);
228    }
229}
230
231#[cfg(test)]
232mod tests {
233    use super::*;
234
235    #[test]
236    fn finished_run_prints_summary_line() {
237        let dir = tempfile::tempdir().unwrap();
238        let wt = dir.path();
239        std::fs::write(
240            wt.join("T1.json"),
241            r#"{"outcome":"verified","turns":41,"cost_usd":0.374}"#,
242        )
243        .unwrap();
244        let mut out = Vec::new();
245        let mut err = Vec::new();
246        let code = wait_in(
247            wt,
248            &["T1".to_string()],
249            false,
250            Duration::from_secs(5),
251            Duration::from_millis(10),
252            Duration::ZERO,
253            &mut out,
254            &mut err,
255        );
256        assert_eq!(code, 0);
257        assert_eq!(String::from_utf8(out).unwrap(), "T1 verified 41 $0.37\n");
258        assert!(err.is_empty());
259    }
260
261    #[test]
262    fn missing_cost_prints_dash() {
263        let dir = tempfile::tempdir().unwrap();
264        let wt = dir.path();
265        std::fs::write(
266            wt.join("T1.json"),
267            r#"{"outcome":"gate_failed","turns":7,"cost_usd":null}"#,
268        )
269        .unwrap();
270        let mut out = Vec::new();
271        let mut err = Vec::new();
272        let code = wait_in(
273            wt,
274            &["T1".to_string()],
275            false,
276            Duration::from_secs(5),
277            Duration::from_millis(10),
278            Duration::ZERO,
279            &mut out,
280            &mut err,
281        );
282        assert_eq!(code, 0);
283        assert_eq!(String::from_utf8(out).unwrap(), "T1 gate_failed 7 -\n");
284        assert!(err.is_empty());
285    }
286
287    #[test]
288    fn dead_run() {
289        let dir = tempfile::tempdir().unwrap();
290        let wt = dir.path();
291        let mut child = std::process::Command::new("true").spawn().unwrap();
292        let pid = child.id();
293        child.wait().unwrap();
294        std::fs::write(wt.join("T1.start.json"), format!(r#"{{"pid": {pid}}}"#)).unwrap();
295        let mut out = Vec::new();
296        let mut err = Vec::new();
297        let code = wait_in(
298            wt,
299            &["T1".to_string()],
300            false,
301            Duration::from_secs(5),
302            Duration::from_millis(10),
303            Duration::ZERO,
304            &mut out,
305            &mut err,
306        );
307        assert_eq!(code, 0);
308        assert_eq!(String::from_utf8(out).unwrap(), "T1 dead - -\n");
309        assert!(err.is_empty());
310    }
311
312    #[test]
313    fn zero_pid_is_dead() {
314        let dir = tempfile::tempdir().unwrap();
315        let wt = dir.path();
316        std::fs::write(wt.join("T1.start.json"), r#"{"pid": 0}"#).unwrap();
317        let mut out = Vec::new();
318        let mut err = Vec::new();
319        let code = wait_in(
320            wt,
321            &["T1".to_string()],
322            false,
323            Duration::from_secs(5),
324            Duration::from_millis(10),
325            Duration::ZERO,
326            &mut out,
327            &mut err,
328        );
329        assert_eq!(code, 0);
330        assert_eq!(String::from_utf8(out).unwrap(), "T1 dead - -\n");
331        assert!(err.is_empty());
332    }
333
334    #[test]
335    fn unknown_id_errors_without_waiting() {
336        let dir = tempfile::tempdir().unwrap();
337        let wt = dir.path();
338        let mut out = Vec::new();
339        let mut err = Vec::new();
340        let start = Instant::now();
341        let code = wait_in(
342            wt,
343            &["T9".to_string()],
344            false,
345            Duration::from_secs(5),
346            Duration::from_millis(10),
347            Duration::from_millis(50),
348            &mut out,
349            &mut err,
350        );
351        let elapsed = start.elapsed();
352        assert_eq!(code, 1);
353        let err_str = String::from_utf8(err).unwrap();
354        assert!(err_str.contains("no dispatch T9 here"));
355        assert!(out.is_empty());
356        assert!(elapsed < Duration::from_secs(1));
357    }
358
359    #[test]
360    fn timeout_with_running_run() {
361        let dir = tempfile::tempdir().unwrap();
362        let wt = dir.path();
363        let pid = std::process::id();
364        std::fs::write(wt.join("T1.start.json"), format!(r#"{{"pid": {pid}}}"#)).unwrap();
365        let mut out = Vec::new();
366        let mut err = Vec::new();
367        let code = wait_in(
368            wt,
369            &["T1".to_string()],
370            false,
371            Duration::from_millis(50),
372            Duration::from_millis(10),
373            Duration::ZERO,
374            &mut out,
375            &mut err,
376        );
377        assert_eq!(code, 2);
378        assert!(out.is_empty());
379        let err_str = String::from_utf8(err).unwrap();
380        assert!(err_str.contains("still running: T1"));
381    }
382
383    #[test]
384    fn all_mode_waits_for_every_run() {
385        let dir = tempfile::tempdir().unwrap();
386        let wt = dir.path();
387        std::fs::write(
388            wt.join("T1.json"),
389            r#"{"outcome":"verified","turns":41,"cost_usd":0.374}"#,
390        )
391        .unwrap();
392        let pid = std::process::id();
393        std::fs::write(wt.join("T2.start.json"), format!(r#"{{"pid": {pid}}}"#)).unwrap();
394        let wt_clone = wt.to_path_buf();
395        std::thread::spawn(move || {
396            std::thread::sleep(Duration::from_millis(100));
397            std::fs::write(
398                wt_clone.join("T2.json"),
399                r#"{"outcome":"verified","turns":10,"cost_usd":0.10}"#,
400            )
401            .unwrap();
402        });
403        let mut out = Vec::new();
404        let mut err = Vec::new();
405        let code = wait_in(
406            wt,
407            &["T1".to_string(), "T2".to_string()],
408            false,
409            Duration::from_secs(5),
410            Duration::from_millis(10),
411            Duration::ZERO,
412            &mut out,
413            &mut err,
414        );
415        assert_eq!(code, 0);
416        let out_str = String::from_utf8(out).unwrap();
417        assert_eq!(out_str, "T1 verified 41 $0.37\nT2 verified 10 $0.10\n");
418        assert!(err.is_empty());
419    }
420
421    #[test]
422    fn any_mode_returns_after_first() {
423        let dir = tempfile::tempdir().unwrap();
424        let wt = dir.path();
425        let pid = std::process::id();
426        std::fs::write(wt.join("T1.start.json"), format!(r#"{{"pid": {pid}}}"#)).unwrap();
427        std::fs::write(
428            wt.join("T2.json"),
429            r#"{"outcome":"verified","turns":12,"cost_usd":0.20}"#,
430        )
431        .unwrap();
432        let mut out = Vec::new();
433        let mut err = Vec::new();
434        let code = wait_in(
435            wt,
436            &["T1".to_string(), "T2".to_string()],
437            true,
438            Duration::from_secs(5),
439            Duration::from_millis(10),
440            Duration::ZERO,
441            &mut out,
442            &mut err,
443        );
444        assert_eq!(code, 0);
445        assert_eq!(String::from_utf8(out).unwrap(), "T2 verified 12 $0.20\n");
446        assert!(err.is_empty());
447    }
448
449    #[test]
450    fn any_mode_prints_all_already_finished() {
451        let dir = tempfile::tempdir().unwrap();
452        let wt = dir.path();
453        std::fs::write(
454            wt.join("T1.json"),
455            r#"{"outcome":"verified","turns":1,"cost_usd":0.01}"#,
456        )
457        .unwrap();
458        std::fs::write(
459            wt.join("T2.json"),
460            r#"{"outcome":"gate_failed","turns":2,"cost_usd":0.02}"#,
461        )
462        .unwrap();
463        let mut out = Vec::new();
464        let mut err = Vec::new();
465        let code = wait_in(
466            wt,
467            &["T1".to_string(), "T2".to_string()],
468            true,
469            Duration::from_secs(5),
470            Duration::from_millis(10),
471            Duration::ZERO,
472            &mut out,
473            &mut err,
474        );
475        assert_eq!(code, 0);
476        let out_str = String::from_utf8(out).unwrap();
477        let lines: Vec<&str> = out_str.lines().collect();
478        assert_eq!(lines, vec!["T1 verified 1 $0.01", "T2 gate_failed 2 $0.02"]);
479        assert!(err.is_empty());
480    }
481
482    #[test]
483    fn any_mode_timeout() {
484        let dir = tempfile::tempdir().unwrap();
485        let wt = dir.path();
486        let pid = std::process::id();
487        std::fs::write(wt.join("T1.start.json"), format!(r#"{{"pid": {pid}}}"#)).unwrap();
488        let mut out = Vec::new();
489        let mut err = Vec::new();
490        let code = wait_in(
491            wt,
492            &["T1".to_string()],
493            true,
494            Duration::from_millis(50),
495            Duration::from_millis(10),
496            Duration::ZERO,
497            &mut out,
498            &mut err,
499        );
500        assert_eq!(code, 2);
501        assert!(out.is_empty());
502        let err_str = String::from_utf8(err).unwrap();
503        assert!(err_str.contains("still running: T1"));
504    }
505
506    #[test]
507    fn summary_written_after_start_counts_as_finished() {
508        let dir = tempfile::tempdir().unwrap();
509        let wt = dir.path();
510        let mut child = std::process::Command::new("true").spawn().unwrap();
511        let pid = child.id();
512        child.wait().unwrap();
513        std::fs::write(wt.join("T1.start.json"), format!(r#"{{"pid": {pid}}}"#)).unwrap();
514        std::fs::write(
515            wt.join("T1.json"),
516            r#"{"outcome":"verified","turns":5,"cost_usd":0.05}"#,
517        )
518        .unwrap();
519        let mut out = Vec::new();
520        let mut err = Vec::new();
521        let code = wait_in(
522            wt,
523            &["T1".to_string()],
524            false,
525            Duration::from_secs(5),
526            Duration::from_millis(10),
527            Duration::ZERO,
528            &mut out,
529            &mut err,
530        );
531        assert_eq!(code, 0);
532        assert_eq!(String::from_utf8(out).unwrap(), "T1 verified 5 $0.05\n");
533        assert!(err.is_empty());
534    }
535
536    #[test]
537    fn unparseable_summary_is_not_finished() {
538        let dir = tempfile::tempdir().unwrap();
539        let wt = dir.path();
540        std::fs::write(wt.join("T1.json"), r#"{"outc"#).unwrap();
541        let pid = std::process::id();
542        std::fs::write(wt.join("T1.start.json"), format!(r#"{{"pid": {pid}}}"#)).unwrap();
543        let mut out = Vec::new();
544        let mut err = Vec::new();
545        let code = wait_in(
546            wt,
547            &["T1".to_string()],
548            false,
549            Duration::from_millis(50),
550            Duration::from_millis(10),
551            Duration::ZERO,
552            &mut out,
553            &mut err,
554        );
555        assert_eq!(code, 2);
556        assert!(out.is_empty());
557        let err_str = String::from_utf8(err).unwrap();
558        assert!(err_str.contains("still running: T1"));
559    }
560
561    #[test]
562    fn duplicate_ids_reported_once() {
563        let dir = tempfile::tempdir().unwrap();
564        let wt = dir.path();
565        std::fs::write(
566            wt.join("T1.json"),
567            r#"{"outcome":"verified","turns":41,"cost_usd":0.374}"#,
568        )
569        .unwrap();
570        let mut out = Vec::new();
571        let mut err = Vec::new();
572        let code = wait_in(
573            wt,
574            &["T1".to_string(), "T1".to_string()],
575            false,
576            Duration::from_secs(5),
577            Duration::from_millis(10),
578            Duration::ZERO,
579            &mut out,
580            &mut err,
581        );
582        assert_eq!(code, 0);
583        assert_eq!(String::from_utf8(out).unwrap(), "T1 verified 41 $0.37\n");
584        assert!(err.is_empty());
585    }
586
587    #[test]
588    fn finishing_run_within_grace_reports_summary() {
589        let dir = tempfile::tempdir().unwrap();
590        let wt = dir.path();
591        let mut child = std::process::Command::new("true").spawn().unwrap();
592        let pid = child.id();
593        child.wait().unwrap();
594        std::fs::write(wt.join("T1.start.json"), format!(r#"{{"pid": {pid}}}"#)).unwrap();
595        let wt_clone = wt.to_path_buf();
596        std::thread::spawn(move || {
597            std::thread::sleep(Duration::from_millis(100));
598            std::fs::write(
599                wt_clone.join("T1.json"),
600                r#"{"outcome":"verified","turns":3,"cost_usd":0.1}"#,
601            )
602            .unwrap();
603        });
604        let mut out = Vec::new();
605        let mut err = Vec::new();
606        let code = wait_in(
607            wt,
608            &["T1".to_string()],
609            false,
610            Duration::from_secs(5),
611            Duration::from_millis(10),
612            Duration::from_secs(2),
613            &mut out,
614            &mut err,
615        );
616        assert_eq!(code, 0);
617        assert_eq!(String::from_utf8(out).unwrap(), "T1 verified 3 $0.10\n");
618        assert!(err.is_empty());
619    }
620
621    #[test]
622    fn dead_run_reported_after_grace() {
623        let dir = tempfile::tempdir().unwrap();
624        let wt = dir.path();
625        let mut child = std::process::Command::new("true").spawn().unwrap();
626        let pid = child.id();
627        child.wait().unwrap();
628        std::fs::write(wt.join("T1.start.json"), format!(r#"{{"pid": {pid}}}"#)).unwrap();
629        let mut out = Vec::new();
630        let mut err = Vec::new();
631        let start = Instant::now();
632        let code = wait_in(
633            wt,
634            &["T1".to_string()],
635            false,
636            Duration::from_secs(5),
637            Duration::from_millis(10),
638            Duration::from_millis(150),
639            &mut out,
640            &mut err,
641        );
642        let elapsed = start.elapsed();
643        assert_eq!(code, 0);
644        assert_eq!(String::from_utf8(out).unwrap(), "T1 dead - -\n");
645        assert!(elapsed >= Duration::from_millis(150));
646        assert!(err.is_empty());
647    }
648
649    #[test]
650    fn timeout_inside_grace_is_still_running() {
651        let dir = tempfile::tempdir().unwrap();
652        let wt = dir.path();
653        let mut child = std::process::Command::new("true").spawn().unwrap();
654        let pid = child.id();
655        child.wait().unwrap();
656        std::fs::write(wt.join("T1.start.json"), format!(r#"{{"pid": {pid}}}"#)).unwrap();
657        let mut out = Vec::new();
658        let mut err = Vec::new();
659        let code = wait_in(
660            wt,
661            &["T1".to_string()],
662            false,
663            Duration::from_millis(100),
664            Duration::from_millis(10),
665            Duration::from_secs(5),
666            &mut out,
667            &mut err,
668        );
669        assert_eq!(code, 2);
670        assert!(out.is_empty());
671        let err_str = String::from_utf8(err).unwrap();
672        assert!(err_str.contains("still running: T1"));
673    }
674
675    #[test]
676    fn summary_without_start_file_written_after_start() {
677        let dir = tempfile::tempdir().unwrap();
678        let wt = dir.path();
679        let wt_clone = wt.to_path_buf();
680        std::thread::spawn(move || {
681            std::thread::sleep(Duration::from_millis(100));
682            std::fs::write(
683                wt_clone.join("T1.json"),
684                r#"{"outcome":"verified","turns":15,"cost_usd":0.25}"#,
685            )
686            .unwrap();
687        });
688        let mut out = Vec::new();
689        let mut err = Vec::new();
690        let code = wait_in(
691            wt,
692            &["T1".to_string()],
693            false,
694            Duration::from_secs(5),
695            Duration::from_millis(10),
696            Duration::from_secs(2),
697            &mut out,
698            &mut err,
699        );
700        assert_eq!(code, 0);
701        assert_eq!(String::from_utf8(out).unwrap(), "T1 verified 15 $0.25\n");
702        assert!(err.is_empty());
703    }
704
705    #[test]
706    fn one_summary_one_never_appearing() {
707        let dir = tempfile::tempdir().unwrap();
708        let wt = dir.path();
709        std::fs::write(
710            wt.join("T1.json"),
711            r#"{"outcome":"verified","turns":10,"cost_usd":0.10}"#,
712        )
713        .unwrap();
714        let mut out = Vec::new();
715        let mut err = Vec::new();
716        let code = wait_in(
717            wt,
718            &["T1".to_string(), "T2".to_string()],
719            false,
720            Duration::from_secs(5),
721            Duration::from_millis(10),
722            Duration::from_millis(50),
723            &mut out,
724            &mut err,
725        );
726        assert_eq!(code, 1);
727        let err_str = String::from_utf8(err).unwrap();
728        assert!(err_str.contains("no dispatch T2 here"));
729        assert!(!err_str.contains("no dispatch T1 here"));
730        assert_eq!(String::from_utf8(out).unwrap(), "T1 verified 10 $0.10\n");
731    }
732
733    #[test]
734    fn timeout_before_grace_with_unknown_id() {
735        let dir = tempfile::tempdir().unwrap();
736        let wt = dir.path();
737        let mut out = Vec::new();
738        let mut err = Vec::new();
739        let code = wait_in(
740            wt,
741            &["T9".to_string()],
742            false,
743            Duration::from_millis(50),
744            Duration::from_millis(10),
745            Duration::from_secs(5),
746            &mut out,
747            &mut err,
748        );
749        assert_eq!(code, 2);
750        assert!(out.is_empty());
751        let err_str = String::from_utf8(err).unwrap();
752        assert!(err_str.contains("still running: T9"));
753    }
754
755    #[test]
756    fn multiple_unknown_ids_reported_in_argument_order() {
757        let dir = tempfile::tempdir().unwrap();
758        let wt = dir.path();
759        let mut out = Vec::new();
760        let mut err = Vec::new();
761        let code = wait_in(
762            wt,
763            &["B".to_string(), "A".to_string()],
764            false,
765            Duration::from_secs(5),
766            Duration::from_millis(10),
767            Duration::from_millis(50),
768            &mut out,
769            &mut err,
770        );
771        assert_eq!(code, 1);
772        assert!(out.is_empty());
773        let err_str = String::from_utf8(err).unwrap();
774        assert_eq!(err_str, "no dispatch B here\nno dispatch A here\n");
775    }
776
777    #[test]
778    fn cli_parses_wait() {
779        use crate::cli::{Cli, Command};
780        use clap::Parser;
781
782        let cli =
783            Cli::try_parse_from(["blyte", "wait", "A", "B", "--any", "--timeout", "10"]).unwrap();
784        assert_eq!(
785            cli.command,
786            Some(Command::Wait {
787                ids: vec!["A".to_string(), "B".to_string()],
788                any: true,
789                timeout: 10,
790            })
791        );
792
793        let cli = Cli::try_parse_from(["blyte", "wait", "A"]).unwrap();
794        assert_eq!(
795            cli.command,
796            Some(Command::Wait {
797                ids: vec!["A".to_string()],
798                any: false,
799                timeout: 280,
800            })
801        );
802
803        assert!(Cli::try_parse_from(["blyte", "wait"]).is_err());
804    }
805}