2 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use PublicInbox::TestCommon;
7 require_mods(qw(DBD::SQLite));
8 require_ok 'PublicInbox::LeiMailSync';
9 my ($dir, $for_destroy) = tmpdir();
10 my $lms = PublicInbox::LeiMailSync->new("$dir/t.sqlite3");
12 $lms->lms_write_prepare;
13 my $ro = PublicInbox::LeiMailSync->new("$dir/t.sqlite3");
14 is_deeply([$ro->folders], [], 'no folders, yet');
16 my $imap = 'imaps://bob@[::1]/INBOX;UIDVALIDITY=9';
17 $lms->lms_write_prepare;
18 my $deadbeef = "\xde\xad\xbe\xef";
19 is($lms->set_src($deadbeef, $imap, 1), 1, 'set IMAP once');
20 ok($lms->set_src($deadbeef, $imap, 1) == 0, 'set IMAP idempotently');
21 is_deeply([$ro->folders], [$imap], 'IMAP folder added');
22 is_deeply([$ro->folders($imap)], [$imap], 'IMAP folder with full GLOB');
23 is_deeply([$ro->folders('imaps://bob@[::1]/INBOX')], [$imap],
24 'IMAP folder with partial GLOB');
26 is_deeply($ro->locations_for($deadbeef),
27 { $imap => [ 1 ] }, 'locations_for w/ imap');
29 my $maildir = 'maildir:/home/user/md';
30 my $fname = 'foo:2,S';
31 $lms->lms_write_prepare;
32 ok($lms->set_src($deadbeef, $maildir, \$fname), 'set Maildir once');
33 ok($lms->set_src($deadbeef, $maildir, \$fname) == 0, 'set Maildir again');
34 is_deeply($ro->locations_for($deadbeef),
35 { $imap => [ 1 ], $maildir => [ $fname ] },
36 'locations_for w/ maildir + imap');
38 if ('mess things up pretend old bug') {
39 $lms->lms_write_prepare;
40 $lms->{dbh}->do('UPDATE folders SET loc = ? WHERE loc = ?', undef,
41 "$maildir/", $maildir);
42 ok(delete $lms->{fmap}, 'clear folder map');
44 $lms->lms_write_prepare;
45 ok($lms->set_src($deadbeef, $maildir, \$fname), 'set Maildir once');
48 is_deeply([sort($ro->folders)], [$imap, $maildir], 'both folders shown');
50 $ro->each_src($maildir, sub {
51 my ($oidbin, $id) = @_;
52 push @res, [ unpack('H*', $oidbin), $id ];
54 is_deeply(\@res, [ ['deadbeef', \$fname] ], 'each_src works on Maildir');
57 $ro->each_src($imap, sub {
58 my ($oidbin, $id) = @_;
59 push @res, [ unpack('H*', $oidbin), $id ];
61 is_deeply(\@res, [ ['deadbeef', 1] ], 'each_src works on IMAP');
63 is_deeply($ro->location_stats($maildir), { 'name.count' => 1 },
64 'Maildir location stats');
65 is_deeply($ro->location_stats($imap),
66 { 'uid.count' => 1, 'uid.max' => 1, 'uid.min' => 1 },
67 'IMAP location stats');
68 $lms->lms_write_prepare;
69 is($lms->clear_src($imap, 1), 1, 'clear_src on IMAP');
70 is($lms->clear_src($maildir, \$fname), 1, 'clear_src on Maildir');
71 ok($lms->clear_src($imap, 1) == 0, 'clear_src again on IMAP');
72 ok($lms->clear_src($maildir, \$fname) == 0, 'clear_src again on Maildir');
73 is_deeply($ro->location_stats($maildir), {}, 'nothing left');