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;
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);
14 test_lei({ tmpdir => $tmpdir }, sub {
15 my $url = "imap://$host_port/t.v2.0";
17 lei_ok(qw(ls-mail-source), "imap://$host_port/");
18 like($lei_out, qr/^t\.v2\.0$/ms, 'shows mailbox');
19 lei_ok(qw(ls-mail-source), $url);
20 is($lei_out, "t.v2.0\n", 'shows only mailbox with filter');
21 lei_ok(qw(ls-mail-source -l), "imap://$host_port/");
22 is(ref(json_utf8->decode($lei_out)), 'ARRAY', 'ls-mail-source JSON');
25 my $out = json_utf8->decode($lei_out);
26 is_deeply($out, [ undef ], 'nothing imported, yet');
28 lei_ok('inspect', $url);
29 is_deeply(json_utf8->decode($lei_out), {}, 'no inspect stats, yet');
31 lei_ok('import', $url);
32 lei_ok('inspect', $url);
33 my $res = json_utf8->decode($lei_out);
34 is(scalar keys %$res, 1, 'got one key in inspect URL');
35 my $re = qr!\Aimap://;AUTH=ANONYMOUS\@\Q$host_port\E
36 /t\.v2\.0;UIDVALIDITY=\d+!x;
37 like((keys %$res)[0], qr/$re\z/, 'got expanded key');
39 lei_ok 'ls-mail-sync';
40 like($lei_out, qr!$re\n\z!, 'ls-mail-sync');
41 chomp(my $u = $lei_out);
42 lei_ok('import', $u, \'UIDVALIDITY match in URL');
44 $u =~ s/;UIDVALIDITY=(\d+)\s*/;UIDVALIDITY=9$1/s;
45 ok(!lei('import', $u), 'UIDVALIDITY mismatch in URL rejected');
46 like($lei_err, qr/UIDVALIDITY mismatch/, 'mismatch noted');
48 lei_ok('inspect', $url);
49 my $inspect = json_utf8->decode($lei_out);
50 my @k = keys %$inspect;
51 is(scalar(@k), 1, 'one URL resolved');
52 is($k[0], $url, 'inspect URL matches');
53 my $stats = $inspect->{$k[0]};
54 is_deeply([ sort keys %$stats ],
55 [ qw(uid.count uid.max uid.min) ], 'keys match');
56 ok($stats->{'uid.min'} < $stats->{'uid.max'}, 'min < max');
57 ok($stats->{'uid.count'} > 0, 'count > 0');
60 is(scalar(grep(/^# blob:/, split(/\n/ms, $lei_out))),
61 $stats->{'uid.count'}, 'lcat on URL dumps folder');
62 lei_ok qw(lcat -f json), $url;
63 $out = json_utf8->decode($lei_out);
64 is(scalar(@$out) - 1, $stats->{'uid.count'}, 'lcat JSON dumps folder');
67 $out = json_utf8->decode($lei_out);
68 ok(scalar(@$out) > 1, 'got imported messages');
69 is(pop @$out, undef, 'trailing JSON null element was null');
71 for (@$out) { $r{ref($_)}++ }
72 is_deeply(\%r, { 'HASH' => scalar(@$out) }, 'all hashes');
73 lei_ok([qw(tag +kw:seen), $url], undef, undef);
75 my $f = "$ENV{HOME}/.local/share/lei/store/mail_sync.sqlite3";
76 ok(-s $f, 'mail_sync tracked for redundant imports');
77 lei_ok('inspect', "blob:$out->[5]->{blob}");
78 my $x = json_utf8->decode($lei_out);
79 is(ref($x->{'lei/store'}), 'ARRAY', 'lei/store in inspect');
80 is(ref($x->{'mail-sync'}), 'HASH', 'sync in inspect');
81 is(ref($x->{'mail-sync'}->{$k[0]}), 'ARRAY', 'UID arrays in inspect')
84 my $psgi_attach = 'cfa3622cbeffc9bd6b0fc66c4d60d420ba74f60d';
85 lei_ok('blob', $psgi_attach);
86 like($lei_out, qr!^Content-Type: multipart/mixed;!sm, 'got full blob');
87 lei_ok('blob', "$psgi_attach:2");
88 is($lei_out, "b64\xde\xad\xbe\xef\n", 'got attachment');
90 lei_ok 'forget-mail-sync', $url;
91 lei_ok 'ls-mail-sync';
92 unlike($lei_out, qr!\Q$host_port\E!, 'sync info gone after forget');
93 my $uid_url = "$url/;UID=".$stats->{'uid.max'};
94 lei_ok 'import', $uid_url;
95 lei_ok 'ls-mail-sync';
96 is($lei_out, "$url\n", 'ls-mail-sync added URL w/o UID');
97 lei_ok 'inspect', $uid_url;
98 $lei_out =~ /([a-f0-9]{40,})/ or
99 xbail 'inspect missed blob with UID URL';
101 lei_ok 'lcat', $uid_url;
102 like $lei_out, qr/^Subject: /sm,
103 'lcat shows mail text with UID URL';
104 like $lei_out, qr/\bblob:$blob\b/, 'lcat showed blob';
106 lei_ok 'lcat', "blob:$blob";
107 is($lei_out, $orig, 'lcat understands blob:...');
108 lei_ok qw(lcat -f json), $uid_url;
109 $out = json_utf8->decode($lei_out);
110 is(scalar(@$out), 2, 'got JSON') or diag explain($out);
111 lei_ok qw(lcat), $url_orig;
112 is($lei_out, $orig, 'lcat w/o UID works');