1 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use File::Temp qw/tempdir/;
8 use PublicInbox::Config;
10 my @mods = qw(DBD::SQLite Search::Xapian HTTP::Request::Common Plack::Test
11 URI::Escape Plack::Builder);
12 foreach my $mod (@mods) {
14 plan skip_all => "$mod missing for psgi_v2_dupes.t" if $@;
17 use_ok 'PublicInbox::V2Writable';
18 my $mainrepo = tempdir('pi-v2_dupes-XXXXXX', TMPDIR => 1, CLEANUP => 1);
20 mainrepo => $mainrepo,
21 name => 'test-v2writable',
23 -primary_address => 'test@example.com',
25 $ibx = PublicInbox::Inbox->new($ibx);
28 my $im = PublicInbox::V2Writable->new($ibx, 1);
31 my $mime = PublicInbox::MIME->create(
33 From => 'a@example.com',
34 To => 'test@example.com',
35 Subject => 'this is a subject',
36 'Message-ID' => '<a-mid@b>',
37 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
39 body => "hello world\n",
41 ok($im->add($mime), 'added one message');
42 $mime->body_set("hello world!\n");
45 local $SIG{__WARN__} = sub { push @warn, @_ };
46 $mime->header_set(Date => 'Fri, 02 Oct 1993 00:01:00 +0000');
47 ok($im->add($mime), 'added duplicate-but-different message');
48 is(scalar(@warn), 1, 'got one warning');
49 my @mids = $mime->header_obj->header_raw('Message-Id');
50 $new_mid = PublicInbox::MID::mid_clean($mids[0]);
53 my $cfgpfx = "publicinbox.v2test";
55 "$cfgpfx.address" => $ibx->{-primary_address},
56 "$cfgpfx.mainrepo" => $mainrepo,
58 my $config = PublicInbox::Config->new($cfg);
59 my $www = PublicInbox::WWW->new($config);
60 my ($res, $raw, @from_);
61 test_psgi(sub { $www->call(@_) }, sub {
63 $res = $cb->(GET('/v2test/a-mid@b/raw'));
65 like($raw, qr/^hello world$/m, 'got first message');
66 like($raw, qr/^hello world!$/m, 'got second message');
67 @from_ = ($raw =~ m/^From /mg);
68 is(scalar(@from_), 2, 'two From_ lines');
70 $res = $cb->(GET("/v2test/$new_mid/raw"));
72 like($raw, qr/^hello world!$/m, 'second message with new Message-Id');
73 @from_ = ($raw =~ m/^From /mg);
74 is(scalar(@from_), 1, 'only one From_ line');
76 # Atom feed should sort by Date: (if Received is missing)
77 $res = $cb->(GET('/v2test/new.atom'));
78 my @bodies = ($res->content =~ />(hello [^<]+)</mg);
79 is_deeply(\@bodies, [ "hello world!\n", "hello world\n" ],
80 'Atom ordering is chronological');
82 # new.html should sort by Date:, too (if Received is missing)
83 $res = $cb->(GET('/v2test/new.html'));
84 @bodies = ($res->content =~ /^(hello [^<]+)$/mg);
85 is_deeply(\@bodies, [ "hello world!\n", "hello world\n" ],
86 'new.html ordering is chronological');
89 $mime->header_set('Message-Id', 'a-mid@b');
90 $mime->body_set("hello ghosts\n");
91 ok($im->add($mime), 'added 3rd duplicate-but-different message');
92 is(scalar(@warn), 2, 'got another warning');
93 like($warn[0], qr/mismatched/, 'warned about mismatched messages');
94 is($warn[0], $warn[1], 'both warnings are the same');
96 @mids = $mime->header_obj->header_raw('Message-Id');
97 my $third = PublicInbox::MID::mid_clean($mids[0]);
100 test_psgi(sub { $www->call(@_) }, sub {
102 $res = $cb->(GET("/v2test/$third/raw"));
103 $raw = $res->content;
104 like($raw, qr/^hello ghosts$/m, 'got third message');
105 @from_ = ($raw =~ m/^From /mg);
106 is(scalar(@from_), 1, 'one From_ line');
108 $res = $cb->(GET('/v2test/a-mid@b/raw'));
109 $raw = $res->content;
110 like($raw, qr/^hello world$/m, 'got first message');
111 like($raw, qr/^hello world!$/m, 'got second message');
112 like($raw, qr/^hello ghosts$/m, 'got third message');
113 @from_ = ($raw =~ m/^From /mg);
114 is(scalar(@from_), 3, 'three From_ lines');
117 eval { require IO::Uncompress::Gunzip };
118 skip 'IO::Uncompress::Gunzip missing', 4 if $@;
120 $res = $cb->(GET('/v2test/a-mid@b/t.mbox.gz'));
122 my $in = $res->content;
123 my $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
124 like($out, qr/^hello world$/m, 'got first in t.mbox.gz');
125 like($out, qr/^hello world!$/m, 'got second in t.mbox.gz');
126 like($out, qr/^hello ghosts$/m, 'got third in t.mbox.gz');
127 @from_ = ($raw =~ m/^From /mg);
128 is(scalar(@from_), 3, 'three From_ lines in t.mbox.gz');