]> Sergey Matveev's repositories - public-inbox.git/blobdiff - t/search.t
search: allow searching user fields (To/Cc/From)
[public-inbox.git] / t / search.t
index 89a2862951b87dbebe5d3f4f88fd2e97d65c0aae..bb0861a10acd46c71d1eeb021bc599068991db99 100644 (file)
@@ -1,14 +1,13 @@
-# Copyright (C) 2015, all contributors <meta@public-inbox.org>
+# Copyright (C) 2015 all contributors <meta@public-inbox.org>
 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
 use strict;
 use warnings;
 use Test::More;
-eval { require PublicInbox::Search; };
+eval { require PublicInbox::SearchIdx; };
 plan skip_all => "Xapian missing for search" if $@;
 use File::Temp qw/tempdir/;
 use Email::MIME;
-use Data::Dumper;
-my $tmpdir = tempdir(CLEANUP => 1);
+my $tmpdir = tempdir('pi-search-XXXXXX', TMPDIR => 1, CLEANUP => 1);
 my $git_dir = "$tmpdir/a.git";
 my ($root_id, $last_id);
 
@@ -16,13 +15,59 @@ is(0, system(qw(git init -q --bare), $git_dir), "git init (main)");
 eval { PublicInbox::Search->new($git_dir) };
 ok($@, "exception raised on non-existent DB");
 
-my $rw = PublicInbox::Search->new($git_dir, 1);
+{
+       my $orig = "FOO " x 30;
+       my $summ = PublicInbox::Search::subject_summary($orig);
+
+       $summ = length($summ);
+       $orig = length($orig);
+       ok($summ < $orig && $summ > 0, "summary shortened ($orig => $summ)");
+
+       $orig = "FOO" x 30;
+       $summ = PublicInbox::Search::subject_summary($orig);
+
+       $summ = length($summ);
+       $orig = length($orig);
+       ok($summ < $orig && $summ > 0,
+          "summary shortened but not empty: $summ");
+}
+
+my $rw = PublicInbox::SearchIdx->new($git_dir, 1);
+$rw->_xdb_acquire;
+$rw->_xdb_release;
+$rw = undef;
 my $ro = PublicInbox::Search->new($git_dir);
 my $rw_commit = sub {
-       $rw = undef;
-       $rw = PublicInbox::Search->new($git_dir, 1);
+       $rw->{xdb}->commit_transaction if $rw && $rw->{xdb};
+       $rw = PublicInbox::SearchIdx->new($git_dir, 1);
+       $rw->_xdb_acquire->begin_transaction;
 };
 
+{
+       # git repository perms
+       is(PublicInbox::SearchIdx->_git_config_perm(undef),
+          &PublicInbox::SearchIdx::PERM_GROUP,
+          "undefined permission is group");
+       is(PublicInbox::SearchIdx::_umask_for(
+            PublicInbox::SearchIdx->_git_config_perm('0644')),
+          0022, "644 => umask(0022)");
+       is(PublicInbox::SearchIdx::_umask_for(
+            PublicInbox::SearchIdx->_git_config_perm('0600')),
+          0077, "600 => umask(0077)");
+       is(PublicInbox::SearchIdx::_umask_for(
+            PublicInbox::SearchIdx->_git_config_perm('0640')),
+          0027, "640 => umask(0027)");
+       is(PublicInbox::SearchIdx::_umask_for(
+            PublicInbox::SearchIdx->_git_config_perm('group')),
+          0007, 'group => umask(0007)');
+       is(PublicInbox::SearchIdx::_umask_for(
+            PublicInbox::SearchIdx->_git_config_perm('everybody')),
+          0002, 'everybody => umask(0002)');
+       is(PublicInbox::SearchIdx::_umask_for(
+            PublicInbox::SearchIdx->_git_config_perm('umask')),
+          umask, 'umask => existing umask');
+}
+
 {
        my $root = Email::MIME->create(
                header_str => [
@@ -41,10 +86,12 @@ my $rw_commit = sub {
                        'Message-ID' => '<last@s>',
                        From => 'John Smith <js@example.com>',
                        To => 'list@example.com',
+                       Cc => 'foo@example.com',
                ],
                body => "goodbye forever :<\n");
 
        my $rv;
+       $rw_commit->();
        $root_id = $rw->add_message($root);
        is($root_id, int($root_id), "root_id is an integer: $root_id");
        $last_id = $rw->add_message($last);
@@ -93,15 +140,6 @@ sub filter_mids {
        my $second = $res->{msgs}->[0];
 
        isnt($first, $second, "offset returned different result from limit");
-
-       foreach my $f (qw(inreplyto references)) {
-               $res = $ro->query($f . ':root@s');
-               @res = filter_mids($res);
-               is_deeply(\@res, [ 'last@s' ],
-                         "got expected results for $f: match");
-               $res = $ro->query($f . ':root');
-               is($res->{total}, 0, "no partial mid match");
-       }
 }
 
 # ghost vivication
@@ -159,7 +197,6 @@ sub filter_mids {
        $rw_commit->();
        $ro->reopen;
        my $long_mid = 'last' . ('x' x 60). '@s';
-       my $long_midc = Digest::SHA::sha1_hex($long_mid);
 
        my $long = Email::MIME->create(
                header_str => [
@@ -177,14 +214,8 @@ sub filter_mids {
 
        $rw_commit->();
        $ro->reopen;
-       my $res = $ro->query('references:root@s');
-       my @res = filter_mids($res);
-       is_deeply(\@res, [ sort('last@s', $long_midc) ],
-                 "got expected results for references: match");
-
-       my $followups = $ro->get_followups('root@s');
-       $followups = [ filter_mids($followups) ];
-       is_deeply($followups, [ filter_mids($res) ], "get_followups matches");
+       my $res;
+       my @res;
 
        my $long_reply_mid = 'reply-to-long@1';
        my $long_reply = Email::MIME->create(
@@ -205,7 +236,7 @@ sub filter_mids {
        $ro->reopen;
        my $t = $ro->get_thread('root@s');
        is($t->{total}, 4, "got all 4 mesages in thread");
-       my @exp = sort($long_reply_mid, 'root@s', 'last@s', $long_midc);
+       my @exp = sort($long_reply_mid, 'root@s', 'last@s', $long_mid);
        @res = filter_mids($t);
        is_deeply(\@res, \@exp, "get_thread works");
 }
@@ -245,10 +276,11 @@ sub filter_mids {
 
 # circular references
 {
+       my $s = 'foo://'. ('Circle' x 15).'/foo';
        my $doc_id = $rw->add_message(Email::MIME->create(
+               header => [ Subject => $s ],
                header_str => [
                        Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
-                       Subject => 'Circle',
                        'Message-ID' => '<circle@a>',
                        'References' => '<circle@a>',
                        'In-Reply-To' => '<circle@a>',
@@ -259,7 +291,74 @@ sub filter_mids {
        ok($doc_id > 0, "doc_id defined with circular reference");
        my $smsg = $rw->lookup_message('circle@a');
        $smsg->ensure_metadata;
-       is($smsg->{references}, undef, "no references created");
+       is($smsg->references, '', "no references created");
+       my $msg = PublicInbox::SearchMsg->load_doc($smsg->{doc});
+       is($s, $msg->mini_mime->header('Subject'), 'long subject not rewritten');
+}
+
+{
+       my $str = eval {
+               my $mbox = 't/utf8.mbox';
+               open(my $fh, '<', $mbox) or die "failed to open mbox: $mbox\n";
+               local $/;
+               <$fh>
+       };
+       $str =~ s/\AFrom [^\n]+\n//s;
+       my $mime = Email::MIME->new($str);
+       my $doc_id = $rw->add_message($mime);
+       ok($doc_id > 0, 'message indexed doc_id with UTF-8');
+       my $smsg = $rw->lookup_message('testmessage@example.com');
+       my $msg = PublicInbox::SearchMsg->load_doc($smsg->{doc});
+
+       # mini_mime technically not valid (I think),
+       # but good enough for displaying HTML:
+       is($mime->header('Subject'), $msg->mini_mime->header('Subject'),
+               'UTF-8 subject preserved');
+}
+
+{
+       my $res = $ro->query('d:19931002..20101002');
+       ok(scalar @{$res->{msgs}} > 0, 'got results within range');
+       $res = $ro->query('d:20101003..');
+       is(scalar @{$res->{msgs}}, 0, 'nothing after 20101003');
+       $res = $ro->query('d:..19931001');
+       is(scalar @{$res->{msgs}}, 0, 'nothing before 19931001');
+}
+
+# names and addresses
+{
+       my $res = $ro->query('t:list@example.com');
+       is(scalar @{$res->{msgs}}, 6, 'searched To: successfully');
+       foreach my $smsg (@{$res->{msgs}}) {
+               like($smsg->to, qr/\blist\@example\.com\b/, 'to appears');
+       }
+
+       $res = $ro->query('tc:list@example.com');
+       is(scalar @{$res->{msgs}}, 6, 'searched To+Cc: successfully');
+       foreach my $smsg (@{$res->{msgs}}) {
+               my $tocc = join("\n", $smsg->to, $smsg->cc);
+               like($tocc, qr/\blist\@example\.com\b/, 'tocc appears');
+       }
+
+       foreach my $pfx ('tcf:', 'c:') {
+               $res = $ro->query($pfx . 'foo@example.com');
+               is(scalar @{$res->{msgs}}, 1,
+                       "searched $pfx successfully for Cc:");
+               foreach my $smsg (@{$res->{msgs}}) {
+                       like($smsg->cc, qr/\bfoo\@example\.com\b/,
+                               'cc appears');
+               }
+       }
+
+       foreach my $pfx ('', 'tcf:', 'f:') {
+               $res = $ro->query($pfx . 'Laggy');
+               is(scalar @{$res->{msgs}}, 1,
+                       "searched $pfx successfully for From:");
+               foreach my $smsg (@{$res->{msgs}}) {
+                       like($smsg->from, qr/Laggy Sender/,
+                               "From appears with $pfx");
+               }
+       }
 }
 
 done_testing();