rskim 2.3.1

The most intelligent context optimization engine for coding agents. Code-aware AST parsing, command rewriting, output compression.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
//! Pytest parser with three-tier degradation (#47)
//!
//! Parses pytest text output into structured [`TestResult`] using a three-tier
//! strategy:
//!
//! - **Tier 1 (text state machine):** Scans all output lines, counting PASSED/FAILED/
//!   SKIPPED/ERROR outcomes and extracting individual test names. Requires the summary
//!   line to produce a `Full` result.
//! - **Tier 2 (passthrough):** Returns raw output unmodified when no summary can be
//!   found at all.
//!
//! ## Usage
//!
//! ```text
//! skim test pytest [args...]          # Execute pytest, parse output
//! pytest ... | skim test pytest       # Parse piped stdin
//! ```

use std::collections::HashSet;
use std::io::{self, IsTerminal, Read};
use std::process::ExitCode;
use std::sync::LazyLock;
use std::time::Duration;

use regex::Regex;

use crate::cmd::{combine_output, user_has_flag};
use crate::output::canonical::{TestEntry, TestOutcome, TestResult, TestSummary};
use crate::output::{strip_ansi, ParseResult};
use crate::runner::{CommandOutput, CommandRunner};

// ============================================================================
// Public entry point
// ============================================================================

/// Run pytest and parse its output, or parse piped stdin.
///
/// Detection logic:
/// - If stdin is a terminal → run pytest (execution mode)
/// - If stdin is not a terminal → attempt to read stdin; if empty, fall back
///   to running pytest (handles test harness environments where stdin is a
///   pipe with no data)
pub(crate) fn run(args: &[String], show_stats: bool) -> anyhow::Result<ExitCode> {
    // Intercept --help/-h: show skim's pytest help, then forward to real pytest
    // so the user sees both skim's flags and pytest's own options.
    if args.iter().any(|a| matches!(a.as_str(), "--help" | "-h")) {
        print_pytest_help();
    }

    let output = if io::stdin().is_terminal() {
        // Terminal: always run pytest
        let final_args = build_args(args);
        let arg_refs: Vec<&str> = final_args.iter().map(String::as_str).collect();
        run_pytest(&arg_refs)?
    } else {
        // Pipe: read stdin, fall back to execution if empty
        let stdin_output = read_stdin()?;
        if stdin_output.stdout.trim().is_empty() {
            // Empty pipe (e.g., test harness) — run pytest instead
            let final_args = build_args(args);
            let arg_refs: Vec<&str> = final_args.iter().map(String::as_str).collect();
            run_pytest(&arg_refs)?
        } else {
            stdin_output
        }
    };

    let combined = combine_output(&output);
    // Strip ANSI escape codes before parsing so color sequences (e.g.,
    // `pytest --color=yes`) do not interfere with string matching.
    let cleaned = strip_ansi(&combined);
    let result = parse(&cleaned);

    emit_result(&result, &output)?;

    if show_stats {
        let (orig, comp) = crate::process::count_token_pair(&cleaned, result.content());
        crate::process::report_token_stats(orig, comp, "");
    }

    // Record analytics (fire-and-forget, non-blocking).
    // Guard to avoid .to_string() allocation when analytics are disabled.
    if crate::analytics::is_analytics_enabled() {
        crate::analytics::try_record_command(
            cleaned,
            result.content().to_string(),
            format!("skim test pytest {}", args.join(" ")),
            crate::analytics::CommandType::Test,
            output.duration,
            Some(result.tier_name()),
        );
    }

    // Exit code: mirror pytest's exit code if we ran it, or infer from parse
    let code = match output.exit_code {
        Some(0) => ExitCode::SUCCESS,
        Some(_) => ExitCode::FAILURE,
        None => {
            // Piped or signal-killed: infer from parse result
            match &result {
                ParseResult::Full(tr) | ParseResult::Degraded(tr, _) => {
                    if tr.summary.fail > 0 {
                        ExitCode::FAILURE
                    } else {
                        ExitCode::SUCCESS
                    }
                }
                ParseResult::Passthrough(_) => ExitCode::FAILURE,
            }
        }
    };

    Ok(code)
}

/// Print skim's pytest-specific help to stdout.
///
/// This is shown before forwarding `--help` to real pytest so the user
/// sees both skim's behavior and pytest's own flags.
fn print_pytest_help() {
    println!("skim test pytest [ARGS...]");
    println!();
    println!("  Run pytest and parse its output into a structured summary.");
    println!();
    println!("  BEHAVIOR:");
    println!("    - Injects --tb=short and -q unless you override them");
    println!("    - Parses output into PASS/FAIL/SKIP counts with failure details");
    println!("    - Supports piped input: pytest ... | skim test pytest");
    println!();
    println!("  FLAGS MANAGED BY SKIM:");
    println!("    --tb=short     Injected unless --tb is already set");
    println!("    -q             Injected unless -q/-v/--quiet/--verbose is set");
    println!();
    println!("--- pytest native help follows ---");
    println!();
}

// ============================================================================
// Arg building
// ============================================================================

/// Build the final argument list for pytest.
///
/// If the user hasn't set `--tb` or `-q`/`--quiet` or `-v`/`--verbose`,
/// inject `--tb=short` and `-q` for cleaner parseable output.
fn build_args(user_args: &[String]) -> Vec<String> {
    let mut args: Vec<String> = user_args.to_vec();

    if !user_has_flag(user_args, &["--tb"]) {
        args.push("--tb=short".to_string());
    }

    if !user_has_flag(user_args, &["-q", "--quiet", "-v", "--verbose"]) {
        args.push("-q".to_string());
    }

    args
}

// ============================================================================
// Command execution
// ============================================================================

/// Execute pytest with the given arguments.
fn run_pytest(args: &[&str]) -> anyhow::Result<CommandOutput> {
    let runner = CommandRunner::new(Some(Duration::from_secs(300)));
    runner
        .run("pytest", args)
        .map_err(|e| anyhow::anyhow!("{e}\n\nHint: Is pytest installed? Try: pip install pytest"))
}

/// Maximum bytes we will read from stdin (64 MiB).
///
/// Consistent with `CommandRunner::read_pipe`'s `MAX_OUTPUT_BYTES` limit.
const MAX_STDIN_BYTES: usize = 64 * 1024 * 1024;

/// Read stdin into a synthetic [`CommandOutput`], capped at [`MAX_STDIN_BYTES`].
///
/// Uses chunked reads (8 KiB) to enforce the size limit without requiring the
/// OS to report exact pipe length up-front.
fn read_stdin() -> anyhow::Result<CommandOutput> {
    let mut buf = Vec::new();
    let mut chunk = [0u8; 8 * 1024];
    let stdin = io::stdin();
    let mut handle = stdin.lock();
    loop {
        let n = handle.read(&mut chunk)?;
        if n == 0 {
            break;
        }
        if buf.len() + n > MAX_STDIN_BYTES {
            anyhow::bail!("stdin exceeded {} byte limit", MAX_STDIN_BYTES);
        }
        buf.extend_from_slice(&chunk[..n]);
    }
    Ok(CommandOutput {
        stdout: String::from_utf8_lossy(&buf).into_owned(),
        stderr: String::new(),
        exit_code: None,
        duration: Duration::ZERO,
    })
}

// ============================================================================
// Three-tier parser
// ============================================================================

/// Parse pytest output using three-tier degradation.
///
/// Returns `Full` if tier 1 succeeds, or `Passthrough` if no summary line is found.
fn parse(output: &str) -> ParseResult<TestResult> {
    // Tier 1: full text state machine
    if let Some(result) = tier1_parse(output) {
        return ParseResult::Full(result);
    }

    // Tier 2: passthrough
    ParseResult::Passthrough(output.to_string())
}

/// Regex matching the pytest summary line structure.
///
/// Matches lines like:
/// - `============================== 5 passed in 0.12s ===============================`
/// - `=== 3 failed in 0.15s ===`
/// - `============== 4 passed, 1 failed, 1 skipped in 0.20s =============`
/// - `1 failed, 2 error in 0.30s`
///
/// The pattern matches `in <duration>s` at the end, with optional `=` padding.
/// Individual counts (passed/failed/skipped/error) are extracted by a separate
/// per-pair regex so that "passed" is not required.
static SUMMARY_LINE_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"=*\s*(?:\d+\s+(?:passed|failed|skipped|error)(?:,\s+)?)+\s+in\s+([\d.]+)s\s*=*")
        .expect("summary line regex is valid")
});

/// Regex extracting individual `N category` pairs from a summary line.
static SUMMARY_PAIR_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"(\d+)\s+(passed|failed|skipped|error)").expect("summary pair regex is valid")
});

/// Parsed summary counts extracted from a pytest summary line.
struct SummaryCounts {
    pass: usize,
    fail: usize,
    skip: usize,
    duration_ms: Option<u64>,
}

/// Try to parse the pytest summary line, extracting counts and duration.
///
/// Returns `None` if the line does not match the summary pattern.
fn parse_summary_line(line: &str) -> Option<SummaryCounts> {
    let line_caps = SUMMARY_LINE_RE.captures(line)?;

    // Extract duration from the capture group
    let duration_ms = line_caps.get(1).and_then(|m| {
        let secs: f64 = m.as_str().parse().ok()?;
        Some((secs * 1000.0) as u64)
    });

    let mut pass: usize = 0;
    let mut fail: usize = 0;
    let mut skip: usize = 0;

    for caps in SUMMARY_PAIR_RE.captures_iter(line) {
        let count: usize = caps[1].parse().unwrap_or(0);
        match &caps[2] {
            "passed" => pass = count,
            "failed" => fail += count,
            "skipped" => skip = count,
            "error" => fail += count,
            _ => {}
        }
    }

    Some(SummaryCounts {
        pass,
        fail,
        skip,
        duration_ms,
    })
}

// ============================================================================
// Tier 1: Text state machine
// ============================================================================

/// Tier 1: Full text state machine parse.
///
/// Scans every line for PASSED/FAILED/SKIPPED/ERROR markers, extracts test names
/// from "short test summary" lines, collects failure output, and validates against
/// the summary line.
fn tier1_parse(output: &str) -> Option<TestResult> {
    let mut entries: Vec<TestEntry> = Vec::new();
    let mut in_failures = false;
    let mut in_summary_info = false;
    let mut current_failure_name: Option<String> = None;
    let mut current_failure_detail: Vec<String> = Vec::new();

    // Track summary values
    let mut summary_counts: Option<SummaryCounts> = None;

    for line in output.lines() {
        let trimmed = line.trim();

        // Detect summary line
        if let Some(counts) = parse_summary_line(trimmed) {
            summary_counts = Some(counts);
            continue;
        }

        // Detect FAILURES section header
        if trimmed.starts_with("===") && trimmed.contains("FAILURES") {
            in_failures = true;
            in_summary_info = false;
            continue;
        }

        // Detect "short test summary info" section
        if trimmed.starts_with("===") && trimmed.contains("short test summary info") {
            in_summary_info = true;
            in_failures = false;
            // Flush any pending failure
            flush_failure(
                &mut entries,
                &mut current_failure_name,
                &mut current_failure_detail,
            );
            continue;
        }

        // Detect any other section header (=== ... ===) that ends the current section
        if trimmed.starts_with("===") && trimmed.ends_with("===") {
            if in_failures {
                flush_failure(
                    &mut entries,
                    &mut current_failure_name,
                    &mut current_failure_detail,
                );
            }
            in_failures = false;
            in_summary_info = false;
            continue;
        }

        // Inside FAILURES section: extract individual test failure blocks
        if in_failures {
            // Test failure headers look like: "________ test_name ________"
            if trimmed.starts_with('_') && trimmed.ends_with('_') {
                // Flush previous failure
                flush_failure(
                    &mut entries,
                    &mut current_failure_name,
                    &mut current_failure_detail,
                );
                // Extract test name from between underscores
                let name = trimmed.trim_matches('_').trim().to_string();
                if !name.is_empty() {
                    current_failure_name = Some(name);
                }
            } else if current_failure_name.is_some() {
                current_failure_detail.push(line.to_string());
            }
            continue;
        }

        // Inside "short test summary info": parse FAILED/ERROR lines
        if in_summary_info {
            let rest = trimmed
                .strip_prefix("FAILED ")
                .or_else(|| trimmed.strip_prefix("ERROR "));
            if let Some(rest) = rest {
                // Format: "FAILED tests/test_b.py::test_two - assert 1 == 2"
                let (name, detail) = if let Some(dash_pos) = rest.find(" - ") {
                    (
                        rest[..dash_pos].to_string(),
                        Some(rest[dash_pos + 3..].to_string()),
                    )
                } else {
                    (rest.to_string(), None)
                };
                entries.push(TestEntry {
                    name,
                    outcome: TestOutcome::Fail,
                    detail,
                });
            }
            continue;
        }

        // Outside special sections: look for per-line PASSED/FAILED/SKIPPED markers
        // These appear in verbose mode output like:
        //   tests/test_a.py::test_one PASSED
        //   tests/test_a.py::test_two FAILED
        if trimmed.ends_with(" PASSED") {
            let name = trimmed.trim_end_matches(" PASSED").to_string();
            entries.push(TestEntry {
                name,
                outcome: TestOutcome::Pass,
                detail: None,
            });
        } else if trimmed.ends_with(" FAILED") {
            let name = trimmed.trim_end_matches(" FAILED").to_string();
            entries.push(TestEntry {
                name,
                outcome: TestOutcome::Fail,
                detail: None,
            });
        } else if trimmed.ends_with(" SKIPPED") {
            let name = trimmed.trim_end_matches(" SKIPPED").to_string();
            entries.push(TestEntry {
                name,
                outcome: TestOutcome::Skip,
                detail: None,
            });
        }
    }

    // Flush any remaining failure
    flush_failure(
        &mut entries,
        &mut current_failure_name,
        &mut current_failure_detail,
    );

    // Must have found a summary line to be a tier 1 result
    let counts = summary_counts?;

    // Deduplicate entries by test name. When pytest outputs verbose mode AND
    // a FAILURES section AND "short test summary info", the same test can
    // appear multiple times. Keep the first occurrence (which has the richest
    // detail from the FAILURES section).
    let mut seen = HashSet::new();
    entries.retain(|e| seen.insert(e.name.clone()));

    let summary = TestSummary {
        pass: counts.pass,
        fail: counts.fail,
        skip: counts.skip,
        duration_ms: counts.duration_ms,
    };

    Some(TestResult::new(summary, entries))
}

/// Flush a pending failure entry from the FAILURES section.
fn flush_failure(
    entries: &mut Vec<TestEntry>,
    name: &mut Option<String>,
    detail_lines: &mut Vec<String>,
) {
    if let Some(test_name) = name.take() {
        let detail = if detail_lines.is_empty() {
            None
        } else {
            // Take only non-empty trimmed lines for concise detail
            let trimmed: Vec<&str> = detail_lines
                .iter()
                .map(|l| l.trim())
                .filter(|l| !l.is_empty())
                .collect();
            if trimmed.is_empty() {
                None
            } else {
                Some(trimmed.join("\n"))
            }
        };
        entries.push(TestEntry {
            name: test_name,
            outcome: TestOutcome::Fail,
            detail,
        });
        detail_lines.clear();
    }
}

// ============================================================================
// Output emission
// ============================================================================

/// Emit the parsed result to stdout/stderr.
fn emit_result(result: &ParseResult<TestResult>, output: &CommandOutput) -> anyhow::Result<()> {
    use std::io::Write;

    let stdout = io::stdout();
    let stderr = io::stderr();
    let mut out = stdout.lock();
    let mut err = stderr.lock();

    match result {
        ParseResult::Full(tr) => {
            writeln!(out, "{tr}")?;
        }
        ParseResult::Degraded(tr, _markers) => {
            writeln!(out, "{tr}")?;
            result.emit_markers(&mut err)?;
        }
        ParseResult::Passthrough(raw) => {
            // Write raw output as-is
            write!(out, "{raw}")?;
            result.emit_markers(&mut err)?;
        }
    }

    // If there were stderr warnings from pytest itself, forward them
    if !output.stderr.is_empty() && !result.is_passthrough() {
        write!(err, "{}", output.stderr)?;
    }

    Ok(())
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    /// Load a fixture file from the test fixtures directory.
    fn load_fixture(name: &str) -> String {
        let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
            .join("tests")
            .join("fixtures")
            .join("cmd")
            .join("test")
            .join(name);
        std::fs::read_to_string(&path)
            .unwrap_or_else(|e| panic!("Failed to load fixture {}: {e}", path.display()))
    }

    // ========================================================================
    // Tier 1 tests
    // ========================================================================

    #[test]
    fn test_tier1_all_pass() {
        let input = load_fixture("pytest_pass.txt");
        let result = parse(&input);

        assert!(
            result.is_full(),
            "expected Full, got {:?}",
            result.tier_name()
        );

        if let ParseResult::Full(tr) = &result {
            assert_eq!(tr.summary.pass, 5, "expected 5 passed");
            assert_eq!(tr.summary.fail, 0, "expected 0 failed");
            assert_eq!(tr.summary.skip, 0, "expected 0 skipped");
        }
    }

    #[test]
    fn test_tier1_with_failures() {
        let input = load_fixture("pytest_fail.txt");
        let result = parse(&input);

        assert!(
            result.is_full(),
            "expected Full, got {:?}",
            result.tier_name()
        );

        if let ParseResult::Full(tr) = &result {
            assert_eq!(tr.summary.pass, 2, "expected 2 passed");
            assert!(tr.summary.fail > 0, "expected failures");
            assert_eq!(tr.summary.fail, 1, "expected 1 failed");

            // Should have at least one failure entry
            let fail_entries: Vec<_> = tr
                .entries
                .iter()
                .filter(|e| e.outcome == TestOutcome::Fail)
                .collect();
            assert!(
                !fail_entries.is_empty(),
                "expected at least one FAIL entry, got none"
            );
        }
    }

    #[test]
    fn test_tier1_mixed() {
        let input = load_fixture("pytest_mixed.txt");
        let result = parse(&input);

        assert!(
            result.is_full(),
            "expected Full, got {:?}",
            result.tier_name()
        );

        if let ParseResult::Full(tr) = &result {
            assert_eq!(tr.summary.pass, 4, "expected 4 passed");
            assert_eq!(tr.summary.fail, 1, "expected 1 failed");
            assert_eq!(tr.summary.skip, 1, "expected 1 skipped");
        }
    }

    #[test]
    fn test_passthrough() {
        let input = "totally unrelated output\nno pytest here";
        let result = parse(input);
        assert!(
            result.is_passthrough(),
            "expected Passthrough, got {:?}",
            result.tier_name()
        );
    }

    // ========================================================================
    // Flag injection tests
    // ========================================================================

    #[test]
    fn test_flag_injection_skipped_with_verbose() {
        let user_args: Vec<String> = vec!["-v".to_string(), "tests/".to_string()];
        let built = build_args(&user_args);

        // Should NOT inject -q (because -v is present)
        assert!(
            !built.contains(&"-q".to_string()),
            "-q should not be injected when -v is present: {built:?}"
        );
        // Should still inject --tb=short (no --tb present)
        assert!(
            built.contains(&"--tb=short".to_string()),
            "--tb=short should be injected: {built:?}"
        );
    }

    #[test]
    fn test_flag_injection_skipped_with_tb() {
        let user_args: Vec<String> = vec!["--tb=long".to_string()];
        let built = build_args(&user_args);

        // Should NOT inject --tb=short (because --tb=long is present)
        assert!(
            !built.contains(&"--tb=short".to_string()),
            "--tb=short should not be injected when --tb=long is present: {built:?}"
        );
        // Should inject -q (no -q/-v/--quiet/--verbose present)
        assert!(
            built.contains(&"-q".to_string()),
            "-q should be injected: {built:?}"
        );
    }

    #[test]
    fn test_flag_injection_default() {
        let user_args: Vec<String> = vec!["tests/".to_string()];
        let built = build_args(&user_args);

        assert!(
            built.contains(&"--tb=short".to_string()),
            "--tb=short should be injected by default: {built:?}"
        );
        assert!(
            built.contains(&"-q".to_string()),
            "-q should be injected by default: {built:?}"
        );
    }

    #[test]
    fn test_flag_injection_skipped_with_quiet() {
        let user_args: Vec<String> = vec!["--quiet".to_string()];
        let built = build_args(&user_args);

        assert!(
            !built.contains(&"-q".to_string()),
            "-q should not be injected when --quiet is present: {built:?}"
        );
    }

    // ========================================================================
    // user_has_flag tests
    // ========================================================================

    #[test]
    fn test_user_has_flag_exact_match() {
        let args = vec!["-v".to_string(), "tests/".to_string()];
        assert!(user_has_flag(&args, &["-v"]));
    }

    #[test]
    fn test_user_has_flag_with_equals() {
        let args = vec!["--tb=long".to_string()];
        assert!(user_has_flag(&args, &["--tb"]));
    }

    #[test]
    fn test_user_has_flag_not_present() {
        let args = vec!["tests/".to_string()];
        assert!(!user_has_flag(&args, &["-v", "--verbose"]));
    }

    // ========================================================================
    // Summary parsing edge cases
    // ========================================================================

    #[test]
    fn test_summary_passed_only() {
        let line =
            "============================== 5 passed in 0.12s ===============================";
        let counts = parse_summary_line(line).expect("should match");
        assert_eq!(counts.pass, 5);
        assert_eq!(counts.fail, 0);
        assert_eq!(counts.skip, 0);
        assert_eq!(counts.duration_ms, Some(120));
    }

    #[test]
    fn test_summary_all_groups() {
        let line = "======= 10 passed, 2 failed, 3 skipped, 1 error in 1.50s =======";
        let counts = parse_summary_line(line).expect("should match");
        assert_eq!(counts.pass, 10);
        assert_eq!(counts.fail, 3); // 2 failed + 1 error
        assert_eq!(counts.skip, 3);
        assert_eq!(counts.duration_ms, Some(1500));
    }

    #[test]
    fn test_summary_no_match_on_garbage() {
        assert!(parse_summary_line("hello world").is_none());
    }

    #[test]
    fn test_summary_failed_only_no_passed() {
        let line = "=== 3 failed in 0.15s ===";
        let counts = parse_summary_line(line).expect("should match failed-only summary");
        assert_eq!(counts.pass, 0, "no passed tests");
        assert_eq!(counts.fail, 3, "3 failed tests");
        assert_eq!(counts.skip, 0, "no skipped tests");
        assert_eq!(counts.duration_ms, Some(150));
    }

    #[test]
    fn test_summary_failed_and_error_no_passed() {
        let line = "=== 1 failed, 2 error in 0.30s ===";
        let counts = parse_summary_line(line).expect("should match failed+error summary");
        assert_eq!(counts.pass, 0);
        assert_eq!(counts.fail, 3); // 1 failed + 2 error
        assert_eq!(counts.skip, 0);
        assert_eq!(counts.duration_ms, Some(300));
    }

    #[test]
    fn test_summary_duration_extraction() {
        let line = "============== 4 passed, 1 failed, 1 skipped in 0.20s =============";
        let counts = parse_summary_line(line).expect("should match");
        assert_eq!(counts.duration_ms, Some(200));
    }

    #[test]
    fn test_summary_quiet_mode_no_equals() {
        // Quiet mode can produce summary without === padding
        let line = "2 passed in 0.00s";
        let counts = parse_summary_line(line).expect("should match quiet mode");
        assert_eq!(counts.pass, 2);
        assert_eq!(counts.fail, 0);
        assert_eq!(counts.duration_ms, Some(0));
    }

    // ========================================================================
    // All-failures fixture test
    // ========================================================================

    #[test]
    fn test_tier1_all_failures() {
        let input = load_fixture("pytest_all_fail.txt");
        let result = parse(&input);

        assert!(
            result.is_full(),
            "expected Full for all-failures output, got {:?}",
            result.tier_name()
        );

        if let ParseResult::Full(tr) = &result {
            assert_eq!(tr.summary.pass, 0, "expected 0 passed");
            assert_eq!(tr.summary.fail, 3, "expected 3 failed");
            assert_eq!(tr.summary.skip, 0, "expected 0 skipped");
            assert!(
                tr.summary.duration_ms.is_some(),
                "duration should be extracted"
            );

            // Should have failure entries
            let fail_entries: Vec<_> = tr
                .entries
                .iter()
                .filter(|e| e.outcome == TestOutcome::Fail)
                .collect();
            assert!(
                !fail_entries.is_empty(),
                "expected at least one FAIL entry for all-failures fixture"
            );
        }
    }

    // ========================================================================
    // Duration extraction tests
    // ========================================================================

    #[test]
    fn test_tier1_extracts_duration() {
        let input = load_fixture("pytest_pass.txt");
        let result = parse(&input);

        if let ParseResult::Full(tr) = &result {
            assert!(
                tr.summary.duration_ms.is_some(),
                "duration_ms should be populated from summary line"
            );
            assert_eq!(tr.summary.duration_ms, Some(120));
        }
    }

    #[test]
    fn test_summary_line_extracts_duration() {
        let input = "============== 4 passed, 1 failed, 1 skipped in 0.20s ==============";
        let counts = parse_summary_line(input);
        assert!(counts.is_some());
        let counts = counts.unwrap();
        assert_eq!(counts.duration_ms, Some(200));
    }

    // ========================================================================
    // Deduplication tests
    // ========================================================================

    #[test]
    fn test_tier1_deduplicates_entries() {
        // Simulate verbose output with short test summary where the same
        // fully-qualified test name appears both as a verbose FAILED line
        // and in the "short test summary info" section.
        let input = "\
tests/test_a.py::test_one PASSED
tests/test_b.py::test_two FAILED
=========================== short test summary info ============================
FAILED tests/test_b.py::test_two - assert 1 == 2
========================= 1 passed, 1 failed in 0.10s =========================";

        let result = parse(input);
        if let ParseResult::Full(tr) = &result {
            // tests/test_b.py::test_two should appear exactly once despite
            // being in both the verbose output and the short summary.
            let fail_entries: Vec<_> = tr
                .entries
                .iter()
                .filter(|e| e.outcome == TestOutcome::Fail)
                .collect();
            assert_eq!(
                fail_entries.len(),
                1,
                "test_two should be deduplicated to a single entry, got {}",
                fail_entries.len()
            );
            // The first occurrence (from verbose line) should be kept
            assert_eq!(fail_entries[0].name, "tests/test_b.py::test_two");
        }
    }
}