use Test2::V0;

use File::Temp qw/tempdir/;
use File::Spec;

use App::Yath::Tester qw/yath/;
use Test2::Harness::Util::File::JSONL;

use Test2::Harness::Util::JSON qw/decode_json/;

my $dir = __FILE__;
$dir =~ s{\.t$}{}g;
$dir =~ s{^\./}{};

yath(
    command => 'test',
    args    => [$dir, '--ext=tx'],
    log     => 1,
    exit    => T(),
    test    => sub {
        my $out     = shift;
        my $logfile = $out->{log}->name;

        $out = yath(
            command => 'failed',
            args    => [$logfile],
            env     => {TABLE_TERM_SIZE => 1000, TS_TERM_SIZE => 1000},
            exit    => 0,
            test    => sub {
                my $out = shift;

                ok(!$out->{exit}, "'failed' command exits true");
                like($out->{output}, qr{fail\.tx}, "'fail.tx' was seen as a failure when reading the log");
                unlike($out->{output}, qr{pass\.tx}, "'pass.tx' was not seen as a failure when reading the log");
            },
        );

        # GH #300: 'yath failed --interactive' should not crash with
        # "The 'quiet' field does not exist"
        $out = yath(
            command => 'failed',
            args    => ['-i', $logfile],
            exit    => 0,
            test    => sub {
                my $out = shift;

                ok(!$out->{exit}, "'failed -i' command exits true");
                unlike($out->{output}, qr{field does not exist}, "'failed -i' does not crash on missing display fields");
            },
        );
    },
);



done_testing;
