#!perl
use Cassandane::Tiny;

sub test_contact_update_grouped_property
    :min_version_3_9 :needs_component_jmap
{
    my ($self) = @_;
    my $jmap = $self->{jmap};

    my $service = $self->{instance}->get_service("http");
    $ENV{DEBUGDAV} = 1;
    my $carddav = Net::CardDAVTalk->new(
        user => 'cassandane',
        password => 'pass',
        host => $service->host(),
        port => $service->port(),
        scheme => 'http',
        url => '/',
        expandurl => 1,
    );

    my $id = 'ae2640cc-234a-4dd9-95cc-3106258445b9';
    my $href = "Default/$id.vcf";
    my $card = <<EOF;
BEGIN:VCARD
VERSION:3.0
UID:$id
N:Gump;Forrest;;Mr.
FN:Forrest Gump
ITEM1.ORG:Bubba Gump Shrimp Co.
ITEM1.X-ABLABEL:
TITLE:Shrimp Man
REV:2008-04-24T19:52:43Z
CATEGORIES:cat1,cat2
END:VCARD
EOF

    $card =~ s/\r?\n/\r\n/gs;
    $carddav->Request('PUT', $href, $card, 'Content-Type' => 'text/vcard');

    my $res = $jmap->CallMethods([
        ['Contact/get', {
        }, 'R1']
    ]);

    $self->assert_equals("Bubba Gump Shrimp Co.",
                         $res->[0][1]{list}[0]{company});

    $res = $jmap->CallMethods([
        ['Contact/set', {
            update => {$id => { company => "BGSCO" }}
        }, "R1"],
        ['Contact/get', {
        }, 'R2']
    ]);

    $self->assert_equals("BGSCO", $res->[1][1]{list}[0]{company});

    $res = $carddav->Request('GET', $href);

    my $newcard = $res->{content};
    $newcard =~ s/\r?\n[ \t]+//gs;  # unfold long properties

    $self->assert_matches(qr/\nITEM1.ORG:BGSCO/, $newcard);
    $self->assert_does_not_match(qr/\nORG:/, $newcard);
}
