#!perl
use Cassandane::Tiny;

sub test_replace_across_backends
    :NoAltNamespace :min_version_3_9
    ($self)
{
    # :min_version_3_9 checks backend1 version.  The test below checks frontend
    my ($maj, $min) = Cassandane::Instance->get_version('murder');
    if ($maj < 3 || ($maj == 3 && $min < 9)) {
        return;
    }

    my $shared = 'shared';

    my $admintalk = $self->{backend2_adminstore}->get_client();

    # create a shared folder (on backend2)
    $admintalk->create($shared);
    $self->assert_str_equals('ok', $admintalk->get_last_completion_response());
    $admintalk->setacl($shared, 'anyone', 'lrswi');
    $self->assert_str_equals('ok', $admintalk->get_last_completion_response());

    # put some messages into the INBOX
    $self->make_message("Message A", store => $self->{frontend_store});
    $self->make_message("Message B", store => $self->{frontend_store});

    my $frontend = $self->{frontend_store}->get_client();

    my $res = $frontend->select('INBOX');
    $self->assert_str_equals('ok', $frontend->get_last_completion_response());

    # expunge the first message so that seqno != uid
    $frontend->store('1', '+flags', '(\\Deleted)');
    $frontend->expunge();

    my %exp;
    $exp{C} = $self->{gen}->generate(subject => "Message C", uid => 1);

    $res = $frontend->_imap_cmd('REPLACE', 0, '', "1", "shared",
                                { Literal => $exp{C}->as_string() });
    $self->assert_str_equals('ok', $frontend->get_last_completion_response());

    $self->check_messages({});

    $self->{frontend_store}->set_folder($shared);
    $self->check_messages(\%exp, store => $self->{frontend_store});
}
