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
//! Go test parser with three-tier degradation (#49)
//!
//! Parses `go test -json` NDJSON output (Tier 1), falls back to regex
//! on `--- PASS/FAIL/SKIP` lines (Tier 2), and passes through raw output
//! when nothing can be parsed (Tier 3).

use std::collections::HashMap;
use std::process::ExitCode;
use std::sync::LazyLock;

use regex::Regex;

use crate::output::canonical::{TestEntry, TestOutcome, TestResult, TestSummary};
use crate::output::ParseResult;
use crate::runner::CommandRunner;

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

/// Execute `go test` with the given args and parse the output.
///
/// Injects `-json` if the user hasn't already set `-json` or `-v`,
/// then runs the command through [`CommandRunner`] and parses output
/// via three-tier degradation.
pub(crate) fn run(args: &[String], show_stats: bool) -> anyhow::Result<ExitCode> {
    let mut go_args: Vec<String> = vec!["test".to_string()];

    // Inject -json before any `--` separator, unless the user already specified
    // -json or -v (verbose mode produces non-JSON output).
    //
    // Go flags use `-flag=false` to explicitly disable a flag, so we use
    // go-specific detection that treats `-v=false` as NOT having `-v`.
    if !go_has_flag(args, "-json") && !go_has_flag(args, "-v") {
        // Find the position of `--` if present, and inject -json before it.
        if let Some(sep_pos) = args.iter().position(|a| a == "--") {
            go_args.extend_from_slice(&args[..sep_pos]);
            go_args.push("-json".to_string());
            go_args.extend_from_slice(&args[sep_pos..]);
        } else {
            go_args.push("-json".to_string());
            go_args.extend_from_slice(args);
        }
    } else {
        go_args.extend_from_slice(args);
    }

    let runner = CommandRunner::new(None);
    let go_args_ref: Vec<&str> = go_args.iter().map(|s| s.as_str()).collect();

    let output = runner.run("go", &go_args_ref).map_err(|e| {
        let msg = e.to_string();
        if msg.contains("failed to execute") {
            anyhow::anyhow!("{}\nHint: install Go from https://go.dev/dl/", msg)
        } else {
            e
        }
    })?;

    // Combine stdout and stderr for parsing (go test writes to both).
    let combined = if output.stderr.is_empty() {
        output.stdout.clone()
    } else {
        format!("{}\n{}", output.stdout, output.stderr)
    };

    let parsed = parse(&combined);

    // Emit the result
    let exit_code = match &parsed {
        ParseResult::Full(result) | ParseResult::Degraded(result, _) => {
            println!("{result}");
            // Emit degradation markers to stderr
            let mut stderr = std::io::stderr().lock();
            let _ = parsed.emit_markers(&mut stderr);

            if result.summary.fail > 0 {
                ExitCode::FAILURE
            } else {
                ExitCode::SUCCESS
            }
        }
        ParseResult::Passthrough(raw) => {
            println!("{raw}");
            let mut stderr = std::io::stderr().lock();
            let _ = parsed.emit_markers(&mut stderr);
            // Mirror the original process exit code
            match output.exit_code {
                Some(0) => ExitCode::SUCCESS,
                _ => ExitCode::FAILURE,
            }
        }
    };

    if show_stats {
        let (orig, comp) = crate::process::count_token_pair(&combined, parsed.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(
            combined,
            parsed.content().to_string(),
            format!("skim test go {}", args.join(" ")),
            crate::analytics::CommandType::Test,
            output.duration,
            Some(parsed.tier_name()),
        );
    }

    Ok(exit_code)
}

// ============================================================================
// Flag detection
// ============================================================================

/// Go-specific flag detection that respects `-flag=false` semantics.
///
/// Unlike the shared `crate::cmd::user_has_flag`, Go CLI flags use
/// `-flag=false` to explicitly disable a boolean flag. This function
/// treats `-v=false` as the flag NOT being set, which is required for
/// correct `-json` injection logic. The shared version does not handle
/// this because `=false` is not a convention outside Go tooling.
fn go_has_flag(args: &[String], flag: &str) -> bool {
    args.iter().any(|a| {
        if a == flag {
            return true;
        }
        if let Some(value) = a.strip_prefix(&format!("{flag}=")) {
            // -v=false means the flag is NOT set
            return value != "false";
        }
        false
    })
}

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

/// Parse go test output through three-tier degradation.
///
/// - Tier 1: NDJSON (`go test -json`) → `ParseResult::Full`
/// - Tier 2: Regex fallback on `--- PASS/FAIL/SKIP` lines → `ParseResult::Degraded`
/// - Tier 3: Raw passthrough → `ParseResult::Passthrough`
fn parse(output: &str) -> ParseResult<TestResult> {
    // Tier 1: Try NDJSON parsing
    if let Some(result) = try_parse_ndjson(output) {
        return ParseResult::Full(result);
    }

    // Tier 2: Regex fallback
    if let Some(result) = try_parse_regex(output) {
        return ParseResult::Degraded(
            result,
            vec!["go test: JSON parse failed, using regex".to_string()],
        );
    }

    // Tier 3: Passthrough
    ParseResult::Passthrough(output.to_string())
}

// ============================================================================
// Tier 1: NDJSON parser
// ============================================================================

/// Parse NDJSON output from `go test -json`.
///
/// Each line is a JSON object with Action, Package, Test (optional),
/// Output (optional), and Elapsed (optional) fields. We track test
/// outcomes by (Package, Test) key and collect output for failed tests.
fn try_parse_ndjson(output: &str) -> Option<TestResult> {
    let mut test_outcomes: HashMap<String, TestOutcome> = HashMap::new();
    let mut test_outputs: HashMap<String, Vec<String>> = HashMap::new();
    let mut package_elapsed: HashMap<String, f64> = HashMap::new();
    let mut found_any_event = false;

    for line in output.lines() {
        let line = line.trim();
        if line.is_empty() {
            continue;
        }

        let event: serde_json::Value = match serde_json::from_str(line) {
            Ok(v) => v,
            Err(_) => continue,
        };

        let action = match event.get("Action").and_then(|v| v.as_str()) {
            Some(a) => a,
            None => continue,
        };

        let package = event.get("Package").and_then(|v| v.as_str()).unwrap_or("");

        let test_name = event.get("Test").and_then(|v| v.as_str());

        // Only track test-level events (not package-level) for test entries
        if let Some(name) = test_name {
            let key = format!("{package}::{name}");

            match action {
                "pass" => {
                    found_any_event = true;
                    test_outcomes.insert(key, TestOutcome::Pass);
                }
                "fail" => {
                    found_any_event = true;
                    test_outcomes.insert(key, TestOutcome::Fail);
                }
                "skip" => {
                    found_any_event = true;
                    test_outcomes.insert(key, TestOutcome::Skip);
                }
                "output" => {
                    // Output is bounded by CommandRunner's 64 MiB cap on total
                    // process output. Within that, per-test accumulation is acceptable.
                    if let Some(text) = event.get("Output").and_then(|v| v.as_str()) {
                        test_outputs.entry(key).or_default().push(text.to_string());
                    }
                }
                // "run", "pause", "cont", "bench" — ignore
                _ => {}
            }
        } else {
            // Package-level events — track elapsed for duration
            if let Some(elapsed) = event.get("Elapsed").and_then(|v| v.as_f64()) {
                if matches!(action, "pass" | "fail") {
                    found_any_event = true;
                    package_elapsed.insert(package.to_string(), elapsed);
                }
            } else if matches!(action, "pass" | "fail") {
                found_any_event = true;
            }
        }
    }

    if !found_any_event {
        return None;
    }

    // Build test entries
    let mut entries: Vec<TestEntry> = Vec::new();
    let mut pass_count: usize = 0;
    let mut fail_count: usize = 0;
    let mut skip_count: usize = 0;

    // Sort keys for deterministic output
    let mut keys: Vec<String> = test_outcomes.keys().cloned().collect();
    keys.sort();

    for key in &keys {
        let outcome = test_outcomes.get(key).unwrap().clone();
        let detail = if outcome == TestOutcome::Fail {
            // Collect output lines for failed tests, trimming trailing whitespace
            test_outputs.get(key).map(|lines| {
                lines
                    .iter()
                    .map(|l| l.trim_end().to_string())
                    .filter(|l| !l.is_empty())
                    .collect::<Vec<_>>()
                    .join("\n")
            })
        } else {
            None
        };

        match outcome {
            TestOutcome::Pass => pass_count += 1,
            TestOutcome::Fail => fail_count += 1,
            TestOutcome::Skip => skip_count += 1,
        }

        entries.push(TestEntry {
            name: key.clone(),
            outcome,
            detail,
        });
    }

    // Compute total duration from package elapsed times
    let total_elapsed_secs: f64 = package_elapsed.values().sum();
    let duration_ms = if total_elapsed_secs > 0.0 {
        Some((total_elapsed_secs * 1000.0) as u64)
    } else {
        None
    };

    let summary = TestSummary {
        pass: pass_count,
        fail: fail_count,
        skip: skip_count,
        duration_ms,
    };

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

// ============================================================================
// Tier 2: Regex fallback
// ============================================================================

/// Matches `--- PASS/FAIL/SKIP: TestName` lines in go test verbose output.
static TEST_RE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"---\s+(PASS|FAIL|SKIP):\s+(\S+)").unwrap());

/// Matches `ok  package/name  0.123s` summary lines.
static SUMMARY_OK_RE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"ok\s+(\S+)\s+([\d.]+)s").unwrap());

/// Parse go test output using regex patterns on `--- PASS/FAIL/SKIP` lines.
///
/// Falls back from NDJSON when the output is plain text (e.g., user ran
/// `go test` without `-json`, or piped non-JSON input).
///
/// Known limitations compared to Tier 1:
/// - Test names are not package-prefixed because `--- PASS/FAIL/SKIP` lines
///   do not include package info. Package is extracted from `ok` summary lines
///   when available and prepended.
/// - Failure details are not collected. Tier 2 cannot reliably extract failure
///   output from plain text.
fn try_parse_regex(output: &str) -> Option<TestResult> {
    let mut entries: Vec<TestEntry> = Vec::new();
    let mut pass_count: usize = 0;
    let mut fail_count: usize = 0;
    let mut skip_count: usize = 0;
    let mut total_duration_secs: f64 = 0.0;
    // Extract package name from `ok` summary lines for prefixing test names.
    let mut package_name: Option<String> = None;

    for line in output.lines() {
        if let Some(caps) = TEST_RE.captures(line) {
            let outcome_str = caps.get(1).unwrap().as_str();
            let name = caps.get(2).unwrap().as_str().to_string();

            let outcome = match outcome_str {
                "PASS" => {
                    pass_count += 1;
                    TestOutcome::Pass
                }
                "FAIL" => {
                    fail_count += 1;
                    TestOutcome::Fail
                }
                "SKIP" => {
                    skip_count += 1;
                    TestOutcome::Skip
                }
                _ => continue,
            };

            entries.push(TestEntry {
                name,
                outcome,
                // Tier 2 cannot reliably extract failure details from plain text.
                detail: None,
            });
        }

        // Extract duration and package name from summary line
        if let Some(caps) = SUMMARY_OK_RE.captures(line) {
            if package_name.is_none() {
                package_name = Some(caps.get(1).unwrap().as_str().to_string());
            }
            if let Ok(secs) = caps.get(2).unwrap().as_str().parse::<f64>() {
                total_duration_secs += secs;
            }
        }
    }

    if entries.is_empty() {
        return None;
    }

    // Prefix test names with package when available (matching Tier 1 format).
    if let Some(ref pkg) = package_name {
        for entry in &mut entries {
            entry.name = format!("{pkg}::{}", entry.name);
        }
    }

    let duration_ms = if total_duration_secs > 0.0 {
        Some((total_duration_secs * 1000.0) as u64)
    } else {
        None
    };

    let summary = TestSummary {
        pass: pass_count,
        fail: fail_count,
        skip: skip_count,
        duration_ms,
    };

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

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

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

    fn fixture_path(name: &str) -> String {
        let manifest_dir = env!("CARGO_MANIFEST_DIR");
        format!("{manifest_dir}/tests/fixtures/go_test/{name}")
    }

    fn read_fixture(name: &str) -> String {
        std::fs::read_to_string(fixture_path(name))
            .unwrap_or_else(|e| panic!("Failed to read fixture {name}: {e}"))
    }

    // ========================================================================
    // Tier 1: NDJSON tests
    // ========================================================================

    #[test]
    fn test_tier1_all_pass() {
        let input = read_fixture("go_test_pass.json");
        let result = parse(&input);

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

        if let ParseResult::Full(test_result) = &result {
            assert_eq!(test_result.summary.pass, 2, "expected 2 passing tests");
            assert_eq!(test_result.summary.fail, 0, "expected 0 failing tests");
            assert_eq!(test_result.summary.skip, 0, "expected 0 skipped tests");
            assert_eq!(test_result.entries.len(), 2, "expected 2 test entries");

            // Verify test names are prefixed with package
            assert!(
                test_result
                    .entries
                    .iter()
                    .all(|e| e.name.starts_with("example.com/pkg::")),
                "expected all test names to be prefixed with package, got: {:?}",
                test_result
                    .entries
                    .iter()
                    .map(|e| &e.name)
                    .collect::<Vec<_>>()
            );

            // Verify duration was extracted
            assert!(
                test_result.summary.duration_ms.is_some(),
                "expected duration to be present"
            );
        }
    }

    #[test]
    fn test_tier1_with_failures() {
        let input = read_fixture("go_test_fail.json");
        let result = parse(&input);

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

        if let ParseResult::Full(test_result) = &result {
            assert_eq!(test_result.summary.pass, 1, "expected 1 passing test");
            assert_eq!(test_result.summary.fail, 1, "expected 1 failing test");

            // Find the failing entry and verify detail is present
            let failed = test_result
                .entries
                .iter()
                .find(|e| e.outcome == TestOutcome::Fail)
                .expect("expected a failing test entry");

            assert!(
                failed.name.contains("TestDiv"),
                "expected failing test name to contain TestDiv, got: {}",
                failed.name
            );

            assert!(
                failed.detail.is_some(),
                "expected detail to be present for failed test"
            );

            let detail = failed.detail.as_ref().unwrap();
            assert!(
                detail.contains("expected 0, got 1"),
                "expected detail to contain error message, got: {detail}"
            );
        }
    }

    #[test]
    fn test_tier1_multi_package() {
        let input = r#"{"Time":"2024-01-01T00:00:00Z","Action":"run","Package":"pkg/a","Test":"TestA"}
{"Time":"2024-01-01T00:00:00Z","Action":"pass","Package":"pkg/a","Test":"TestA","Elapsed":0.001}
{"Time":"2024-01-01T00:00:00Z","Action":"pass","Package":"pkg/a","Elapsed":0.002}
{"Time":"2024-01-01T00:00:00Z","Action":"run","Package":"pkg/b","Test":"TestB"}
{"Time":"2024-01-01T00:00:00Z","Action":"pass","Package":"pkg/b","Test":"TestB","Elapsed":0.001}
{"Time":"2024-01-01T00:00:00Z","Action":"pass","Package":"pkg/b","Elapsed":0.003}
"#;

        let result = parse(input);
        assert!(
            result.is_full(),
            "expected Full, got {}",
            result.tier_name()
        );

        if let ParseResult::Full(test_result) = &result {
            assert_eq!(test_result.summary.pass, 2, "expected 2 passing tests");
            assert_eq!(test_result.entries.len(), 2, "expected 2 test entries");

            // Verify both packages are represented in test names
            let names: Vec<&str> = test_result
                .entries
                .iter()
                .map(|e| e.name.as_str())
                .collect();
            assert!(
                names.contains(&"pkg/a::TestA"),
                "expected pkg/a::TestA in entries, got: {names:?}"
            );
            assert!(
                names.contains(&"pkg/b::TestB"),
                "expected pkg/b::TestB in entries, got: {names:?}"
            );
        }
    }

    // ========================================================================
    // Tier 2: Regex fallback tests
    // ========================================================================

    #[test]
    fn test_tier2_regex_fallback() {
        let input = read_fixture("go_test_text.txt");
        let result = parse(&input);

        assert!(
            result.is_degraded(),
            "expected Degraded, got {}",
            result.tier_name()
        );

        if let ParseResult::Degraded(test_result, markers) = &result {
            assert_eq!(test_result.summary.pass, 2, "expected 2 passing tests");
            assert_eq!(test_result.summary.fail, 0, "expected 0 failing tests");
            assert_eq!(test_result.entries.len(), 2, "expected 2 test entries");

            // Verify marker indicates regex fallback
            assert!(
                markers.contains(&"go test: JSON parse failed, using regex".to_string()),
                "expected 'go test: JSON parse failed, using regex' marker, got: {markers:?}"
            );

            // Verify duration was extracted from summary line
            assert!(
                test_result.summary.duration_ms.is_some(),
                "expected duration to be present from ok line"
            );

            // Verify test names are package-prefixed from ok summary line
            assert!(
                test_result
                    .entries
                    .iter()
                    .all(|e| e.name.starts_with("example.com/pkg::")),
                "expected all Tier 2 test names to be package-prefixed, got: {:?}",
                test_result
                    .entries
                    .iter()
                    .map(|e| &e.name)
                    .collect::<Vec<_>>()
            );
        }
    }

    // ========================================================================
    // Tier 3: Passthrough test
    // ========================================================================

    #[test]
    fn test_tier3_passthrough() {
        let input = "some random output\nwith no test patterns\nat all\n";
        let result = parse(input);

        assert!(
            result.is_passthrough(),
            "expected Passthrough, got {}",
            result.tier_name()
        );
    }

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

    #[test]
    fn test_flag_injection_skipped_with_v() {
        let args = vec!["-v".to_string(), "./...".to_string()];
        assert!(go_has_flag(&args, "-v"), "expected -v to be detected");
        assert!(
            !go_has_flag(&args, "-json"),
            "expected -json to NOT be detected"
        );
    }

    #[test]
    fn test_flag_injection_skipped_with_json() {
        let args = vec!["-json".to_string(), "./...".to_string()];
        assert!(go_has_flag(&args, "-json"), "expected -json to be detected");
        assert!(!go_has_flag(&args, "-v"), "expected -v to NOT be detected");
    }

    #[test]
    fn test_user_has_flag_with_equals() {
        let args = vec!["-json=true".to_string()];
        assert!(
            go_has_flag(&args, "-json"),
            "expected -json=true to match -json"
        );
    }

    #[test]
    fn test_user_has_flag_no_match() {
        let args = vec![
            "./...".to_string(),
            "-run".to_string(),
            "TestFoo".to_string(),
        ];
        assert!(
            !go_has_flag(&args, "-json"),
            "expected -json to NOT be detected"
        );
        assert!(!go_has_flag(&args, "-v"), "expected -v to NOT be detected");
    }

    // ========================================================================
    // Edge cases
    // ========================================================================

    #[test]
    fn test_empty_input() {
        let result = parse("");
        assert!(
            result.is_passthrough(),
            "expected Passthrough for empty input, got {}",
            result.tier_name()
        );
    }

    #[test]
    fn test_ndjson_with_only_package_events() {
        // Package-level pass/fail without any Test-level events should still
        // produce a Full result (found_any_event = true) but with no entries.
        let input = r#"{"Time":"2024-01-01T00:00:00Z","Action":"output","Package":"example.com/pkg","Output":"ok  \texample.com/pkg\t0.005s\n"}
{"Time":"2024-01-01T00:00:00Z","Action":"pass","Package":"example.com/pkg","Elapsed":0.005}
"#;

        let result = parse(input);
        assert!(
            result.is_full(),
            "expected Full for package-only events, got {}",
            result.tier_name()
        );

        if let ParseResult::Full(test_result) = &result {
            assert_eq!(
                test_result.entries.len(),
                0,
                "expected 0 test entries for package-only events"
            );
            assert_eq!(
                test_result.summary.pass, 0,
                "expected 0 pass (no test-level events)"
            );
        }
    }

    #[test]
    fn test_ndjson_skip_action() {
        let input = r#"{"Time":"2024-01-01T00:00:00Z","Action":"run","Package":"pkg","Test":"TestSkipped"}
{"Time":"2024-01-01T00:00:00Z","Action":"skip","Package":"pkg","Test":"TestSkipped","Elapsed":0.0}
"#;

        let result = parse(input);
        assert!(
            result.is_full(),
            "expected Full, got {}",
            result.tier_name()
        );

        if let ParseResult::Full(test_result) = &result {
            assert_eq!(test_result.summary.skip, 1, "expected 1 skipped test");
            assert_eq!(test_result.entries[0].outcome, TestOutcome::Skip);
        }
    }

    #[test]
    fn test_ndjson_malformed_lines_skipped() {
        let input = "not json\n{\"Action\":\"pass\",\"Package\":\"pkg\",\"Test\":\"TestA\"}\nalso not json\n";
        let result = parse(input);
        assert!(
            result.is_full(),
            "expected Full (valid NDJSON mixed with garbage), got {}",
            result.tier_name()
        );

        if let ParseResult::Full(test_result) = &result {
            assert_eq!(
                test_result.summary.pass, 1,
                "expected 1 passing test from valid line"
            );
        }
    }

    #[test]
    fn test_regex_with_skip_outcome() {
        let input = "=== RUN   TestSkipped\n--- SKIP: TestSkipped (0.00s)\nok      example.com/pkg 0.003s\n";
        let result = parse(input);
        // This should be Tier 2 (no NDJSON), Degraded
        // Actually, there's no valid JSON so Tier 1 fails, then Tier 2 regex finds it
        assert!(
            result.is_degraded(),
            "expected Degraded, got {}",
            result.tier_name()
        );

        if let ParseResult::Degraded(test_result, _) = &result {
            assert_eq!(test_result.summary.skip, 1, "expected 1 skipped test");
            // Verify package prefix from ok summary line
            assert_eq!(
                test_result.entries[0].name, "example.com/pkg::TestSkipped",
                "expected package-prefixed name"
            );
        }
    }

    // ========================================================================
    // `--` separator and flag edge cases
    // ========================================================================

    #[test]
    fn test_separator_flag_injection() {
        // When `--` is present, `-json` must be injected before it so the
        // Go toolchain sees the flag, while args after `--` pass through.
        let args = vec![
            "./...".to_string(),
            "--".to_string(),
            "-run".to_string(),
            "TestFoo".to_string(),
        ];

        let mut go_args: Vec<String> = vec!["test".to_string()];
        if !go_has_flag(&args, "-json") && !go_has_flag(&args, "-v") {
            if let Some(sep_pos) = args.iter().position(|a| a == "--") {
                go_args.extend_from_slice(&args[..sep_pos]);
                go_args.push("-json".to_string());
                go_args.extend_from_slice(&args[sep_pos..]);
            } else {
                go_args.push("-json".to_string());
                go_args.extend_from_slice(&args);
            }
        } else {
            go_args.extend_from_slice(&args);
        }

        // -json should appear before `--`
        let json_pos = go_args.iter().position(|a| a == "-json").unwrap();
        let sep_pos = go_args.iter().position(|a| a == "--").unwrap();
        assert!(
            json_pos < sep_pos,
            "expected -json (pos {json_pos}) before -- (pos {sep_pos}), got: {go_args:?}"
        );
    }

    #[test]
    fn test_v_equals_false_still_injects_json() {
        // `-v=false` explicitly disables verbose mode, so -json should be injected.
        let args = vec!["-v=false".to_string(), "./...".to_string()];
        assert!(
            !go_has_flag(&args, "-v"),
            "expected -v=false to NOT be detected as -v"
        );
    }

    #[test]
    fn test_v_equals_true_skips_json_injection() {
        // `-v=true` enables verbose mode, so -json should NOT be injected.
        let args = vec!["-v=true".to_string(), "./...".to_string()];
        assert!(
            go_has_flag(&args, "-v"),
            "expected -v=true to be detected as -v"
        );
    }
}