use Test::More tests => 3;
use Cwd 'abs_path';
use Dpkg::IPC;

my $lib = abs_path('lib');
my $dir = 't/pkgjslock';
$ENV{PATH} = abs_path('tools') . ":$ENV{PATH}";

# Build the package and generate pkgjs-lock.json in a fresh process (the
# nodepath cache is global and would hide a difference between the two runs)
sub genlock {
    local $ENV{DEB_BUILD_PROFILES} = shift // '';
    spawn(
        exec       => [ 'dh', 'build', '--with', 'nodejs' ],
        chdir      => $dir,
        wait_child => 1
    );
    spawn(
        exec => [
            'perl', "-I$lib", '-MDebian::PkgJs::PackageLock',
            '-e',   'buildPackageLock(".", "pkgjs-lock.json")'
        ],
        chdir      => $dir,
        wait_child => 1
    );
    my $res;
    if ( open my $f, '<', "$dir/pkgjs-lock.json" ) {
        local $/;
        $res = <$f>;
        close $f;
    }
    unlink "$dir/pkgjs-lock.json";
    spawn(
        exec       => [ 'dh_auto_clean', '--buildsystem=nodejs' ],
        chdir      => $dir,
        wait_child => 1
    );
    spawn( exec => ['dh_clean'], chdir => $dir, wait_child => 1 );
    return $res;
}

my $withtest = genlock();
my $nocheck  = genlock('nocheck');

ok( $withtest, 'pkgjs-lock.json generated' );
like( $withtest, qr#/usr/share/nodejs/\@types/node#,
    'test-flagged extlink recorded with its system path' );
is( $nocheck, $withtest, 'pkgjs-lock.json does not depend on nocheck' );
