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;
6 use List::Util qw(sum);
8 my $doc1 = eml_load('t/plack-qp.eml');
9 $doc1->header_set('Date', PublicInbox::Smsg::date({ds => time - (86400 * 5)}));
10 my $doc2 = eml_load('t/utf8.eml');
11 $doc2->header_set('Date', PublicInbox::Smsg::date({ds => time - (86400 * 4)}));
12 my $doc3 = eml_load('t/msg_iter-order.eml');
13 $doc3->header_set('Date', PublicInbox::Smsg::date({ds => time - (86400 * 4)}));
15 my $pre_existing = <<'EOF';
16 From x Mon Sep 17 00:00:00 2001
17 Message-ID: <import-before@example.com>
19 Date: Sat, 02 Oct 2010 00:00:00 +0000
25 my $home = $ENV{HOME};
26 my $in = $doc1->as_string;
27 lei_ok [qw(import -q -F eml -)], undef, { 0 => \$in, %$lei_opt };
28 lei_ok qw(q -q --save z:0.. d:last.week..), '-o', "MAILDIR:$home/md/";
29 my %before = map { $_ => 1 } glob("$home/md/cur/*");
30 my $f = (keys %before)[0] or xbail({before => \%before});
31 is_deeply(eml_load($f), $doc1, 'doc1 matches');
32 lei_ok qw(ls-mail-sync);
33 is($lei_out, "maildir:$home/md\n", 'canonicalized mail sync name');
35 my @s = glob("$home/.local/share/lei/saved-searches/md-*");
36 is(scalar(@s), 1, 'got one saved search');
37 my $cfg = PublicInbox::Config->new("$s[0]/lei.saved-search");
38 is($cfg->{'lei.q.output'}, "maildir:$home/md", 'canonicalized output');
39 is_deeply($cfg->{'lei.q'}, ['z:0..', 'd:last.week..'],
40 'store relative time, not parsed (absolute) timestamp');
42 # ensure "lei up" works, since it compliments "lei q --save"
43 $in = $doc2->as_string;
44 lei_ok [qw(import -q -F eml -)], undef, { 0 => \$in, %$lei_opt };
45 lei_ok qw(up -q md -C), $home;
46 lei_ok qw(up -q . -C), "$home/md";
47 lei_ok qw(up -q), "/$home/md";
48 my %after = map { $_ => 1 } glob("$home/md/{new,cur}/*");
49 is(delete $after{(keys(%before))[0]}, 1, 'original message kept');
50 is(scalar(keys %after), 1, 'one new message added');
51 $f = (keys %after)[0] or xbail({after => \%after});
52 is_deeply(eml_load($f), $doc2, 'doc2 matches');
55 lei_ok [qw(q --save - -o), "mboxcl2:mbcl2" ],
56 undef, { -C => $home, %$lei_opt, 0 => \'d:last.week..'};
57 @s = glob("$home/.local/share/lei/saved-searches/mbcl2-*");
58 $cfg = PublicInbox::Config->new("$s[0]/lei.saved-search");
59 is_deeply $cfg->{'lei.q'}, 'd:last.week..',
60 'q --stdin stores relative time';
61 my $size = -s "$home/mbcl2";
62 ok(defined($size) && $size > 0, 'results written');
63 lei_ok([qw(up mbcl2)], undef, { -C => $home, %$lei_opt });
64 is(-s "$home/mbcl2", $size, 'size unchanged on noop up');
66 $in = $doc3->as_string;
67 lei_ok [qw(import -q -F eml -)], undef, { 0 => \$in, %$lei_opt };
68 lei_ok([qw(up mbcl2)], undef, { -C => $home, %$lei_opt });
69 ok(-s "$home/mbcl2" > $size, 'size increased after up');
71 ok(!lei(qw(up -q), $home), 'up fails w/o --save');
73 lei_ok qw(ls-search); my @d = split(/\n/, $lei_out);
74 lei_ok qw(ls-search -z); my @z = split(/\0/, $lei_out);
75 is_deeply(\@d, \@z, '-z output matches non-z');
76 is_deeply(\@d, [ "$home/mbcl2", "$home/md" ],
77 'ls-search output alphabetically sorted');
78 lei_ok qw(ls-search -l);
79 my $json = PublicInbox::Config->json->decode($lei_out);
80 ok($json && $json->[0]->{output}, 'JSON has output');
81 lei_ok qw(_complete lei up);
82 like($lei_out, qr!^\Q$home/mbcl2\E$!sm, 'complete got mbcl2 output');
83 like($lei_out, qr!^\Q$home/md\E$!sm, 'complete got maildir output');
85 unlink("$home/mbcl2") or xbail "unlink $!";
86 lei_ok qw(_complete lei up);
87 like($lei_out, qr!^\Q$home/mbcl2\E$!sm,
88 'mbcl2 output shown despite unlink');
89 lei_ok([qw(up mbcl2)], undef, { -C => $home, %$lei_opt });
90 ok(-f "$home/mbcl2" && -s _ == 0, 'up recreates on missing output');
93 open my $mb, '>', "$home/mbrd" or xbail "open $!";
94 print $mb $pre_existing;
95 close $mb or xbail "close: $!";
96 lei_ok(qw(q --save -o mboxrd:mbrd m:qp@example.com -C), $home);
97 open $mb, '<', "$home/mbrd" or xbail "open $!";
98 is_deeply([grep(/pre-existing/, <$mb>)], [],
99 'pre-existing messsage gone w/o augment');
101 lei_ok(qw(q m:import-before@example.com));
102 is(json_utf8->decode($lei_out)->[0]->{'s'},
103 'pre-existing', '--save imported before clobbering');
106 open $mb, '>', "$home/mbrd-aug" or xbail "open $!";
107 print $mb $pre_existing;
108 close $mb or xbail "close: $!";
109 lei_ok(qw(q -a --save -o mboxrd:mbrd-aug m:qp@example.com -C), $home);
110 open $mb, '<', "$home/mbrd-aug" or xbail "open $!";
111 $mb = do { local $/; <$mb> };
112 like($mb, qr/pre-existing/, 'pre-existing message preserved w/ -a');
113 like($mb, qr/<qp\@example\.com>/, 'new result written w/ -a');
115 lei_ok(qw(up --all=local));
117 ok(!lei(qw(forget-search), "$home/bogus"), 'bogus forget');
118 lei_ok qw(_complete lei forget-search);
119 like($lei_out, qr/mbrd-aug/, 'forget-search completion');
120 lei_ok(qw(forget-search -v), "$home/mbrd-aug");
121 is($lei_out, '', 'no output');
122 like($lei_err, qr/\bmbrd-aug\b/, '-v (verbose) reported unlinks');
123 lei_ok qw(_complete lei forget-search);
124 unlike($lei_out, qr/mbrd-aug/,
125 'forget-search completion cleared after forget');
126 ok(!lei('up', "$home/mbrd-aug"), 'lei up fails after forget');
129 my $o = "$home/dd-mid";
130 $in = $doc2->as_string . "\n-------\nappended list sig\n";
131 lei_ok [qw(import -q -F eml -)], undef, { 0 => \$in, %$lei_opt };
132 lei_ok(qw(q --dedupe=mid --save m:testmessage@example.com -o), $o);
133 my @m = glob("$o/cur/*");
134 is(scalar(@m), 1, '--dedupe=mid w/ --save');
135 $in = $doc2->as_string . "\n-------\nanother list sig\n";
136 lei_ok [qw(import -q -F eml -)], undef, { 0 => \$in, %$lei_opt };
138 is_deeply([glob("$o/cur/*")], \@m, 'lei up dedupe=mid works');
140 for my $dd (qw(content)) {
142 lei_ok(qw(q --save m:testmessage@example.com -o), $o,
144 @m = glob("$o/cur/*");
145 is(scalar(@m), 3, 'all 3 matches with dedupe='.$dd);
150 my $ibx = create_inbox 'ibx', indexlevel => 'medium',
151 tmpdir => "$home/v1", sub {};
152 lei_ok(qw(q --save --dedupe=oid m:qp@example.com -o), $o,
153 '-I', $ibx->{inboxdir});
154 @m = glob("$o/cur/*");
155 is(scalar(@m), 1, 'got first result');
157 my $im = $ibx->importer(0);
158 my $diff = "X-Insignificant-Header: x\n".$doc1->as_string;
159 $im->add(PublicInbox::Eml->new($diff));
162 @m = glob("$o/{new,cur}/*");
163 is(scalar(@m), 2, 'got 2nd result due to different OID');
166 symlink($o, "$home/ln -s") or
167 skip "symlinks not supported in $home?: $!", 1;
168 lei_ok('up', "$home/ln -s");
171 my $v2 = "$home/v2"; # v2: as an output destination
172 my (@before, @after);
173 require PublicInbox::MboxReader;
174 lei_ok(qw(q z:0.. -o), "v2:$v2");
175 lei_ok(qw(q z:0.. -o), "mboxrd:$home/before", '--only', $v2, '-j1,1');
176 open my $fh, '<', "$home/before";
177 PublicInbox::MboxReader->mboxrd($fh, sub { push @before, $_[0] });
178 isnt(scalar(@before), 0, 'initial v2 written');
179 my $orig = sum(map { -f $_ ? -s _ : () } (
180 glob("$v2/git/0.git/objects/*/*")));
181 lei_ok(qw(import t/data/0001.patch));
183 lei_ok(qw(q z:0.. -o), "mboxrd:$home/after", '--only', $v2, '-j1,1');
184 open $fh, '<', "$home/after";
185 PublicInbox::MboxReader->mboxrd($fh, sub { push @after, $_[0] });
187 my $last = shift @after;
188 $last->header_set('Status');
189 is_deeply($last, eml_load('t/data/0001.patch'), 'lei up worked on v2');
190 is_deeply(\@before, \@after, 'got same results');
192 my $v2s = "$home/v2s";
193 lei_ok(qw(q --shared z:0.. -o), "v2:$v2s");
194 my $shared = sum(map { -f $_ ? -s _ : () } (
195 glob("$v2s/git/0.git/objects/*/*")));
196 ok($shared < $orig, 'fewer bytes stored with --shared') or
197 diag "shared=$shared orig=$orig";
199 lei_ok([qw(edit-search), $v2s], { VISUAL => 'cat', EDITOR => 'cat' });
200 like($lei_out, qr/^\[lei/sm, 'edit-search can cat');