2 # Copyright (C) 2020 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/lst";
15 my $lst = PublicInbox::LeiStore->new($store_dir, { creat => 1 });
17 my $oid = $lst->add_eml(eml_load('t/data/0001.patch'));
18 like($oid, qr/\A[0-9a-f]+\z/, 'add returned OID');
19 my $eml = eml_load('t/data/0001.patch');
20 is($lst->add_eml($eml), undef, 'idempotent');
23 my $es = $lst->search;
24 my $msgs = $es->over->query_xover(0, 1000);
25 is(scalar(@$msgs), 1, 'one message');
26 is($msgs->[0]->{blob}, $oid, 'blob matches');
27 my $mset = $es->mset("mid:$msgs->[0]->{mid}");
28 is($mset->size, 1, 'search works');
29 is_deeply($es->mset_to_artnums($mset), [ $msgs->[0]->{num} ],
31 my @kw = $es->msg_keywords(($mset->items)[0]);
32 is_deeply(\@kw, [], 'no flags');
35 for my $parallel (0, 1) {
36 $lst->{priv_eidx}->{parallel} = $parallel;
37 my $docids = $lst->set_eml_keywords($eml, qw(seen draft));
38 is(scalar @$docids, 1, 'set keywords on one doc');
40 my @kw = $lst->search->msg_keywords($docids->[0]);
41 is_deeply(\@kw, [qw(draft seen)], 'kw matches');
43 $docids = $lst->add_eml_keywords($eml, qw(seen draft));
45 is(scalar @$docids, 1, 'idempotently added keywords to doc');
46 @kw = $lst->search->msg_keywords($docids->[0]);
47 is_deeply(\@kw, [qw(draft seen)], 'kw matches after noop');
49 $docids = $lst->remove_eml_keywords($eml, qw(seen draft));
50 is(scalar @$docids, 1, 'removed from one doc');
52 @kw = $lst->search->msg_keywords($docids->[0]);
53 is_deeply(\@kw, [], 'kw matches after remove');
55 $docids = $lst->remove_eml_keywords($eml, qw(answered));
56 is(scalar @$docids, 1, 'removed from one doc (idempotently)');
58 @kw = $lst->search->msg_keywords($docids->[0]);
59 is_deeply(\@kw, [], 'kw matches after remove (idempotent)');
61 $docids = $lst->add_eml_keywords($eml, qw(answered));
62 is(scalar @$docids, 1, 'added to empty doc');
64 @kw = $lst->search->msg_keywords($docids->[0]);
65 is_deeply(\@kw, ['answered'], 'kw matches after add');
67 $docids = $lst->set_eml_keywords($eml);
68 is(scalar @$docids, 1, 'set to clobber');
70 @kw = $lst->search->msg_keywords($docids->[0]);
71 is_deeply(\@kw, [], 'set clobbers all');