#!perl
use Cassandane::Tiny;

sub test_mailbox_rename_to_clash_both
    :min_version_3_3
    :JMAPExtensions
{
    my ($self) = @_;

    my $jmap = $self->{jmap};

    # we need the mail extensions for isSeenShared
    my @using = @{ $jmap->DefaultUsing() };
    push @using, 'https://cyrusimap.org/ns/jmap/mail';
    $jmap->DefaultUsing(\@using);

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

    xlog $self, "create mailboxes";
    $imaptalk->create("INBOX.Foo") || die;
    $imaptalk->create("INBOX.Foo.A") || die;
    $imaptalk->create("INBOX.Bar") || die;
    $imaptalk->create("INBOX.Bar.B") || die;

    xlog $self, "fetch mailboxes";
    my $res = $jmap->Call('Mailbox/get', {});
    my %mboxids = map { $_->{name} => $_->{id} } @{$res->{list}};

    xlog $self, "move INBOX.Foo.A to INBOX.Bar.B";
    $res = $jmap->Call('Mailbox/set', {
      update => {
        $mboxids{A} => {
          parentId => $mboxids{Bar},
          name => "B",
        }
      }
    });

    # rejected due to name existing
    $self->assert_str_equals("name", $res->{notUpdated}{$mboxids{A}}{properties}[0]);

    $res = $jmap->Call('Mailbox/get', {});
    my %mboxids2 = map { $_->{name} => $_->{id} } @{$res->{list}};
    $self->assert_deep_equals(\%mboxids, \%mboxids2);

    # there were no renames
    $self->assert_syslog_does_not_match($self->{instance},
                                        qr/auditlog: rename/);
}
