#!perl
use Cassandane::Tiny;

sub test_email_set_email_duplicates_mailbox_counts
    :min_version_3_1 :needs_component_sieve :needs_component_jmap
{
    my ($self) = @_;
    my $jmap = $self->{jmap};
    my $imap = $self->{store}->get_client();

    my $inboxid = $self->getinbox()->{id};

    # This is the opposite of a tooManyMailboxes error. It makes
    # sure that duplicate emails within a mailbox do not count
    # as multiple mailbox instances.

    my $accountCapabilities = $self->get_account_capabilities();
    my $maxMailboxesPerEmail = $accountCapabilities->{'urn:ietf:params:jmap:mail'}{maxMailboxesPerEmail};

    $self->assert($maxMailboxesPerEmail > 0);

    my $todo = $maxMailboxesPerEmail - 2;

    open(my $F, 'data/mime/simple.eml') || die $!;
    for (1..$todo) {
      $imap->create("INBOX.M$_") || die;

      # two copies in each folder
      $imap->append("INBOX.M$_", $F) || die $@;
    }
    close($F);

    my $res = $jmap->CallMethods([
        ['Email/query', { }, "R1"],
        ['Email/get', {
            '#ids' => {
                resultOf => 'R1',
                name => 'Email/query',
                path => '/ids'
            },
            properties => ['mailboxIds']
        }, 'R2'],
    ]);
    $self->assert_num_equals(1, scalar @{$res->[0][1]{ids}});
    $self->assert_num_equals($todo, scalar keys %{$res->[1][1]{list}[0]{mailboxIds}});

    my $emailId = $res->[0][1]{ids}[0];
    $res = $jmap->CallMethods([
        ['Email/set', {
            update => {
                $emailId => {
                    'keywords/foo' => JSON::true,
                    "mailboxIds/$inboxid" => JSON::true,
                },
            }
        }, 'R1'],
    ]);
    $self->assert(exists $res->[0][1]{updated}{$emailId});
}
