#!perl
use Cassandane::Tiny;

# The vnd.fastmail.jmapStates event parameter reports a state string per JMAP
# data type.  It is built from the canonical jmap_data_types table, so it must
# list the modern types (AddressBook, ContactCard) and must NOT list the types
# that were superseded (Contact, ContactGroup) or that have no state counter
# (Racl).  We also check that a state advances when its type changes.

# Returns the jmapStates hash from the (single) MessageNew event produced by
# delivering one message.
sub _deliver_and_states ($self, $subject, $uid) {
    $self->{instance}->getnotify();

    my $msg = $self->{gen}->generate(subject => $subject, uid => $uid);
    my $ret = $self->{instance}->deliver($msg, user => "cassandane");
    $self->assert_num_equals(0, $ret);

    my @events = map { decode_json($_->{MESSAGE}) }
                 $self->{instance}->getnotify->@*;
    my ($new) = grep { $_->{event} eq 'MessageNew' } @events;
    $self->assert_not_null($new, "got a MessageNew event");

    my $states = $new->{'vnd.fastmail.jmapStates'};
    $self->assert_not_null($states, "MessageNew carried vnd.fastmail.jmapStates");
    return $states;
}

sub test_jmap_states ($self) {
    my $states = $self->_deliver_and_states("first", 1);

    # These are the types where (for compact ids, which are on by default in
    # test users) we have a "J" prefix.
    my %expect_j = map {; $_ => 1 } qw( Mailbox Email Calendar CalendarEvent );

    # The modern type set, drawn from the canonical jmap_data_types table.
    for my $type (qw(
        Mailbox Email EmailSubmission
        Calendar CalendarEvent
        AddressBook ContactCard
        Note SieveScript VacationResponse
        Quota
    )) {
        $self->assert_not_null($states->{$type}, "jmapStates has $type");

        my $maybe_j = $expect_j{$type} ? 'J' : q{};
        # A state string is an optional "J" prefix followed by a modseq.
        $self->assert_matches(
            qr/^${maybe_j}\d+$/,
            $states->{$type},
            "jmapStates $type looks about right",
        );
    }

    # Superseded or stateless types must not appear.
    for my $type (qw(Contact ContactGroup Racl)) {
        $self->assert(! exists $states->{$type}, "jmapStates omits $type");
    }

    my sub strip ($n) { $n =~ s/^J//; return $n + 0 }

    # Delivering another message must advance the Email/Mailbox state.
    my $states2 = $self->_deliver_and_states("second", 2);

    $self->assert(
        strip($states2->{Email}) > strip($states->{Email}),
        "Email state advanced after a new message ($states->{Email} -> $states2->{Email})"
    );

    # With no quota root, nothing has touched the quota counter.
    $self->assert_str_equals($states->{Quota}, $states2->{Quota});

    # Creating a quota root writes a quota record, which must advance the
    # Quota state reported by the next event.
    my $admintalk = $self->{adminstore}->get_client();
    $admintalk->setquota('user.cassandane', [ 'STORAGE', 100000 ]);
    $self->assert_str_equals('ok', $admintalk->get_last_completion_response());

    my $states3 = $self->_deliver_and_states("third", 3);

    $self->assert(
        strip($states3->{Quota}) > strip($states2->{Quota}),
        "Quota state advanced after setquota ($states2->{Quota} -> $states3->{Quota})"
    );
}
