]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei-import-nntp.t
1eb41e0ed2f9425d60333c46792f8781ba5a031a
[public-inbox.git] / t / lei-import-nntp.t
1 #!perl -w
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;
5 require_git 2.6;
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;
9 my $sock = tcp_server;
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);
14 undef $sock;
15 test_lei({ tmpdir => $tmpdir }, sub {
16         lei_ok(qw(q z:1..));
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');
26
27         lei_ok('import', $url);
28         lei_ok(qw(q z:1..));
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');
32         my %r;
33         for (@$out) { $r{ref($_)}++ }
34         is_deeply(\%r, { 'HASH' => scalar(@$out) }, 'all hashes');
35
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');
40
41         ok(!lei(qw(import), "$url/12-1"), 'backwards range rejected');
42
43         # new home
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');
49
50         my $end = $high - 1;
51         lei_ok qw(import), "$url/$high";
52         lei_ok qw(q z:0..); my $one = json_utf8->decode($lei_out);
53         pop @$one; # trailing null
54         is(scalar(@$one), 1, 'only 1 result');
55
56         local $ENV{HOME} = "$tmpdir/h3";
57         lei_ok qw(import), "$url/$low-$end";
58         lei_ok qw(q z:0..); my $start = json_utf8->decode($lei_out);
59         pop @$start; # trailing null
60         is(scalar(@$start), scalar(map { $_ } ($low..$end)),
61                 'range worked as expected');
62         my %seen;
63         for (@$start, @$one) {
64                 is($seen{$_->{blob}}++, 0, "blob $_->{blob} seen once");
65         }
66 });
67 done_testing;