]> Sergey Matveev's repositories - public-inbox.git/blobdiff - xt/net_writer-imap.t
msg_part_text: discover text in application/octet-stream
[public-inbox.git] / xt / net_writer-imap.t
index da43592621f2e08ecfcfa574e2c016e7df67617c..11a10e742d68abe13fd1f62541457aa15b9004dc 100644 (file)
@@ -7,6 +7,8 @@ use POSIX qw(strftime);
 use PublicInbox::OnDestroy;
 use PublicInbox::URIimap;
 use PublicInbox::Config;
+use PublicInbox::DS;
+use PublicInbox::InboxIdle;
 use Fcntl qw(O_EXCL O_WRONLY O_CREAT);
 my $imap_url = $ENV{TEST_IMAP_WRITE_URL} or
        plan skip_all => 'TEST_IMAP_WRITE_URL unset';
@@ -91,7 +93,7 @@ my $smsg = bless { kw => [ 'seen' ] }, 'PublicInbox::Smsg';
 $imap_append->($mic, $folder, undef, $smsg, eml_load('t/plack-qp.eml'));
 $nwr->{quiet} = 1;
 my $imap_slurp_all = sub {
-       my ($u, $uid, $kw, $eml, $res) = @_;
+       my ($url, $uid, $kw, $eml, $res) = @_;
        push @$res, [ $kw, $eml ];
 };
 $nwr->imap_each($folder_uri, $imap_slurp_all, my $res = []);
@@ -138,10 +140,80 @@ test_lei(sub {
        $nwr->imap_each($folder_uri, $imap_slurp_all, my $empty = []);
        is(scalar(@$empty), 0, 'no results w/o augment');
 
-       lei_ok qw(convert -F eml t/msg_iter-order.eml -o), $$folder_uri;
+       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, [ [ [], eml_load('t/msg_iter-order.eml') ] ],
-               'converted to IMAP destination');
+       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');
 });
 
 undef $cleanup; # remove temporary folder