#!perl
use Cassandane::Tiny;

sub test_getmetadata_depth
    :AnnotationAllowUndefined
{
    my ($self) = @_;

    xlog $self, "test the GETMETADATA command with DEPTH option";

    my $imaptalk = $self->{store}->get_client();
    # data thanks to hipsteripsum.me
    my $folder = 'INBOX.denim';
    my %entries = (
        '/shared/selvage' => 'locavore',
        '/shared/selvage/portland' => 'ennui',
        '/shared/selvage/leggings' => 'scenester',
        '/shared/selvage/portland/mustache' => 'terry richardson',
        '/shared/selvage/portland/mustache/american' => 'messenger bag',
        '/shared/selvage/portland/mustache/american/apparel' => 'street art',
    );
    my $rootentry = '/shared/selvage';
    my $res;

    xlog $self, "Create folder";
    $imaptalk->create($folder)
        or die "Cannot create mailbox $folder: $@";

    xlog $self, "Setup metadata";
    foreach my $entry (sort keys %entries)
    {
        $imaptalk->setmetadata($folder, $entry, $entries{$entry})
            or die "Cannot setmetadata: $@";
    }

    xlog $self, "Getting metadata with no DEPTH";
    $res = getmetadata($imaptalk, $folder, $rootentry);
    $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());
    $self->assert_not_null($res);
    $self->assert_deep_equals({ $folder => { $rootentry => $entries{$rootentry} } } , $res);

    xlog $self, "Getting metadata with DEPTH 0 in the right place";
    $res = getmetadata($imaptalk, $folder, [ DEPTH => 0 ], $rootentry);
    $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());
    $self->assert_not_null($res);
    $self->assert_deep_equals({ $folder => { $rootentry => $entries{$rootentry} } } , $res);

    xlog $self, "Getting metadata with DEPTH 1 in the right place";
    my @subset = ( qw(/shared/selvage /shared/selvage/portland /shared/selvage/leggings) );
    $res = getmetadata($imaptalk, $folder, [ DEPTH => 1 ], $rootentry);
    $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());
    $self->assert_not_null($res);
    $self->assert_deep_equals({ $folder => { map { $_ => $entries{$_} } @subset } }, $res);

    xlog $self, "Getting metadata with DEPTH infinity in the right place";
    $res = getmetadata($imaptalk, $folder, [ DEPTH => 'infinity' ], $rootentry);
    $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());
    $self->assert_not_null($res);
    $self->assert_deep_equals({ $folder => { %entries } } , $res);

    xlog $self, "Getting metadata with DEPTH 0 in the wrong place";
    $res = getmetadata($imaptalk, $folder, [ DEPTH => 0 ], $rootentry);
    $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());
    $self->assert_not_null($res);
    $self->assert_deep_equals({ $folder => { $rootentry => $entries{$rootentry} } } , $res);

    xlog $self, "Getting metadata with DEPTH 1 in the wrong place";
    $res = getmetadata($imaptalk, $folder, [ DEPTH => 1 ], $rootentry);
    $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());
    $self->assert_not_null($res);
    $self->assert_deep_equals({ $folder => { map { $_ => $entries{$_} } @subset } }, $res);

    xlog $self, "Getting metadata with DEPTH infinity in the wrong place";
    $res = getmetadata($imaptalk, $folder, [ DEPTH => 'infinity' ], $rootentry);
    $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());
    $self->assert_not_null($res);
    $self->assert_deep_equals({ $folder => { %entries } } , $res);
}
