#!perl
use Cassandane::Tiny;

sub test_contactgroup_set_cardgroup_get ($self)
{
    my $jmap = $self->{jmap};

    xlog $self, "create contacts";
    my $res = $jmap->CallMethods([['Contact/set', {create => {
                        "1" => { firstName => "foo", lastName => "last1" },
                        "2" => { firstName => "bar", lastName => "last2" }
                    }}, "R1"]]);
    my $contact1 = $res->[0][1]{created}{"1"}{id};
    my $contact2 = $res->[0][1]{created}{"2"}{id};

    xlog $self, "create contact group";
    $res = $jmap->CallMethods([['ContactGroup/set', {
        create => {
            "1" => {
                name => "group1",
                contactIds => [$contact1, $contact2],
                otherAccountContactIds => {
                    other1 => ['contact3'],
                    other2 => ['contact4']
                }
            }
        }
    }, "R2"]]);

    xlog $self, "get contact group";
    $res = $jmap->CallMethods([['ContactCard/get', { }, "R3"]]);
    $self->assert_str_equals('group', $res->[0][1]{list}[2]{kind});
    $self->assert_str_equals('group1', $res->[0][1]{list}[2]{name}{full});
    $self->assert_deep_equals({
        $contact1 => JSON::true,
        $contact2 => JSON::true,
        contact3 => JSON::true,
        contact4 => JSON::true
    }, $res->[0][1]{list}[2]{members});
}
