'use strict';

const proxyquire = require('proxyquire');
const assert = require('assert');

let actualMessage;
let actualOpts;
const mockSlack = function(opts) {
    return function(message, opts, callback) {
        actualMessage = message;
        actualOpts = opts;
        process.nextTick(function() {
            callback(null, 'mocked');
        });
    };
};

describe('VersionEyeSlack class', function() {
    let sut;
    let versioneyeApiKey = 'versioneye-api-key-dummy';
    let slackWebhookUrl = 'https://hooks.slack.com/services/dummy/webhook/url';

    beforeEach(function() {
        actualMessage = null;
        actualOpts = null;
    });

    describe('postNotifications', function() {
        const setupSut = function(notifications) {
            const VersionEyeSlack = proxyquire('../../index', {
                'versioneye-api-client': class {
                    constructor(apiKey, opt_baseUri) {
                        this.me = {
                            listNotifications: function() {
                                return Promise.resolve({
                                    "notifications": notifications
                                });
                            }
                        }
                    }
                },
                'slack-incoming-webhook': mockSlack
            });
            sut = new VersionEyeSlack(versioneyeApiKey, slackWebhookUrl);
        };

        it('post unread notifications of VersionEye to Slack', function() {
            setupSut([
                {
                    "read": true,
                    "product": {
                        "prod_key": "read_prod_key",
                        "language": "read_language",
                        "name": "read_name"
                    },
                    "version": "2.0.0"
                },
                {
                    "read": false,
                    "product": {
                        "prod_key": "unread_prod_key",
                        "language": "unread_language",
                        "name": "unread_name"
                    },
                    "version": "1.0.0"
                }
            ]);

            return sut.postNotifications().then(response => {
                assert(actualMessage === 'There are notifications for new releases!');
                assert.deepEqual(actualOpts, {
                    'attachments': [{
                        'text': '<https://www.versioneye.com/unread_language/unread_prod_key|unread_name> (1.0.0)'
                    }]
                });
                assert(response === 'mocked');
            });
        });

        it('not post to slack when no unread notifications', function() {
            setupSut([]);

            return sut.postNotifications().then(response => {
                assert(actualMessage === null);
                assert(actualOpts === null);
                assert(response === 'no notifications');
            });
        });
    });

    describe('postProjectSummary', function() {
        beforeEach(function() {
            const VersionEyeSlack = proxyquire('../../index', {
                'versioneye-api-client': class {
                    constructor(apiKey, opt_baseUri) {
                        this.projects = {
                            list: function() {
                                return Promise.resolve([
                                    {
                                        "id": {
                                            "$oid": "project1-id"
                                        },
                                        "name": "project1"
                                    },
                                    {
                                        "id": {
                                            "$oid": "project2-id"
                                        },
                                        "name": "project2"
                                    }
                                ]);
                            },
                            show: function(projectId) {
                                return Promise.resolve({
                                    "id": projectId,
                                    "out_number_sum": 0,
                                    "licenses_red_sum": 0,
                                    "licenses_unknown_sum": 0,
                                    "sv_count_sum": 0
                                });
                            }
                        }
                    }
                },
                'slack-incoming-webhook': mockSlack
            });
            sut = new VersionEyeSlack(versioneyeApiKey, slackWebhookUrl);
        });

        it('Post a summary for the specific project', function() {
            return sut.postProjectSummary('project1').then(response => {
                assert(actualMessage === 'Project summary for <https://www.versioneye.com/user/projects/project1-id|project1>');
                assert.deepEqual(actualOpts, {
                    'attachments': [
                        {
                            color: 'good',
                            text: 'Outdated: 0'
                        },
                        {
                            color: 'good',
                            text: 'Licenses: 0 : 0'
                        },
                        {
                            color: 'good',
                            text: 'Security: 0'
                        }
                    ]
                });
                assert(response === 'mocked');
            });
        });
    });
});

@@ -5625,29 +5625,333 @@
  %7D);%0A        %7D);
+%0A%0A        it('catch Error when the specific project does not exist', function() %7B%0A            return sut.postProjectSummary('unknown').then(response =%3E %7B%0A                assert(false);%0A            %7D).catch(err =%3E %7B%0A                assert(err.message === 'Not found: unknown');%0A            %7D);%0A        %7D);
 %0A    %7D);%0A%7D);%0A

add a test for postProjectSummary