### ----------------------------------------------------
### -- App::ecogen
### -- Licenses: Artistic-2.0
### -- Authors: ugexe
### -- File: t/01-downgrade.rakutest
### ----------------------------------------------------
use Test:ver<6.c+>;
plan 2;

use App::ecogen;
use JSON::Fast;

my $prefix = $*TMPDIR.child('app-ecogen');
$prefix.mkdir;

class App::ecogen::test does Ecosystem {
    has $.prefix;
    has @.meta-data;
    method IO { self.prefix.IO }
    method meta-uris { }
    method package-list(|) {
        return @.meta-data
    }
}


subtest 'array depends' => {
    my $test-prefix     = $prefix.child('test-array');
    my $test-json-path  = $prefix.child('test-array.json');
    my $test-json-path1 = $prefix.child('test-array1.json');

    my $meta-json = q/{
        "meta-version": 1,
        "name": "test-array",
        "depends": [
            "foo:from<native>",
            "bar",
            {
                "name": "baz",
                "hints": { }
            }
        ]
    }/;
    my @metas = (from-json($meta-json),);

    my $app = App::ecogen::test.new(:prefix($test-prefix), :meta-data(@metas));
    $app.update-local-package-list;

    my $test-json  = from-json($test-json-path.slurp);
    my $test-json1 = from-json($test-json-path1.slurp);

    my @downgraded = do {
        my @m = @metas.clone;
        @m[0]<meta-version> = 0;
        @m[0]<depends> = ["bar", "baz"];
        @m;
    }

    is-deeply @metas, $test-json;
    is-deeply @downgraded, $test-json;
}

subtest 'hash depends' => {
    my $test-prefix     = $prefix.child('test-hash');
    my $test-json-path  = $prefix.child('test-hash.json');
    my $test-json-path1 = $prefix.child('test-hash1.json');

    my $meta-json = q/{
        "meta-version": 1,
        "name": "test-hash",
        "depends": {
            "build": {
                "requires": [
                    "foo:from<native>",
                    "bar",
                    {
                        "name": "baz",
                        "hints": { }
                    }
                ]
            }
        }
    }/;
    my @metas = (from-json($meta-json),);

    my $app = App::ecogen::test.new(:prefix($test-prefix), :meta-data(@metas));
    $app.update-local-package-list;

    my $test-json  = from-json($test-json-path.slurp);
    my $test-json1 = from-json($test-json-path1.slurp);

    my @downgraded = do {
        my @m = @metas.clone;
        @m[0]<meta-version> = 0;
        @m[0]<build-depends> = ["bar", "baz"];
        @m;
    }

    is-deeply @metas, $test-json;
    is-deeply @downgraded, $test-json;
}

END {
    if $prefix {
        .unlink for $prefix.dir;
        $prefix.rmdir;
    }
}


