#!/usr/bin/env perl

# Regression coverage for run-tests' own CLI argument parsing - a thin wrapper with no dedicated
# module, so this shells out to the real script rather than testing a library function.

use strict;
use warnings;
use FindBin;

my $passed = 0;
my $failed = 0;

sub ok {
    my ($cond, $desc) = @_;
    if ($cond) { print "  PASS: $desc\n"; $passed++; return 1; }
    else       { print "  FAIL: $desc\n"; $failed++; return 0; }
}

my $runTests = "$FindBin::Bin/../sneakernet/testing/run-tests";
my $catDir   = "$FindBin::Bin/../sneakernet/testing/catalogue";

# ===========================================================================
print "=== '--parts 0' selects only Part 0, not the whole catalogue ===\n";
# ===========================================================================
{
    # Regression: Getopt::Long stores '0' as the string "0" in $opt{parts}, and
    # 'elsif ($opt{parts})' treats that as false (Perl's "0" is falsy) - --parts 0 silently fell
    # through to "no selection given" and ran every step in the catalogue instead of just Part 0.
    my $out = `perl "$runTests" --explain --catalogue-dir "$catDir" --parts 0 2>&1`;
    ok($out =~ /\b0\.1\b/,  "Part 0's own steps (e.g. 0.1) are selected");
    ok($out !~ /\b18\.1\b/, "a step from a later Part (18.1) is NOT selected - --parts 0 no longer runs everything");
    my ($count) = $out =~ /(\d+) step\(s\) selected/;
    ok(defined($count) && $count == 6, "exactly Part 0's 6 steps are selected (got " . ($count // '?') . ")");
}

print "\n=== Summary ===\n";
print "Passed: $passed\n";
print "Failed: $failed\n";
exit($failed > 0 ? 1 : 0);
