]> Sergey Matveev's repositories - public-inbox.git/blob - t/watch_filter_rubylang.t
xt/mem-imapd-tls: update aliases to DSdeflate subs
[public-inbox.git] / t / watch_filter_rubylang.t
1 # Copyright (C) 2019-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use PublicInbox::TestCommon;
6 use Test::More;
7 use PublicInbox::Eml;
8 use PublicInbox::Config;
9 require_mods(qw(DBD::SQLite Search::Xapian));
10 use_ok 'PublicInbox::Watch';
11 use_ok 'PublicInbox::Emergency';
12 my ($tmpdir, $for_destroy) = tmpdir();
13 local $ENV{PI_CONFIG} = "$tmpdir/pi_config";
14
15 my @v = qw(V1);
16 SKIP: {
17         if (require_git(2.6, 1)) {
18                 use_ok 'PublicInbox::V2Writable';
19                 push @v, 'V2';
20         } else {
21                 skip 'git 2.6+ needed for V2', 40;
22         }
23 }
24
25 for my $v (@v) {
26         my @warn;
27         local $SIG{__WARN__} = sub { push @warn, @_ };
28         my $cfgpfx = "publicinbox.$v";
29         my $inboxdir = "$tmpdir/$v";
30         my $maildir = "$tmpdir/md-$v";
31         my $spamdir = "$tmpdir/spam-$v";
32         my $addr = "test-$v\@example.com";
33         my @cmd = ('-init', '-Lfull', "-$v", $v, $inboxdir,
34                 "http://example.com/$v", $addr);
35         ok(run_script(\@cmd), 'public-inbox init');
36         PublicInbox::Emergency->new($spamdir);
37
38         for my $i (1..15) {
39                 my $msg = <<EOF;
40 From: user\@example.com
41 To: $addr
42 Subject: blah $i
43 X-Mail-Count: $i
44 Message-Id: <a.$i\@b.com>
45 Date: Sat, 05 Jan 2019 04:19:17 +0000
46
47 something
48 EOF
49                 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
50         }
51
52         my $spam = <<EOF;
53 From: spammer\@example.com
54 To: $addr
55 Subject: spam
56 X-Mail-Count: 99
57 Message-Id: <a.99\@b.com>
58 Date: Sat, 05 Jan 2019 04:19:17 +0000
59
60 spam
61 EOF
62         PublicInbox::Emergency->new($maildir)->prepare(\"$spam");
63
64         my $orig = <<EOF;
65 $cfgpfx.address=$addr
66 $cfgpfx.inboxdir=$inboxdir
67 $cfgpfx.watch=maildir:$maildir
68 $cfgpfx.filter=PublicInbox::Filter::RubyLang
69 $cfgpfx.altid=serial:alerts:file=msgmap.sqlite3
70 publicinboxwatch.watchspam=maildir:$spamdir
71 EOF
72         my $cfg = PublicInbox::Config->new(\$orig);
73         my $ibx = $cfg->lookup_name($v);
74         $ibx->{-no_fsync} = 1;
75         ok($ibx, 'found inbox by name');
76
77         my $w = PublicInbox::Watch->new($cfg);
78         for my $i (1..2) {
79                 $w->scan('full');
80         }
81
82         # make sure all serials are searchable:
83         for my $i (1..15) {
84                 my $mset = $ibx->search->mset("alerts:$i");
85                 is($mset->size, 1, "got one result for alerts:$i");
86                 my $msgs = $ibx->search->mset_to_smsg($ibx, $mset);
87                 is($msgs->[0]->{mid}, "a.$i\@b.com", "got expected MID for $i");
88         }
89         is($ibx->search->mset('b:spam')->size, 1, 'got spam message');
90
91         my $nr = unlink <$maildir/new/*>;
92         is(16, $nr);
93         {
94                 PublicInbox::Emergency->new($spamdir)->prepare(\$spam);
95                 my @new = glob("$spamdir/new/*");
96                 my @p = split(m!/+!, $new[0]);
97                 ok(link($new[0], "$spamdir/cur/".$p[-1].":2,S"));
98                 is(unlink($new[0]), 1);
99         }
100         $w->scan('full');
101
102         $cfg = PublicInbox::Config->new(\$orig);
103         $ibx = $cfg->lookup_name($v);
104         $ibx->{-no_fsync} = 1;
105         is($ibx->search->reopen->mset('b:spam')->size, 0, 'spam removed');
106
107         is_deeply([], \@warn, 'no warnings');
108 }
109
110 done_testing();