2 # Copyright (C) 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 note explain([$ro->folders($imap)]);
23 note explain([$imap, [$ro->folders]]);
24 is_deeply([$ro->folders($imap)], [$imap], 'IMAP folder with full GLOB');
25 is_deeply([$ro->folders('imaps://bob@[::1]/INBOX')], [$imap],
26 'IMAP folder with partial GLOB');
28 is_deeply($ro->locations_for($deadbeef),
29 { $imap => [ 1 ] }, 'locations_for w/ imap');
31 my $maildir = 'maildir:/home/user/md';
32 my $fname = 'foo:2,S';
33 $lms->lms_write_prepare;
34 ok($lms->set_src($deadbeef, $maildir, \$fname), 'set Maildir once');
35 ok($lms->set_src($deadbeef, $maildir, \$fname) == 0, 'set Maildir again');
36 is_deeply($ro->locations_for($deadbeef),
37 { $imap => [ 1 ], $maildir => [ $fname ] },
38 'locations_for w/ maildir + imap');
40 if ('mess things up pretend old bug') {
41 $lms->lms_write_prepare;
42 diag "messing things up";
43 $lms->{dbh}->do('UPDATE folders SET loc = ? WHERE loc = ?', undef,
44 "$maildir/", $maildir);
45 ok(delete $lms->{fmap}, 'clear folder map');
47 $lms->lms_write_prepare;
48 ok($lms->set_src($deadbeef, $maildir, \$fname), 'set Maildir once');
51 is_deeply([sort($ro->folders)], [$imap, $maildir], 'both folders shown');
53 $ro->each_src($maildir, sub {
54 my ($oidbin, $id) = @_;
55 push @res, [ unpack('H*', $oidbin), $id ];
57 is_deeply(\@res, [ ['deadbeef', \$fname] ], 'each_src works on Maildir');
60 $ro->each_src($imap, sub {
61 my ($oidbin, $id) = @_;
62 push @res, [ unpack('H*', $oidbin), $id ];
64 is_deeply(\@res, [ ['deadbeef', 1] ], 'each_src works on IMAP');
66 is_deeply($ro->location_stats($maildir), { 'name.count' => 1 },
67 'Maildir location stats');
68 is_deeply($ro->location_stats($imap),
69 { 'uid.count' => 1, 'uid.max' => 1, 'uid.min' => 1 },
70 'IMAP location stats');
71 $lms->lms_write_prepare;
72 is($lms->clear_src($imap, 1), 1, 'clear_src on IMAP');
73 is($lms->clear_src($maildir, \$fname), 1, 'clear_src on Maildir');
74 ok($lms->clear_src($imap, 1) == 0, 'clear_src again on IMAP');
75 ok($lms->clear_src($maildir, \$fname) == 0, 'clear_src again on Maildir');
76 is_deeply($ro->location_stats($maildir), {}, 'nothing left');