From ad2b7f9b9e3138a0bccabd76ef46a2b587eda34d Mon Sep 17 00:00:00 2001 From: Ricardo Signes Date: Mon, 30 Jan 2017 16:06:29 -0500 Subject: [PATCH 1/2] Test that sorting on date matches date header Are we sorting by internaldate? message.date should be that. Are we sorting by header? message.date should be that. Based on earlier work by Matthew Horsfall. --- lib/JMAP/TestSuite/Account.pm | 1 + t/getMessageList-sort-on-date.t | 106 ++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 t/getMessageList-sort-on-date.t diff --git a/lib/JMAP/TestSuite/Account.pm b/lib/JMAP/TestSuite/Account.pm index 55600ba..b8a42c2 100644 --- a/lib/JMAP/TestSuite/Account.pm +++ b/lib/JMAP/TestSuite/Account.pm @@ -41,6 +41,7 @@ package JMAP::TestSuite::AccountContext { To => 'example@example.biz', Subject => 'This is a test', 'Message-Id' => $arg->{message_id} // "", + ( $arg->{date} ? ( Date => $arg->{date} ) : () ), ], body => "This is a very simple message.", ); diff --git a/t/getMessageList-sort-on-date.t b/t/getMessageList-sort-on-date.t new file mode 100644 index 0000000..645260d --- /dev/null +++ b/t/getMessageList-sort-on-date.t @@ -0,0 +1,106 @@ +use strict; +use warnings; + +use JMAP::TestSuite; +use JMAP::TestSuite::Util qw(batch_ok); + +use Test::Deep::JType; +use Test::More; + +use DateTime; +use Email::MessageID; + +my $server = JMAP::TestSuite->get_server; + +$server->simple_test(sub { + my ($context) = @_; + + my $tester = $context->tester; + + # Get us a mailbox to play with + my $batch = $context->create_batch(mailbox => { + x => { name => "Folder X at $^T.$$" }, + }); + + batch_ok($batch); + + ok( $batch->is_entirely_successful, "created a mailbox"); + my $x = $batch->result_for('x'); + + my %message = ( + 2016 => { + header => "Sun, 25 Dec 2016 12:00:01 −0300", + date => '2016-12-25T12:00:01Z', + }, + 2011 => { + header => "Sun, 25 Dec 2011 12:00:01 −0300", + date => '2011-12-25T12:00:01Z', + }, + 2005 => { + header => "Sun, 25 Dec 2005 12:00:01 −0300", + date => '2005-12-25T12:00:01Z', + }, + ); + + # Import messages with specific dates, expect + # the highest to come back when sorting by date desc + for my $year (sort { $b <=> $a } keys %message) { + my ($hdr_date, $date) = $message{$year}->@{ qw(header date) }; + subtest "message with date header $hdr_date" => sub { + my $blob = $context->email_blob(generic => { + message_id => Email::MessageID->new->in_brackets, + date => $hdr_date, + }); + + ok($blob->is_success, "our upload succeeded (" . $blob->blobId . ")"); + + $batch = $context->import_messages({ + msg => { blobId => $blob, mailboxIds => [ $x->id ] }, + }); + + batch_ok($batch); + + ok($batch->is_entirely_successful, "we uploaded and imported messages"); + + my $message_id = $batch->result_for('msg')->id; + $message{$year}{id} = $message_id; + }; + } + + my $res = $tester->request([ + [ + getMessageList => { + filter => { inMailbox => $x->id }, + sort => [ 'date desc' ], + limit => 1, + fetchMessages => jtrue(), + fetchMessageProperties => [ 'date', ] , + } + ], + ]); + + my $first = $res->sentence(1)->arguments->{list}[0]; + my ($which) = grep { $message{$_}{id} eq $first->{id} } keys %message; + + unless ($which) { + fail("the message we got back is not one we just created?!"); + return; + } + + # Okay, so either we're sorting by internal date so we should have a date in + # 2017 or later --or-- we're sorting by header and should have the 2016 date. + if ($which eq '2005') { + cmp_ok( + $first->{date}, 'gt', '2017-01-01', + "sort by internaldate, return internaldate" + ); + return; + } elsif ($which eq '2016') { + is($first->{date}, $message{2016}{date}, "sort by header, return header"); + return; + } + + fail("something weird is going on: $which"); +}); + +done_testing; From 22eca93e8c0636bc1236f5324d58b1cbbd178d69 Mon Sep 17 00:00:00 2001 From: Ricardo Signes Date: Mon, 30 Jan 2017 17:35:04 -0500 Subject: [PATCH 2/2] message.date: it should definitely be internaldate --- t/getMessageList-sort-on-date.t | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/t/getMessageList-sort-on-date.t b/t/getMessageList-sort-on-date.t index 645260d..8b733ac 100644 --- a/t/getMessageList-sort-on-date.t +++ b/t/getMessageList-sort-on-date.t @@ -32,10 +32,6 @@ $server->simple_test(sub { header => "Sun, 25 Dec 2016 12:00:01 −0300", date => '2016-12-25T12:00:01Z', }, - 2011 => { - header => "Sun, 25 Dec 2011 12:00:01 −0300", - date => '2011-12-25T12:00:01Z', - }, 2005 => { header => "Sun, 25 Dec 2005 12:00:01 −0300", date => '2005-12-25T12:00:01Z', @@ -82,25 +78,18 @@ $server->simple_test(sub { my $first = $res->sentence(1)->arguments->{list}[0]; my ($which) = grep { $message{$_}{id} eq $first->{id} } keys %message; - unless ($which) { - fail("the message we got back is not one we just created?!"); - return; - } + is($which, 2005, "the more recently imported message is newer"); - # Okay, so either we're sorting by internal date so we should have a date in - # 2017 or later --or-- we're sorting by header and should have the 2016 date. - if ($which eq '2005') { - cmp_ok( - $first->{date}, 'gt', '2017-01-01', - "sort by internaldate, return internaldate" - ); - return; - } elsif ($which eq '2016') { - is($first->{date}, $message{2016}{date}, "sort by header, return header"); - return; - } + isnt( + $first->{date}, + $message{$which}{date}, + "the message.date is not from the ancient header value" + ); - fail("something weird is going on: $which"); + cmp_ok( + $first->{date}, 'gt', '2017-01-01', + "...and we return an internaldate in the plausible present" + ); }); done_testing;