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 my $doc1 = eml_load('t/plack-qp.eml');
7 $doc1->header_set('Date', PublicInbox::Smsg::date({ds => time - (86400 * 5)}));
8 my $doc2 = eml_load('t/utf8.eml');
9 $doc2->header_set('Date', PublicInbox::Smsg::date({ds => time - (86400 * 4)}));
10 my $doc3 = eml_load('t/msg_iter-order.eml');
11 $doc3->header_set('Date', PublicInbox::Smsg::date({ds => time - (86400 * 4)}));
13 my $pre_existing = <<'EOF';
14 From x Mon Sep 17 00:00:00 2001
15 Message-ID: <import-before@example.com>
17 Date: Sat, 02 Oct 2010 00:00:00 +0000
23 my $home = $ENV{HOME};
24 my $in = $doc1->as_string;
25 lei_ok [qw(import -q -F eml -)], undef, { 0 => \$in, %$lei_opt };
26 lei_ok qw(q -q --save z:0.. d:last.week..), '-o', "$home/md/";
27 my %before = map { $_ => 1 } glob("$home/md/cur/*");
28 is_deeply(eml_load((keys %before)[0]), $doc1, 'doc1 matches');
30 my @s = glob("$home/.local/share/lei/saved-searches/md-*");
31 is(scalar(@s), 1, 'got one saved search');
32 my $cfg = PublicInbox::Config->new("$s[0]/lei.saved-search");
33 is_deeply($cfg->{'lei.q'}, ['z:0..', 'd:last.week..'],
34 'store relative time, not parsed (absolute) timestamp');
36 # ensure "lei up" works, since it compliments "lei q --save"
37 $in = $doc2->as_string;
38 lei_ok [qw(import -q -F eml -)], undef, { 0 => \$in, %$lei_opt };
39 lei_ok qw(up -q md -C), $home;
40 lei_ok qw(up -q . -C), "$home/md";
41 lei_ok qw(up -q), "/$home/md";
42 my %after = map { $_ => 1 } glob("$home/md/cur/*");
43 is(delete $after{(keys(%before))[0]}, 1, 'original message kept');
44 is(scalar(keys %after), 1, 'one new message added');
45 is_deeply(eml_load((keys %after)[0]), $doc2, 'doc2 matches');
48 lei_ok [qw(q --save - -o), "mboxcl2:mbcl2" ],
49 undef, { -C => $home, %$lei_opt, 0 => \'d:last.week..'};
50 @s = glob("$home/.local/share/lei/saved-searches/mbcl2-*");
51 $cfg = PublicInbox::Config->new("$s[0]/lei.saved-search");
52 is_deeply $cfg->{'lei.q'}, 'd:last.week..',
53 'q --stdin stores relative time';
54 my $size = -s "$home/mbcl2";
55 ok(defined($size) && $size > 0, 'results written');
56 lei_ok([qw(up mbcl2)], undef, { -C => $home, %$lei_opt });
57 is(-s "$home/mbcl2", $size, 'size unchanged on noop up');
59 $in = $doc3->as_string;
60 lei_ok [qw(import -q -F eml -)], undef, { 0 => \$in, %$lei_opt };
61 lei_ok([qw(up mbcl2)], undef, { -C => $home, %$lei_opt });
62 ok(-s "$home/mbcl2" > $size, 'size increased after up');
64 ok(!lei(qw(up -q), $home), 'up fails w/o --save');
66 lei_ok qw(ls-search); my @d = split(/\n/, $lei_out);
67 lei_ok qw(ls-search -z); my @z = split(/\0/, $lei_out);
68 is_deeply(\@d, \@z, '-z output matches non-z');
69 is_deeply(\@d, [ "$home/mbcl2", "$home/md/" ],
70 'ls-search output alphabetically sorted');
71 lei_ok qw(ls-search -l);
72 my $json = PublicInbox::Config->json->decode($lei_out);
73 ok($json && $json->[0]->{output}, 'JSON has output');
74 lei_ok qw(_complete lei up);
75 like($lei_out, qr!^\Q$home/mbcl2\E$!sm, 'complete got mbcl2 output');
76 like($lei_out, qr!^\Q$home/md/\E$!sm, 'complete got maildir output');
78 unlink("$home/mbcl2") or xbail "unlink $!";
79 lei_ok qw(_complete lei up);
80 like($lei_out, qr!^\Q$home/mbcl2\E$!sm,
81 'mbcl2 output shown despite unlink');
82 lei_ok([qw(up mbcl2)], undef, { -C => $home, %$lei_opt });
83 ok(-f "$home/mbcl2" && -s _ == 0, 'up recreates on missing output');
86 open my $mb, '>', "$home/mbrd" or xbail "open $!";
87 print $mb $pre_existing;
88 close $mb or xbail "close: $!";
89 lei_ok(qw(q --save -o mboxrd:mbrd m:qp@example.com -C), $home);
90 open $mb, '<', "$home/mbrd" or xbail "open $!";
91 is_deeply([grep(/pre-existing/, <$mb>)], [],
92 'pre-existing messsage gone w/o augment');
94 lei_ok(qw(q m:import-before@example.com));
95 is(json_utf8->decode($lei_out)->[0]->{'s'},
96 'pre-existing', '--save imported before clobbering');
99 open $mb, '>', "$home/mbrd-aug" or xbail "open $!";
100 print $mb $pre_existing;
101 close $mb or xbail "close: $!";
102 lei_ok(qw(q -a --save -o mboxrd:mbrd-aug m:qp@example.com -C), $home);
103 open $mb, '<', "$home/mbrd-aug" or xbail "open $!";
104 $mb = do { local $/; <$mb> };
105 like($mb, qr/pre-existing/, 'pre-existing message preserved w/ -a');
106 like($mb, qr/<qp\@example\.com>/, 'new result written w/ -a');
108 lei_ok(qw(up --all=local));
110 ok(!lei(qw(forget-search), "$home/bogus"), 'bogus forget');
111 lei_ok qw(_complete lei forget-search);
112 like($lei_out, qr/mbrd-aug/, 'forget-search completion');
113 lei_ok(qw(forget-search -v), "$home/mbrd-aug");
114 is($lei_out, '', 'no output');
115 like($lei_err, qr/\bmbrd-aug\b/, '-v (verbose) reported unlinks');
116 lei_ok qw(_complete lei forget-search);
117 unlike($lei_out, qr/mbrd-aug/,
118 'forget-search completion cleared after forget');
119 ok(!lei('up', "$home/mbrd-aug"), 'lei up fails after forget');
122 my $o = "$home/dd-mid";
123 $in = $doc2->as_string . "\n-------\nappended list sig\n";
124 lei_ok [qw(import -q -F eml -)], undef, { 0 => \$in, %$lei_opt };
125 lei_ok(qw(q --dedupe=mid --save m:testmessage@example.com -o), $o);
126 my @m = glob("$o/cur/*");
127 is(scalar(@m), 1, '--dedupe=mid w/ --save');
128 $in = $doc2->as_string . "\n-------\nanother list sig\n";
129 lei_ok [qw(import -q -F eml -)], undef, { 0 => \$in, %$lei_opt };
131 is_deeply([glob("$o/cur/*")], \@m, 'lei up dedupe=mid works');
133 for my $dd (qw(content)) {
135 lei_ok(qw(q --save m:testmessage@example.com -o), $o,
137 @m = glob("$o/cur/*");
138 is(scalar(@m), 3, 'all 3 matches with dedupe='.$dd);
143 my $ibx = create_inbox 'ibx', indexlevel => 'medium',
144 tmpdir => "$home/v1", sub {};
145 lei_ok(qw(q --save --dedupe=oid m:qp@example.com -o), $o,
146 '-I', $ibx->{inboxdir});
147 @m = glob("$o/cur/*");
148 is(scalar(@m), 1, 'got first result');
150 my $im = $ibx->importer(0);
151 my $diff = "X-Insignificant-Header: x\n".$doc1->as_string;
152 $im->add(PublicInbox::Eml->new($diff));
155 @m = glob("$o/cur/*");
156 is(scalar(@m), 2, 'got 2nd result due to different OID');
159 symlink($o, "$home/ln -s") or
160 skip "symlinks not supported in $home?: $!", 1;
161 lei_ok('up', "$home/ln -s");