#!perl
use Cassandane::Tiny;

# A creation id from one AddressBook/set call can be used by a later call's
# onSuccessSetIsDefault.
sub test_addressbook_set_isdefault_with_creationid ($self)
{
    my $jmap = $self->default_user->jmap;

    xlog $self, "create an addressbook, then make it default in a later call";
    my $res = $jmap->request([
        [ 'AddressBook/set' => {
            create => { 1 => { name => 'foo' } },
        } ],
        [ 'AddressBook/set' => {
            onSuccessSetIsDefault => '#1',
        } ],
    ]);
    my ($create, $set_default) = map { $_->as_set } $res->sentences;

    xlog $self, "the first call created the addressbook";
    my $abookid = $create->created_id(1);
    $self->assert_not_null($abookid);
    $self->assert_cmp_deeply({}, $create->create_errors);

    xlog $self, "the second call made the new addressbook the default";
    $self->assert_cmp_deeply(
        superhashof({ $abookid => superhashof({ isDefault => JSON::true }) }),
        $set_default->updated,
    );
    $self->assert_cmp_deeply({}, $set_default->update_errors);
}
