+ is(unlink(glob("$spamdir/cur/*")), 1, 'unlinked trained spam');
+}
+
+{
+ my $fail_bin = getcwd()."/t/fail-bin";
+ ok(-x "$fail_bin/spamc", "mock spamc exists");
+ my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc ham mock
+ local $ENV{PATH} = $fail_path;
+ PublicInbox::Emergency->new($maildir)->prepare(\$msg);
+ $config->{'publicinboxwatch.spamcheck'} = 'spamc';
+ {
+ local $SIG{__WARN__} = sub {}; # quiet spam check warning
+ PublicInbox::Watch->new($config)->scan('full');
+ }
+ @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
+ is(scalar @list, 0, 'tree has no files spamc checked');
+ is(unlink(glob("$maildir/new/*")), 1);
+}
+
+{
+ my $main_bin = getcwd()."/t/main-bin";
+ ok(-x "$main_bin/spamc", "mock spamc exists");
+ my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
+ local $ENV{PATH} = $main_path;
+ PublicInbox::Emergency->new($maildir)->prepare(\$msg);
+ $config->{'publicinboxwatch.spamcheck'} = 'spamc';
+ @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
+ PublicInbox::Watch->new($config)->scan('full');
+ @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
+ is(scalar @list, 1, 'tree has one file after spamc checked');
+
+ # XXX: workaround some weird caching/memoization in cat-file,
+ # shouldn't be an issue in real-world use, though...
+ $git = PublicInbox::Git->new($git_dir);
+
+ my $mref = $git->cat_file('refs/heads/master:'.$list[0]);
+ like($$mref, qr/something\n\z/s, 'message scrubbed on import');
+}
+
+# end-to-end test which actually uses inotify/kevent
+{
+ my $env = { PI_CONFIG => $cfg_path };
+ $git->cleanup;
+
+ # n.b. --no-scan is only intended for testing atm
+ my $wm = start_script([qw(-watch --no-scan)], $env);
+ my $eml = eml_load('t/data/0001.patch');
+ $eml->header_set('Cc', $addr);
+ my $em = PublicInbox::Emergency->new($maildir);
+ $em->prepare(\($eml->as_string));
+
+ use_ok 'PublicInbox::InboxIdle';
+ use_ok 'PublicInbox::DS';
+ my $delivered = 0;
+ my $cb = sub {
+ my ($ibx) = @_;
+ diag "message delivered to `$ibx->{name}'";
+ $delivered++;
+ };
+ PublicInbox::DS->Reset;
+ my $ii = PublicInbox::InboxIdle->new($config);
+ my $obj = bless \$cb, 'PublicInbox::TestCommon::InboxWakeup';
+ $config->each_inbox(sub { $_[0]->subscribe_unlock('ident', $obj) });
+ PublicInbox::DS->SetPostLoopCallback(sub { $delivered == 0 });
+
+ # wait for -watch to setup inotify watches
+ my $sleep = 1;
+ if (eval { require Linux::Inotify2 } && -d "/proc/$wm->{pid}/fd") {
+ my $end = time + 2;
+ my (@ino, @ino_info);
+ do {
+ @ino = grep {
+ (readlink($_)//'') =~ /\binotify\b/
+ } glob("/proc/$wm->{pid}/fd/*");
+ } until (@ino || time > $end || !tick);
+ if (scalar(@ino) == 1) {
+ my $ino_fd = (split('/', $ino[0]))[-1];
+ my $ino_fdinfo = "/proc/$wm->{pid}/fdinfo/$ino_fd";
+ while (time < $end && open(my $fh, '<', $ino_fdinfo)) {
+ @ino_info = grep(/^inotify wd:/, <$fh>);
+ last if @ino_info >= 3;
+ tick;
+ }
+ $sleep = undef if @ino_info >= 3;
+ }
+ }
+ if ($sleep) {
+ diag "waiting ${sleep}s for -watch to start up";
+ sleep $sleep;
+ }
+
+ $em->commit; # wake -watch up
+ diag 'waiting for -watch to import new message';
+ PublicInbox::DS->EventLoop;
+ $wm->kill;
+ $wm->join;
+ $ii->close;
+ PublicInbox::DS->Reset;
+ my $head = $git->qx(qw(cat-file commit HEAD));
+ my $subj = $eml->header('Subject');
+ like($head, qr/^\Q$subj\E/sm, 'new commit made');
+}
+
+sub is_maildir {
+ my ($dir) = @_;
+ PublicInbox::Watch::is_maildir($dir);