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 strict; use v5.10.1; use PublicInbox::TestCommon;
6 require_mods(qw(json DBD::SQLite Search::Xapian));
7 use PublicInbox::MboxReader;
8 use PublicInbox::InboxWritable;
9 my ($ro_home, $cfg_path) = setup_public_inboxes;
10 my $sock = tcp_server;
11 my ($tmpdir, $for_destroy) = tmpdir;
12 my $cmd = [ '-httpd', '-W0', "--stdout=$tmpdir/1", "--stderr=$tmpdir/2" ];
13 my $env = { PI_CONFIG => $cfg_path };
14 my $td = start_script($cmd, $env, { 3 => $sock }) or BAIL_OUT("-httpd: $?");
15 my $host_port = tcp_host_port($sock);
16 my $url = "http://$host_port/t2/";
17 my $exp1 = [ eml_load('t/plack-qp.eml') ];
18 my $exp2 = [ eml_load('t/iso-2202-jp.eml') ];
19 my $slurp_emls = sub {
20 open my $fh, '<', $_[0] or BAIL_OUT "open: $!";
22 PublicInbox::MboxReader->mboxrd($fh, sub {
24 $eml->header_set('Status');
30 test_lei({ tmpdir => $tmpdir }, sub {
31 my $o = "$ENV{HOME}/o.mboxrd";
32 my @cmd = ('q', '-o', "mboxrd:$o", 'm:qp@example.com');
34 ok(-f $o && !-s _, 'output exists but is empty');
35 unlink $o or BAIL_OUT $!;
36 lei_ok(@cmd, '-I', $url);
37 is_deeply($slurp_emls->($o), $exp1, 'got results after remote search');
38 unlink $o or BAIL_OUT $!;
40 ok(-f $o && -s _, 'output exists after import but is not empty') or
42 is_deeply($slurp_emls->($o), $exp1, 'got results w/o remote search');
43 unlink $o or BAIL_OUT $!;
45 $cmd[-1] = 'm:199707281508.AAA24167@hoyogw.example';
46 lei_ok(@cmd, '-I', $url, '--no-import-remote');
47 is_deeply($slurp_emls->($o), $exp2, 'got another after remote search');
48 unlink $o or BAIL_OUT $!;
50 ok(-f $o && !-s _, '--no-import-remote did not memoize');
52 open my $fh, '>', "$o.lock";
53 $cmd[-1] = 'm:qp@example.com';
54 unlink $o or BAIL_OUT $!;
55 lei_ok(@cmd, '--lock=none');
56 ok(-f $o && -s _, '--lock=none respected');
57 unlink $o or BAIL_OUT $!;
58 ok(!lei(@cmd, '--lock=dotlock,timeout=0.000001'), 'dotlock fails');
59 ok(-f $o && !-s _, 'nothing output on lock failure');
60 unlink "$o.lock" or BAIL_OUT $!;
61 lei_ok(@cmd, '--lock=dotlock,timeout=0.000001',
62 \'succeeds after lock removal');
64 # XXX memoize this external creation
65 my $inboxdir = "$ENV{HOME}/tmp_git";
66 my $ibx = PublicInbox::InboxWritable->new({
68 -primary_address => 'lei@example.com',
69 inboxdir => $inboxdir,
70 indexlevel => 'medium',
72 my $im = $ibx->importer(0);
73 $im->add(eml_load('t/utf8.eml')) or BAIL_OUT '->add';
76 run_script(['-index', $inboxdir], undef) or BAIL_OUT '-init';
77 lei_ok(qw(add-external -q), $inboxdir);
78 lei_ok(qw(q -o), "mboxrd:$o", '--only', $url,
79 'm:testmessage@example.com');
80 ok(-s $o, 'got result from remote external');
81 my $exp = eml_load('t/utf8.eml');
82 is_deeply($slurp_emls->($o), [$exp], 'got expected result');
83 lei_ok(qw(q --no-external -o), "mboxrd:/dev/stdout",
84 'm:testmessage@example.com');
85 is($lei_out, '', 'message not imported when in local external');
87 open $fh, '>', $o or BAIL_OUT;
88 print $fh <<'EOF' or BAIL_OUT;
89 From a@z Mon Sep 17 00:00:00 2001
90 From: nobody@localhost
91 Date: Sat, 13 Mar 2021 18:23:01 +0600
92 Message-ID: <never-before-seen@example.com>
97 close $fh or BAIL_OUT;
98 lei_ok(qw(q -o), "mboxrd:$o", 'm:testmessage@example.com');
99 is_deeply($slurp_emls->($o), [$exp],
100 'got expected result after clobber') or diag $lei_err;
101 lei_ok(qw(q -o mboxrd:/dev/stdout m:never-before-seen@example.com));
102 like($lei_out, qr/seen\@example\.com>\nStatus: OR\n\nwhatever/sm,
103 '--import-before imported totally unseen message');