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');
72 like($lei_err, qr/--save was not used/, 'error noted --save');
74 lei_ok qw(ls-search); my @d = split(/\n/, $lei_out);
75 lei_ok qw(ls-search -z); my @z = split(/\0/, $lei_out);
76 is_deeply(\@d, \@z, '-z output matches non-z');
77 is_deeply(\@d, [ "$home/mbcl2", "$home/md" ],
78 'ls-search output alphabetically sorted');
79 lei_ok qw(ls-search -l);
80 my $json = PublicInbox::Config->json->decode($lei_out);
81 ok($json && $json->[0]->{output}, 'JSON has output');
82 lei_ok qw(_complete lei up);
83 like($lei_out, qr!^\Q$home/mbcl2\E$!sm, 'complete got mbcl2 output');
84 like($lei_out, qr!^\Q$home/md\E$!sm, 'complete got maildir output');
86 unlink("$home/mbcl2") or xbail "unlink $!";
87 lei_ok qw(_complete lei up);
88 like($lei_out, qr!^\Q$home/mbcl2\E$!sm,
89 'mbcl2 output shown despite unlink');
90 lei_ok([qw(up mbcl2)], undef, { -C => $home, %$lei_opt });
91 ok(-f "$home/mbcl2" && -s _ == 0, 'up recreates on missing output');
94 open my $mb, '>', "$home/mbrd" or xbail "open $!";
95 print $mb $pre_existing;
96 close $mb or xbail "close: $!";
97 lei_ok(qw(q --save -o mboxrd:mbrd m:qp@example.com -C), $home);
98 open $mb, '<', "$home/mbrd" or xbail "open $!";
99 is_deeply([grep(/pre-existing/, <$mb>)], [],
100 'pre-existing messsage gone w/o augment');
102 lei_ok(qw(q m:import-before@example.com));
103 is(json_utf8->decode($lei_out)->[0]->{'s'},
104 'pre-existing', '--save imported before clobbering');
107 open $mb, '>', "$home/mbrd-aug" or xbail "open $!";
108 print $mb $pre_existing;
109 close $mb or xbail "close: $!";
110 lei_ok(qw(q -a --save -o mboxrd:mbrd-aug m:qp@example.com -C), $home);
111 open $mb, '<', "$home/mbrd-aug" or xbail "open $!";
112 $mb = do { local $/; <$mb> };
113 like($mb, qr/pre-existing/, 'pre-existing message preserved w/ -a');
114 like($mb, qr/<qp\@example\.com>/, 'new result written w/ -a');
116 lei_ok(qw(up --all=local));
118 ok(!lei(qw(forget-search), "$home/bogus"), 'bogus forget');
119 like($lei_err, qr/--save was not used/, 'error noted --save');
121 lei_ok qw(_complete lei forget-search);
122 like($lei_out, qr/mbrd-aug/, 'forget-search completion');
123 lei_ok(qw(forget-search -v), "$home/mbrd-aug");
124 is($lei_out, '', 'no output');
125 like($lei_err, qr/\bmbrd-aug\b/, '-v (verbose) reported unlinks');
126 lei_ok qw(_complete lei forget-search);
127 unlike($lei_out, qr/mbrd-aug/,
128 'forget-search completion cleared after forget');
129 ok(!lei('up', "$home/mbrd-aug"), 'lei up fails after forget');
130 like($lei_err, qr/--save was not used/, 'error noted --save');
133 my $o = "$home/dd-mid";
134 $in = $doc2->as_string . "\n-------\nappended list sig\n";
135 lei_ok [qw(import -q -F eml -)], undef, { 0 => \$in, %$lei_opt };
136 lei_ok(qw(q --dedupe=mid --save m:testmessage@example.com -o), $o);
137 my @m = glob("$o/cur/*");
138 is(scalar(@m), 1, '--dedupe=mid w/ --save');
139 $in = $doc2->as_string . "\n-------\nanother list sig\n";
140 lei_ok [qw(import -q -F eml -)], undef, { 0 => \$in, %$lei_opt };
142 is_deeply([glob("$o/cur/*")], \@m, 'lei up dedupe=mid works');
144 for my $dd (qw(content)) {
146 lei_ok(qw(q --save m:testmessage@example.com -o), $o,
148 @m = glob("$o/cur/*");
149 is(scalar(@m), 3, 'all 3 matches with dedupe='.$dd);
154 my $ibx = create_inbox 'ibx', indexlevel => 'medium',
155 tmpdir => "$home/v1", sub {};
156 lei_ok(qw(q --save --dedupe=oid m:qp@example.com -o), $o,
157 '-I', $ibx->{inboxdir});
158 @m = glob("$o/cur/*");
159 is(scalar(@m), 1, 'got first result');
161 my $im = $ibx->importer(0);
162 my $diff = "X-Insignificant-Header: x\n".$doc1->as_string;
163 $im->add(PublicInbox::Eml->new($diff));
166 @m = glob("$o/{new,cur}/*");
167 is(scalar(@m), 2, 'got 2nd result due to different OID');
170 symlink($o, "$home/ln -s") or
171 skip "symlinks not supported in $home?: $!", 1;
172 lei_ok('up', "$home/ln -s");
175 my $v2 = "$home/v2"; # v2: as an output destination
176 my (@before, @after);
177 require PublicInbox::MboxReader;
178 lei_ok(qw(q z:0.. -o), "v2:$v2");
179 lei_ok(qw(q z:0.. -o), "mboxrd:$home/before", '--only', $v2, '-j1,1');
180 open my $fh, '<', "$home/before";
181 PublicInbox::MboxReader->mboxrd($fh, sub { push @before, $_[0] });
182 isnt(scalar(@before), 0, 'initial v2 written');
183 my $orig = sum(map { -f $_ ? -s _ : () } (
184 glob("$v2/git/0.git/objects/*/*")));
185 lei_ok(qw(import t/data/0001.patch));
187 lei_ok(qw(q z:0.. -o), "mboxrd:$home/after", '--only', $v2, '-j1,1');
188 open $fh, '<', "$home/after";
189 PublicInbox::MboxReader->mboxrd($fh, sub { push @after, $_[0] });
191 my $last = shift @after;
192 $last->header_set('Status');
193 is_deeply($last, eml_load('t/data/0001.patch'), 'lei up worked on v2');
194 is_deeply(\@before, \@after, 'got same results');
196 my $v2s = "$home/v2s";
197 lei_ok(qw(q --shared z:0.. -o), "v2:$v2s");
198 my $shared = sum(map { -f $_ ? -s _ : () } (
199 glob("$v2s/git/0.git/objects/*/*")));
200 ok($shared < $orig, 'fewer bytes stored with --shared') or
201 diag "shared=$shared orig=$orig";
203 lei_ok([qw(edit-search), $v2s], { VISUAL => 'cat', EDITOR => 'cat' });
204 like($lei_out, qr/^\[lei/sm, 'edit-search can cat');
206 lei_ok('-C', "$home/v2s",
207 qw(q -q --save -o ../s m:testmessage@example.com));
208 lei_ok qw(ls-search);
209 unlike $lei_out, qr{/\.\./s$}sm, 'relative path not in ls-search';
210 like $lei_out, qr{^\Q$home\E/s$}sm,
211 'absolute path appears in ls-search';
212 lei_ok qw(up ../s -C), "$home/v2s", \'relative lei up';
213 lei_ok qw(up), "$home/s", \'absolute lei up';