#!perl
use Cassandane::Tiny;

sub test_normalize_snippets
    :min_version_3_0
{
    my ($self) = @_;

    # Set up test message with funny characters
use utf8;
    my @terms = ( "gären", "советской", "diĝir", "naïve", "léger" );
no utf8;
    my $body = encode_base64(encode('UTF-8', join(' ', @terms)));
    $body =~ s/\r?\n/\r\n/gs;

    xlog $self, "Generate and index test messages.";
    my %params = (
        mime_charset => "utf-8",
        mime_encoding => 'base64',
        body => $body,
    );
    $self->make_message("1", %params) || die;

    $self->{instance}->run_command({cyrus => 1}, 'squatter');

    my $talk = $self->{store}->get_client();

    # Connect to IMAP
    xlog $self, "Select INBOX";
    my $r = $talk->select("INBOX") || die;
    my $uidvalidity = $talk->get_response_code('uidvalidity');
    my $uids = $talk->search('1:*', 'NOT', 'DELETED');

    # Assert that diacritics are matched and returned
    foreach my $term (@terms) {
        $r = $self->get_snippets('INBOX', $uids, { text => $term });
        $self->assert_num_not_equals(index($r->{snippets}[0][3], "<mark>$term</mark>"), -1);
    }

    # Assert that search without diacritics matches
    if ($self->{skipdiacrit}) {
        my $term = "naive";
        xlog $self, "Get snippets for FUZZY text \"$term\"";
        $r = $self->get_snippets('INBOX', $uids, { 'text' => $term });
use utf8;
        $self->assert_num_not_equals(index($r->{snippets}[0][3], "<mark>naïve</mark>"), -1);
no utf8;
    }

}
