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 Net::NNTP));
7 my ($ro_home, $cfg_path) = setup_public_inboxes;
8 my ($tmpdir, $for_destroy) = tmpdir;
10 my $cmd = [ '-nntpd', '-W0', "--stdout=$tmpdir/1", "--stderr=$tmpdir/2" ];
11 my $env = { PI_CONFIG => $cfg_path };
12 my $td = start_script($cmd, $env, { 3 => $sock }) or BAIL_OUT("-nntpd $?");
13 my $host_port = tcp_host_port($sock);
15 test_lei({ tmpdir => $tmpdir }, sub {
17 my $out = json_utf8->decode($lei_out);
18 is_deeply($out, [ undef ], 'nothing imported, yet');
19 my $url = "nntp://$host_port/t.v2";
20 lei_ok(qw(ls-mail-source), "nntp://$host_port/");
21 like($lei_out, qr/^t\.v2$/ms, 'shows newsgroup');
22 lei_ok(qw(ls-mail-source), $url);
23 is($lei_out, "t.v2\n", 'shows only newsgroup with filter');
24 lei_ok(qw(ls-mail-source -l), "nntp://$host_port/");
25 is(ref(json_utf8->decode($lei_out)), 'ARRAY', 'ls-mail-source JSON');
27 lei_ok('import', $url);
28 lei_ok "lcat", "nntp://$host_port/testmessage\@example.com";
30 lei_ok "lcat", "nntp://example.com/testmessage\@example.com";
31 my $remote = $lei_out;
32 is($local, $remote, 'Message-ID used even from unknown host');
34 $out = json_utf8->decode($lei_out);
35 ok(scalar(@$out) > 1, 'got imported messages');
36 is(pop @$out, undef, 'trailing JSON null element was null');
38 for (@$out) { $r{ref($_)}++ }
39 is_deeply(\%r, { 'HASH' => scalar(@$out) }, 'all hashes');
41 my $f = "$ENV{HOME}/.local/share/lei/store/mail_sync.sqlite3";
42 ok(-s $f, 'mail_sync exists tracked for redundant imports');
43 lei_ok 'ls-mail-sync';
44 like($lei_out, qr!\A\Q$url\E\n\z!, 'ls-mail-sync output as-expected');
46 ok(!lei(qw(import), "$url/12-1"), 'backwards range rejected');
49 local $ENV{HOME} = "$tmpdir/h2";
50 lei_ok(qw(ls-mail-source -l), $url);
51 my $ls = json_utf8->decode($lei_out);
52 my ($high, $low) = @{$ls->[0]}{qw(high low)};
53 ok($high > $low, 'high > low');
56 lei_ok qw(import), "$url/$high";
57 lei_ok('inspect', $url); is_xdeeply(json_utf8->decode($lei_out), {
58 $url => { 'article.count' => 1,
59 'article.min' => $high,
60 'article.max' => $high, }
61 }, 'inspect output for URL after single message') or diag $lei_out;
62 lei_ok('inspect', "$url/$high");
63 my $x = json_utf8->decode($lei_out);
64 like($x->{$url}->{$high}, qr/\A[a-f0-9]{40,}\z/, 'inspect shows blob');
65 lei_ok qw(lcat -f json), "$url/$high";
66 my $lcat = json_utf8->decode($lei_out);
67 is($lcat->[1], undef, 'only one result for lcat');
68 is($lcat->[0]->{blob}, $x->{$url}->{$high},
69 'lcat showed correct blob');
71 lei_ok 'ls-mail-sync';
72 is($lei_out, "$url\n", 'article number not stored as folder');
73 lei_ok qw(q z:0..); my $one = json_utf8->decode($lei_out);
74 pop @$one; # trailing null
75 is(scalar(@$one), 1, 'only 1 result');
77 local $ENV{HOME} = "$tmpdir/h3";
78 lei_ok qw(import), "$url/$low-$end";
79 lei_ok('inspect', $url); is_xdeeply(json_utf8->decode($lei_out), {
80 $url => { 'article.count' => $end - $low + 1,
81 'article.min' => $low,
82 'article.max' => $end, }
83 }, 'inspect output for URL after range') or diag $lei_out;
84 lei_ok('inspect', "$url/$low-$end");
85 $x = json_utf8->decode($lei_out);
86 is_deeply([ ($low..$end) ], [ sort { $a <=> $b } keys %{$x->{$url}} ],
87 'inspect range shows range');
88 is(scalar(grep(/\A[a-f0-9]{40,}\z/, values %{$x->{$url}})),
89 $end - $low + 1, 'all values are git blobs');
91 lei_ok qw(lcat -f json), "$url/$low";
92 $lcat = json_utf8->decode($lei_out);
93 is($lcat->[1], undef, 'only one result for lcat');
94 is($lcat->[0]->{blob}, $x->{$url}->{$low},
95 'lcat showed correct blob');
96 lei_ok qw(lcat -f json), "$url/$low-$end";
97 $lcat = json_utf8->decode($lei_out);
100 my $tip = shift @$lcat;
101 is($x->{$url}->{$_}, $tip->{blob}, "blob matches art #$_");
104 lei_ok 'ls-mail-sync';
105 is($lei_out, "$url\n", 'article range not stored as folder');
106 lei_ok qw(q z:0..); my $start = json_utf8->decode($lei_out);
107 pop @$start; # trailing null
108 is(scalar(@$start), scalar(map { $_ } ($low..$end)),
109 'range worked as expected');
111 for (@$start, @$one) {
112 is($seen{$_->{blob}}++, 0, "blob $_->{blob} seen once");