/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var tape = require( 'tape' );
var validate = require( './../lib/validate.js' );


// TESTS //

tape( 'main export a validation function', function test( t ) {
	t.equal( typeof validate, 'function', 'main export is a function' );
	t.end();
});

tape( 'if an options argument is not an object, the function returns a type error', function test( t ) {
	var values;
	var err;
	var i;

	values = [
		'5',
		5,
		NaN,
		null,
		void 0,
		true,
		false,
		[],
		function noop() {}
	];

	for ( i = 0; i < values.length; i++ ) {
		err = validate( {}, values[ i ] );
		t.equal( err instanceof TypeError, true, 'returns type error when provided ' + values[i] );
	}
	t.end();
});

tape( 'if provided a `protocol` option which is not a primitive string, the function returns a type error', function test( t ) {
	var values;
	var opts;
	var err;
	var i;

	values = [
		5,
		NaN,
		null,
		void 0,
		true,
		false,
		[],
		{},
		function noop() {}
	];

	for ( i = 0; i < values.length; i++ ) {
		opts = {
			'protocol': values[ i ]
		};
		err = validate( {}, opts );
		t.equal( err instanceof TypeError, true, 'returns type error when provided ' + values[i] );
	}
	t.end();
});

tape( 'if provided a `protocol` option which is not either `https` or `http`, the function returns an error', function test( t ) {
	var values;
	var opts;
	var err;
	var i;

	values = [
		'beep',
		'boop',
		'bap',
		'ftp',
		'smtp',
		'udp'
	];

	for ( i = 0; i < values.length; i++ ) {
		opts = {
			'protocol': values[ i ]
		};
		err = validate( {}, opts );
		t.equal( err instanceof Error, true, 'returns an error when provided ' + values[i] );
	}
	t.end();
});

tape( 'if provided a `hostname` option which is not a primitive string, the function returns a type error', function test( t ) {
	var values;
	var opts;
	var err;
	var i;

	values = [
		5,
		NaN,
		null,
		void 0,
		true,
		false,
		[],
		{},
		function noop() {}
	];

	for ( i = 0; i < values.length; i++ ) {
		opts = {
			'hostname': values[ i ]
		};
		err = validate( {}, opts );
		t.equal( err instanceof TypeError, true, 'returns type error when provided ' + values[i] );
	}
	t.end();
});

tape( 'if provided a `port` option which is not a nonnegative integer, the function returns a type error', function test( t ) {
	var values;
	var opts;
	var err;
	var i;

	values = [
		'5',
		3.14,
		-1,
		NaN,
		null,
		void 0,
		true,
		false,
		[],
		{},
		function noop() {}
	];

	for ( i = 0; i < values.length; i++ ) {
		opts = {
			'port': values[ i ]
		};
		err = validate( {}, opts );
		t.equal( err instanceof TypeError, true, 'returns type error when provided ' + values[i] );
	}
	t.end();
});

tape( 'if provided a `pathname` option which is not a primitive string, the function returns a type error', function test( t ) {
	var values;
	var opts;
	var err;
	var i;

	values = [
		5,
		NaN,
		null,
		void 0,
		true,
		false,
		[],
		{},
		function noop() {}
	];

	for ( i = 0; i < values.length; i++ ) {
		opts = {
			'pathname': values[ i ]
		};
		err = validate( {}, opts );
		t.equal( err instanceof TypeError, true, 'returns type error when provided ' + values[i] );
	}
	t.end();
});

tape( 'if provided a `page` option which is not a positive integer, the function returns a type error', function test( t ) {
	var values;
	var opts;
	var err;
	var i;

	values = [
		'5',
		-1,
		3.14,
		NaN,
		null,
		void 0,
		true,
		false,
		[],
		{},
		function noop() {}
	];

	for ( i = 0; i < values.length; i++ ) {
		opts = {
			'page': values[ i ]
		};
		err = validate( {}, opts );
		t.equal( err instanceof TypeError, true, 'returns type error when provided ' + values[i] );
	}
	t.end();
});

tape( 'if provided a `last_page` option which is not a positive integer, the function returns a type error', function test( t ) {
	var values;
	var opts;
	var err;
	var i;

	values = [
		'5',
		-1,
		3.14,
		NaN,
		null,
		void 0,
		true,
		false,
		[],
		{},
		function noop() {}
	];

	for ( i = 0; i < values.length; i++ ) {
		opts = {
			'last_page': values[ i ]
		};
		err = validate( {}, opts );
		t.equal( err instanceof TypeError, true, 'returns type error when provided ' + values[i] );
	}
	t.end();
});

tape( 'if provided a `per_page` option which is not a positive integer, the function returns a type error', function test( t ) {
	var values;
	var opts;
	var err;
	var i;

	values = [
		'5',
		-1,
		3.14,
		NaN,
		null,
		void 0,
		true,
		false,
		[],
		{},
		function noop() {}
	];

	for ( i = 0; i < values.length; i++ ) {
		opts = {
			'per_page': values[ i ]
		};
		err = validate( {}, opts );
		t.equal( err instanceof TypeError, true, 'returns type error when provided ' + values[i] );
	}
	t.end();
});

tape( 'if provided a `query` option which is not a primitive string, the function returns a type error', function test( t ) {
	var values;
	var opts;
	var err;
	var i;

	values = [
		5,
		NaN,
		null,
		void 0,
		true,
		false,
		[],
		{},
		function noop() {}
	];

	for ( i = 0; i < values.length; i++ ) {
		opts = {
			'query': values[ i ]
		};
		err = validate( {}, opts );
		t.equal( err instanceof TypeError, true, 'returns type error when provided ' + values[i] );
	}
	t.end();
});

tape( 'if provided a `token` option which is not a primitive string, the function returns a type error', function test( t ) {
	var values;
	var opts;
	var err;
	var i;

	values = [
		5,
		NaN,
		null,
		void 0,
		true,
		false,
		[],
		{},
		function noop() {}
	];

	for ( i = 0; i < values.length; i++ ) {
		opts = {
			'token': values[ i ]
		};
		err = validate( {}, opts );
		t.equal( err instanceof TypeError, true, 'returns type error when provided ' + values[i] );
	}
	t.end();
});

tape( 'if provided an `accept` option which is not a primitive string, the function returns a type error', function test( t ) {
	var values;
	var opts;
	var err;
	var i;

	values = [
		5,
		NaN,
		null,
		void 0,
		true,
		false,
		[],
		{},
		function noop() {}
	];

	for ( i = 0; i < values.length; i++ ) {
		opts = {
			'accept': values[ i ]
		};
		err = validate( {}, opts );
		t.equal( err instanceof TypeError, true, 'returns type error when provided ' + values[i] );
	}
	t.end();
});

tape( 'if provided a `useragent` option which is not a primitive string, the function returns a type error', function test( t ) {
	var values;
	var opts;
	var err;
	var i;

	values = [
		5,
		NaN,
		null,
		void 0,
		true,
		false,
		[],
		{},
		function noop() {}
	];

	for ( i = 0; i < values.length; i++ ) {
		opts = {
			'useragent': values[ i ]
		};
		err = validate( {}, opts );
		t.equal( err instanceof TypeError, true, 'returns type error when provided ' + values[i] );
	}
	t.end();
});

tape( 'the function returns `null` if all options are valid', function test( t ) {
	var options;
	var opts;
	var err;

	opts = {};
	options = {
		'protocol': 'http',
		'hostname': 'beep.com',
		'port': 8080,
		'pathname': '/api/v3',
		'page': 2,
		'last_page': 5,
		'per_page': 30,
		'query': 'beep=boop',
		'token': 'abcdefg',
		'accept': 'beepboop',
		'useragent': 'beeper-booper'
	};

	err = validate( opts, options );
	t.strictEqual( err, null, 'returns null' );
	t.deepEqual( opts, options, 'sets options' );

	t.end();
});

tape( 'the function ignores unrecognized options', function test( t ) {
	var options;
	var opts;
	var err;

	opts = {};
	options = {
		'beep': 'boop',
		'a': 5,
		'b': null,
		'c': 'woot'
	};

	err = validate( opts, options );
	t.strictEqual( err, null, 'returns null' );
	t.deepEqual( opts, {}, 'ignores unrecognized options' );

	t.end();
});
