
use strict;
use warnings;

use lib 'tests/perl_tests/lib';
use JBlibs;

use Test::More;
use Test::Deep;
use Capture::Tiny 'tee';

use File::Copy 'copy';
use File::Copy::Recursive 'dircopy';
use File::Path qw( rmtree );
use File::Temp;

use FileSlurping qw( slurp slurp_tree );

use Bio::JBrowse::Cmd::IndexNames;

sub run_with {
    my @args = @_;
    return tee {
        Bio::JBrowse::Cmd::IndexNames->new( @args )->run;
    };
}

# creation of two identical test instances causes test failure
my $tempdir = new_volvox_sandbox();
my ( $stdout ) = run_with (
    '--out'   => "$tempdir",
    '--hashBits' => 16,
    '--completionLimit' => 15
    );
{
    my $got = read_names($tempdir);
    my $expected = read_names('tests/data/volvox_formatted_names');
    is_deeply( $got, $expected, 'got right data from volvox test data run' );
}

my $tempdir2 = new_volvox_sandbox();
run_with (
    '--out'   => "$tempdir2",
    '--hashBits' => 16,
    '--completionLimit' => 15
    );
{
    my $got = read_names($tempdir2);
    my $expected = read_names('tests/data/volvox_formatted_names');
    is_deeply( $got, $expected, 'got right data from volvox test data run round 2 (should fail)' );
}
done_testing;

sub read_names {
    my $d = slurp_tree(shift.'/names');
    delete $d->{'meta.json'}{last_changed_entry};
    return $d;
}

sub new_volvox_sandbox {
    my $tempdir = shift||File::Temp->newdir( CLEANUP => $ENV{KEEP_ALL} ? 0 : 1 );
    dircopy( 'tests/data/volvox_formatted_names', $tempdir );
    copy( 'sample_data/raw/volvox/volvox.filtered.vcf.gz',
          "$tempdir/volvox.filtered.vcf.gz"
          ) or die $!;
    copy( 'sample_data/raw/volvox/volvox.test.vcf.gz',
          "$tempdir/volvox.test.vcf.gz"
          ) or die $!;
    rmtree( "$tempdir/names" );
    if( -d "$tempdir/names" ) { print STDERR "WTF\n"; }
    return $tempdir;
}
