2 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
7 use PublicInbox::TestCommon;
8 require_mods(qw(DBD::SQLite Search::Xapian));
10 require_ok 'PublicInbox::LeiStore';
11 require_ok 'PublicInbox::ExtSearch';
12 my ($home, $for_destroy) = tmpdir();
13 my $opt = { 1 => \(my $out = ''), 2 => \(my $err = '') };
14 my $store_dir = "$home/sto";
15 local $ENV{GIT_COMMITTER_EMAIL} = 'lei@example.com';
16 local $ENV{GIT_COMMITTER_NAME} = 'lei user';
17 my $sto = PublicInbox::LeiStore->new($store_dir, { creat => 1 });
19 my $smsg = $sto->add_eml(eml_load('t/data/0001.patch'));
20 like($smsg->{blob}, qr/\A[0-9a-f]+\z/, 'add returned OID');
21 my $eml = eml_load('t/data/0001.patch');
22 is($sto->add_eml($eml), undef, 'idempotent');
25 my $es = $sto->search;
26 ok($es->can('isrch'), ref($es). ' can ->isrch (for SolverGit)');
27 my $msgs = $es->over->query_xover(0, 1000);
28 is(scalar(@$msgs), 1, 'one message');
29 is($msgs->[0]->{blob}, $smsg->{blob}, 'blob matches');
30 my $mset = $es->mset("mid:$msgs->[0]->{mid}");
31 is($mset->size, 1, 'search works');
32 is_deeply($es->mset_to_artnums($mset), [ $msgs->[0]->{num} ],
34 my $mi = ($mset->items)[0];
35 my @kw = PublicInbox::Search::xap_terms('K', $mi->get_document);
36 is_deeply(\@kw, [], 'no flags');
39 for my $parallel (0, 1) {
40 $sto->{priv_eidx}->{parallel} = $parallel;
41 my $docids = $sto->set_eml_vmd($eml, { kw => [ qw(seen draft) ] });
42 is(scalar @$docids, 1, 'set keywords on one doc');
44 my @kw = $sto->search->msg_keywords($docids->[0]);
45 is_deeply(\@kw, [qw(draft seen)], 'kw matches');
47 $docids = $sto->add_eml_vmd($eml, {kw => [qw(seen draft)]});
49 is(scalar @$docids, 1, 'idempotently added keywords to doc');
50 @kw = $sto->search->msg_keywords($docids->[0]);
51 is_deeply(\@kw, [qw(draft seen)], 'kw matches after noop');
53 $docids = $sto->remove_eml_vmd($eml, {kw => [qw(seen draft)]});
54 is(scalar @$docids, 1, 'removed from one doc');
56 @kw = $sto->search->msg_keywords($docids->[0]);
57 is_deeply(\@kw, [], 'kw matches after remove');
59 $docids = $sto->remove_eml_vmd($eml, {kw=> [qw(answered)]});
60 is(scalar @$docids, 1, 'removed from one doc (idempotently)');
62 @kw = $sto->search->msg_keywords($docids->[0]);
63 is_deeply(\@kw, [], 'kw matches after remove (idempotent)');
65 $docids = $sto->add_eml_vmd($eml, {kw => [qw(answered)]});
66 is(scalar @$docids, 1, 'added to empty doc');
68 @kw = $sto->search->msg_keywords($docids->[0]);
69 is_deeply(\@kw, ['answered'], 'kw matches after add');
71 $docids = $sto->set_eml_vmd($eml, { kw => [] });
72 is(scalar @$docids, 1, 'set to clobber');
74 @kw = $sto->search->msg_keywords($docids->[0]);
75 is_deeply(\@kw, [], 'set clobbers all');
77 my $set = eml_load('t/plack-qp.eml');
78 $set->header_set('Message-ID', "<set\@$parallel>");
79 my $ret = $sto->set_eml($set, { kw => [ 'seen' ] });
80 is(ref $ret, 'PublicInbox::Smsg', 'initial returns smsg');
81 my $ids = $sto->set_eml($set, { kw => [ 'seen' ] });
82 is_deeply($ids, [ $ret->{num} ], 'set_eml idempotent');
83 $ids = $sto->set_eml($set, { kw => [ qw(seen answered) ] });
84 is_deeply($ids, [ $ret->{num} ], 'set_eml to change kw');
86 @kw = $sto->search->msg_keywords($ids->[0]);
87 is_deeply(\@kw, [qw(answered seen)], 'set changed kw');
91 require_mods(qw(Storable), 1);
92 ok($sto->can('ipc_do'), 'ipc_do works if we have Storable');
93 $eml->header_set('Message-ID', '<ipc-test@example>');
94 my $pid = $sto->ipc_worker_spawn('lei-store');
95 ok($pid > 0, 'got a worker');
96 my $smsg = $sto->ipc_do('set_eml', $eml, { kw => [ qw(seen) ] });
97 is(ref($smsg), 'PublicInbox::Smsg', 'set_eml works over ipc');
98 my $ids = $sto->ipc_do('set_eml', $eml, { kw => [ qw(seen) ] });
99 is_deeply($ids, [ $smsg->{num} ], 'docid returned');
101 $eml->header_set('Message-ID');
102 my $no_mid = $sto->ipc_do('set_eml', $eml, { kw => [ qw(seen) ] });
103 my $wait = $sto->ipc_do('done');
104 my @kw = $sto->search->msg_keywords($no_mid->{num});
105 is_deeply(\@kw, [qw(seen)], 'ipc set changed kw');
107 is(ref($smsg), 'PublicInbox::Smsg', 'no mid works ipc');
108 $ids = $sto->ipc_do('set_eml', $eml, { kw => [ qw(seen) ] });
109 is_deeply($ids, [ $no_mid->{num} ], 'docid returned w/o mid w/ ipc');
110 $sto->ipc_do('done');
111 $sto->ipc_worker_stop;
112 $ids = $sto->ipc_do('set_eml', $eml, { kw => [ qw(seen answered) ] });
113 is_deeply($ids, [ $no_mid->{num} ], 'docid returned w/o mid w/o ipc');
114 $wait = $sto->ipc_do('done');
116 my $lse = $sto->search;
117 @kw = $lse->msg_keywords($no_mid->{num});
118 is_deeply(\@kw, [qw(answered seen)], 'set changed kw w/o ipc');
119 is($lse->kw_changed($eml, [qw(answered seen)]), 0,
120 'kw_changed false when unchanged');
121 is($lse->kw_changed($eml, [qw(answered seen flagged)]), 1,
122 'kw_changed true when +flagged');
123 is($lse->kw_changed(eml_load('t/plack-qp.eml'), ['seen']), undef,
124 'kw_changed undef on unknown message');
128 require_mods(qw(HTTP::Date), 1);
129 my $now = HTTP::Date::time2str(time);
130 $now =~ s/GMT/+0000/ or xbail "no GMT in $now";
131 my $eml = PublicInbox::Eml->new(<<"EOM");
132 Received: (listserv\@example.com) by example.com via listexpand
133 id abcde (ORCPT <rfc822;u\@example.com>);
136 Subject: timezone-dependent test
138 WHAT IS TIME ANYMORE?
141 my $smsg = $sto->add_eml($eml);
142 ok($smsg && $smsg->{blob}, 'recently received message');
144 local $ENV{TZ} = 'GMT+5';
145 my $lse = $sto->search;
146 my $qstr = 'rt:1.hour.ago.. s:timezone';
147 $lse->query_approxidate($lse->git, $qstr);
148 my $mset = $lse->mset($qstr);
149 is($mset->size, 1, 'rt:1.hour.ago.. works w/ local time');