2 # Copyright (C) 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(lei json DBD::SQLite 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') or
50 local $ENV{HOME} = "$tmpdir/h2";
51 lei_ok(qw(ls-mail-source -l), $url);
52 my $ls = json_utf8->decode($lei_out);
53 my ($high, $low) = @{$ls->[0]}{qw(high low)};
54 ok($high > $low, 'high > low');
57 lei_ok qw(import), "$url/$high";
58 lei_ok('inspect', $url); is_xdeeply(json_utf8->decode($lei_out), {
59 $url => { 'article.count' => 1,
60 'article.min' => $high,
61 'article.max' => $high, }
62 }, 'inspect output for URL after single message') or diag $lei_out;
63 lei_ok('inspect', "$url/$high");
64 my $x = json_utf8->decode($lei_out);
65 like($x->{$url}->{$high}, qr/\A[a-f0-9]{40,}\z/, 'inspect shows blob');
66 lei_ok qw(lcat -f json), "$url/$high";
67 my $lcat = json_utf8->decode($lei_out);
68 is($lcat->[1], undef, 'only one result for lcat');
69 is($lcat->[0]->{blob}, $x->{$url}->{$high},
70 'lcat showed correct blob');
72 lei_ok 'ls-mail-sync';
73 is($lei_out, "$url\n", 'article number not stored as folder');
74 lei_ok qw(q z:0..); my $one = json_utf8->decode($lei_out);
75 pop @$one; # trailing null
76 is(scalar(@$one), 1, 'only 1 result');
78 local $ENV{HOME} = "$tmpdir/h3";
79 lei_ok qw(import), "$url/$low-$end";
80 lei_ok('inspect', $url); is_xdeeply(json_utf8->decode($lei_out), {
81 $url => { 'article.count' => $end - $low + 1,
82 'article.min' => $low,
83 'article.max' => $end, }
84 }, 'inspect output for URL after range') or diag $lei_out;
85 lei_ok('inspect', "$url/$low-$end");
86 $x = json_utf8->decode($lei_out);
87 is_deeply([ ($low..$end) ], [ sort { $a <=> $b } keys %{$x->{$url}} ],
88 'inspect range shows range');
89 is(scalar(grep(/\A[a-f0-9]{40,}\z/, values %{$x->{$url}})),
90 $end - $low + 1, 'all values are git blobs');
92 lei_ok qw(lcat -f json), "$url/$low";
93 $lcat = json_utf8->decode($lei_out);
94 is($lcat->[1], undef, 'only one result for lcat');
95 is($lcat->[0]->{blob}, $x->{$url}->{$low},
96 'lcat showed correct blob');
97 lei_ok qw(lcat -f json), "$url/$low-$end";
98 $lcat = json_utf8->decode($lei_out);
101 my $tip = shift @$lcat;
102 is($x->{$url}->{$_}, $tip->{blob}, "blob matches art #$_");
105 lei_ok 'ls-mail-sync';
106 is($lei_out, "$url\n", 'article range not stored as folder');
107 lei_ok qw(q z:0..); my $start = json_utf8->decode($lei_out);
108 pop @$start; # trailing null
109 is(scalar(@$start), scalar(map { $_ } ($low..$end)),
110 'range worked as expected');
112 for (@$start, @$one) {
113 is($seen{$_->{blob}}++, 0, "blob $_->{blob} seen once");