1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use PublicInbox::TestCommon;
6 require_mods(qw(DBD::SQLite Xapian));
7 use_ok 'PublicInbox::Watch';
8 use_ok 'PublicInbox::Emergency';
9 my ($tmpdir, $for_destroy) = tmpdir();
10 local $ENV{PI_CONFIG} = "$tmpdir/pi_config";
14 if (require_git(2.6, 1)) {
15 use_ok 'PublicInbox::V2Writable';
18 skip 'git 2.6+ needed for V2', 40;
24 local $SIG{__WARN__} = sub { push @warn, @_ };
25 my $inboxdir = "$tmpdir/$v";
26 my $maildir = "$tmpdir/md-$v";
27 my $spamdir = "$tmpdir/spam-$v";
28 my $addr = "test-$v\@example.com";
29 my @cmd = ('-init', '-Lfull', "-$v", $v, $inboxdir,
30 "http://example.com/$v", $addr);
31 ok(run_script(\@cmd), 'public-inbox init');
32 PublicInbox::Emergency->new($spamdir);
36 From: user\@example.com
40 Message-Id: <a.$i\@b.com>
41 Date: Sat, 05 Jan 2019 04:19:17 +0000
45 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
49 From: spammer\@example.com
53 Message-Id: <a.99\@b.com>
54 Date: Sat, 05 Jan 2019 04:19:17 +0000
58 PublicInbox::Emergency->new($maildir)->prepare(\"$spam");
59 my $cfg = cfg_new $tmpdir, <<EOM;
63 watch = maildir:$maildir
64 filter = PublicInbox::Filter::RubyLang
65 altid = serial:alerts:file=msgmap.sqlite3
67 watchspam = maildir:$spamdir
69 my $ibx = $cfg->lookup_name($v);
70 $ibx->{-no_fsync} = 1;
71 ok($ibx, 'found inbox by name');
73 my $w = PublicInbox::Watch->new($cfg);
78 # make sure all serials are searchable:
80 my $mset = $ibx->search->mset("alerts:$i");
81 is($mset->size, 1, "got one result for alerts:$i");
82 my $msgs = $ibx->search->mset_to_smsg($ibx, $mset);
83 is($msgs->[0]->{mid}, "a.$i\@b.com", "got expected MID for $i");
85 is($ibx->search->mset('b:spam')->size, 1, 'got spam message');
87 my $nr = unlink <$maildir/new/*>;
90 PublicInbox::Emergency->new($spamdir)->prepare(\$spam);
91 my @new = glob("$spamdir/new/*");
92 my @p = split(m!/+!, $new[0]);
93 ok(link($new[0], "$spamdir/cur/".$p[-1].":2,S"));
94 is(unlink($new[0]), 1);
98 # ensure orderly destruction to avoid SQLite segfault:
99 PublicInbox::DS->Reset;
101 $cfg = PublicInbox::Config->new($cfg->{-f});
102 $ibx = $cfg->lookup_name($v);
103 $ibx->{-no_fsync} = 1;
104 is($ibx->search->reopen->mset('b:spam')->size, 0, 'spam removed');
106 is_deeply([], \@warn, 'no warnings');