Added matchinfo tests

Change-Id: Idfbc7ebf3a1dd8524425999b4ab15cbe64bb68c8
diff --git a/lib/Kalamar/Controller/Search2.pm b/lib/Kalamar/Controller/Search2.pm
index af9d0b5..7bb0b99 100644
--- a/lib/Kalamar/Controller/Search2.pm
+++ b/lib/Kalamar/Controller/Search2.pm
@@ -22,6 +22,7 @@
   my $err = $tx->error;
 
   if ($err) {
+    # print $err->code, "\n";
     return Mojo::Promise->new->reject([
       [$err->{code}, $err->{message}]
     ]);
@@ -42,6 +43,7 @@
     return Mojo::Promise->new->reject($err);
   };
 
+  # TODO: What does status mean?
   if ($json->{status}) {
     return Mojo::Promise->new->reject([
       [undef, 'Middleware error ' . $json->{'status'}]
@@ -154,6 +156,8 @@
       $match->{matchID} =~ s!^match\-(?:[^\/]+\/){2}[^-]+?-!!;
   };
 
+  return unless $match->{textSigle};
+
   # Set IDs based on the sigle
   (
     $match->{corpusID},
@@ -315,6 +319,7 @@
 
     # Wrap a user agent method with a promise
     $promise = $c->user->auth_request_p(get => $url)
+
       # TODO: Better use a single then
       ->then(\&_catch_http_errors)
       ->then(
@@ -337,6 +342,10 @@
   # Wait for rendering
   $c->render_later;
 
+  # Choose the snippet based on the parameter
+  my $template = scalar $v->param('snippet') ? 'snippet' : 'search2';
+  $c->stash(template => $template);
+
   # Process response
   $promise->then(
     sub {
@@ -385,23 +394,25 @@
       return $c->_process_matches($json);
     }
 
+  # Render template
+  )->then(
+    sub {
+      return $c->render(
+        q => $query,
+        ql => $query{ql},
+        start_page => $query{p},
+      );
+    }
+
   # Deal with errors
   )->catch(
     sub {
       $c->_notify_on_errors(shift);
-    }
-
-  # Render template
-  )->finally(
-    sub {
-      # Choose the snippet based on the parameter
-      my $template = scalar $v->param('snippet') ? 'snippet' : 'search2';
-
       return $c->render(
-        template => $template,
+        results => c(),
         q => $query,
         ql => $query{ql},
-        start_page => $query{p},
+        start_page => 1
       );
     }
   )
diff --git a/t/fixtures/fake_backend.pl b/t/fixtures/fake_backend.pl
index 9573f3f..a49b05f 100644
--- a/t/fixtures/fake_backend.pl
+++ b/t/fixtures/fake_backend.pl
@@ -117,8 +117,10 @@
     $c->stash('matchId')
   ));
 
+  my $slug = slugify($file);
+
   # Get response based on query parameter
-  my $response = $c->load_response(slugify($file));
+  my $response = $c->load_response($slug);
   return $c->render(%$response);
 };
 
diff --git a/t/fixtures/response_matchinfo_fail_x_x_p0-1.json b/t/fixtures/response_matchinfo_fail_x_x_p0-1.json
new file mode 100644
index 0000000..13f9ef4
--- /dev/null
+++ b/t/fixtures/response_matchinfo_fail_x_x_p0-1.json
@@ -0,0 +1,7 @@
+{
+  "status":200,
+  "json" : {
+    "@context":"http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
+    "met
+  }
+}
diff --git a/t/fixtures/response_matchinfo_goe_agf_02286_p-2-0.json b/t/fixtures/response_matchinfo_goe_agf_02286_p-2-0.json
new file mode 100644
index 0000000..d743984
--- /dev/null
+++ b/t/fixtures/response_matchinfo_goe_agf_02286_p-2-0.json
@@ -0,0 +1,14 @@
+{
+  "status":200,
+  "json" : {
+    "errors":[
+      [730,"Invalid match identifier","match-GOE/AGF/02286-p-2-0"]
+    ],
+    "@context":"http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
+    "meta":{
+      "version":"Krill-0.58.0"
+    },
+    "startMore":true,
+    "endMore":true
+  }
+}
diff --git a/t/fixtures/response_matchinfo_goe_agf_02286_p75682-75683.json b/t/fixtures/response_matchinfo_goe_agf_02286_p75682-75683.json
new file mode 100644
index 0000000..9f91d10
--- /dev/null
+++ b/t/fixtures/response_matchinfo_goe_agf_02286_p75682-75683.json
@@ -0,0 +1,28 @@
+{
+  "status":200,
+  "json" : {
+    "@context":"http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
+    "meta":{
+      "version":"Krill-0.58.0"
+    },
+    "field":"tokens",
+    "pubPlace":"München",
+    "textSigle":"GOE/AGF/02286",
+    "docSigle":"GOE/AGF",
+    "corpusSigle":"GOE",
+    "title":"Materialien zur Geschichte der Farbenlehre",
+    "author":"Goethe, Johann Wolfgang von",
+    "availability":"QAO-NC",
+    "layerInfos":"corenlp/c=spans corenlp/p=tokens corenlp/s=spans dereko/s=spans malt/d=rels marmot/m=tokens marmot/p=tokens opennlp/p=tokens opennlp/s=spans tt/l=tokens tt/p=tokens",
+    "startMore":true,
+    "endMore":true,
+    "license":"QAO-NC",
+    "snippet":"<span class=\"context-left\"><span class=\"more\"></span></span><span class=\"match\"><mark>Baum</mark></span><span class=\"context-right\"><span class=\"more\"></span></span>",
+    "matchID":"match-GOE/AGF/02286-p75682-75683",
+    "pubDate":"1982",
+    "context":{
+      "left":["token",0],
+      "right":["token",0]
+    }
+  }
+}
diff --git a/t/fixtures/response_matchinfo_notfound2_x_x_p0-1.json b/t/fixtures/response_matchinfo_notfound2_x_x_p0-1.json
new file mode 100644
index 0000000..4a7e430
--- /dev/null
+++ b/t/fixtures/response_matchinfo_notfound2_x_x_p0-1.json
@@ -0,0 +1,4 @@
+{
+  "status":404,
+  "inline" : ""
+}
diff --git a/t/fixtures/response_matchinfo_notfound_x_x_p0-1.json b/t/fixtures/response_matchinfo_notfound_x_x_p0-1.json
new file mode 100644
index 0000000..b3cdf57
--- /dev/null
+++ b/t/fixtures/response_matchinfo_notfound_x_x_p0-1.json
@@ -0,0 +1,13 @@
+{
+  "status":200,
+  "json" : {
+    "@context":"http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
+    "meta":{
+      "version":"Krill-0.58.0"
+    },
+    "textSigle":"NOTFOUND/X/X",
+    "startMore":true,
+    "endMore":true,
+    "matchID":"match-NOTFOUND/X/X-p0-1"
+  }
+}
diff --git a/t/match_info.t b/t/match_info.t
index 42a1aa7..3e4b72c 100644
--- a/t/match_info.t
+++ b/t/match_info.t
@@ -28,6 +28,46 @@
 $t->get_ok('/corpus2/WPD15/232/39681/p2133-2134?spans=false&foundry=*')
   ->status_is(200)
   ->json_is('/textSigle', 'WPD15/232/39681')
+  ->json_like('/snippet', qr!<span class=\"context-left\">!)
   ;
 
+$t->get_ok('/corpus2/GOE/AGF/02286/p75682-75683')
+  ->status_is(200)
+  ->json_is('/textSigle', 'GOE/AGF/02286')
+  ->json_is('/title','Materialien zur Geschichte der Farbenlehre')
+  ;
+
+# TODO:
+#   It's surprising, that it doesn't return a 404!
+$t->get_ok('/corpus2/notfound/X/X/p0-1')
+  ->status_is(200)
+  ->json_is('/textSigle', 'NOTFOUND/X/X')
+  ->json_is('/corpusID', undef)
+  ;
+
+# TODO:
+#   Should probably return a 500!
+$t->get_ok('/corpus2/fail/x/x/p0-0')
+  ->status_is(200)
+  ->json_is('/notifications/0/0', 'error')
+  ->json_is('/notifications/0/1', '500: Internal Server Error')
+  ;
+
+# TODO:
+#   Should probably return a 4xx!
+$t->get_ok('/corpus2/GOE/AGF/02286/p-2-0')
+  ->status_is(200)
+  ->json_is('/notifications/0/0', 'error')
+  ->json_is('/notifications/0/1', '730: Invalid match identifier')
+  ;
+
+# TODO:
+#   It's surprising, that it doesn't return a 404!
+$t->get_ok('/corpus2/notfound2/X/X/p0-1')
+  ->status_is(200)
+  ->json_is('/notifications/0/0', 'error')
+  ->json_is('/notifications/0/1', '404: Not Found')
+  ;
+
+
 done_testing;