#!perl
use Cassandane::Tiny;

sub test_mailbox_move_to_deleted_parent
    :min_version_3_6
    ($self)
{
    my $jmap = $self->{jmap};

    # Assert mailboxes are created in the right order.
    my $res = $jmap->CallMethods([["Mailbox/set", {
        create => {
            "C" => { name => "C", parentId => "#B",  role => undef },
            "B" => { name => "B", parentId => undef, role => undef },
            "A" => { name => "A", parentId => undef, role => undef },
        }
    } ]]);

    $self->assert_not_null($res->[0][1]{created}{A});
    $self->assert_not_null($res->[0][1]{created}{B});
    $self->assert_not_null($res->[0][1]{created}{C});
    my $idA = $res->[0][1]{created}{A}{id};
    my $idB = $res->[0][1]{created}{B}{id};
    my $idC = $res->[0][1]{created}{C}{id};

    # Destroy "A"
    $res = $jmap->CallMethods([['Mailbox/set', {
        destroy => [ $idA ],
    }, "R1"]]);
    $self->assert_num_equals(1, scalar @{$res->[0][1]{destroyed}});
    $self->assert_null($res->[0][1]{notDestroyed});

    # Try to move "B" under a non-existant mailbox
    $res = $jmap->CallMethods([['Mailbox/set', {
        update => {
            $idB => { parentId => "nosuchid" },
        }
    }, "R1"]]);
    $self->assert_null($res->[0][1]{updated});
    $self->assert_not_null($res->[0][1]{notUpdated});

    # Try to move "B" under "A"
    $res = $jmap->CallMethods([['Mailbox/set', {
        update => {
            $idB => { parentId => $idA },
        }
    }, "R1"]]);
    $self->assert_null($res->[0][1]{updated});
    $self->assert_not_null($res->[0][1]{notUpdated});

}
