Restructure test suite

Change-Id: Ibbd4bbb27203f9ffa89ae3e3a36b16f455b3523c
diff --git a/t/api_v0.1.t b/t/api_v0.1.t
deleted file mode 100644
index 4d8a560..0000000
--- a/t/api_v0.1.t
+++ /dev/null
@@ -1,97 +0,0 @@
-#!/usr/bin/env perl
-use strict;
-use warnings;
-use Test::More skip_all => 'No remote tests';
-use Test::Mojo;
-use Mojo::JSON;
-use Mojo::ByteStream 'b';
-use utf8;
-
-my $t = Test::Mojo->new;
-
-my $url = Mojo::URL->new('http://10.0.10.13:8888/api/v0.1/');
-
-# Get resources - corpus
-$t->get_ok($url->clone->path('corpus'))
-  ->status_is(200)
-  ->header_is('Content-Type', 'application/json; charset=utf-8')
-  ->json_is('/0/id', 'WPD')
-  ->json_is('/0/name', 'WPD')
-  ->json_is('/0/path', 'WPD')
-  ->json_is('/0/managed', Mojo::JSON->true)
-  ->json_is('/0/statistics/documents', 196510)
-  ->json_is('/0/statistics/tokens', 51545081)
-  ->json_is('/0/statistics/sentences', 4116282)
-  ->json_is('/0/statistics/paragraphs', 2034752);
-
-# Get resources - collection
-$t->get_ok($url->clone->path('collection'))
-  ->status_is(200)
-  ->json_is('/0/managed', Mojo::JSON->true)
-  ->json_is('/0/name', 'Wikipedia')
-#  ->json_is('/0/description', 'Die freie Enzyklopädie')
-  ;
-
-# Get resources - query
-$t->get_ok($url->clone->path('query'))
-  ->status_is(200)
-  ->json_is([]);
-
-# Get resources - foundry
-$t->get_ok($url->clone->path('foundry'))
-  ->status_is(200);
-
-# Get resources - layer
-$t->get_ok($url->clone->path('layer'))
-  ->status_is(200);
-
-# Stats
-$t->get_ok($url->clone->path('corpus/WPD/stats'))
-  ->json_is('/documents', 196510)
-  ->json_is('/tokens', 51545081)
-  ->json_is('/sentences', 4116282)
-  ->json_is('/paragraphs', 2034752)
-  ->status_is(200);
-
-# Matchinfo
-$t->get_ok($url->clone->path('corpus/WPD/SSS.04897/p29-30/matchInfo'))
-  ->json_is('/author', 'Darkone,Dramburg,Fusslkopp')
-  ->json_is('/textClass', 'freizeit-unterhaltung reisen')
-  ->json_is('/corpusID', 'WPD')
-  ->json_is('/title', 'Schloss Hohenzieritz')
-  ->json_is('/docID', 'WPD_SSS.04897')
-  ->json_is('/ID', 'match-WPD!WPD_SSS.04897-p29-30')
-  ->json_is('/snippet', "<span class=\"context-left\"><span class=\"more\"></span></span><span class=\"match\">Haus</span><span class=\"context-right\"><span class=\"more\"></span></span>")
-  ->status_is(200);
-
-# Matchinfo
-$t->get_ok($url->clone->path('corpus/WPD/SSS.04897/p29-30/matchInfo')->query({ foundry => '*'}))
-  ->json_is('/author', 'Darkone,Dramburg,Fusslkopp')
-  ->json_is('/textClass', 'freizeit-unterhaltung reisen')
-  ->json_is('/corpusID', 'WPD')
-  ->json_is('/title', 'Schloss Hohenzieritz')
-  ->json_is('/docID', 'WPD_SSS.04897')
-  ->json_is('/ID', 'match-WPD!WPD_SSS.04897-p23-45')
-  ->json_has('/snippet')
-  ->status_is(200);
-
-# Search
-# Check serialization
-$t->get_ok($url->clone->path('search')->query({ q => 'contains(<s>, [orth=Test])', ql => 'poliqarp'}))
-  ->json_is('/startIndex', 0)
-  ->json_like('/totalResults', qr/\d+/)
-  ->json_is('/itemsPerPage', 25)
-  ->status_is(200);
-
-my $tx = $t->ua->build_tx('TRACE', $url->clone->path('search')->query({ q => 'contains(<s>, [orth=Test])', ql => 'poliqarp'}));
-$tx = $t->ua->start($tx);
-
-$t->tx($tx)
-  ->json_is('/@context', 'http://ids-mannheim.de/ns/KorAP/json-ld/v0.2/context.jsonld')
-  ->json_is('/query/@type', 'korap:group')
-  ->json_is('/query/operation', 'operation:position')
-  ->json_is('/query/operands/0/@type', 'korap:span')
-  ->json_is('/query/operands/0/key', 's')
-  ->status_is(200);
-
-done_testing;
diff --git a/t/corpus_info.t b/t/corpus_info.t
index 502ffd2..551d052 100644
--- a/t/corpus_info.t
+++ b/t/corpus_info.t
@@ -14,11 +14,11 @@
 
 # Mount fake backend
 # Get the fixture path
-my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, 'fixtures');
+my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, 'server');
 my $fake_backend = $t->app->plugin(
   Mount => {
     $mount_point =>
-      $fixtures_path->child('fake_backend.pl')
+      $fixtures_path->child('mock.pl')
   }
 );
 # Configure fake backend
diff --git a/t/doc.t b/t/doc.t
index 55ac2a5..52e2f1a 100644
--- a/t/doc.t
+++ b/t/doc.t
@@ -1,5 +1,4 @@
 use Mojo::Base -strict;
-use lib '../lib', 'lib';
 use Test::More;
 use Test::Mojo;
 
diff --git a/t/fixtures.t b/t/fixtures.t
index ea0047c..598b918 100644
--- a/t/fixtures.t
+++ b/t/fixtures.t
@@ -4,9 +4,9 @@
 use Mojo::File qw/path/;
 
 # Get the fixture path
-my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, 'fixtures');
+my $mock_server = path(Mojo::File->new(__FILE__)->dirname, 'server')->child('mock.pl');
 
-my $t = Test::Mojo->new($fixtures_path->child('fake_backend.pl'));
+my $t = Test::Mojo->new($mock_server);
 
 $t->get_ok('/')
   ->status_is(200)
diff --git a/t/match_info.t b/t/match_info.t
index dd0c7f6..7403d52 100644
--- a/t/match_info.t
+++ b/t/match_info.t
@@ -14,11 +14,11 @@
 
 # Mount fake backend
 # Get the fixture path
-my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, 'fixtures');
+my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, 'server');
 my $fake_backend = $t->app->plugin(
   Mount => {
     $mount_point =>
-      $fixtures_path->child('fake_backend.pl')
+      $fixtures_path->child('mock.pl')
   }
 );
 # Configure fake backend
diff --git a/t/non_blocking_api.t b/t/non_blocking_api.t
deleted file mode 100644
index d2055e1..0000000
--- a/t/non_blocking_api.t
+++ /dev/null
@@ -1,220 +0,0 @@
-use Mojo::Base -strict;
-use lib '../lib', 'lib';
-use Test::More skip_all => 'No remote tests';
-use Test::Mojo;
-use Mojo::URL;
-use Benchmark qw/:hireswallclock/;
-
-my $t = Test::Mojo->new('Kalamar');
-
-$t->app->routes->get('/searchtest')->to(
-  cb => sub {
-    my $c = shift;
-    $c->render(inline => <<'TEMPLATE');
-%= search query => param('q'), start_page => param('p'), no_cache => 1, begin
-<h1><%= search->query %></h1>
-<p id="api"><%= search->api %></p>
-<p id="cutoff"><%= search->cutoff %></p>
-<p id="ql"><%= search->query_language %></p>
-<p id="no_cache"><%= search->no_cache %></p>
-<p id="start_page"><%= search->start_page %></p>
-<p id="total_results"><%= search->total_results %></p>
-<p id="api_request"><%= search->api_request %></p>
-%=  search_results begin
-  <li><%= $_->{ID} %></li>
-%   end
-% end
-TEMPLATE
-  }
-);
-
-my $exttemplate = <<'EXTTEMPLATE';
-<h1><%= search->query %></h1>
-<p id="api"><%= search->api %></p>
-<p id="cutoff"><%= search->cutoff %></p>
-<p id="ql"><%= search->query_language %></p>
-<p id="no_cache"><%= search->no_cache %></p>
-<p id="start_page"><%= search->start_page %></p>
-<p id="total_results"><%= search->total_results %></p>
-<p id="api_request"><%= search->api_request %></p>
-%=  search_results begin
-  <li><%= $_->{ID} %></li>
-%   end
-EXTTEMPLATE
-
-
-$t->app->routes->get('/searchasync')->to(
-  cb => sub {
-    my $c = shift;
-    $c->search(
-      query => $c->param('q'),
-      start_page => $c->param('p'),
-      no_cache => 1,
-      cb => sub {
-	return $c->render(inline => $exttemplate);
-      }
-    );
-  }
-);
-
-my $tracetemplate = <<'TRACETEMPLATE';
-<h1><%= search->query %></h1>
-<p id="api"><%= search->api %></p>
-<p id="cutoff"><%= search->cutoff %></p>
-<p id="ql"><%= search->query_language %></p>
-<p id="api_request"><%= search->api_request %></p>
-<p id="query-jsonld"><%= dumper search->query_jsonld %></p>
-TRACETEMPLATE
-
-$t->app->routes->get('/traceasync')->to(
-  cb => sub {
-    my $c = shift;
-    $c->search->trace(
-      query => $c->param('q'),
-      sub {
-	return $c->render(inline => $tracetemplate);
-      }
-    );
-  }
-);
-
-my $matchtemplate = <<'MATCHTEMPLATE';
-<p id="api"><%= search->api %></p>
-<p id="api_request"><%= search->api_request %></p>
-<p id="search-result"><%= search->results->first->{docID} . '.' . search->results->first->{textID} %></p>
-MATCHTEMPLATE
-
-
-$t->app->routes->get('/matchinfo')->to(
-  cb => sub {
-    my $c = shift;
-    $c->search->match(
-      corpus_id => $c->param('corpus_id'),
-      doc_id    => $c->param('doc_id'),
-      text_id   => $c->param('text_id'),
-      match_id  => $c->param('match_id'),
-      foundry => '*',
-      sub {
-	return $c->render(inline => $matchtemplate);
-      }
-    );
-  }
-);
-
-my $colltemplate = <<'COLLTEMPLATE';
-<p id="api"><%= search->api %></p>
-<p id="api_request"><%= search->api_request %></p>
-<p id="search-resource"><%= stash('search.resource')->[0]->{name} %></p>
-COLLTEMPLATE
-
-$t->app->routes->get('/collectioninfo')->to(
-  cb => sub {
-    my $c = shift;
-    $c->search->resource(
-      type => 'collection',
-      sub {
-	return $c->render(inline => $colltemplate);
-      }
-    );
-  }
-);
-
-
-$t->app->routes->get('/collectionandsearch-parallel')->to(
-  cb => sub {
-    my $c = shift;
-    $c->delay(
-      sub {
-	my $delay = shift;
-	$c->search->resource(
-	  type => 'collection',
-	  $delay->begin
-	);
-
-	$c->search(
-	  query => $c->param('q'),
-	  start_page => $c->param('p'),
-	  no_cache => 1,
-	  cb => $delay->begin
-	);
-      },
-      sub {
-	return $c->render(
-	  inline => $exttemplate .
-	    q!<p id="search-resource"><%= stash('search.resource')->[0]->{name} %></p>!
-	  );
-      }
-    )
-  }
-);
-
-my $query = 'startswith(<s>,[mate/m=gender:masc]{3,5})';
-
-# Search everything in parallel!
-$t->get_ok(Mojo::URL->new('/collectionandsearch-parallel')->query({q => $query}))
-  ->status_is(200)
-  ->text_is('.notify-error', '')
-  ->text_is('h1', $query)
-  ->text_is('#api', 'http://10.0.10.13:7070/api/v0.1/')
-  ->text_is('#ql', 'poliqarp')
-  ->text_is('#search-resource', 'Wikipedia');
-
-$t->get_ok('/collectioninfo')
-  ->status_is(200)
-  ->text_is('#search-resource', 'Wikipedia');
-
-# http://10.0.10.14:6666/corpus/WPD/WWW.04738/p265-266
-$t->get_ok(Mojo::URL->new('/matchinfo')->query({
-  corpus_id => 'WPD',
-  doc_id => 'WWW',
-  text_id => '04738',
-  match_id => 'p265-266'
-}))
-  ->status_is(200)
-  ->text_is('#search-result', 'WWW.04738');
-
-$t->get_ok(Mojo::URL->new('/traceasync')->query({q => $query}))
-  ->status_is(200)
-  ->text_is('.notify-error', '')
-  ->text_is('h1', $query)
-  ->text_is('#api', 'http://10.0.10.13:7070/api/v0.1/')
-  ->text_is('#ql', 'poliqarp')
-  ->text_like('#query-jsonld', qr!korap:boundary!);
-
-my $t0 = Benchmark->new;
-
-$t->get_ok(Mojo::URL->new('/searchasync')->query({q => $query}))
-  ->status_is(200)
-  ->text_is('.notify-error', '')
-  ->text_is('h1', $query)
-  ->text_is('#api', 'http://10.0.10.13:7070/api/v0.1/')
-  ->text_is('#cutoff', '')
-  ->text_is('#ql', 'poliqarp')
-  ->text_is('#no_cache', 1)
-  ->text_is('#start_page', 1)
-  ->text_is('#total_results', 54215)
-#  ->text_is('li', 'p265-266')
-  ;
-
-my $t1 = Benchmark->new;
-
-$t->get_ok(Mojo::URL->new('/searchtest')->query({q => $query}))
-  ->status_is(200)
-  ->text_is('.notify-error', '')
-  ->text_is('h1', $query)
-  ->text_is('#api', 'http://10.0.10.13:7070/api/v0.1/')
-  ->text_is('#cutoff', '')
-  ->text_is('#ql', 'poliqarp')
-  ->text_is('#no_cache', 1)
-  ->text_is('#start_page', 1)
-  ->text_is('#total_results', 54215)
-#  ->text_is('li', 'p265-266')
-  ;
-
-diag 'sync  ' . timestr(timediff(Benchmark->new, $t1));
-diag 'async ' . timestr(timediff($t1, $t0));
-
-# Check time_exceeded!
-
-done_testing;
-__END__
diff --git a/t/query.t b/t/query.t
index 97f4997..933276f 100644
--- a/t/query.t
+++ b/t/query.t
@@ -14,11 +14,11 @@
 
 # Mount fake backend
 # Get the fixture path
-my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, 'fixtures');
+my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, 'server');
 my $fake_backend = $t->app->plugin(
   Mount => {
     $mount_point =>
-      $fixtures_path->child('fake_backend.pl')
+      $fixtures_path->child('mock.pl')
   }
 );
 # Configure fake backend
@@ -79,6 +79,15 @@
   ->text_like('div.notify-error:nth-of-type(2)', qr!302: Could not parse query .+? \[orth=das.+?!)
   ;
 
+# Check for query error with ql (from remote.t)
+$t->get_ok('/?q=[orth=das&ql=poliqarp')
+  ->element_exists('.notify-error')
+  ->text_is('.notify-error', '302: Parantheses/brackets unbalanced.')
+  ->content_like(qr!KorAP\.koralQuery =!)
+  ->text_is('.no-results:nth-of-type(1)', 'Unable to perform the search.')
+  ;
+
+
 # Query with partial cache (for total results)
 $t->get_ok('/?q=baum')
   ->status_is(200)
diff --git a/t/remote.t b/t/remote.t
deleted file mode 100644
index a04ca5c..0000000
--- a/t/remote.t
+++ /dev/null
@@ -1,83 +0,0 @@
-use Mojo::Base -strict;
-use Mojo::File qw/path/;
-use Test::More;
-use Test::Mojo;
-
-my $mount_point = '/api/';
-$ENV{KALAMAR_API} = $mount_point;
-
-my $t = Test::Mojo->new('Kalamar');
-$t->app->defaults('auth_support' => 1);
-
-# Mount fake backend
-# Get the fixture path
-my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, 'fixtures');
-my $fake_backend = $t->app->plugin(
-  Mount => {
-    $mount_point =>
-      $fixtures_path->child('fake_backend.pl')
-  }
-);
-
-# Configure fake backend
-$fake_backend->pattern->defaults->{app}->log($t->app->log);
-
-
-
-if (0) {
-
-
-# Check paging
-$t->get_ok('/?q=Baum')
-  ->text_like('h1 span', qr/KorAP: Find .Baum./i)
-  # ->text_is('pre.query.serial span', 'JSON-LD Serialization for "test"')
-  ->text_like('#total-results', qr/\d+$/)
-  ->text_is('#pagination a[rel=self] span', 1)
-  ->element_exists_not('#ql-field option[value=poliqarp][selected]')
-  ->element_exists_not('#ql-field option[value=cosmas2][selected]')
-  ;
-
-# Check paging
-$t->get_ok('/?q=test&p=2&ql=cosmas2')
-  ->text_like('#total-results', qr/\d+$/)
-  ->text_is('#pagination a[rel=self] span', 2)
-  ->element_exists('#ql-field option[value=cosmas2][selected]')
-  ->element_exists_not('#ql-field option[value=poliqarp][selected]')
-;
-
-# Check paging
-$t->get_ok('/?q=test&p=2&ql=cosmas2')
-  ->text_like('#total-results', qr/\d+$/)
-  ->text_is('#pagination a[rel=self] span', 2)
-  ->element_exists('#ql-field option[value=cosmas2][selected]')
-  ->element_exists_not('#ql-field option[value=poliqarp][selected]')
-;
-
-# Check for authorization
-#   this should just trigger a fixture - it's not serious
-$t->get_ok('/?q=test&p=2&ql=cosmas3')
-  ->element_exists_not('#pagination a[rel=self] span')
-  ->element_exists('#search #no-results')
-  ->text_like('.notify-error', qr!No entity found!)
-  ;
-
-
-# Check for server error
-$t->get_ok('/?q=server_fail&ql=poliqarp')
-  ->element_exists('.notify-error')
-  ->text_is('.notify-error', '500: Internal Server Error (remote)')
-  ;
-
-};
-
-
-# Check for query error
-$t->get_ok('/?q=[orth=das&ql=poliqarp')
-  ->element_exists('.notify-error')
-  ->text_is('.notify-error', '302: Parantheses/brackets unbalanced.')
-  ->content_like(qr!KorAP\.koralQuery =!)
-  ->text_is('.no-results:nth-of-type(1)', 'Unable to perform the search.')
-  ;
-
-done_testing;
-__END__
diff --git a/t/fixtures/fake_backend.pl b/t/server/mock.pl
similarity index 97%
rename from t/fixtures/fake_backend.pl
rename to t/server/mock.pl
index 7506d84..18fd05d 100644
--- a/t/fixtures/fake_backend.pl
+++ b/t/server/mock.pl
@@ -12,7 +12,7 @@
 # This is an API fake server with fixtures
 
 my $secret = 's3cr3t';
-my $fixture_path = path(Mojo::File->new(__FILE__)->dirname);
+my $fixture_path = path(Mojo::File->new(__FILE__)->dirname)->child('..', 'fixtures');
 
 helper jwt_encode => sub {
   shift;
diff --git a/t/plugin-server.pl b/t/server/plugin.pl
similarity index 100%
rename from t/plugin-server.pl
rename to t/server/plugin.pl
diff --git a/t/text_info.t b/t/text_info.t
index 7c06bf6..49be159 100644
--- a/t/text_info.t
+++ b/t/text_info.t
@@ -14,11 +14,11 @@
 
 # Mount fake backend
 # Get the fixture path
-my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, 'fixtures');
+my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, 'server');
 my $fake_backend = $t->app->plugin(
   Mount => {
     $mount_point =>
-      $fixtures_path->child('fake_backend.pl')
+      $fixtures_path->child('mock.pl')
   }
 );
 # Configure fake backend
diff --git a/t/user.t b/t/user.t
index baa8967..665a623 100644
--- a/t/user.t
+++ b/t/user.t
@@ -16,11 +16,11 @@
 
 # Mount fake backend
 # Get the fixture path
-my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, 'fixtures');
+my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, 'server');
 my $fake_backend = $t->app->plugin(
   Mount => {
     $mount_point =>
-      $fixtures_path->child('fake_backend.pl')
+      $fixtures_path->child('mock.pl')
   }
 );
 # Configure fake backend