'use strict';

const App = require('../index');
const logger = require('paradox-logger')('test');
const request = require('request-promise');
const fs = require('fs');
const should = require('should');

const assert = require('chai').assert;

describe('app', function() {

    let app;
    let port;

    before(() => {
        app = require('./app.class');

        return app
            .start()
            .then(() => {
                port = app.express_server.address().port;
            })
            .catch(err => {
                logger.exception(err);
            });
    });

    it('api controladores', () => {
        let options = {
            uri: 'http://localhost:' + port + '/admin/api/test',
            json: true,
        };

        return request(options)
            .then(data => {
                assert.equal(data, 1234);
            });
    });

    it('api endpoints', () => {
        let options = {
            uri: 'http://localhost:' + port + '/api/health',
            json: true,
        };

        return request(options)
            .then(data => {
                assert.equal(data, 'ok');
            });
    });

    it('static', () => {
        let options = {
            uri: 'http://localhost:' + port + '/sandbox/test.json',
            json: true,
        };

        return request(options)
            .then(data => {
                assert.equal(data, 1234);
            });
    });

    it('controller with namespace', () => {
        let options = {
            uri: 'http://localhost:' + port + '/api/image/fetch/start',
            json: true,
        };

        return request(options)
            .then(data => {
                assert.equal(data.success, true);
                assert.equal(data.result, 1234);
            });
    });

    it('controller without namespace', () => {
        let options = {
            uri: 'http://localhost:' + port + '/api/image-fetch/start',
            json: true,
        };

        return request(options)
            .then(data => {
                assert.equal(data.success, true);
                assert.equal(data.result, 1234);
            });
    });

    it('should receive files', () => {
        let options = {
            uri: 'http://localhost:' + port + '/api/image/fetch/upload',
            json: true,
            formData: {
                attachments: [
                    fs.createReadStream('./image.jpg'),
                    fs.createReadStream('./image.jpg'),
                ],
                field: 'test',
                field_json: JSON.stringify({
                    test: [
                        1, 2, 3
                    ]
                }),
                custom_file: {
                    value: fs.createReadStream('./image.jpg'),
                    options: {
                        filename: 'test.jpg',
                        contentType: 'image/jpeg',
                    }
                }
            }
        };

        return request(options)
            .then(data => {
                assert.equal(data.success, true);

                assert.equal(data.result.files.length, 3);
                assert.equal(Object.values(data.result.query).length, 2);

                (data.result.query.field_json).should.be.a.Object();
                (data.result.query.field_json.test).should.be.a.Array().of.length(3);
            });
    });

    it('admin', () => {
        let options = {
            uri: 'http://localhost:' + port + '/admin/',
        };

        return request(options)
            .then(data => {
                assert.include(data, 'Test | Admin');
            });
    });

    // it('ng-admin', () => {
    //     let options = {
    //         uri: 'http://localhost:' + port + '/admin/ng-admin/build/ng-admin.min.css',
    //     };
    //
    //     return request(options)
    //         .then(data => {
    //             assert.include(data, 'css');
    //         });
    // });
});