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);
29 $out = json_utf8->decode($lei_out);
30 ok(scalar(@$out) > 1, 'got imported messages');
31 is(pop @$out, undef, 'trailing JSON null element was null');
33 for (@$out) { $r{ref($_)}++ }
34 is_deeply(\%r, { 'HASH' => scalar(@$out) }, 'all hashes');
36 my $f = "$ENV{HOME}/.local/share/lei/store/mail_sync.sqlite3";
37 ok(-s $f, 'mail_sync exists tracked for redundant imports');
38 lei_ok 'ls-mail-sync';
39 like($lei_out, qr!\A\Q$url\E\n\z!, 'ls-mail-sync output as-expected');
41 ok(!lei(qw(import), "$url/12-1"), 'backwards range rejected');
44 local $ENV{HOME} = "$tmpdir/h2";
45 lei_ok(qw(ls-mail-source -l), $url);
46 my $ls = json_utf8->decode($lei_out);
47 my ($high, $low) = @{$ls->[0]}{qw(high low)};
48 ok($high > $low, 'high > low');
51 lei_ok qw(import), "$url/$high";
52 lei_ok('inspect', $url); is_xdeeply(json_utf8->decode($lei_out), {
53 $url => { 'article.count' => 1,
54 'article.min' => $high,
55 'article.max' => $high, }
56 }, 'inspect output for URL after single message') or diag $lei_out;
57 lei_ok('inspect', "$url/$high");
58 my $x = json_utf8->decode($lei_out);
59 like($x->{$url}->{$high}, qr/\A[a-f0-9]{40,}\z/, 'inspect shows blob');
61 lei_ok 'ls-mail-sync';
62 is($lei_out, "$url\n", 'article number not stored as folder');
63 lei_ok qw(q z:0..); my $one = json_utf8->decode($lei_out);
64 pop @$one; # trailing null
65 is(scalar(@$one), 1, 'only 1 result');
67 local $ENV{HOME} = "$tmpdir/h3";
68 lei_ok qw(import), "$url/$low-$end";
69 lei_ok('inspect', $url); is_xdeeply(json_utf8->decode($lei_out), {
70 $url => { 'article.count' => $end - $low + 1,
71 'article.min' => $low,
72 'article.max' => $end, }
73 }, 'inspect output for URL after range') or diag $lei_out;
74 lei_ok('inspect', "$url/$low-$end");
75 $x = json_utf8->decode($lei_out);
76 is_deeply([ ($low..$end) ], [ sort { $a <=> $b } keys %{$x->{$url}} ],
77 'inspect range shows range');
78 is(scalar(grep(/\A[a-f0-9]{40,}\z/, values %{$x->{$url}})),
79 $end - $low + 1, 'all values are git blobs');
81 lei_ok 'ls-mail-sync';
82 is($lei_out, "$url\n", 'article range not stored as folder');
83 lei_ok qw(q z:0..); my $start = json_utf8->decode($lei_out);
84 pop @$start; # trailing null
85 is(scalar(@$start), scalar(map { $_ } ($low..$end)),
86 'range worked as expected');
88 for (@$start, @$one) {
89 is($seen{$_->{blob}}++, 0, "blob $_->{blob} seen once");