/**
* @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 resolve = require( 'path' ).resolve;
var tape = require( 'tape' );
var proxyquire = require( 'proxyquire' );
var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives;
var isArray = require( '@stdlib/assert/is-array' );
var findREADMEs = require( './../lib/sync.js' );


// VARIABLES //

var dir = resolve( __dirname, '..', '..' );


// TESTS //

tape( 'main export is a function', function test( t ) {
	t.ok( true, __filename );
	t.strictEqual( typeof findREADMEs, 'function', 'main export is a function' );
	t.end();
});

tape( 'the function throws an error if provided an invalid option', function test( t ) {
	t.throws( foo, TypeError, 'throws error' );
	t.end();
	function foo() {
		var opts = {
			'dir': null
		};
		findREADMEs( opts );
	}
});

tape( 'the function returns a string array (if at least one package README is found)', function test( t ) {
	var findREADMEs;
	var count;
	var files;
	var opts;

	findREADMEs = proxyquire( './../lib/sync.js', {
		'glob': {
			'sync': glob
		},
		'@stdlib/fs/exists': {
			'sync': exists
		}
	});

	count = 0;
	opts = {
		'dir': dir
	};
	files = findREADMEs( opts );

	t.strictEqual( isStringArray( files ), true, 'returns a string array' );
	t.strictEqual( files.length, 1, 'expected length' );

	t.end();

	function glob() {
		return [
			'/beep/boop/package.json',
			'/a/b/c/d/e/f/g/h/beep/boop/bop/package.json'
		];
	}

	function exists() {
		count += 1;
		if ( count < 2 ) {
			return true;
		}
		return false;
	}
});

tape( 'the function returns an empty array if unable to find any package READMEs', function test( t ) {
	var findREADMEs;
	var files;

	findREADMEs = proxyquire( './../lib/sync.js', {
		'glob': {
			'sync': glob
		},
		'@stdlib/fs/exists': {
			'sync': exists
		}
	});

	files = findREADMEs();

	t.strictEqual( isArray( files ), true, 'returns an array' );
	t.strictEqual( files.length, 0, 'returns an empty array' );

	t.end();

	function glob() {
		return [ '/beep/boop/package.json' ];
	}

	function exists() {
		return false;
	}
});

tape( 'the function returns an empty array if unable to resolve any packages', function test( t ) {
	var findREADMEs;
	var files;

	findREADMEs = proxyquire( './../lib/sync.js', {
		'glob': {
			'sync': glob
		}
	});

	files = findREADMEs();

	t.strictEqual( isArray( files ), true, 'returns an array' );
	t.strictEqual( files.length, 0, 'returns an empty array' );

	t.end();

	function glob() {
		return [];
	}
});
