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);
9 use File::Path qw(remove_tree);
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;
25 my $for_sort = create_dir 'size', sub {
28 write_file '>', $name, "Subject: ".($_ x $_)."\n\n";
32 my $stale = create_dir 'stale', sub {
33 write_file '>', 4, "Subject: msg 4\n\n";
34 write_file '>', '.mh_sequences', <<EOM;
40 my $mhr = PublicInbox::MHreader->new("$normal/", $cwdfh);
41 $mhr->{sort} = [ '' ];
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);
52 $mhr->mh_each_eml(sub { push @res, \@_; });
53 is scalar(@res), 1, 'ignored stale messages';
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;
61 @eml = sort { $a->header_raw($h) cmp $b->header_raw($h) } @eml;
62 my @has = map { scalar $_->header_raw($h) } @eml;
64 [ 'replied a', 'replied b', 'unseen', 'unseen flagged' ],
67 @has = map { scalar $_->header_raw($h) } @eml;
68 is_xdeeply \@has, [ 'A', 'A', undef, 'F' ], 'answered and flagged kw';
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;
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",
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";
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;