+$res = $mic = $mics = undef;
+
+test_lei(sub {
+ my ($ro_home, $cfg_path) = setup_public_inboxes;
+ my $cfg = PublicInbox::Config->new($cfg_path);
+ $cfg->each_inbox(sub {
+ my ($ibx) = @_;
+ lei_ok qw(add-external -q), $ibx->{inboxdir} or BAIL_OUT;
+ });
+
+ # cred_link[0] may be on a different (hopefully encrypted) FS,
+ # we only symlink to it here, so we don't copy any sensitive data
+ # into the temporary directory
+ if (@cred_link && !symlink($cred_link[0], $ENV{HOME}.$cred_link[1])) {
+ diag "symlink @cred_link: $! (non-fatal)";
+ $cred_set = undef;
+ }
+ $set_cred_helper->("$ENV{HOME}/.gitconfig", $cred_set) if $cred_set;
+
+ lei_ok qw(q f:qp@example.com -o), $$folder_uri;
+ $nwr->imap_each($folder_uri, $imap_slurp_all, my $res = []);
+ is(scalar(@$res), 1, 'got one deduped result') or diag explain($res);
+ is_deeply($res->[0]->[1], $plack_qp_eml,
+ 'lei q wrote expected result');
+
+ lei_ok qw(q f:matz -a -o), $$folder_uri;
+ $nwr->imap_each($folder_uri, $imap_slurp_all, my $aug = []);
+ is(scalar(@$aug), 2, '2 results after augment') or diag explain($aug);
+ my $exp = $res->[0]->[1]->as_string;
+ is(scalar(grep { $_->[1]->as_string eq $exp } @$aug), 1,
+ 'original remains after augment');
+ $exp = eml_load('t/iso-2202-jp.eml')->as_string;
+ is(scalar(grep { $_->[1]->as_string eq $exp } @$aug), 1,
+ 'new result shown after augment');
+
+ lei_ok qw(q s:thisbetternotgiveanyresult -o), $folder_uri->as_string;
+ $nwr->imap_each($folder_uri, $imap_slurp_all, my $empty = []);
+ is(scalar(@$empty), 0, 'no results w/o augment');
+
+ my $f = 't/utf8.eml'; # <testmessage@example.com>
+ $exp = eml_load($f);
+ lei_ok qw(convert -F eml -o), $$folder_uri, $f;
+ my (@uid, @res);
+ $nwr->imap_each($folder_uri, sub {
+ my ($u, $uid, $kw, $eml) = @_;
+ push @uid, $uid;
+ push @res, [ $kw, $eml ];
+ });
+ is_deeply(\@res, [ [ [], $exp ] ], 'converted to IMAP destination');
+ is(scalar(@uid), 1, 'got one UID back');
+ lei_ok qw(q -o /dev/stdout m:testmessage@example.com --no-external);
+ is_deeply(json_utf8->decode($lei_out), [undef],
+ 'no results before import');
+
+ lei_ok qw(import -F eml), $f, \'import local copy w/o keywords';
+
+ $nwr->imap_set_kw($folder_uri, $uid[0], [ 'seen' ])->expunge
+ or BAIL_OUT "expunge $@";
+ @res = ();
+ $nwr->imap_each($folder_uri, $imap_slurp_all, \@res);
+ is_deeply(\@res, [ [ ['seen'], $exp ] ], 'seen flag set') or
+ diag explain(\@res);
+
+ lei_ok qw(q s:thisbetternotgiveanyresult -o), $folder_uri->as_string,
+ \'clobber folder but import flag';
+ $nwr->imap_each($folder_uri, $imap_slurp_all, $empty = []);
+ is_deeply($empty, [], 'clobbered folder');
+ lei_ok qw(q -o /dev/stdout m:testmessage@example.com --no-external);
+ $res = json_utf8->decode($lei_out)->[0];
+ is_deeply([@$res{qw(m kw)}], ['testmessage@example.com', ['seen']],
+ 'kw set');
+
+ $mic = $nwr->mic_for_folder($folder_uri);
+ for my $kw (qw(Deleted Seen Answered Draft)) {
+ my $buf = <<EOM;
+From: x\@example.com
+Message-ID: <$kw\@test.example.com>
+
+EOM
+ $mic->append_string($folder_uri->mailbox, $buf, "\\$kw")
+ or BAIL_OUT "append $kw $@";
+ }
+ # $mic->expunge or BAIL_OUT "expunge: $@";
+ $mic->disconnect;
+
+ my $inboxdir = "$ENV{HOME}/wtest";
+ my @cmd = (qw(-init -Lbasic wtest), $inboxdir,
+ qw(https://example.com/wtest wtest@example.com));
+ run_script(\@cmd) or BAIL_OUT "init wtest";
+ xsys(qw(git config), "--file=$ENV{HOME}/.public-inbox/config",
+ 'publicinbox.wtest.watch',
+ $$folder_uri) == 0 or BAIL_OUT "git config $?";
+ my $watcherr = "$ENV{HOME}/watch.err";
+ open my $err_wr, '>>', $watcherr or BAIL_OUT $!;
+ my $pub_cfg = PublicInbox::Config->new;
+ PublicInbox::DS->Reset;
+ my $ii = PublicInbox::InboxIdle->new($pub_cfg);
+ my $cb = sub { PublicInbox::DS->SetPostLoopCallback(sub {}) };
+ my $obj = bless \$cb, 'PublicInbox::TestCommon::InboxWakeup';
+ $pub_cfg->each_inbox(sub { $_[0]->subscribe_unlock('ident', $obj) });
+ my $w = start_script(['-watch'], undef, { 2 => $err_wr });
+ diag 'waiting for initial fetch...';
+ PublicInbox::DS->EventLoop;
+ my $ibx = $pub_cfg->lookup_name('wtest');
+ my $mm = $ibx->mm;
+ ok(defined($mm->num_for('Seen@test.example.com')),
+ '-watch takes seen message');
+ ok(defined($mm->num_for('Answered@test.example.com')),
+ '-watch takes answered message');
+ ok(!defined($mm->num_for('Deleted@test.example.com')),
+ '-watch ignored \\Deleted');
+ ok(!defined($mm->num_for('Draft@test.example.com')),
+ '-watch ignored \\Draft');
+});