]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei-q-save.t
lei up: further improve Maildir canonicalization
[public-inbox.git] / t / lei-q-save.t
1 #!perl -w
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;
5 use PublicInbox::Smsg;
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)}));
12
13 test_lei(sub {
14         my $home = $ENV{HOME};
15         my $in = $doc1->as_string;
16         lei_ok [qw(import -q -F eml -)], undef, { 0 => \$in, %$lei_opt };
17         lei_ok qw(q -q --save z:0.. d:last.week..), '-o', "$home/md/";
18         my %before = map { $_ => 1 } glob("$home/md/cur/*");
19         is_deeply(eml_load((keys %before)[0]), $doc1, 'doc1 matches');
20
21         my @s = glob("$home/.local/share/lei/saved-searches/md-*");
22         is(scalar(@s), 1, 'got one saved search');
23         my $cfg = PublicInbox::Config->new("$s[0]/lei.saved-search");
24         is_deeply($cfg->{'lei.q'}, ['z:0..', 'd:last.week..'],
25                 'store relative time, not parsed (absolute) timestamp');
26
27         # ensure "lei up" works, since it compliments "lei q --save"
28         $in = $doc2->as_string;
29         lei_ok [qw(import -q -F eml -)], undef, { 0 => \$in, %$lei_opt };
30         opendir my $dh, '.' or xbail "opendir .: $!";
31         lei_ok qw(up -q md -C), $home;
32         lei_ok qw(up -q . -C), "$home/md";
33         lei_ok qw(up -q), "/$home/md";
34         chdir($dh) or xbail "fchdir . $!";
35         my %after = map { $_ => 1 } glob("$home/md/cur/*");
36         is(delete $after{(keys(%before))[0]}, 1, 'original message kept');
37         is(scalar(keys %after), 1, 'one new message added');
38         is_deeply(eml_load((keys %after)[0]), $doc2, 'doc2 matches');
39
40         # check stdin
41         lei_ok [qw(q --save - -o), "mboxcl2:mbcl2" ],
42                 undef, { -C => $home, %$lei_opt, 0 => \'d:last.week..'};
43         @s = glob("$home/.local/share/lei/saved-searches/mbcl2-*");
44         $cfg = PublicInbox::Config->new("$s[0]/lei.saved-search");
45         is_deeply $cfg->{'lei.q'}, 'd:last.week..',
46                 'q --stdin stores relative time';
47         my $size = -s "$home/mbcl2";
48         ok(defined($size) && $size > 0, 'results written');
49         lei_ok([qw(up mbcl2)], undef, { -C => $home, %$lei_opt });
50         is(-s "$home/mbcl2", $size, 'size unchanged on noop up');
51
52         $in = $doc3->as_string;
53         lei_ok [qw(import -q -F eml -)], undef, { 0 => \$in, %$lei_opt };
54         lei_ok([qw(up mbcl2)], undef, { -C => $home, %$lei_opt });
55         ok(-s "$home/mbcl2" > $size, 'size increased after up');
56
57         ok(!lei(qw(up -q), $home), 'up fails w/o --save');
58 });
59 done_testing;