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 ok($im->add($mime), 'added duplicate-but-different message');
47 is(scalar(@warn), 1, 'got one warning');
48 my @mids = $mime->header_obj->header_raw('Message-Id');
49 $new_mid = PublicInbox::MID::mid_clean($mids[0]);
52 my $cfgpfx = "publicinbox.v2test";
54 "$cfgpfx.address" => $ibx->{-primary_address},
55 "$cfgpfx.mainrepo" => $mainrepo,
57 my $config = PublicInbox::Config->new($cfg);
58 my $www = PublicInbox::WWW->new($config);
59 my ($res, $raw, @from_);
60 test_psgi(sub { $www->call(@_) }, sub {
62 $res = $cb->(GET('/v2test/a-mid@b/raw'));
64 like($raw, qr/^hello world$/m, 'got first message');
65 like($raw, qr/^hello world!$/m, 'got second message');
66 @from_ = ($raw =~ m/^From /mg);
67 is(scalar(@from_), 2, 'two From_ lines');
69 $res = $cb->(GET("/v2test/$new_mid/raw"));
71 like($raw, qr/^hello world!$/m, 'second message with new Message-Id');
72 @from_ = ($raw =~ m/^From /mg);
73 is(scalar(@from_), 1, 'only one From_ line');
76 $mime->header_set('Message-Id', 'a-mid@b');
77 $mime->body_set("hello ghosts\n");
78 ok($im->add($mime), 'added 3rd duplicate-but-different message');
79 is(scalar(@warn), 2, 'got another warning');
80 like($warn[0], qr/mismatched/, 'warned about mismatched messages');
81 is($warn[0], $warn[1], 'both warnings are the same');
83 @mids = $mime->header_obj->header_raw('Message-Id');
84 my $third = PublicInbox::MID::mid_clean($mids[0]);
87 test_psgi(sub { $www->call(@_) }, sub {
89 $res = $cb->(GET("/v2test/$third/raw"));
91 like($raw, qr/^hello ghosts$/m, 'got third message');
92 @from_ = ($raw =~ m/^From /mg);
93 is(scalar(@from_), 1, 'one From_ line');
95 $res = $cb->(GET('/v2test/a-mid@b/raw'));
97 like($raw, qr/^hello world$/m, 'got first message');
98 like($raw, qr/^hello world!$/m, 'got second message');
99 like($raw, qr/^hello ghosts$/m, 'got third message');
100 @from_ = ($raw =~ m/^From /mg);
101 is(scalar(@from_), 3, 'three From_ lines');