]> Sergey Matveev's repositories - public-inbox.git/blobdiff - t/search.t
search: avoid creating ghosts for circular References
[public-inbox.git] / t / search.t
index 201578d4cc8a8cbf124a1cddad903c856bbb8a24..55abe9e8bdd083d10105168731ae96f56aabcc55 100644 (file)
@@ -3,8 +3,9 @@
 use strict;
 use warnings;
 use Test::More;
+eval { require PublicInbox::Search; };
+plan skip_all => "Xapian missing for search" if $@;
 use File::Temp qw/tempdir/;
-use PublicInbox::Search;
 use Email::MIME;
 use Data::Dumper;
 my $tmpdir = tempdir(CLEANUP => 1);
@@ -17,12 +18,16 @@ ok($@, "exception raised on non-existent DB");
 
 my $rw = PublicInbox::Search->new($git_dir, 1);
 my $ro = PublicInbox::Search->new($git_dir);
+my $rw_commit = sub {
+       $rw = undef;
+       $rw = PublicInbox::Search->new($git_dir, 1);
+};
 
 {
        my $root = Email::MIME->create(
                header_str => [
                        Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
-                       Subject => 'hello world',
+                       Subject => 'Hello world',
                        'Message-ID' => '<root@s>',
                        From => 'John Smith <js@example.com>',
                        To => 'list@example.com',
@@ -31,7 +36,7 @@ my $ro = PublicInbox::Search->new($git_dir);
        my $last = Email::MIME->create(
                header_str => [
                        Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
-                       Subject => 'Re: hello world',
+                       Subject => 'Re: Hello world',
                        'In-Reply-To' => '<root@s>',
                        'Message-ID' => '<last@s>',
                        From => 'John Smith <js@example.com>',
@@ -48,10 +53,11 @@ my $ro = PublicInbox::Search->new($git_dir);
 
 sub filter_mids {
        my ($res) = @_;
-       sort(map { (split(/\n/, $_))[0] } @{$res->{msgs}});
+       sort(map { $_->mid } @{$res->{msgs}});
 }
 
 {
+       $rw_commit->();
        $ro->reopen;
        my $found = $ro->lookup_message('<root@s>');
        ok($found, "message found");
@@ -70,19 +76,19 @@ sub filter_mids {
                is($res->{count}, 0, "path variant `$p' does not match");
        }
 
-       $res = $ro->query('subject:(hello world)');
+       $res = $ro->query('subject:(Hello world)');
        @res = filter_mids($res);
        is_deeply(\@res, \@exp, 'got expected results for subject:() match');
 
-       $res = $ro->query('subject:"hello world"');
+       $res = $ro->query('subject:"Hello world"');
        @res = filter_mids($res);
        is_deeply(\@res, \@exp, 'got expected results for subject:"" match');
 
-       $res = $ro->query('subject:"hello world"', {limit => 1});
+       $res = $ro->query('subject:"Hello world"', {limit => 1});
        is(scalar @{$res->{msgs}}, 1, "limit works");
        my $first = $res->{msgs}->[0];
 
-       $res = $ro->query('subject:"hello world"', {offset => 1});
+       $res = $ro->query('subject:"Hello world"', {offset => 1});
        is(scalar @{$res->{msgs}}, 1, "offset works");
        my $second = $res->{msgs}->[0];
 
@@ -100,7 +106,7 @@ sub filter_mids {
 
 # ghost vivication
 {
-       $rw->reopen;
+       $rw_commit->();
        my $rmid = '<ghost-message@s>';
        my $reply_to_ghost = Email::MIME->create(
                header_str => [
@@ -134,6 +140,7 @@ sub filter_mids {
 
 # search thread on ghost
 {
+       $rw_commit->();
        $ro->reopen;
 
        # Subject:
@@ -144,13 +151,12 @@ sub filter_mids {
 
        # body
        $res = $ro->query('goodbye');
-       is((split(/\n/, $res->{msgs}->[0]))[0], 'last@s',
-          'got goodbye message body');
+       is($res->{msgs}->[0]->mid, 'last@s', 'got goodbye message body');
 }
 
 # long message-id
 {
-       $rw->reopen;
+       $rw_commit->();
        $ro->reopen;
        my $long_mid = 'last' . ('x' x 60). '@s';
        my $long_midc = Digest::SHA::sha1_hex($long_mid);
@@ -169,15 +175,16 @@ sub filter_mids {
        my $long_id = $rw->add_message($long);
        is($long_id, int($long_id), "long_id is an integer: $long_id");
 
+       $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 $replies = $ro->get_replies('root@s');
-       $replies = [ filter_mids($replies) ];
-       is_deeply($replies, [ filter_mids($res) ], "get_replies matches");
+       my $followups = $ro->get_followups('root@s');
+       $followups = [ filter_mids($followups) ];
+       is_deeply($followups, [ filter_mids($res) ], "get_followups matches");
 
        my $long_reply_mid = 'reply-to-long@1';
        my $long_reply = Email::MIME->create(
@@ -194,6 +201,7 @@ sub filter_mids {
                body => "no References\n");
        ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
 
+       $rw_commit->();
        $ro->reopen;
        my $t = $ro->get_thread('root@s');
        is($t->{count}, 4, "got all 4 mesages in thread");
@@ -204,11 +212,11 @@ sub filter_mids {
 
 # quote prioritization
 {
-       $rw->reopen;
+       $rw_commit->();
        $rw->add_message(Email::MIME->create(
                header_str => [
                        Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
-                       Subject => 'hello',
+                       Subject => 'Hello',
                        'Message-ID' => '<quote@a>',
                        From => 'Quoter <quoter@example.com>',
                        To => 'list@example.com',
@@ -218,7 +226,7 @@ sub filter_mids {
        $rw->add_message(Email::MIME->create(
                header_str => [
                        Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
-                       Subject => 'hello',
+                       Subject => 'Hello',
                        'Message-ID' => '<nquote@a>',
                        From => 'Non-Quoter<non-quoter@example.com>',
                        To => 'list@example.com',
@@ -226,15 +234,34 @@ sub filter_mids {
                body => "theatre\nfade\n"));
        my $res = $rw->query("theatre");
        is($res->{count}, 2, "got both matches");
-       like($res->{msgs}->[0], qr/\Anquote\@a/, "non-quoted scores higher");
-       like($res->{msgs}->[1], qr/\Aquote\@a/, "quoted result still returned");
+       is($res->{msgs}->[0]->mid, 'nquote@a', "non-quoted scores higher");
+       is($res->{msgs}->[1]->mid, 'quote@a', "quoted result still returned");
 
        $res = $rw->query("illusions");
        is($res->{count}, 1, "got a match for quoted text");
-       like($res->{msgs}->[0], qr/\Aquote\@a/,
+       is($res->{msgs}->[0]->mid, 'quote@a',
                "quoted result returned if nothing else");
 }
 
+# circular references
+{
+       my $doc_id = $rw->add_message(Email::MIME->create(
+               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>',
+                       From => 'Circle <circle@example.com>',
+                       To => 'list@example.com',
+               ],
+               body => "LOOP!\n"));
+       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");
+}
+
 done_testing();
 
 1;