#!perl
use Cassandane::Tiny;

sub test_sync_collection
{
    my ($self) = @_;

    my $CardDAV = $self->{carddav};

    my $homeset = "/dav/addressbooks/user/cassandane";
    my $bookId = "Default";

    my $uid1 = "3b678b69-ca41-461e-b2c7-f96b9fe48d68";
    my $uid2 = "addr1\@example.com";
    my $uid3 = "addr2\@example.com";

    my $vcard1 = Net::CardDAVTalk::VCard->new_fromstring(<<EOF);
BEGIN:VCARD
VERSION:3.0
UID:$uid1
N:Gump;Forrest;;Mr.
FN:Forrest Gump
ORG:Bubba Gump Shrimp Co.
TITLE:Shrimp Man
REV:2008-04-24T19:52:43Z
END:VCARD
EOF

    my $vcard2 = Net::CardDAVTalk::VCard->new_fromstring(<<EOF);
BEGIN:VCARD
VERSION:4.0
NICKNAME:me
UID:$uid2
FN:Cyrus Daboo
EMAIL:cdaboo\@example.com
END:VCARD
EOF

    my $vcard3 = Net::CardDAVTalk::VCard->new_fromstring(<<EOF);
BEGIN:VCARD
VERSION:4.0
NICKNAME:eric
UID:$uid3
FN:Eric York
END:VCARD
EOF

    my $href1 = $CardDAV->NewContact($bookId, $vcard1);
    my $href2 = $CardDAV->NewContact($bookId, $vcard2);

    my ($adds, $removes, $errors, $syncToken) =
        $CardDAV->SyncContactLinks($bookId);

    $self->assert_equals(scalar %$adds, 2);
    $self->assert_not_null($adds->{"$homeset/$href1"});
    $self->assert_not_null($adds->{"$homeset/$href2"});
    $self->assert_deep_equals($removes, []);
    $self->assert_deep_equals($errors, []);

    $CardDAV->DeleteContact("$homeset/$href1");

    my $href3 = $CardDAV->NewContact($bookId, $vcard3);

    ($adds, $removes, $errors, $syncToken) =
        $CardDAV->SyncContactLinks($bookId, syncToken => $syncToken);

    $self->assert_equals(scalar %$adds, 1);
    $self->assert_not_null($adds->{"$homeset/$href3"});
    $self->assert_equals(scalar @$removes, 1);
    $self->assert_str_equals("$homeset/$href1", $removes->[0]);
    $self->assert_deep_equals($errors, []);

    ($adds, $removes, $errors, $syncToken) =
        $CardDAV->SyncContactLinks($bookId, syncToken => $syncToken);

    $self->assert_deep_equals($adds, {});
    $self->assert_deep_equals($removes, []);
    $self->assert_deep_equals($errors, []);
}
