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>
4 use strict; use v5.10.1; use PublicInbox::TestCommon;
5 use Fcntl qw(SEEK_SET);
6 use PublicInbox::Spawn qw(which);
8 my @onions = qw(http://hjrcffqmbrq6wope.onion/meta/
9 http://czquwvybam4bgbro.onion/meta/
10 http://ou63pmih66umazou.onion/meta/);
12 my $test_external_remote = sub {
16 skip "$k unset", $nr if !$url;
17 which('curl') or skip 'no curl', $nr;
18 which('torsocks') or skip 'no torsocks', $nr if $url =~ m!\.onion/!;
19 my $mid = '20140421094015.GA8962@dcvr.yhbt.net';
20 my @cmd = ('q', '--only', $url, '-q', "m:$mid");
21 ok($lei->(@cmd), "query $url");
22 is($lei_err, '', "no errors on $url");
23 my $res = json_utf8->decode($lei_out);
24 is($res->[0]->{'m'}, "<$mid>", "got expected mid from $url");
25 ok($lei->(@cmd, 'd:..20101002'), 'no results, no error');
26 is($lei_err, '', 'no output on 404, matching local FS behavior');
27 is($lei_out, "[null]\n", 'got null results');
31 my ($ro_home, $cfg_path) = setup_public_inboxes;
33 my $home = $ENV{HOME};
34 my $config_file = "$home/.config/lei/config";
35 my $store_dir = "$home/.local/share/lei";
36 ok($lei->('ls-external'), 'ls-external works');
37 is($lei_out.$lei_err, '', 'ls-external no output, yet');
38 ok(!-e $config_file && !-e $store_dir,
39 'nothing created by ls-external');
41 ok(!$lei->('add-external', "$home/nonexistent"),
42 "fails on non-existent dir");
43 ok($lei->('ls-external'), 'ls-external works after add failure');
44 is($lei_out.$lei_err, '', 'ls-external still has no output');
45 my $cfg = PublicInbox::Config->new($cfg_path);
46 $cfg->each_inbox(sub {
48 ok($lei->(qw(add-external -q), $ibx->{inboxdir}),
50 is($lei_out.$lei_err, '', 'no output');
52 ok(-s $config_file && -e $store_dir,
53 'add-external created config + store');
54 my $lcfg = PublicInbox::Config->new($config_file);
55 $cfg->each_inbox(sub {
57 is($lcfg->{"external.$ibx->{inboxdir}.boost"}, 0,
58 "configured boost on $ibx->{name}");
60 $lei->('ls-external');
61 like($lei_out, qr/boost=0\n/s, 'ls-external has output');
62 ok($lei->(qw(add-external -q https://EXAMPLE.com/ibx)), 'add remote');
63 is($lei_err, '', 'no warnings after add-external');
65 ok($lei->(qw(_complete lei forget-external)), 'complete for externals');
66 my %comp = map { $_ => 1 } split(/\s+/, $lei_out);
67 ok($comp{'https://example.com/ibx/'}, 'forget external completion');
68 $cfg->each_inbox(sub {
70 ok($comp{$ibx->{inboxdir}}, "local $ibx->{name} completion");
72 for my $u (qw(h http https https: https:/ https:// https://e
73 https://example https://example. https://example.co
74 https://example.com https://example.com/
75 https://example.com/i https://example.com/ibx)) {
76 ok($lei->(qw(_complete lei forget-external), $u),
77 "partial completion for URL $u");
78 is($lei_out, "https://example.com/ibx/\n",
79 "completed partial URL $u");
80 for my $qo (qw(-I --include --exclude --only)) {
81 ok($lei->(qw(_complete lei q), $qo, $u),
82 "partial completion for URL q $qo $u");
83 is($lei_out, "https://example.com/ibx/\n",
84 "completed partial URL $u on q $qo");
87 ok($lei->(qw(_complete lei add-external), 'https://'),
88 'add-external hostname completion');
89 is($lei_out, "https://example.com/\n", 'completed up to hostname');
91 $lei->('ls-external');
92 like($lei_out, qr!https://example\.com/ibx/!s, 'added canonical URL');
93 is($lei_err, '', 'no warnings on ls-external');
94 ok($lei->(qw(forget-external -q https://EXAMPLE.com/ibx)),
96 $lei->('ls-external');
97 unlike($lei_out, qr!https://example\.com/ibx/!s,
98 'removed canonical URL');
100 ok(!$lei->(qw(q s:prefix -o /dev/null -f maildir)), 'bad maildir');
101 like($lei_err, qr!/dev/null exists and is not a directory!,
103 is($? >> 8, 1, 'errored out with exit 1');
105 ok(!$lei->(qw(q s:prefix -f mboxcl2 -o), $home), 'bad mbox');
106 like($lei_err, qr!\Q$home\E exists and is not a writable file!,
108 is($? >> 8, 1, 'errored out with exit 1');
110 ok(!$lei->(qw(q s:prefix -o /dev/stdout -f Mbox2)), 'bad format');
111 like($lei_err, qr/bad mbox --format=mbox2/, 'error shown');
112 is($? >> 8, 1, 'errored out with exit 1');
114 # note, on a Bourne shell users should be able to use either:
115 # s:"use boolean prefix"
116 # "s:use boolean prefix"
117 # or use single quotes, it should not matter. Users only need
118 # to know shell quoting rules, not Xapian quoting rules.
119 # No double-quoting should be imposed on users on the CLI
120 $lei->('q', 's:use boolean prefix');
121 like($lei_out, qr/search: use boolean prefix/,
122 'phrase search got result');
123 my $res = json_utf8->decode($lei_out);
124 is(scalar(@$res), 2, 'only 2 element array (1 result)');
125 is($res->[1], undef, 'final element is undef'); # XXX should this be?
126 is(ref($res->[0]), 'HASH', 'first element is hashref');
127 $lei->('q', '--pretty', 's:use boolean prefix');
128 my $pretty = json_utf8->decode($lei_out);
129 is_deeply($res, $pretty, '--pretty is identical after decode');
132 open my $fh, '+>', undef or BAIL_OUT $!;
134 print $fh 's:use' or BAIL_OUT $!;
135 seek($fh, 0, SEEK_SET) or BAIL_OUT $!;
136 ok($lei->([qw(q -q --stdin)], undef, { %$lei_opt, 0 => $fh }),
137 '--stdin on regular file works');
138 like($lei_out, qr/use boolean/, '--stdin on regular file');
141 pipe(my ($r, $w)) or BAIL_OUT $!;
142 print $w 's:use' or BAIL_OUT $!;
143 close $w or BAIL_OUT $!;
144 ok($lei->([qw(q -q --stdin)], undef, { %$lei_opt, 0 => $r }),
145 '--stdin on pipe file works');
146 like($lei_out, qr/use boolean prefix/, '--stdin on pipe');
148 ok(!$lei->(qw(q -q --stdin s:use)), "--stdin and argv don't mix");
150 for my $fmt (qw(ldjson ndjson jsonl)) {
151 $lei->('q', '-f', $fmt, 's:use boolean prefix');
152 is($lei_out, json_utf8->encode($pretty->[0])."\n", "-f $fmt");
155 require IO::Uncompress::Gunzip;
156 for my $sfx ('', '.gz') {
157 my $f = "$home/mbox$sfx";
158 $lei->('q', '-o', "mboxcl2:$f", 's:use boolean prefix');
159 my $cat = $sfx eq '' ? sub {
160 open my $mb, '<', $f or fail "no mbox: $!";
163 my $z = IO::Uncompress::Gunzip->new($f, MultiStream=>1);
166 my @s = grep(/^Subject:/, $cat->());
167 is(scalar(@s), 1, "1 result in mbox$sfx");
168 $lei->('q', '-a', '-o', "mboxcl2:$f", 's:see attachment');
169 is(grep(!/^#/, $lei_err), 0, 'no errors from augment');
170 @s = grep(/^Subject:/, my @wtf = $cat->());
171 is(scalar(@s), 2, "2 results in mbox$sfx");
173 $lei->('q', '-a', '-o', "mboxcl2:$f", 's:nonexistent');
174 is(grep(!/^#/, $lei_err), 0, "no errors on no results ($sfx)");
176 my @s2 = grep(/^Subject:/, $cat->());
178 "same 2 old results w/ --augment and bad search $sfx");
180 $lei->('q', '-o', "mboxcl2:$f", 's:nonexistent');
182 is_deeply(\@res, [], "clobber w/o --augment $sfx");
184 ok(!$lei->('q', '-o', "$home/mbox", 's:nope'),
185 'fails if mbox format unspecified');
186 ok(!$lei->(qw(q --no-local s:see)), '--no-local');
187 is($? >> 8, 1, 'proper exit code');
188 like($lei_err, qr/no local or remote.+? to search/, 'no inbox');
190 TEST_LEI_EXTERNAL_HTTPS => 'https://public-inbox.org/meta/',
191 TEST_LEI_EXTERNAL_ONION => $onions[int(rand(scalar(@onions)))],
193 for my $k (keys %e) {
194 my $url = $ENV{$k} // '';
195 $url = $e{$k} if $url eq '1';
196 $test_external_remote->($url, $k);