#!perl
use Cassandane::Tiny;

sub test_create_inherit_color
    :min_version_3_9 :AltNameSpace :needs_component_jmap
    :want_service_http
{
    my ($self) = @_;

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

    xlog $self, "Create mailbox with color";
    my $res = $jmap->CallMethods([['Mailbox/set', {
        create => {
            1 => {
                parentId => JSON::null,
                name => 'foo',
                color => "coral",
            },
        },
    }, "R1"]]);
    $self->assert_not_null($res->[0][1]{created}{1});

    my $hitfolder = "foo.bar";

    xlog $self, "Install the sieve script";
    my $scriptname = 'flatPack';
    $self->{instance}->install_sieve_script(<<EOF
require ["fileinto", "mailbox"];
fileinto :create "$hitfolder";
EOF
    );

    xlog $self, "Deliver a message";
    my $msg = $self->{gen}->generate(subject => "msg1");
    $self->{instance}->deliver($msg);

    xlog $self, "Check that the message made it";
    my $talk = $self->{store}->get_client();
    $talk->select($hitfolder);
    $self->assert_num_equals(1, $talk->get_response_code('exists'));

    xlog $self, "Check that :created mailbox inherited color";
    $res = $jmap->CallMethods([['Mailbox/get', {}, "R1"]]);
    my %m = map { $_->{name} => $_ } @{$res->[0][1]{list}};
    $self->assert_str_equals("coral", $m{"bar"}->{color});
}
