2 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use v5.12; use PublicInbox::TestCommon;
6 require_mods(qw(json DBD::SQLite Xapian));
7 use PublicInbox::DS qw(now);
8 my ($ro_home, $cfg_path) = setup_public_inboxes;
11 my $args = $opt{args} // [];
12 my $mid = $opt{mid} // 'testmessage@example.com';
13 lei_ok('q', "m:$mid", @$args);
14 my $res = json_utf8->decode($lei_out);
15 is($res->[1], undef, 'only got one result');
16 my $msg = $opt{msg} ? " $opt{msg}" : '';
17 ($exp ? is_deeply($res->[0]->{kw}, $exp, "got @$exp$msg")
18 : is($res->[0]->{kw}, undef, "got undef$msg")) or
22 ($exp ? is_deeply($res->[0]->{L}, $exp, "got @$exp$msg")
23 : is($res->[0]->{L}, undef, "got undef$msg")) or
29 lei_ok(qw(ls-label)); is($lei_out, '', 'no labels, yet');
30 lei_ok(qw(import t/utf8.eml));
31 lei_ok(qw(tag t/utf8.eml +kw:flagged +L:urgent));
32 $check_kw->(['flagged'], L => ['urgent']);
33 lei_ok(qw(ls-label)); is($lei_out, "urgent\n", 'label found');
34 ok(!lei(qw(tag -F eml t/utf8.eml +kw:seeen)), 'bad kw rejected');
35 like($lei_err, qr/`seeen' is not one of/, 'got helpful error');
37 ok(!lei(qw(tag -F eml t/utf8.eml +k:seen)), 'bad prefix rejected');
38 like($lei_err, qr/Unable to handle.*\Q+k:seen\E/, 'bad prefix noted');
40 ok(!lei(qw(tag -F eml t/utf8.eml)), 'no keywords');
41 like($lei_err, qr/no keywords or labels specified/,
42 'lack of kw/L noted');
44 my $mb = "$ENV{HOME}/mb";
45 my $md = "$ENV{HOME}/md";
46 lei_ok(qw(q m:testmessage@example.com -o), "mboxrd:$mb");
47 ok(-s $mb, 'wrote mbox result');
48 lei_ok(qw(q m:testmessage@example.com -o), $md);
49 my @fn = glob("$md/cur/*");
50 scalar(@fn) == 1 or xbail $lei_err, 'no mail', \@fn;
51 rename($fn[0], "$fn[0]S") or BAIL_OUT "rename $!";
52 $check_kw->(['flagged'], msg => 'after bad request');
53 lei_ok(qw(tag -F eml t/utf8.eml -kw:flagged));
54 $check_kw->(undef, msg => 'keyword cleared');
55 lei_ok(qw(tag -F mboxrd +kw:seen), $mb);
56 $check_kw->(['seen'], msg => 'mbox Status ignored');
57 lei_ok(qw(tag -kw:seen +kw:answered), $md);
58 $check_kw->(['answered'], msg => 'Maildir Status ignored');
60 open my $in, '<', 't/utf8.eml' or BAIL_OUT $!;
61 lei_ok([qw(tag -F eml - +kw:seen +L:nope)],
62 undef, { %$lei_opt, 0 => $in });
63 $check_kw->(['answered', 'seen'], msg => 'stdin works');
64 lei_ok(qw(q L:urgent));
65 my $res = json_utf8->decode($lei_out);
66 is($res->[0]->{'m'}, 'testmessage@example.com', 'L: query works');
67 lei_ok(qw(q kw:seen));
68 my $r2 = json_utf8->decode($lei_out);
69 is_deeply($r2, $res, 'kw: query works, too') or
70 diag explain([$r2, $res]);
72 lei_ok(qw(_complete lei tag));
73 my %c = map { $_ => 1 } split(/\s+/, $lei_out);
74 ok($c{'+L:urgent'} && $c{'-L:urgent'} &&
75 $c{'+L:nope'} && $c{'-L:nope'}, 'completed with labels');
77 my $mid = 'qp@example.com';
78 lei_ok qw(q -f mboxrd --only), "$ro_home/t2", "mid:$mid";
80 lei_ok [qw(tag -F mboxrd --stdin +kw:seen +L:qp)],
81 undef, { %$lei_opt, 0 => \$in };
82 $check_kw->(['seen'], L => ['qp'], mid => $mid,
83 args => [ '--only', "$ro_home/t2" ],
84 msg => 'external-only message');
86 is($lei_out, "nope\nqp\nurgent\n", 'ls-label shows qp');
88 lei_ok qw(tag -F eml t/utf8.eml +L:inbox +L:x);
89 lei_ok qw(q m:testmessage@example.com);
90 $check_kw->([qw(answered seen)], L => [qw(inbox nope urgent x)]);
92 is($lei_out, "inbox\nnope\nqp\nurgent\nx\n", 'ls-label shows qp');
95 is(json_utf8->decode($lei_out)->[0]->{blob},
96 $r2->[0]->{blob}, 'label search works');
98 ok(!lei(qw(tag -F eml t/utf8.eml +L:ALLCAPS)), '+L:ALLCAPS fails');
100 is($lei_out, "inbox\nnope\nqp\nurgent\nx\n", 'ls-label unchanged');
102 if (0) { # TODO label+kw search w/ externals
103 lei_ok(qw(q L:qp), "mid:$mid", '--only', "$ro_home/t2");
105 lei_ok qw(tag +L:nope -F eml t/data/binary.patch);
106 like $lei_err, qr/\b1 unimported messages/, 'noted unimported'
109 my $delay = $ENV{TEST_LEI_COMMIT_DELAY} // 1;
110 lei_ok qw(tag -F eml t/utf8.eml +L:utf8), "--commit-delay=$delay";
112 unlike $lei_out, qr/\butf8\b/, 'commit-delay delays label' or
113 warn "E: consider increasing TEST_LEI_COMMIT_DELAY=$delay";
116 diag "waiting for lei/store commit... (--commit-delay=$delay)";
120 } until (!lei('ls-label') || $lei_out =~ /\butf8\b/ || now > $end);
121 like($lei_out, qr/\butf8\b/, 'commit-delay eventually commits');