]> Sergey Matveev's repositories - public-inbox.git/blob - t/search.t
www: drop --subject from "git send-email" instructions
[public-inbox.git] / t / search.t
1 # Copyright (C) 2015-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 PublicInbox::TestCommon;
7 require_mods(qw(DBD::SQLite Search::Xapian));
8 require PublicInbox::SearchIdx;
9 require PublicInbox::Inbox;
10 require PublicInbox::InboxWritable;
11 use PublicInbox::Eml;
12 use POSIX qw(strftime);
13 my ($tmpdir, $for_destroy) = tmpdir();
14 my $git_dir = "$tmpdir/a.git";
15 my $ibx = PublicInbox::Inbox->new({ inboxdir => $git_dir });
16 my ($root_id, $last_id);
17
18 is(0, xsys(qw(git init --shared -q --bare), $git_dir), "git init (main)")
19         or BAIL_OUT("`git init --shared' failed, weird FS or seccomp?");
20 eval { PublicInbox::Search->new($ibx)->xdb };
21 ok($@, "exception raised on non-existent DB");
22
23 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
24 $ibx->with_umask(sub {
25         $rw->idx_acquire;
26         $rw->idx_release;
27 });
28 $rw = undef;
29 my $rw_commit = sub {
30         $rw->commit_txn_lazy if $rw;
31         $rw = PublicInbox::SearchIdx->new($ibx, 1);
32         $rw->{qp_flags} = 0; # quiet a warning
33         $rw->begin_txn_lazy;
34         $ibx->search->reopen;
35 };
36
37 sub oct_is ($$$) {
38         my ($got, $exp, $msg) = @_;
39         is(sprintf('0%03o', $got), sprintf('0%03o', $exp), $msg);
40 }
41
42 {
43         # git repository perms
44         oct_is($ibx->_git_config_perm(),
45                 &PublicInbox::InboxWritable::PERM_GROUP,
46                 'undefined permission is group');
47         my @t = (
48                 [ '0644', 0022, '644 => umask(0022)' ],
49                 [ '0600', 0077, '600 => umask(0077)' ],
50                 [ '0640', 0027, '640 => umask(0027)' ],
51                 [ 'group', 0007, 'group => umask(0007)' ],
52                 [ 'everybody', 0002, 'everybody => umask(0002)' ],
53                 [ 'umask', umask, 'umask => existing umask' ],
54         );
55         for (@t) {
56                 my ($perm, $exp, $msg) = @$_;
57                 my $got = PublicInbox::InboxWritable::_umask_for(
58                         PublicInbox::InboxWritable->_git_config_perm($perm));
59                 oct_is($got, $exp, $msg);
60         }
61 }
62
63 {
64         my $crlf_adjust = \&PublicInbox::Smsg::crlf_adjust;
65         is($crlf_adjust->("hi\r\nworld\r\n"), 0, 'no adjustment needed');
66         is($crlf_adjust->("hi\nworld\n"), 2, 'LF-only counts two CR');
67         is($crlf_adjust->("hi\r\nworld\n"), 1, 'CRLF/LF-mix 1 counts 1 CR');
68         is($crlf_adjust->("hi\nworld\r\n"), 1, 'CRLF/LF-mix 2 counts 1 CR');
69 }
70
71 $ibx->with_umask(sub {
72         my $root = PublicInbox::Eml->new(<<'EOF');
73 Date: Fri, 02 Oct 1993 00:00:00 +0000
74 Subject: Hello world
75 Message-ID: <root@s>
76 From: John Smith <js@example.com>
77 To: list@example.com
78 List-Id: I'm not mad <i.m.just.bored>
79
80 \m/
81 EOF
82         my $last = PublicInbox::Eml->new(<<'EOF');
83 Date: Sat, 02 Oct 2010 00:00:00 +0000
84 Subject: Re: Hello world
85 In-Reply-To: <root@s>
86 Message-ID: <last@s>
87 From: John Smith <js@example.com>
88 To: list@example.com
89 Cc: foo@example.com
90 List-Id: there's nothing <left.for.me.to.do>
91
92 goodbye forever :<
93 EOF
94         my $rv;
95         $rw_commit->();
96         $root_id = $rw->add_message($root);
97         is($root_id, int($root_id), "root_id is an integer: $root_id");
98         $last_id = $rw->add_message($last);
99         is($last_id, int($last_id), "last_id is an integer: $last_id");
100 });
101
102 sub filter_mids {
103         my ($msgs) = @_;
104         sort(map { $_->{mid} } @$msgs);
105 }
106
107 my $query = sub {
108         my ($query_string, $opt) = @_;
109         my $mset = $ibx->search->mset($query_string, $opt);
110         $ibx->search->mset_to_smsg($ibx, $mset);
111 };
112
113 {
114         $rw_commit->();
115         my $found = $query->('m:root@s');
116         is(scalar(@$found), 1, "message found");
117         is($found->[0]->{mid}, 'root@s', 'mid set correctly') if @$found;
118
119         my ($res, @res);
120         my @exp = sort qw(root@s last@s);
121
122         $res = $query->('s:(Hello world)');
123         @res = filter_mids($res);
124         is_deeply(\@res, \@exp, 'got expected results for s:() match');
125
126         $res = $query->('s:"Hello world"');
127         @res = filter_mids($res);
128         is_deeply(\@res, \@exp, 'got expected results for s:"" match');
129
130         $res = $query->('s:"Hello world"', {limit => 1});
131         is(scalar @$res, 1, "limit works");
132         my $first = $res->[0];
133
134         $res = $query->('s:"Hello world"', {offset => 1});
135         is(scalar @$res, 1, "offset works");
136         my $second = $res->[0];
137
138         isnt($first, $second, "offset returned different result from limit");
139 }
140
141 # ghost vivication
142 $ibx->with_umask(sub {
143         $rw_commit->();
144         my $rmid = '<ghost-message@s>';
145         my $reply_to_ghost = PublicInbox::Eml->new(<<"EOF");
146 Date: Sat, 02 Oct 2010 00:00:00 +0000
147 Subject: Re: ghosts
148 Message-ID: <ghost-reply\@s>
149 In-Reply-To: $rmid
150 From: Time Traveler <tt\@example.com>
151 To: list\@example.com
152
153 -_-
154 EOF
155         my $rv;
156         my $reply_id = $rw->add_message($reply_to_ghost);
157         is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
158
159         my $was_ghost = PublicInbox::Eml->new(<<"EOF");
160 Date: Sat, 02 Oct 2010 00:00:01 +0000
161 Subject: ghosts
162 Message-ID: $rmid
163 From: Laggy Sender <lag\@example.com>
164 To: list\@example.com
165
166 are real
167 EOF
168         my $ghost_id = $rw->add_message($was_ghost);
169         is($ghost_id, int($ghost_id), "ghost_id is an integer: $ghost_id");
170         my $msgs = $rw->{oidx}->get_thread('ghost-message@s');
171         is(scalar(@$msgs), 2, 'got both messages in ghost thread');
172         foreach (qw(sid tid)) {
173                 is($msgs->[0]->{$_}, $msgs->[1]->{$_}, "{$_} match");
174         }
175         isnt($msgs->[0]->{num}, $msgs->[1]->{num}, "num do not match");
176         ok($_->{num} > 0, 'positive art num') foreach @$msgs
177 });
178
179 # search thread on ghost
180 {
181         $rw_commit->();
182
183         # subject
184         my $res = $query->('ghost');
185         my @exp = sort qw(ghost-message@s ghost-reply@s);
186         my @res = filter_mids($res);
187         is_deeply(\@res, \@exp, 'got expected results for Subject match');
188
189         # body
190         $res = $query->('goodbye');
191         is(scalar(@$res), 1, "goodbye message found");
192         is($res->[0]->{mid}, 'last@s', 'got goodbye message body') if @$res;
193
194         # datestamp
195         $res = $query->('dt:20101002000001..20101002000001');
196         @res = filter_mids($res);
197         is_deeply(\@res, ['ghost-message@s'], 'exact Date: match works');
198         $res = $query->('dt:20101002000002..20101002000002');
199         is_deeply($res, [], 'exact Date: match down to the second');
200 }
201
202 # long message-id
203 $ibx->with_umask(sub {
204         $rw_commit->();
205         my $long_mid = 'last' . ('x' x 60). '@s';
206         my $long = PublicInbox::Eml->new(<<EOF);
207 Date: Sat, 02 Oct 2010 00:00:00 +0000
208 Subject: long message ID
209 References: <root\@s> <last\@s>
210 In-Reply-To: <last\@s>
211 Message-ID: <$long_mid>,
212 From: "Long I.D." <long-id\@example.com>
213 To: list\@example.com
214
215 wut
216 EOF
217         my $long_id = $rw->add_message($long);
218         is($long_id, int($long_id), "long_id is an integer: $long_id");
219
220         $rw_commit->();
221         my $res;
222         my @res;
223
224         my $long_reply_mid = 'reply-to-long@1';
225         my $long_reply = PublicInbox::Eml->new(<<EOF);
226 Subject: I break references
227 Date: Sat, 02 Oct 2010 00:00:00 +0000
228 Message-ID: <$long_reply_mid>
229 In-Reply-To: <$long_mid>
230 From: no1 <no1\@example.com>
231 To: list\@example.com
232
233 no References
234 EOF
235         ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
236
237         $rw_commit->();
238         my $t = $ibx->over->get_thread('root@s');
239         is(scalar(@$t), 4, "got all 4 messages in thread");
240         my @exp = sort($long_reply_mid, 'root@s', 'last@s', $long_mid);
241         @res = filter_mids($t);
242         is_deeply(\@res, \@exp, "get_thread works");
243 });
244
245 # quote prioritization
246 $ibx->with_umask(sub {
247         $rw_commit->();
248         $rw->add_message(PublicInbox::Eml->new(<<'EOF'));
249 Date: Sat, 02 Oct 2010 00:00:01 +0000
250 Subject: Hello
251 Message-ID: <quote@a>
252 From: Quoter <quoter@example.com>
253 To: list@example.com
254
255 > theatre illusions
256 fade
257 EOF
258         $rw->add_message(PublicInbox::Eml->new(<<'EOF'));
259 Date: Sat, 02 Oct 2010 00:00:02 +0000
260 Subject: Hello
261 Message-ID: <nquote@a>
262 From: Non-Quoter<non-quoter@example.com>
263 To: list@example.com
264
265 theatre
266 fade
267 EOF
268         $rw_commit->();
269         my $res = $query->("theatre");
270         is(scalar(@$res), 2, "got both matches");
271         if (@$res == 2) {
272                 is($res->[0]->{mid}, 'nquote@a', 'non-quoted scores higher');
273                 is($res->[1]->{mid}, 'quote@a', 'quoted result still returned');
274         }
275         $res = $query->("illusions");
276         is(scalar(@$res), 1, "got a match for quoted text");
277         is($res->[0]->{mid}, 'quote@a',
278                 "quoted result returned if nothing else") if scalar(@$res);
279 });
280
281 # circular references
282 $ibx->with_umask(sub {
283         my $s = 'foo://'. ('Circle' x 15).'/foo';
284         my $doc_id = $rw->add_message(PublicInbox::Eml->new(<<EOF));
285 Subject: $s
286 Date: Sat, 02 Oct 2010 00:00:01 +0000
287 Message-ID: <circle\@a>
288 References: <circle\@a>
289 In-Reply-To: <circle\@a>
290 From: Circle <circle\@example.com>
291 To: list\@example.com
292
293 LOOP!
294 EOF
295         ok($doc_id > 0, "doc_id defined with circular reference");
296         $rw_commit->();
297         my $smsg = $query->('m:circle@a', {limit=>1})->[0];
298         is(defined($smsg), 1, 'found m:circl@a');
299         if (defined $smsg) {
300                 is($smsg->{references}, '', "no references created");
301                 is($smsg->{subject}, $s, 'long subject not rewritten');
302         }
303 });
304
305 {
306         my $msgs = $query->('d:19931002..20101002');
307         ok(scalar(@$msgs) > 0, 'got results within range');
308         $msgs = $query->('d:20101003..');
309         is(scalar(@$msgs), 0, 'nothing after 20101003');
310         $msgs = $query->('d:..19931001');
311         is(scalar(@$msgs), 0, 'nothing before 19931001');
312 }
313
314 $ibx->with_umask(sub {
315         my $mime = eml_load 't/utf8.eml';
316         my $doc_id = $rw->add_message($mime);
317         ok($doc_id > 0, 'message indexed doc_id with UTF-8');
318         $rw_commit->();
319         my $msg = $query->('m:testmessage@example.com', {limit => 1})->[0];
320         is(defined($msg), 1, 'found testmessage@example.com');
321         if (defined $msg) {
322                 is($mime->header('Subject'), $msg->{subject},
323                         'UTF-8 subject preserved');
324         }
325 });
326
327 # names and addresses
328 {
329         my $mset = $ibx->search->mset('t:list@example.com');
330         is($mset->size, 9, 'searched To: successfully');
331         foreach my $m ($mset->items) {
332                 my $smsg = $ibx->over->get_art($m->get_docid);
333                 like($smsg->{to}, qr/\blist\@example\.com\b/, 'to appears');
334                 my $doc = $m->get_document;
335                 my $col = PublicInbox::Search::BYTES();
336                 my $bytes = PublicInbox::Search::int_val($doc, $col);
337                 like($bytes, qr/\A[0-9]+\z/, '$bytes stored as digit');
338                 ok($bytes > 0, '$bytes is > 0');
339                 is($bytes, $smsg->{bytes}, 'bytes Xapian value matches Over');
340
341                 $col = PublicInbox::Search::UID();
342                 my $uid = PublicInbox::Search::int_val($doc, $col);
343                 is($uid, $smsg->{num}, 'UID column matches {num}');
344                 is($uid, $m->get_docid, 'UID column matches docid');
345         }
346
347         $mset = $ibx->search->mset('tc:list@example.com');
348         is($mset->size, 9, 'searched To+Cc: successfully');
349         foreach my $m ($mset->items) {
350                 my $smsg = $ibx->over->get_art($m->get_docid);
351                 my $tocc = join("\n", $smsg->{to}, $smsg->{cc});
352                 like($tocc, qr/\blist\@example\.com\b/, 'tocc appears');
353         }
354
355         foreach my $pfx ('tcf:', 'c:') {
356                 my $mset = $ibx->search->mset($pfx . 'foo@example.com');
357                 is($mset->items, 1, "searched $pfx successfully for Cc:");
358                 foreach my $m ($mset->items) {
359                         my $smsg = $ibx->over->get_art($m->get_docid);
360                         like($smsg->{cc}, qr/\bfoo\@example\.com\b/,
361                                 'cc appears');
362                 }
363         }
364
365         foreach my $pfx ('', 'tcf:', 'f:') {
366                 my $res = $query->($pfx . 'Laggy');
367                 is(scalar(@$res), 1,
368                         "searched $pfx successfully for From:");
369                 foreach my $smsg (@$res) {
370                         like($smsg->{from_name}, qr/Laggy Sender/,
371                                 "From appears with $pfx");
372                 }
373         }
374 }
375
376 {
377         $rw_commit->();
378         my $res = $query->('b:hello');
379         is(scalar(@$res), 0, 'no match on body search only');
380         $res = $query->('bs:smith');
381         is(scalar(@$res), 0,
382                 'no match on body+subject search for From');
383
384         $res = $query->('q:theatre');
385         is(scalar(@$res), 1, 'only one quoted body');
386         like($res->[0]->{from_name}, qr/\AQuoter/,
387                 'got quoted body') if (scalar(@$res));
388
389         $res = $query->('nq:theatre');
390         is(scalar @$res, 1, 'only one non-quoted body');
391         like($res->[0]->{from_name}, qr/\ANon-Quoter/,
392                 'got non-quoted body') if (scalar(@$res));
393
394         foreach my $pfx (qw(b: bs:)) {
395                 $res = $query->($pfx . 'theatre');
396                 is(scalar @$res, 2, "searched both bodies for $pfx");
397                 like($res->[0]->{from_name}, qr/\ANon-Quoter/,
398                         "non-quoter first for $pfx") if scalar(@$res);
399         }
400 }
401
402 $ibx->with_umask(sub {
403         my $amsg = eml_load 't/search-amsg.eml';
404         my $oid = ('0'x40);
405         my $smsg = bless { blob => $oid }, 'PublicInbox::Smsg';
406         ok($rw->add_message($amsg, $smsg), 'added attachment');
407         $rw_commit->();
408         my $n = $query->('n:attached_fart.txt');
409         is(scalar @$n, 1, 'got result for n:');
410         my $res = $query->('part_deux.txt');
411         is(scalar @$res, 1, 'got result without n:');
412         is($n->[0]->{mid}, $res->[0]->{mid},
413                 'same result with and without') if scalar(@$res);
414         my $txt = $query->('"inside another"');
415         is(scalar @$txt, 1, 'found inside another');
416         is($txt->[0]->{mid}, $res->[0]->{mid},
417                 'search inside text attachments works') if scalar(@$txt);
418
419         my $art;
420         if (scalar(@$n) >= 1) {
421                 my $mid = $n->[0]->{mid};
422                 my ($id, $prev);
423                 $art = $ibx->over->next_by_mid($mid, \$id, \$prev);
424                 ok($art, 'article exists in OVER DB');
425         }
426         $rw->_msgmap_init;
427         $rw->unindex_eml($oid, $amsg);
428         $rw->commit_txn_lazy;
429         SKIP: {
430                 skip('$art not defined', 1) unless defined $art;
431                 is($ibx->over->get_art($art->{num}), undef,
432                         'gone from OVER DB');
433         };
434 });
435
436 my $all_mask = 07777;
437 my $dir_mask = 02770;
438
439 # FreeBSD and apparently OpenBSD does not allow non-root users to set S_ISGID,
440 # so git doesn't set it, either (see DIR_HAS_BSD_GROUP_SEMANTICS in git.git)
441 if ($^O =~ /(?:free|open)bsd/i) {
442         $all_mask = 0777;
443         $dir_mask = 0770;
444 }
445
446 foreach my $f ("$git_dir/public-inbox/msgmap.sqlite3",
447                 "$git_dir/public-inbox",
448                 glob("$git_dir/public-inbox/xapian*/"),
449                 glob("$git_dir/public-inbox/xapian*/*")) {
450         my @st = stat($f);
451         my ($bn) = (split(m!/!, $f))[-1];
452         oct_is($st[2] & $all_mask, -f _ ? 0660 : $dir_mask,
453                 "sharedRepository respected for $bn");
454 }
455
456 $ibx->with_umask(sub {
457         $rw_commit->();
458         my $digits = '10010260936330';
459         my $ua = 'Pine.LNX.4.10';
460         my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
461         is($ibx->search->mset("m:$digits")->size, 0, 'no results yet');
462         my $pine = PublicInbox::Eml->new(<<EOF);
463 Subject: blah
464 Message-ID: <$mid>
465 From: torvalds\@transmeta
466 To: list\@example.com
467
468 EOF
469         my $x = $rw->add_message($pine);
470         $rw->commit_txn_lazy;
471         $ibx->search->reopen;
472         is($ibx->search->mset("m:$digits")->size, 1,
473                 'searching only digit yielded result');
474
475         my $wild = $digits;
476         for my $i (1..6) {
477                 chop($wild);
478                 is($ibx->search->mset("m:$wild*")->size, 1,
479                         "searching chopped($i) digit yielded result $wild ");
480         }
481         is($ibx->search->mset('m:Pine m:LNX m:10010260936330')->size, 1);
482 });
483
484 { # List-Id searching
485         my $found = $query->('lid:i.m.just.bored');
486         is_deeply([ filter_mids($found) ], [ 'root@s' ],
487                 'got expected mid on exact lid: search');
488
489         $found = $query->('lid:just.bored');
490         is_deeply($found, [], 'got nothing on lid: search');
491
492         $found = $query->('lid:*.just.bored');
493         is_deeply($found, [], 'got nothing on lid: search');
494
495         $found = $query->('l:i.m.just.bored');
496         is_deeply([ filter_mids($found) ], [ 'root@s' ],
497                 'probabilistic search works on full List-Id contents');
498
499         $found = $query->('l:just.bored');
500         is_deeply([ filter_mids($found) ], [ 'root@s' ],
501                 'probabilistic search works on partial List-Id contents');
502
503         $found = $query->('lid:mad');
504         is_deeply($found, [], 'no match on phrase with lid:');
505
506         $found = $query->('lid:bored');
507         is_deeply($found, [], 'no match on partial List-Id with lid:');
508
509         $found = $query->('l:nothing');
510         is_deeply($found, [], 'matched on phrase with l:');
511 }
512
513 $ibx->with_umask(sub {
514         $rw_commit->();
515         my $doc_id = $rw->add_message(eml_load('t/data/message_embed.eml'));
516         ok($doc_id > 0, 'messages within messages');
517         $rw->commit_txn_lazy;
518         $ibx->search->reopen;
519         my $n_test_eml = $query->('n:test.eml');
520         is(scalar(@$n_test_eml), 1, 'got a result');
521         my $n_embed2x_eml = $query->('n:embed2x.eml');
522         is_deeply($n_test_eml, $n_embed2x_eml, '.eml filenames searchable');
523         for my $m (qw(20200418222508.GA13918@dcvr 20200418222020.GA2745@dcvr
524                         20200418214114.7575-1-e@yhbt.net)) {
525                 is($query->("m:$m")->[0]->{mid},
526                         '20200418222508.GA13918@dcvr', 'probabilistic m:'.$m);
527                 is($query->("mid:$m")->[0]->{mid},
528                         '20200418222508.GA13918@dcvr', 'boolean mid:'.$m);
529         }
530         is($query->('dfpost:4dc62c50')->[0]->{mid},
531                 '20200418222508.GA13918@dcvr',
532                 'diff search reaches inside message/rfc822');
533         is($query->('s:"mail header experiments"')->[0]->{mid},
534                 '20200418222508.GA13918@dcvr',
535                 'Subject search reaches inside message/rfc822');
536
537         $doc_id = $rw->add_message(eml_load('t/data/binary.patch'));
538         $rw->commit_txn_lazy;
539         $ibx->search->reopen;
540         my $res = $query->('HcmV');
541         is_deeply($res, [], 'no results against trailer');
542         $res = $query->('IcmZPo000310RR91');
543         is_deeply($res, [], 'no results against 1-byte binary patch');
544         $res = $query->('"GIT binary patch"');
545         is(scalar(@$res), 1, 'got binary result from "GIT binary patch"');
546         is($res->[0]->{mid}, 'binary-patch-test@example', 'msgid for binary');
547         my $s = $query->('"literal 1"');
548         is_deeply($s, $res, 'got binary result from exact literal size');
549         $s = $query->('"literal 2"');
550         is_deeply($s, [], 'no results for wrong size');
551 });
552
553 SKIP: {
554         my ($s, $g) = ($ibx->search, $ibx->git);
555         my $q = $s->query_argv_to_string($g, ["quoted phrase"]);
556         is($q, q["quoted phrase"], 'quoted phrase');
557         $q = $s->query_argv_to_string($g, ['s:pa ce']);
558         is($q, q[s:"pa ce"], 'space with prefix');
559         $q = $s->query_argv_to_string($g, ["\(s:pa ce", "AND", "foo\)"]);
560         is($q, q[(s:"pa ce" AND foo)], 'space AND foo');
561
562         local $ENV{TZ} = 'UTC';
563         my $now = strftime('%H:%M:%S', gmtime(time));
564         if ($now =~ /\A23:(?:59|60)/ || $now =~ /\A00:00:0[01]\z/) {
565                 skip 'too close to midnight, time is tricky', 6;
566         }
567         $q = $s->query_argv_to_string($g, [qw(d:20101002 blah)]);
568         is($q, 'd:20101002..20101003 blah', 'YYYYMMDD expanded to range');
569         $q = $s->query_argv_to_string($g, [qw(d:2010-10-02)]);
570         is($q, 'd:20101002..20101003', 'YYYY-MM-DD expanded to range');
571         $q = $s->query_argv_to_string($g, [qw(rt:2010-10-02.. yy)]);
572         $q =~ /\Art:(\d+)\.\. yy/ or fail("rt: expansion failed: $q");
573         is(strftime('%Y-%m-%d', gmtime($1//0)), '2010-10-02', 'rt: beg expand');
574         $q = $s->query_argv_to_string($g, [qw(rt:..2010-10-02 zz)]);
575         $q =~ /\Art:\.\.(\d+) zz/ or fail("rt: expansion failed: $q");
576         is(strftime('%Y-%m-%d', gmtime($1//0)), '2010-10-02', 'rt: end expand');
577         $q = $s->query_argv_to_string($g, [qw(something dt:2010-10-02..)]);
578         like($q, qr/\Asomething dt:20101002\d{6}\.\./, 'dt: expansion');
579         $q = $s->query_argv_to_string($g, [qw(x dt:yesterday.. y)]);
580         my $exp = strftime('%Y%m%d', gmtime(time - 86400));
581         like($q, qr/x dt:$exp[0-9]{6}\.\. y/, '"yesterday" handled');
582         $q = $s->query_argv_to_string($g, [qw(x dt:20101002054123)]);
583         is($q, 'x dt:20101002054123..20101003054123', 'single dt: expanded');
584         $q = $s->query_argv_to_string($g, [qw(x dt:2010-10-02T05:41:23Z)]);
585         is($q, 'x dt:20101002054123..20101003054123', 'ISO8601 dt: expanded');
586         $q = $s->query_argv_to_string($g, [qw(rt:1970..1971)]);
587         $q =~ /\Art:(\d+)\.\.(\d+)\z/ or fail "YYYY rt: expansion: $q";
588         my ($beg, $end) = ($1, $2);
589         is(strftime('%Y', gmtime($beg)), 1970, 'rt: starts at 1970');
590         is(strftime('%Y', gmtime($end)), 1971, 'rt: ends at 1971');
591         $q = $s->query_argv_to_string($g, [qw(rt:1970-01-01)]);
592         $q =~ /\Art:(\d+)\.\.(\d+)\z/ or fail "YYYY-MM-DD rt: expansion: $q";
593         ($beg, $end) = ($1, $2);
594         is(strftime('%Y-%m-%d', gmtime($beg)), '1970-01-01',
595                         'rt: date-only w/o range');
596         is(strftime('%Y-%m-%d', gmtime($end)), '1970-01-02',
597                         'rt: date-only auto-end');
598         $q = $s->query_argv_to_string($g, [qw{OR (rt:1993-10-02)}]);
599         like($q, qr/\AOR \(rt:749\d{6}\.\.749\d{6}\)\z/,
600                 'trailing parentheses preserved');
601
602         my $qs = qq[f:bob rt:1993-10-02..2010-10-02];
603         $s->query_approxidate($g, $qs);
604         like($qs, qr/\Af:bob rt:749\d{6}\.\.128\d{7}\z/,
605                 'no phrases, no problem');
606
607         my $orig = $qs = qq[f:bob "d:1993-10-02..2010-10-02"];
608         $s->query_approxidate($g, $qs);
609         is($qs, $orig, 'phrase preserved');
610
611         $orig = $qs = qq[f:bob "d:1993-10-02..2010-10-02 "] .
612                         qq["dt:1993-10-02..2010-10-02 " \x{201c}];
613         $s->query_approxidate($g, $qs);
614         is($qs, $orig, 'phrase preserved even with escaped ""');
615
616         $orig = $qs = qq[f:bob "hello world" d:1993-10-02..2010-10-02];
617         $s->query_approxidate($g, $qs);
618         is($qs, qq[f:bob "hello world" d:19931002..20101002],
619                 'post-phrase date corrected');
620
621         # Xapian uses "" to escape " inside phrases, we don't explictly
622         # handle that, but are able to pass the result through unchanged
623         for my $pair (["\x{201c}", "\x{201d}"], ['"', '"']) {
624                 my ($x, $y) = @$pair;
625                 $orig = $qs = qq[${x}hello d:1993-10-02.."" world$y];
626                 $s->query_approxidate($g, $qs);
627                 is($qs, $orig, 'phrases unchanged \x'.ord($x).'-\x'.ord($y));
628
629                 $s->query_approxidate($g, my $tmp = "$qs d:..2010-10-02");
630                 is($tmp, "$orig d:..20101002",
631                         'two phrases did not throw off date parsing');
632
633                 $orig = $qs = qq[${x}hello d:1993-10-02..$y$x world$y];
634                 $s->query_approxidate($g, $qs);
635                 is($qs, $orig, 'phrases unchanged \x'.ord($x).'-\x'.ord($y));
636
637                 $s->query_approxidate($g, $tmp = "$qs d:..2010-10-02");
638                 is($tmp, "$orig d:..20101002",
639                         'two phrases did not throw off date parsing');
640         }
641
642         my $x_days_ago = strftime('%Y%m%d', gmtime(time - (5 * 86400)));
643         $orig = $qs = qq[broken d:5.days.ago..];
644         $s->query_approxidate($g, $qs);
645         like($qs, qr/\Abroken dt:$x_days_ago[0-9]{6}\.\./,
646                 'date.phrase.with.dots');
647
648         $orig = $qs = 'd:20101002..now';
649         $s->query_approxidate($g, $qs);
650         like($qs, qr/\Adt:20101002000000\.\.[0-9]{14}\z/,
651                 'approxidate on range-end only');
652
653         $ENV{TEST_EXPENSIVE} or
654                 skip 'TEST_EXPENSIVE not set for argv overflow check', 1;
655         my @w;
656         local $SIG{__WARN__} = sub { push @w, @_ }; # for pure Perl version
657         my @fail = map { 'd:1993-10-02..2010-10-02' } (1..(4096 * 32));
658         eval { $s->query_argv_to_string($g, \@fail) };
659         ok($@, 'exception raised');
660 }
661
662 done_testing();