+ my @new = <$faildir/new/*>;
+ is(scalar @new, 1, "PI_EMERGENCY is written to");
+ }
+
+ $fail_bad_header->($good_rev, "bad recipient", <<"");
+From: Me <me\@example.com>
+To: You <you\@example.com>
+Message-Id: <bad-recipient\@example.com>
+Subject: hihi
+Date: Thu, 01 Jan 1970 00:00:00 +0000
+
+ my $fail = $fail_bad_header->($good_rev, "duplicate Message-ID", <<"");
+From: Me <me\@example.com>
+To: You <you\@example.com>
+Cc: $addr
+Message-ID: <blah\@example.com>
+Subject: hihi
+Date: Thu, 01 Jan 1970 00:00:00 +0000
+
+ like($fail->[2], qr/CONFLICT/, "duplicate Message-ID message");
+
+ $fail_bad_header->($good_rev, "missing From:", <<"");
+To: $addr
+Message-ID: <missing-from\@example.com>
+Subject: hihi
+Date: Thu, 01 Jan 1970 00:00:00 +0000
+
+ $fail_bad_header->($good_rev, "short subject:", <<"");
+To: $addr
+From: cat\@example.com
+Message-ID: <short-subject\@example.com>
+Subject: a
+Date: Thu, 01 Jan 1970 00:00:00 +0000
+
+ $fail_bad_header->($good_rev, "no date", <<"");
+To: $addr
+From: u\@example.com
+Message-ID: <no-date\@example.com>
+Subject: hihi
+
+ $fail_bad_header->($good_rev, "bad date", <<"");
+To: $addr
+From: u\@example.com
+Message-ID: <bad-date\@example.com>
+Subject: hihi
+Date: deadbeef
+
+}
+
+# spam training
+{
+ local $ENV{PI_EMERGENCY} = $faildir;
+ local $ENV{HOME} = $home;
+ local $ENV{ORIGINAL_RECIPIENT} = $addr;
+ local $ENV{PATH} = $main_path;
+ my $mid = 'spam-train@example.com';
+ my $in = <<EOF;
+From: Spammer <spammer\@example.com>
+To: You <you\@example.com>
+Cc: $addr
+Message-ID: <$mid>
+Subject: this message will be trained as spam
+Date: Thu, 01 Jan 1970 00:00:00 +0000
+
+EOF
+ {
+ # deliver the spam message, first
+ ok(run_script(['-mda'], undef, { 0 => \$in }));
+ my $path = mid2path($mid);
+ my $msg = $git->cat_file("HEAD:$path");
+ like($$msg, qr/\Q$mid\E/, "message delivered");
+
+ # now train it
+ local $ENV{GIT_AUTHOR_EMAIL} = 'trainer@example.com';
+ local $ENV{GIT_COMMITTER_EMAIL} = 'trainer@example.com';
+ local $ENV{GIT_COMMITTER_NAME};
+ delete $ENV{GIT_COMMITTER_NAME};
+ ok(run_script(['-learn', 'spam'], undef, { 0 => $msg }),
+ "no failure from learning spam");
+ ok(run_script(['-learn', 'spam'], undef, { 0 => $msg }),
+ "no failure from learning spam idempotently");
+ }
+}
+
+# train ham message
+{
+ local $ENV{PI_EMERGENCY} = $faildir;
+ local $ENV{HOME} = $home;
+ local $ENV{ORIGINAL_RECIPIENT} = $addr;
+ local $ENV{PATH} = $main_path;
+ my $mid = 'ham-train@example.com';
+ my $in = <<EOF;
+From: False-positive <hammer\@example.com>
+To: You <you\@example.com>
+Cc: $addr
+Message-ID: <$mid>
+Subject: this message will be trained as spam
+Date: Thu, 01 Jan 1970 00:00:00 +0000
+
+EOF
+ # now train it
+ # these should be overridden
+ local $ENV{GIT_AUTHOR_EMAIL} = 'trainer@example.com';
+ local $ENV{GIT_COMMITTER_EMAIL} = 'trainer@example.com';
+
+ ok(run_script(['-learn', 'ham'], undef, { 0 => \$in }),
+ "learned ham without failure");
+ my $path = mid2path($mid);
+ my $msg = $git->cat_file("HEAD:$path");
+ like($$msg, qr/\Q$mid\E/, "ham message delivered");
+ ok(run_script(['-learn', 'ham'], undef, { 0 => \$in }),
+ "learned ham idempotently ");
+
+ # ensure trained email is filtered, too
+ my $mime = eml_load 't/mda-mime.eml';
+ ($mid) = ($mime->header_raw('message-id') =~ /<([^>]+)>/);
+ {
+ $in = $mime->as_string;
+ ok(run_script(['-learn', 'ham'], undef, { 0 => \$in }),
+ "learned ham without failure");
+ my $path = mid2path($mid);
+ $msg = $git->cat_file("HEAD:$path");
+ like($$msg, qr/<\Q$mid\E>/, "ham message delivered");
+ unlike($$msg, qr/<html>/i, '<html> filtered');