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