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