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 use File::Path qw(remove_tree);
9 my $stop_daemon = sub { # needed since we don't have inotify
10 lei_ok qw(daemon-pid);
11 chomp(my $pid = $lei_out);
12 $pid > 0 or xbail "bad pid: $pid";
13 kill('TERM', $pid) or xbail "kill: $!";
16 kill(0, $pid) or last;
18 kill(0, $pid) and xbail "daemon still running (PID:$pid)";
21 test_lei({ daemon_only => 1 }, sub {
22 my $d = "$ENV{HOME}/d";
23 my ($ro_home, $cfg_path) = setup_public_inboxes;
24 lei_ok qw(daemon-pid);
25 lei_ok qw(add-external), "$ro_home/t2";
26 lei_ok qw(q mid:testmessage@example.com -o), "Maildir:$d";
27 my (@o) = glob("$d/*/*");
28 scalar(@o) == 1 or xbail('multiple results', \@o);
29 my ($bn0) = ($o[0] =~ m!/([^/]+)\z!);
31 my $oid = '9bf1002c49eb075df47247b74d69bcd555e23422';
32 lei_ok 'inspect', "blob:$oid";
33 my $before = json_utf8->decode($lei_out);
34 my $exp0 = { 'mail-sync' => { "maildir:$d" => [ $bn0 ] } };
35 is_deeply($before, $exp0, 'inspect shows expected');
39 $dst =~ s/:2,.*\z// and $dst =~ s!/cur/!/new/! and
40 rename($o[0], $dst) or xbail "rename($o[0] => $dst): $!";
42 lei_ok 'inspect', "blob:$oid";
43 is_deeply(json_utf8->decode($lei_out),
44 $before, 'inspect unchanged immediately after restart');
45 lei_ok 'refresh-mail-sync', '--all';
46 lei_ok 'inspect', "blob:$oid";
47 my ($bn1) = ($dst =~ m!/([^/]+)\z!);
48 my $exp1 = { 'mail-sync' => { "maildir:$d" => [ $bn1 ] } };
49 is_deeply(json_utf8->decode($lei_out), $exp1,
50 'refresh-mail-sync updated location');
53 rename($dst, "$d/unwatched") or xbail "rename $dst out-of-the-way $!";
55 lei_ok 'refresh-mail-sync', $d;
56 lei_ok 'inspect', "blob:$oid";
57 is($lei_out, '{}', 'no known locations after "removal"');
58 lei_ok 'refresh-mail-sync', "Maildir:$d";
61 rename("$d/unwatched", $dst) or xbail "rename $dst back";
63 lei_ok 'refresh-mail-sync', "Maildir:$d";
64 lei_ok 'inspect', "blob:$oid";
65 is_deeply(json_utf8->decode($lei_out), $exp1,
66 'replaced file noted again');
71 lei_ok 'refresh-mail-sync', '--all';
72 lei_ok 'inspect', "blob:$oid";
73 is($lei_out, '{}', 'no known locations after "removal"');
74 lei_ok 'ls-mail-sync';
75 is($lei_out, '', 'no sync left when folder is gone');
78 require_mods(qw(-imapd -nntpd Mail::IMAPClient Net::NNTP), 1);
79 require File::Copy; # stdlib
80 my $home = $ENV{HOME};
82 my $cfg_path2 = "$home/cfg2";
83 File::Copy::cp($cfg_path, $cfg_path2);
84 my $env = { PI_CONFIG => $cfg_path2 };
86 for my $x (qw(imapd)) {
88 $sock_cls //= ref($s);
89 my $cmd = [ "-$x", '-W0', "--stdout=$home/$x.out",
90 "--stderr=$home/$x.err" ];
91 my $td = start_script($cmd, $env, { 3 => $s}) or xbail("-$x");
93 addr => (my $scalar = tcp_host_port($s)),
98 my $url = "imap://$srv->{imapd}->{addr}/t.v1.0";
99 lei_ok 'import', $url, '+L:v1';
100 lei_ok 'inspect', "blob:$oid";
101 $before = json_utf8->decode($lei_out);
102 my @f = grep(m!\Aimap://;AUTH=ANONYMOUS\@\Q$srv->{imapd}->{addr}\E!,
103 keys %{$before->{'mail-sync'}});
104 is(scalar(@f), 1, 'got IMAP folder') or xbail(\@f);
105 xsys([qw(git config), '-f', $cfg_path2,
106 qw(--unset publicinbox.t1.newsgroup)]) and
107 xbail "git config $?";
108 $stop_daemon->(); # drop IMAP IDLE
109 $srv->{imapd}->{td}->kill('HUP');
111 lei_ok 'refresh-mail-sync', $url;
112 lei_ok 'inspect', "blob:$oid";
113 my $after = json_utf8->decode($lei_out);
114 ok(!$after->{'mail-sync'}, 'no sync info for non-existent mailbox');
115 lei_ok 'ls-mail-sync';
116 unlike $lei_out, qr!^\Q$f[0]\E!, 'IMAP folder gone from mail_sync';
118 # simulate server downtime
119 $url = "imap://$srv->{imapd}->{addr}/t.v2.0";
120 lei_ok 'import', $url, '+L:v2';
122 lei_ok 'inspect', "blob:$oid";
124 delete $srv->{imapd}->{td}; # kill + join daemon
126 ok(!(lei 'refresh-mail-sync', $url), 'URL fails on dead -imapd');
127 ok(!(lei 'refresh-mail-sync', '--all'), '--all fails on dead -imapd');
129 # restart server (somewhat dangerous since we released the socket)
130 my $listen = $sock_cls->new(
133 Type => Socket::SOCK_STREAM(),
136 LocalAddr => $srv->{imapd}->{addr},
137 ) or xbail "$sock_cls->new: $!";
138 my $cmd = $srv->{imapd}->{cmd};
139 $srv->{imapd}->{td} = start_script($cmd, $env, { 3 => $listen }) or
141 lei_ok 'refresh-mail-sync', '--all';
142 lei_ok 'inspect', "blob:$oid";
143 is($lei_out, $before, 'no changes when server was down');
144 }; # imapd+nntpd stuff