'use strict';
let expect    = require('chai').expect;
const fs      = require('fs');
const request = require('request');
const rq      = require('request-promise-native');
let latest;
let stable;

describe('Test Repository', function () {
    it('Test Repository: latest', done => {
        let text = fs.readFileSync(__dirname + '/../sources-dist.json');
        try {
            latest = JSON.parse(text);
        } catch (e) {
            expect(e).to.be.null;
        }
        done();
    });

    it('Test Repository: stable', done => {
        let text = fs.readFileSync(__dirname + '/../sources-dist-stable.json');
        try {
            stable = JSON.parse(text);
        } catch (e) {
            expect(e).to.be.null;
        }
        done();
    });

    it('Test Repository: compare types', done => {
        this.timeout(120000);
        for (let id in stable) {
            if (stable.hasOwnProperty(id)) {
                expect(id).to.be.equal(id.toLowerCase());
                expect(latest[id], id + ' not in latest but in stable').to.be.not.undefined;
                expect(latest[id].type).to.be.not.undefined;
                expect(latest[id].type).to.be.not.equal('');
                expect(latest[id].type).to.be.equal(stable[id].type);
            }
        }
        let count = 0;
        // compare types with io-package.json
        for (let id in latest) {
            if (latest.hasOwnProperty(id)) {
                expect(id).to.be.equal(id.toLowerCase());
                if (latest[id].meta && latest[id].meta.match(/io-package\.json$/)) {
                    count++;
                    (function (_type, _id) {
                        // console.log('Check "' + _id + '"');
                        request(latest[_id].meta, function (error, response, body) {
                            let pack;
                            try {
                                pack = JSON.parse(body);
                            } catch (e) {
                                console.error('Cannot parse pack "' + _id + '": ' + body);
                                expect(e).to.be.null;
                            }

                            if (pack && pack.common && pack.common.type !== _type) {
                                console.error('Types in "' + _id + '" are not equal: ' + pack.common.type  + ' !== ' + _type);
                            }

                            /*expect(pack).to.be.not.undefined;
                            expect(pack.common).to.be.not.undefined;
                            expect(pack.common.type).to.be.equal(_type);*/

                            if (!--count) {
                                done();
                            }
                            // console.log('Only ' + count + ' urls left');
                        });
                    })(latest[id].type, id);
                }
            }
        }
        if (!count) done();
    });

    it('Test Repository: check latest vs. stable', done => {
        console.log();
        for (let id in latest) {
            if (latest.hasOwnProperty(id) && !stable.hasOwnProperty(id)) {
                console.log('Info: Adapter "' + id + '" is not in stable.')
            }
        }
        done();
    });
	
	async function checkRepos(repos) {
		let error = false;		
		for (let id in repos) {
		    if (!repos.hasOwnProperty(id)) continue;
			let repo = repos[id];
			try{
				let res = await rq(repo.meta, { method: 'GET', json: true });				
				if (res.common.name !== id && id !== 'admin' && id !== 'admin-2') {
					console.error('adapter names are not equal: ' + id  + ' !== ' + res.common.name);
					error = true;
				}
				if (res.common.type !== repo.type) {
					console.info('adapter types are not equal in ' + id  + ': ' + repo.type + ' !== ' + res.common.type);
				}
			}
			catch(err){
				console.error('Meta of adapter ' + id + ': ' + repo.meta + ' not getable');
				error = true;
			}
			if (repo.icon) {
				try{
					let res = await rq(repo.icon, { method: 'GET', json: true });
				}
				catch(err){
					console.error('Icon of adapter ' + id + ': ' + repo.icon + ' not getable');
					error = true;
				}
			}
			//console.info('done with adapter ' + id);
        }
		if (error)
			throw "Error occured, see console output";
	}
	
	it('Test all Packages in latest are loadable via http and name is equal to io-package.json are ', async function () {
		this.timeout(120000);   		
        await checkRepos(latest);
    });
	
	it('Test all Packages in stable are loadable via http and name is equal to io-package.json are ', async function () {
		this.timeout(120000);   		
        await checkRepos(stable);
    });

});

@@ -846,39 +846,8 @@
  %7B%0D%0A
-        this.timeout(120000);%0D%0A
     
@@ -2974,35 +2974,51 @@
  done();%0D%0A    %7D)
+.timeout(120000)
 ;%0D%0A
-
 %0D%0A    it('Test R
@@ -3537,17 +3537,16 @@
 .meta, %7B
- 
 method: 
@@ -3566,16 +3566,11 @@
 true
- 
 %7D);
-%09%09%09%09
 %0D%0A%09%09
@@ -4352,20 +4352,31 @@
 ror)
-%0D%0A%09%09%09
+ %7B%0D%0A            
 throw 
-%22
+'
 Erro
@@ -4408,12 +4408,23 @@
 tput
-%22
+'
 ;%0D%0A
+        %7D%0D%0A
 %09%7D%0D%0A
@@ -4550,38 +4550,8 @@
  %7B%0D%0A
-%09%09this.timeout(120000);   %09%09%0D%0A
     
@@ -4583,24 +4583,40 @@
 st);%0D%0A    %7D)
+.timeout(120000)
 ;%0D%0A%09%0D%0A%09it('T
@@ -4733,38 +4733,8 @@
  %7B%0D%0A
-%09%09this.timeout(120000);   %09%09%0D%0A
     
@@ -4760,28 +4760,44 @@
 s(stable);%0D%0A
-
     %7D)
+.timeout(120000)
 ;%0D%0A%0D%0A%7D);%0D%0A

Fix tests