#!perl
use Cassandane::Tiny;

sub test_mailbox_version
    :Conversations
    ($self)
{
    my $admintalk = $self->{adminstore}->get_client();

    xlog $self, "Create user INBOX with index v19";
    $admintalk->_imap_cmd('CREATE', 0, '',
                          "user.other", [ 'VERSION', '19' ]);
    $self->assert_str_equals('ok', $admintalk->get_last_completion_response());

    xlog $self, "Verify INBOX with index v19";
    my $dir = $self->{instance}->folder_to_directory('user.other');
    my $file = "$dir/cyrus.index";
    my $fh = IO::File->new($file, "+<");
    die "NO SUCH FILE $file" unless $fh;
    my $index = Cyrus::IndexFile->new($fh);
    $self->assert_num_equals(19, $index->header('MinorVersion'));

    xlog $self, "Create user INBOX.foo";
    $admintalk->create('user.other.foo');

    xlog $self, "Verify INBOX.foo with index v19";
    $dir = $self->{instance}->folder_to_directory('user.other.foo');
    $file = "$dir/cyrus.index";
    $fh = IO::File->new($file, "+<");
    die "NO SUCH FILE $file" unless $fh;
    $index = Cyrus::IndexFile->new($fh);
    $self->assert_num_equals(19, $index->header('MinorVersion'));

    xlog $self, "Verify conv.db is v1";
    my $basedir = $self->{instance}->{basedir};
    my $outfile = "$basedir/conv-output.txt";
    $self->{instance}->run_command({ cyrus => 1,
                                     redirects => { stdout => $outfile } },
                                   'ctl_conversationsdb', '-d', 'other');
    my $data = slurp_file($outfile);
    $self->assert_matches(qr/\$VERSION\t1/, $data);

    xlog $self, "Create user INBOX with index v20 and enable compactids";
    $admintalk->_imap_cmd('CREATE', 0, '',
                          "user.other2", [ 'VERSION', '20', 'COMPACTIDS' ]);
    $self->assert_str_equals('ok', $admintalk->get_last_completion_response());

    xlog $self, "Verify INBOX with index v20";
    $dir = $self->{instance}->folder_to_directory('user.other2');
    $file = "$dir/cyrus.index";
    $fh = IO::File->new($file, "+<");
    die "NO SUCH FILE $file" unless $fh;
    $index = Cyrus::IndexFile->new($fh);
    $self->assert_num_equals(20, $index->header('MinorVersion'));

    xlog $self, "Verify conv.db is v2 and compactids are enabled";
    my $outfile2 = "$basedir/conv-output.txt";
    $self->{instance}->run_command({ cyrus => 1,
                                     redirects => { stdout => $outfile2 } },
                                   'ctl_conversationsdb', '-d', 'other2');
    $data = slurp_file($outfile2);
    $self->assert_matches(qr/\$VERSION\t2/, $data);
    $self->assert_matches(qr/\$COMPACT_EMAILIDS\t1/, $data);
}
