#!perl
use Cassandane::Tiny;

sub test_percent_altns_no_intermediates
    :UnixHierarchySep :VirtDomains :CrossDomains :AltNamespace :min_version_3_5
{
    my ($self) = @_;

    my $imaptalk = $self->{store}->get_client();
    my $admintalk = $self->{adminstore}->get_client();

    # INBOX needs to exist even if we can't see it
    $admintalk->create('user/bar');

    foreach my $Folder ("user/cassandane/INBOX/sub", "user/cassandane/AEARLY",
                        "user/cassandane/sub2", "user/cassandane/sub2/achild",
                        "user/cassandane/INBOX/very/deep/one",
                        "user/cassandane/not/so/deep",
                        # stuff you can't see
                        "user/cassandane/INBOX",
                        "user/cassandane/inbox",
                        "user/cassandane/inbox/subnobody/deep",
                        "user/cassandane/Inbox/subnobody/deep",
                        # other users
                        "user/bar/Trash",
                        "user/foo",
                        "user/foo/really/deep",
                        # shared
                        "shared stuff/something") {
        $admintalk->create($Folder);
        $admintalk->setacl($Folder, 'cassandane' => 'lrswipkxtecd');
    }

    xlog $self, "List *";
    my $data = $imaptalk->list("", "*");
    $self->assert_mailbox_structure($data, '/', {
        'INBOX' => '\\HasChildren',
        'INBOX/sub' => '\\HasNoChildren',
        'INBOX/very' => '\\HasChildren',
        'INBOX/very/deep' => '\\HasChildren',
        'INBOX/very/deep/one' => '\\HasNoChildren',
        'AEARLY' => '\\HasNoChildren',
        'not' => '\\HasChildren',
        'not/so' => '\\HasChildren',
        'not/so/deep' => '\\HasNoChildren',
        'sub2' => '\\HasChildren',
        'sub2/achild' => '\\HasNoChildren',
        'Alt Folders/INBOX' => '\\HasNoChildren \\Noinferiors',
        'Alt Folders/Inbox' => '\\HasChildren',
        'Alt Folders/Inbox/subnobody' => '\\HasChildren',
        'Alt Folders/Inbox/subnobody/deep' => '\\HasNoChildren',
        'Alt Folders/inbox' => '\\HasChildren',
        'Alt Folders/inbox/subnobody' => '\\HasChildren',
        'Alt Folders/inbox/subnobody/deep' => '\\HasNoChildren',
        'Other Users/bar@defdomain/Trash' => '\\HasNoChildren',
        'Other Users/foo@defdomain' => '\\HasChildren',
        'Other Users/foo@defdomain/really' => '\\HasChildren',
        'Other Users/foo@defdomain/really/deep' => '\\HasNoChildren',
        'Shared Folders/shared stuff@defdomain' => '\\HasChildren',
        'Shared Folders/shared stuff@defdomain/something' => '\\HasNoChildren',
    });

    xlog $self, "List *%";
    $data = $imaptalk->list("", "*%");
    $self->assert_mailbox_structure($data, '/', {
        'INBOX' => '\\HasChildren',
        'INBOX/sub' => '\\HasNoChildren',
        'INBOX/very' => '\\HasChildren',
        'INBOX/very/deep' => '\\HasChildren',
        'INBOX/very/deep/one' => '\\HasNoChildren',
        'AEARLY' => '\\HasNoChildren',
        'not' => '\\HasChildren',
        'not/so' => '\\HasChildren',
        'not/so/deep' => '\\HasNoChildren',
        'sub2' => '\\HasChildren',
        'sub2/achild' => '\\HasNoChildren',
        'Alt Folders' => '\\Noselect \\HasChildren',
        'Alt Folders/INBOX' => '\\HasNoChildren \\Noinferiors',
        'Alt Folders/inbox' => '\\HasChildren',
        'Alt Folders/inbox/subnobody' => '\\HasChildren',
        'Alt Folders/inbox/subnobody/deep' => '\\HasNoChildren',
        'Alt Folders/Inbox' => '\\HasChildren',
        'Alt Folders/Inbox/subnobody' => '\\HasChildren',
        'Alt Folders/Inbox/subnobody/deep' => '\\HasNoChildren',
        'Other Users' => '\\Noselect \\HasChildren',
        'Other Users/bar@defdomain' => '\\Noselect \\HasChildren',
        'Other Users/bar@defdomain/Trash' => '\\HasNoChildren',
        'Other Users/foo@defdomain' => '\\HasChildren',
        'Other Users/foo@defdomain/really' => '\\HasChildren',
        'Other Users/foo@defdomain/really/deep' => '\\HasNoChildren',
        'Shared Folders' => '\\Noselect \\HasChildren',
        'Shared Folders/shared stuff@defdomain' => '\\HasChildren',
        'Shared Folders/shared stuff@defdomain/something' => '\\HasNoChildren',
    });

    xlog $self, "List %";
    $data = $imaptalk->list("", "%");
    $self->assert_mailbox_structure($data, '/', {
        'INBOX' => '\\HasChildren',
        'AEARLY' => '\\HasNoChildren',
        'not' => '\\HasChildren',
        'sub2' => '\\HasChildren',
        'Alt Folders' => '\\Noselect \\HasChildren',
        'Other Users' => '\\Noselect \\HasChildren',
        'Shared Folders' => '\\Noselect \\HasChildren',
    });

    # check some partials

    xlog $self, "List INBOX/*";
    $data = $imaptalk->list("INBOX/", "*");
    $self->assert_mailbox_structure($data, '/', {
        'INBOX/sub' => '\\HasNoChildren',
        'INBOX/very' => '\\HasChildren',
        'INBOX/very/deep' => '\\HasChildren',
        'INBOX/very/deep/one' => '\\HasNoChildren',
    });

    xlog $self, "List INBOX/*%";
    $data = $imaptalk->list("INBOX/", "*%");
    $self->assert_mailbox_structure($data, '/', {
        'INBOX/sub' => '\\HasNoChildren',
        'INBOX/very' => '\\HasChildren',
        'INBOX/very/deep' => '\\HasChildren',
        'INBOX/very/deep/one' => '\\HasNoChildren',
    });

    xlog $self, "List INBOX/%";
    $data = $imaptalk->list("INBOX/", "%");
    $self->assert_mailbox_structure($data, '/', {
        'INBOX/sub' => '\\HasNoChildren',
        'INBOX/very' => '\\HasChildren',
    });

    xlog $self, "List AEARLY/*";
    $data = $imaptalk->list("AEARLY/", "*");
    $self->assert_mailbox_structure($data, '/', {});

    xlog $self, "List AEARLY/*%";
    $data = $imaptalk->list("AEARLY/", "*%");
    $self->assert_mailbox_structure($data, '/', {});

    xlog $self, "List AEARLY/%";
    $data = $imaptalk->list("AEARLY/", "%");
    $self->assert_mailbox_structure($data, '/', {});

    xlog $self, "List sub2/*";
    $data = $imaptalk->list("sub2/", "*");
    $self->assert_mailbox_structure($data, '/', {
        'sub2/achild' => '\\HasNoChildren',
    });

    xlog $self, "List sub2/*%";
    $data = $imaptalk->list("sub2/", "*%");
    $self->assert_mailbox_structure($data, '/', {
        'sub2/achild' => '\\HasNoChildren',
    });

    xlog $self, "List sub2/%";
    $data = $imaptalk->list("sub2/", "%");
    $self->assert_mailbox_structure($data, '/', {
        'sub2/achild' => '\\HasNoChildren',
    });

    xlog $self, "List Alt Folders/*";
    $data = $imaptalk->list("Alt Folders/", "*");
    $self->assert_mailbox_structure($data, '/', {
        'Alt Folders/INBOX' => '\\HasNoChildren \\Noinferiors',
        'Alt Folders/inbox' => '\\HasChildren',
        'Alt Folders/inbox/subnobody' => '\\HasChildren',
        'Alt Folders/inbox/subnobody/deep' => '\\HasNoChildren',
        'Alt Folders/Inbox' => '\\HasChildren',
        'Alt Folders/Inbox/subnobody' => '\\HasChildren',
        'Alt Folders/Inbox/subnobody/deep' => '\\HasNoChildren',
    });

    xlog $self, "List Alt Folders/*%";
    $data = $imaptalk->list("Alt Folders/", "*%");
    $self->assert_mailbox_structure($data, '/', {
        'Alt Folders/INBOX' => '\\HasNoChildren \\Noinferiors',
        'Alt Folders/inbox' => '\\HasChildren',
        'Alt Folders/inbox/subnobody' => '\\HasChildren',
        'Alt Folders/inbox/subnobody/deep' => '\\HasNoChildren',
        'Alt Folders/Inbox' => '\\HasChildren',
        'Alt Folders/Inbox/subnobody' => '\\HasChildren',
        'Alt Folders/Inbox/subnobody/deep' => '\\HasNoChildren',
    });

    xlog $self, "List Alt Folders/%";
    $data = $imaptalk->list("Alt Folders/", "%");
    $self->assert_mailbox_structure($data, '/', {
        'Alt Folders/INBOX' => '\\HasNoChildren \\Noinferiors',
        'Alt Folders/inbox' => '\\HasChildren',
        'Alt Folders/Inbox' => '\\HasChildren',
    });

    xlog $self, "List Other Users";
    $data = $imaptalk->list("", "Other Users");
    $self->assert_mailbox_structure($data, '/', {
        'Other Users' => '\\Noselect \\HasChildren',
    });

    xlog $self, "List Other Users/*";
    $data = $imaptalk->list("Other Users/", "*");
    $self->assert_mailbox_structure($data, '/', {
        'Other Users/bar@defdomain/Trash' => '\\HasNoChildren',
        'Other Users/foo@defdomain' => '\\HasChildren',
        'Other Users/foo@defdomain/really' => '\\HasChildren',
        'Other Users/foo@defdomain/really/deep' => '\\HasNoChildren',
    });

    xlog $self, "List Other Users/*%";
    $data = $imaptalk->list("Other Users/", "*%");
    $self->assert_mailbox_structure($data, '/', {
        'Other Users/bar@defdomain' => '\\Noselect \\HasChildren',
        'Other Users/bar@defdomain/Trash' => '\\HasNoChildren',
        'Other Users/foo@defdomain' => '\\HasChildren',
        'Other Users/foo@defdomain/really' => '\\HasChildren',
        'Other Users/foo@defdomain/really/deep' => '\\HasNoChildren',
    });

    xlog $self, "List Other Users/%";
    $data = $imaptalk->list("Other Users/", "%");
    $self->assert_mailbox_structure($data, '/', {
        'Other Users/bar@defdomain' => '\\Noselect \\HasChildren',
        'Other Users/foo@defdomain' => '\\HasChildren',
    });

}
