]> Sergey Matveev's repositories - public-inbox.git/blob - t/psgi_search.t
search: use git approxidate in WWW and "lei q --stdin"
[public-inbox.git] / t / psgi_search.t
1 # Copyright (C) 2017-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use IO::Uncompress::Gunzip qw(gunzip);
7 use PublicInbox::Eml;
8 use PublicInbox::Config;
9 use PublicInbox::Inbox;
10 use PublicInbox::InboxWritable;
11 use bytes (); # only for bytes::length
12 use PublicInbox::TestCommon;
13 my @mods = qw(DBD::SQLite Search::Xapian HTTP::Request::Common Plack::Test
14                 URI::Escape Plack::Builder);
15 require_mods(@mods);
16 use_ok($_) for (qw(HTTP::Request::Common Plack::Test));
17 use_ok 'PublicInbox::WWW';
18 use_ok 'PublicInbox::SearchIdx';
19 my ($tmpdir, $for_destroy) = tmpdir();
20
21 my $ibx = PublicInbox::Inbox->new({
22         inboxdir => $tmpdir,
23         address => 'git@vger.kernel.org',
24         name => 'test',
25 });
26 $ibx = PublicInbox::InboxWritable->new($ibx);
27 $ibx->init_inbox(1);
28 my $im = $ibx->importer(0);
29 my $digits = '10010260936330';
30 my $ua = 'Pine.LNX.4.10';
31 my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
32
33 # n.b. these headers are not properly RFC2047-encoded
34 my $mime = PublicInbox::Eml->new(<<EOF);
35 Subject: test Ævar
36 Message-ID: <$mid>
37 From: Ævar Arnfjörð Bjarmason <avarab\@example>
38 To: git\@vger.kernel.org
39
40 EOF
41 $im->add($mime);
42
43 $im->add(PublicInbox::Eml->new(<<""));
44 Message-ID: <reply\@asdf>
45 From: replier <r\@example.com>
46 In-Reply-To: <$mid>
47 Subject: mismatch
48
49 $mime = PublicInbox::Eml->new(<<'EOF');
50 Subject:
51 Message-ID: <blank-subject@example.com>
52 From: blank subject <blank-subject@example.com>
53 To: git@vger.kernel.org
54
55 EOF
56 $im->add($mime);
57
58 $mime = PublicInbox::Eml->new(<<'EOF');
59 Message-ID: <no-subject-at-all@example.com>
60 From: no subject at all <no-subject-at-all@example.com>
61 To: git@vger.kernel.org
62
63 EOF
64 $im->add($mime);
65
66 $im->done;
67 PublicInbox::SearchIdx->new($ibx, 1)->index_sync;
68
69 my $cfgpfx = "publicinbox.test";
70 my $cfg = PublicInbox::Config->new(\<<EOF);
71 $cfgpfx.address=git\@vger.kernel.org
72 $cfgpfx.inboxdir=$tmpdir
73 EOF
74 my $www = PublicInbox::WWW->new($cfg);
75 test_psgi(sub { $www->call(@_) }, sub {
76         my ($cb) = @_;
77         my ($html, $res);
78         my $approxidate = '1.hour.from.now';
79         for my $req ('/test/?q=%C3%86var', '/test/?q=%25C3%2586var') {
80                 $res = $cb->(GET($req."+d:..$approxidate"));
81                 $html = $res->content;
82                 like($html, qr/<title>&#198;var d:\.\.\Q$approxidate\E/,
83                         'HTML escaped in title, "d:..$APPROXIDATE" preserved');
84                 my @res = ($html =~ m/\?q=(.+var)\+d:\.\.\Q$approxidate\E/g);
85                 ok(scalar(@res), 'saw query strings');
86                 my %uniq = map { $_ => 1 } @res;
87                 is(1, scalar keys %uniq, 'all query values identical in HTML');
88                 is('%C3%86var', (keys %uniq)[0], 'matches original query');
89                 ok(index($html, 'by &#198;var Arnfj&#246;r&#240; Bjarmason')
90                         >= 0, "displayed Ævar's name properly in HTML");
91                 like($html, qr/download mbox\.gz: .*?"full threads"/s,
92                         '"full threads" download option shown');
93         }
94         like($html, qr/Initial query\b.*?returned no.results, used:.*instead/s,
95                 'noted retry on double-escaped query {-uxs_retried}');
96
97         my $warn = [];
98         local $SIG{__WARN__} = sub { push @$warn, @_ };
99         $res = $cb->(GET('/test/?q=s:test&l=5e'));
100         is($res->code, 200, 'successful search result');
101         is_deeply([], $warn, 'no warnings from non-numeric comparison');
102
103         $res = $cb->(POST('/test/?q=s:bogus&x=m'));
104         is($res->code, 404, 'failed search result gives 404');
105         is_deeply([], $warn, 'no warnings');
106
107         my $mid_re = qr/\Q$mid\E/o;
108         while (length($digits) > 8) {
109                 $res = $cb->(GET("/test/$ua.$digits/"));
110                 is($res->code, 300, 'partial match found while truncated');
111                 like($res->content, qr/\b1 partial match found\b/);
112                 like($res->content, $mid_re, 'found mid in response');
113                 chop($digits);
114         }
115
116         $res = $cb->(GET('/test/'));
117         $html = $res->content;
118         like($html, qr/\bhref="no-subject-at-all[^>]+>\(no subject\)</,
119                 'subject-less message linked from "/$INBOX/"');
120         like($html, qr/\bhref="blank-subject[^>]+>\(no subject\)</,
121                 'blank subject message linked from "/$INBOX/"');
122         like($html, qr/test &#198;var/,
123                 "displayed Ævar's name properly in topic view");
124
125         $res = $cb->(GET('/test/?q=tc:git'));
126         like($html, qr/\bhref="no-subject-at-all[^>]+>\(no subject\)</,
127                 'subject-less message linked from "/$INBOX/?q=..."');
128         like($html, qr/\bhref="blank-subject[^>]+>\(no subject\)</,
129                 'blank subject message linked from "/$INBOX/?q=..."');
130         $res = $cb->(GET('/test/no-subject-at-all@example.com/raw'));
131         like($res->header('Content-Disposition'),
132                 qr/filename=no-subject\.txt/);
133         $res = $cb->(GET('/test/no-subject-at-all@example.com/t.mbox.gz'));
134         like($res->header('Content-Disposition'),
135                 qr/filename=no-subject\.mbox\.gz/);
136
137         # "full threads" mbox.gz download
138         $res = $cb->(POST('/test/?q=s:test+d:..1.hour.from.now&x=m&t'));
139         is($res->code, 200, 'successful mbox download with threads');
140         gunzip(\($res->content) => \(my $before));
141         is_deeply([ "Message-ID: <$mid>\n", "Message-ID: <reply\@asdf>\n" ],
142                 [ grep(/^Message-ID:/m, split(/^/m, $before)) ],
143                 'got full thread');
144
145         # clobber has_threadid to emulate old versions:
146         {
147                 my $sidx = PublicInbox::SearchIdx->new($ibx, 0);
148                 my $xdb = $sidx->idx_acquire;
149                 $xdb->set_metadata('has_threadid', '0');
150                 $sidx->idx_release;
151         }
152         $cfg->each_inbox(sub { delete $_[0]->{search} });
153         $res = $cb->(GET('/test/?q=s:test'));
154         is($res->code, 200, 'successful search w/o has_threadid');
155         unlike($html, qr/download mbox\.gz: .*?"full threads"/s,
156                 '"full threads" download option not shown w/o has_threadid');
157
158         # in case somebody uses curl to bypass <form>
159         $res = $cb->(POST("/test/?q=s:test+d:..$approxidate&x=m&t"));
160         is($res->code, 200, 'successful mbox download w/ threads');
161         gunzip(\($res->content) => \(my $after));
162         isnt($before, $after);
163 });
164
165 done_testing();