]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei-import-imap.t
lei forget-mail-sync: new command to drop sync information
[public-inbox.git] / t / lei-import-imap.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_mods(qw(lei -imapd Mail::IMAPClient));
6 my ($ro_home, $cfg_path) = setup_public_inboxes;
7 my ($tmpdir, $for_destroy) = tmpdir;
8 my $sock = tcp_server;
9 my $cmd = [ '-imapd', '-W0', "--stdout=$tmpdir/1", "--stderr=$tmpdir/2" ];
10 my $env = { PI_CONFIG => $cfg_path };
11 my $td = start_script($cmd, $env, { 3 => $sock }) or BAIL_OUT("-imapd: $?");
12 my $host_port = tcp_host_port($sock);
13 undef $sock;
14 test_lei({ tmpdir => $tmpdir }, sub {
15         my $url = "imap://$host_port/t.v2.0";
16
17         lei_ok(qw(q z:1..));
18         my $out = json_utf8->decode($lei_out);
19         is_deeply($out, [ undef ], 'nothing imported, yet');
20
21         lei_ok('inspect', $url);
22         is_deeply(json_utf8->decode($lei_out), {}, 'no inspect stats, yet');
23
24         lei_ok('import', $url);
25         lei_ok('inspect', $url);
26         my $res = json_utf8->decode($lei_out);
27         is(scalar keys %$res, 1, 'got one key in inspect URL');
28         my $re = qr!\Aimap://;AUTH=ANONYMOUS\@\Q$host_port\E
29                         /t\.v2\.0;UIDVALIDITY=\d+!x;
30         like((keys %$res)[0], qr/$re\z/, 'got expanded key');
31
32         lei_ok 'ls-mail-sync';
33         like($lei_out, qr!$re\n\z!, 'ls-mail-sync');
34         chomp(my $u = $lei_out);
35         lei_ok('import', $u, \'UIDVALIDITY match in URL');
36         $url = $u;
37         $u =~ s/;UIDVALIDITY=(\d+)\s*/;UIDVALIDITY=9$1/s;
38         ok(!lei('import', $u), 'UIDVALIDITY mismatch in URL rejected');
39
40         lei_ok('inspect', $url);
41         my $inspect = json_utf8->decode($lei_out);
42         my @k = keys %$inspect;
43         is(scalar(@k), 1, 'one URL resolved');
44         is($k[0], $url, 'inspect URL matches');
45         my $stats = $inspect->{$k[0]};
46         is_deeply([ sort keys %$stats ],
47                 [ qw(uid.count uid.max uid.min) ], 'keys match');
48         ok($stats->{'uid.min'} < $stats->{'uid.max'}, 'min < max');
49         ok($stats->{'uid.count'} > 0, 'count > 0');
50
51         lei_ok(qw(q z:1..));
52         $out = json_utf8->decode($lei_out);
53         ok(scalar(@$out) > 1, 'got imported messages');
54         is(pop @$out, undef, 'trailing JSON null element was null');
55         my %r;
56         for (@$out) { $r{ref($_)}++ }
57         is_deeply(\%r, { 'HASH' => scalar(@$out) }, 'all hashes');
58         lei_ok([qw(tag +kw:seen), $url], undef, undef);
59
60         my $f = "$ENV{HOME}/.local/share/lei/store/mail_sync.sqlite3";
61         ok(-s $f, 'mail_sync tracked for redundant imports');
62         lei_ok('inspect', "blob:$out->[5]->{blob}");
63         my $x = json_utf8->decode($lei_out);
64         is(ref($x->{'lei/store'}), 'ARRAY', 'lei/store in inspect');
65         is(ref($x->{'mail-sync'}), 'HASH', 'sync in inspect');
66         is(ref($x->{'mail-sync'}->{$k[0]}), 'ARRAY', 'UID arrays in inspect')
67                 or diag explain($x);
68
69         my $psgi_attach = 'cfa3622cbeffc9bd6b0fc66c4d60d420ba74f60d';
70         lei_ok('blob', $psgi_attach);
71         like($lei_out, qr!^Content-Type: multipart/mixed;!sm, 'got full blob');
72         lei_ok('blob', "$psgi_attach:2");
73         is($lei_out, "b64\xde\xad\xbe\xef\n", 'got attachment');
74
75         lei_ok 'forget-mail-sync', $url;
76         lei_ok 'ls-mail-sync';
77         unlike($lei_out, qr!\Q$host_port\E!, 'sync info gone after forget');
78 });
79 done_testing;