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 require './t/common.perl';
8 use File::Temp qw/tempdir/;
10 use PublicInbox::Config;
12 use PublicInbox::MID qw(mids);
13 my @mods = qw(DBD::SQLite Search::Xapian HTTP::Request::Common Plack::Test
14 URI::Escape Plack::Builder);
15 foreach my $mod (@mods) {
17 plan skip_all => "$mod missing for psgi_v2_dupes.t" if $@;
20 use_ok 'PublicInbox::V2Writable';
21 my $mainrepo = tempdir('pi-v2_dupes-XXXXXX', TMPDIR => 1, CLEANUP => 1);
23 mainrepo => $mainrepo,
24 name => 'test-v2writable',
26 -primary_address => 'test@example.com',
28 $ibx = PublicInbox::Inbox->new($ibx);
31 my $im = PublicInbox::V2Writable->new($ibx, 1);
34 my $mime = PublicInbox::MIME->create(
36 From => 'a@example.com',
37 To => 'test@example.com',
38 Subject => 'this is a subject',
39 'Message-ID' => '<a-mid@b>',
40 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
42 body => "hello world\n",
44 ok($im->add($mime), 'added one message');
45 $mime->body_set("hello world!\n");
48 local $SIG{__WARN__} = sub { push @warn, @_ };
49 $mime->header_set(Date => 'Fri, 02 Oct 1993 00:01:00 +0000');
50 ok($im->add($mime), 'added duplicate-but-different message');
51 is(scalar(@warn), 1, 'got one warning');
52 my $mids = mids($mime->header_obj);
53 $new_mid = $mids->[1];
56 my $cfgpfx = "publicinbox.v2test";
58 "$cfgpfx.address" => $ibx->{-primary_address},
59 "$cfgpfx.mainrepo" => $mainrepo,
61 my $config = PublicInbox::Config->new($cfg);
62 my $www = PublicInbox::WWW->new($config);
63 my ($res, $raw, @from_);
64 test_psgi(sub { $www->call(@_) }, sub {
66 $res = $cb->(GET('/v2test/a-mid@b/raw'));
68 like($raw, qr/^hello world$/m, 'got first message');
69 like($raw, qr/^hello world!$/m, 'got second message');
70 @from_ = ($raw =~ m/^From /mg);
71 is(scalar(@from_), 2, 'two From_ lines');
73 $res = $cb->(GET("/v2test/$new_mid/raw"));
75 like($raw, qr/^hello world!$/m, 'second message with new Message-Id');
76 @from_ = ($raw =~ m/^From /mg);
77 is(scalar(@from_), 1, 'only one From_ line');
79 # Atom feed should sort by Date: (if Received is missing)
80 $res = $cb->(GET('/v2test/new.atom'));
81 my @bodies = ($res->content =~ />(hello [^<]+)</mg);
82 is_deeply(\@bodies, [ "hello world!\n", "hello world\n" ],
83 'Atom ordering is chronological');
85 # new.html should sort by Date:, too (if Received is missing)
86 $res = $cb->(GET('/v2test/new.html'));
87 @bodies = ($res->content =~ /^(hello [^<]+)$/mg);
88 is_deeply(\@bodies, [ "hello world!\n", "hello world\n" ],
89 'new.html ordering is chronological');
92 $mime->header_set('Message-Id', 'a-mid@b');
93 $mime->body_set("hello ghosts\n");
94 ok($im->add($mime), 'added 3rd duplicate-but-different message');
95 is(scalar(@warn), 2, 'got another warning');
96 like($warn[0], qr/mismatched/, 'warned about mismatched messages');
97 is($warn[0], $warn[1], 'both warnings are the same');
99 $mids = mids($mime->header_obj);
100 my $third = $mids->[-1];
103 test_psgi(sub { $www->call(@_) }, sub {
105 $res = $cb->(GET("/v2test/$third/raw"));
106 $raw = $res->content;
107 like($raw, qr/^hello ghosts$/m, 'got third message');
108 @from_ = ($raw =~ m/^From /mg);
109 is(scalar(@from_), 1, 'one From_ line');
111 $res = $cb->(GET('/v2test/a-mid@b/raw'));
112 $raw = $res->content;
113 like($raw, qr/^hello world$/m, 'got first message');
114 like($raw, qr/^hello world!$/m, 'got second message');
115 like($raw, qr/^hello ghosts$/m, 'got third message');
116 @from_ = ($raw =~ m/^From /mg);
117 is(scalar(@from_), 3, 'three From_ lines');
118 $config->each_inbox(sub { $_[0]->search->reopen });
121 eval { require IO::Uncompress::Gunzip };
122 skip 'IO::Uncompress::Gunzip missing', 4 if $@;
124 $res = $cb->(GET('/v2test/a-mid@b/t.mbox.gz'));
126 my $in = $res->content;
127 my $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
128 like($out, qr/^hello world$/m, 'got first in t.mbox.gz');
129 like($out, qr/^hello world!$/m, 'got second in t.mbox.gz');
130 like($out, qr/^hello ghosts$/m, 'got third in t.mbox.gz');
131 @from_ = ($out =~ m/^From /mg);
132 is(scalar(@from_), 3, 'three From_ lines in t.mbox.gz');
135 $res = $cb->(POST('/v2test/?q=m:a-mid@b&x=m'));
137 $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
138 like($out, qr/^hello world$/m, 'got first in mbox POST');
139 like($out, qr/^hello world!$/m, 'got second in mbox POST');
140 like($out, qr/^hello ghosts$/m, 'got third in mbox POST');
141 @from_ = ($out =~ m/^From /mg);
142 is(scalar(@from_), 3, 'three From_ lines in mbox POST');
144 # all.mbox.gz interface
145 $res = $cb->(GET('/v2test/all.mbox.gz'));
147 $status = IO::Uncompress::Gunzip::gunzip(\$in => \$out);
148 like($out, qr/^hello world$/m, 'got first in all.mbox');
149 like($out, qr/^hello world!$/m, 'got second in all.mbox');
150 like($out, qr/^hello ghosts$/m, 'got third in all.mbox');
151 @from_ = ($out =~ m/^From /mg);
152 is(scalar(@from_), 3, 'three From_ lines in all.mbox');
155 $res = $cb->(GET('/v2test/?q=m:a-mid@b&x=t'));
156 is($res->code, 200, 'success with threaded search');
157 my $raw = $res->content;
158 ok($raw =~ s/\A.*>Results 1-3 of 3<//s, 'got all results');
159 my @over = ($raw =~ m/\d{4}-\d+-\d+\s+\d+:\d+ (.+)$/gm);
160 is_deeply(\@over, [ '<a', '` <a', '` <a' ], 'threaded messages show up');
162 local $SIG{__WARN__} = 'DEFAULT';
163 $res = $cb->(GET('/v2test/a-mid@b/'));
164 $raw = $res->content;
165 like($raw, qr/^hello world$/m, 'got first message');
166 like($raw, qr/^hello world!$/m, 'got second message');
167 like($raw, qr/^hello ghosts$/m, 'got third message');
168 @from_ = ($raw =~ m/>From: /mg);
169 is(scalar(@from_), 3, 'three From: lines');
170 foreach my $mid ('a-mid@b', $new_mid, $third) {
171 like($raw, qr/<\Q$mid\E>/s, "Message-ID $mid shown");
173 like($raw, qr/\b3\+ messages\b/, 'thread overview shown');
175 my $exp = [ qw(<a-mid@b> <reuse@mid>) ];
176 $mime->header_set('Message-Id', @$exp);
177 $mime->header_set('Subject', '4th dupe');
178 local $SIG{__WARN__} = sub {};
179 ok($im->add($mime), 'added one message');
181 my @h = $mime->header('Message-ID');
182 is_deeply($exp, \@h, 'reused existing Message-ID');
184 $config->each_inbox(sub { $_[0]->search->reopen });
186 $res = $cb->(GET('/v2test/new.atom'));
187 my @ids = ($res->content =~ m!<id>urn:uuid:([^<]+)</id>!sg);
190 is_deeply([qw(1 1 1 1)], [values %ids], 'feed ids unique');
192 $res = $cb->(GET('/v2test/reuse@mid/T/'));
193 $raw = $res->content;
194 like($raw, qr/\b4\+ messages\b/, 'thread overview shown with /T/');
195 @over = ($raw =~ m/^\d{4}-\d+-\d+\s+\d+:\d+ (.+)$/gm);
196 is_deeply(\@over, [ '<a', '` <a', '` <a', '` <a' ],
197 'duplicate messages share the same root');
199 $res = $cb->(GET('/v2test/reuse@mid/t/'));
200 $raw = $res->content;
201 like($raw, qr/\b4\+ messages\b/, 'thread overview shown with /t/');
203 $res = $cb->(GET('/v2test/0/info/refs'));
204 is($res->code, 200, 'got info refs for dumb clones');
205 $res = $cb->(GET('/v2test/info/refs'));
206 is($res->code, 404, 'unpartitioned git URL fails');
208 # ensure conflicted attachments can be resolved
209 foreach my $body (qw(old new)) {
211 PublicInbox::MIME->create(
212 attributes => { content_type => 'text/plain' },
215 PublicInbox::MIME->create(
217 filename => 'attach.txt',
218 content_type => 'text/plain',
223 $mime = PublicInbox::MIME->create(
225 header_str => [ From => 'root@z',
226 'Message-ID' => '<a@dup>',
229 ok($im->add($mime), "added attachment $body");
232 $config->each_inbox(sub { $_[0]->search->reopen });
233 $res = $cb->(GET('/v2test/a@dup/'));
234 my @links = ($res->content =~ m!"\.\./([^/]+/2-attach\.txt)\"!g);
235 is(scalar(@links), 2, 'both attachment links exist');
236 isnt($links[0], $links[1], 'attachment links are different');
238 my $old = $cb->(GET('/v2test/' . $links[0]));
239 my $new = $cb->(GET('/v2test/' . $links[1]));
240 is($old->content, 'old', 'got expected old content');
241 is($new->content, 'new', 'got expected new content');