lei/store: stop shard workers + cat-file on idle
[public-inbox.git] / t / mh_reader.t
blob95a7be4aa9f7b1dff6b69fada1bd0f0223c5bac1
1 #!perl -w
2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use PublicInbox::TestCommon;
5 require_ok 'PublicInbox::MHreader';
6 use PublicInbox::IO qw(write_file);
7 use PublicInbox::Lock;
8 use PublicInbox::Eml;
9 use File::Path qw(remove_tree);
10 use autodie;
11 opendir my $cwdfh, '.';
13 my $normal = create_dir 'normal', sub {
14         write_file '>', 3, "Subject: replied a\n\n";
15         write_file '>', 4, "Subject: replied b\n\n";
16         write_file '>', 1, "Subject: unseen\n\n";
17         write_file '>', 2, "Subject: unseen flagged\n\n";
18         write_file '>', '.mh_sequences', <<EOM;
19 unseen: 1 2
20 flagged: 2
21 replied: 3 4
22 EOM
25 my $for_sort = create_dir 'size', sub {
26         for (1..3) {
27                 my $name = 10 - $_;
28                 write_file '>', $name, "Subject: ".($_ x $_)."\n\n";
29         }
32 my $stale = create_dir 'stale', sub {
33         write_file '>', 4, "Subject: msg 4\n\n";
34         write_file '>', '.mh_sequences', <<EOM;
35 unseen: 1 2
36 EOM
40         my $mhr = PublicInbox::MHreader->new("$normal/", $cwdfh);
41         $mhr->{sort} = [ '' ];
42         my @res;
43         $mhr->mh_each_eml(sub { push @res, \@_; }, [ 'bogus' ]);
44         is scalar(@res), 4, 'got 4 messages' or diag explain(\@res);
45         is_deeply [map { $_->[1] } @res], [1, 2, 3, 4],
46                 'got messages in expected order';
47         is scalar(grep { $_->[4]->[0] eq 'bogus' } @res), scalar(@res),
48                 'cb arg passed to all messages' or diag explain(\@res);
50         $mhr = PublicInbox::MHreader->new("$stale/", $cwdfh);
51         @res = ();
52         $mhr->mh_each_eml(sub { push @res, \@_; });
53         is scalar(@res), 1, 'ignored stale messages';
56 test_lei(sub {
57         lei_ok qw(convert -f mboxrd), $normal;
58         my @msgs = grep /\S/s, split /^From .[^\n]+\n/sm, $lei_out;
59         my @eml = map { PublicInbox::Eml->new($_) } @msgs;
60         my $h = 'Subject';
61         @eml = sort { $a->header_raw($h) cmp $b->header_raw($h) } @eml;
62         my @has = map { scalar $_->header_raw($h) } @eml;
63         is_xdeeply \@has,
64                 [ 'replied a', 'replied b', 'unseen', 'unseen flagged' ],
65                 'subjects sorted';
66         $h = 'X-Status';
67         @has = map { scalar $_->header_raw($h) } @eml;
68         is_xdeeply \@has, [ 'A', 'A', undef, 'F' ], 'answered and flagged kw';
69         $h = 'Status';
70         @has = map { scalar $_->header_raw($h) } @eml;
71         is_xdeeply \@has, ['RO', 'RO', 'O', 'O'], 'read and old';
72         lei_ok qw(import +L:normal), $normal;
73         lei_ok qw(q L:normal -f mboxrd);
74         @msgs = grep /\S/s, split /^From .[^\n]+\n/sm, $lei_out;
75         my @eml2 = map { PublicInbox::Eml->new($_) } @msgs;
76         $h = 'Subject';
77         @eml2 = sort { $a->header_raw($h) cmp $b->header_raw($h) } @eml2;
78         is_xdeeply \@eml2, \@eml, 'import preserved kw';
80         lei_ok 'ls-mail-sync';
81         is $lei_out, 'mh:'.File::Spec->rel2abs($normal)."\n",
82                 'mail sync stored';
84         lei_ok qw(convert -s size -f mboxrd), "mh:$for_sort";
85         chomp(my @s = grep /^Subject:/, split(/^/sm, $lei_out));
86         s/^Subject: // for @s;
87         is_xdeeply \@s, [ 1, 22, 333 ], 'sorted by size';
89         for my $s ([], [ 'name' ], [ 'sequence' ]) {
90                 lei_ok qw(convert -f mboxrd), "mh:$for_sort", '-s', @$s;
91                 chomp(@s = grep /^Subject:/, split(/^/sm, $lei_out));
92                 s/^Subject: // for @s;
93                 my $desc = "@$s" || '(default)';
94                 is_xdeeply \@s, [ 333, 22, 1 ], "sorted by: $desc";
95         }
97         lei_ok qw(import +L:sorttest), "MH:$for_sort";
98         lei_ok 'ls-mail-sync', $for_sort;
99         is $lei_out, 'mh:'.File::Spec->rel2abs($for_sort)."\n",
100                 "mail sync stored with `MH' normalized to `mh'";
101         lei_ok qw(index), 'mh:'.$stale;
102         lei qw(q -f mboxrd), 's:msg 4';
103         like $lei_out, qr/^Subject: msg 4\nStatus: RO\n\n\n/ms,
104                 "message retrieved after `lei index'";
106         lei_ok qw(convert -s none -f text), "mh:$for_sort", \'--sort=none';
108         # ensure sort works for _input_ when output disallows sort
109         my $v2out = "$ENV{HOME}/v2-out";
110         for my $sort (['--sort=sequence'], []) { # sequence is the default
111                 lei_ok qw(convert), @$sort, "mh:$for_sort", '-o', "v2:$v2out";
112                 my $g = PublicInbox::Git->new("$v2out/git/0.git");
113                 chomp(my @l = $g->qx(qw(log --pretty=oneline --format=%s)));
114                 is_xdeeply \@l, [1, 22, 333], 'sequence order preserved for v2';
115                 File::Path::remove_tree $v2out;
116         }
119 done_testing;