"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const chai_1 = require("chai");
const shared_1 = require("../../shared");
const server_commands_1 = require("../../../../shared/server-commands");
describe('Test notifications API', function () {
    let server;
    let userNotifications = [];
    let userToken;
    let emails = [];
    before(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(120000);
            const res = yield (0, shared_1.prepareNotificationsTest)(1);
            emails = res.emails;
            userToken = res.userAccessToken;
            userNotifications = res.userNotifications;
            server = res.servers[0];
            yield server.subscriptions.add({ token: userToken, targetUri: 'root_channel@' + server.host });
            for (let i = 0; i < 10; i++) {
                yield server.videos.randomUpload({ wait: false });
            }
            yield (0, server_commands_1.waitJobs)([server]);
        });
    });
    describe('Notification list & count', function () {
        it('Should correctly list notifications', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const { data, total } = yield server.notifications.list({ token: userToken, start: 0, count: 2 });
                (0, chai_1.expect)(data).to.have.lengthOf(2);
                (0, chai_1.expect)(total).to.equal(10);
            });
        });
    });
    describe('Mark as read', function () {
        it('Should mark as read some notifications', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const { data } = yield server.notifications.list({ token: userToken, start: 2, count: 3 });
                const ids = data.map(n => n.id);
                yield server.notifications.markAsRead({ token: userToken, ids });
            });
        });
        it('Should have the notifications marked as read', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const { data } = yield server.notifications.list({ token: userToken, start: 0, count: 10 });
                (0, chai_1.expect)(data[0].read).to.be.false;
                (0, chai_1.expect)(data[1].read).to.be.false;
                (0, chai_1.expect)(data[2].read).to.be.true;
                (0, chai_1.expect)(data[3].read).to.be.true;
                (0, chai_1.expect)(data[4].read).to.be.true;
                (0, chai_1.expect)(data[5].read).to.be.false;
            });
        });
        it('Should only list read notifications', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const { data } = yield server.notifications.list({ token: userToken, start: 0, count: 10, unread: false });
                for (const notification of data) {
                    (0, chai_1.expect)(notification.read).to.be.true;
                }
            });
        });
        it('Should only list unread notifications', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const { data } = yield server.notifications.list({ token: userToken, start: 0, count: 10, unread: true });
                for (const notification of data) {
                    (0, chai_1.expect)(notification.read).to.be.false;
                }
            });
        });
        it('Should mark as read all notifications', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield server.notifications.markAsReadAll({ token: userToken });
                const body = yield server.notifications.list({ token: userToken, start: 0, count: 10, unread: true });
                (0, chai_1.expect)(body.total).to.equal(0);
                (0, chai_1.expect)(body.data).to.have.lengthOf(0);
            });
        });
    });
    describe('Notification settings', function () {
        let baseParams;
        before(() => {
            baseParams = {
                server,
                emails,
                socketNotifications: userNotifications,
                token: userToken
            };
        });
        it('Should not have notifications', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(20000);
                yield server.notifications.updateMySettings({
                    token: userToken,
                    settings: Object.assign(Object.assign({}, (0, shared_1.getAllNotificationsSettings)()), { newVideoFromSubscription: 0 })
                });
                {
                    const info = yield server.users.getMyInfo({ token: userToken });
                    (0, chai_1.expect)(info.notificationSettings.newVideoFromSubscription).to.equal(0);
                }
                const { name, shortUUID } = yield server.videos.randomUpload();
                const check = { web: true, mail: true };
                yield (0, shared_1.checkNewVideoFromSubscription)(Object.assign(Object.assign({}, baseParams), { check, videoName: name, shortUUID, checkType: 'absence' }));
            });
        });
        it('Should only have web notifications', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(20000);
                yield server.notifications.updateMySettings({
                    token: userToken,
                    settings: Object.assign(Object.assign({}, (0, shared_1.getAllNotificationsSettings)()), { newVideoFromSubscription: 1 })
                });
                {
                    const info = yield server.users.getMyInfo({ token: userToken });
                    (0, chai_1.expect)(info.notificationSettings.newVideoFromSubscription).to.equal(1);
                }
                const { name, shortUUID } = yield server.videos.randomUpload();
                {
                    const check = { mail: true, web: false };
                    yield (0, shared_1.checkNewVideoFromSubscription)(Object.assign(Object.assign({}, baseParams), { check, videoName: name, shortUUID, checkType: 'absence' }));
                }
                {
                    const check = { mail: false, web: true };
                    yield (0, shared_1.checkNewVideoFromSubscription)(Object.assign(Object.assign({}, baseParams), { check, videoName: name, shortUUID, checkType: 'presence' }));
                }
            });
        });
        it('Should only have mail notifications', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(20000);
                yield server.notifications.updateMySettings({
                    token: userToken,
                    settings: Object.assign(Object.assign({}, (0, shared_1.getAllNotificationsSettings)()), { newVideoFromSubscription: 2 })
                });
                {
                    const info = yield server.users.getMyInfo({ token: userToken });
                    (0, chai_1.expect)(info.notificationSettings.newVideoFromSubscription).to.equal(2);
                }
                const { name, shortUUID } = yield server.videos.randomUpload();
                {
                    const check = { mail: false, web: true };
                    yield (0, shared_1.checkNewVideoFromSubscription)(Object.assign(Object.assign({}, baseParams), { check, videoName: name, shortUUID, checkType: 'absence' }));
                }
                {
                    const check = { mail: true, web: false };
                    yield (0, shared_1.checkNewVideoFromSubscription)(Object.assign(Object.assign({}, baseParams), { check, videoName: name, shortUUID, checkType: 'presence' }));
                }
            });
        });
        it('Should have email and web notifications', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(20000);
                yield server.notifications.updateMySettings({
                    token: userToken,
                    settings: Object.assign(Object.assign({}, (0, shared_1.getAllNotificationsSettings)()), { newVideoFromSubscription: 1 | 2 })
                });
                {
                    const info = yield server.users.getMyInfo({ token: userToken });
                    (0, chai_1.expect)(info.notificationSettings.newVideoFromSubscription).to.equal(1 | 2);
                }
                const { name, shortUUID } = yield server.videos.randomUpload();
                yield (0, shared_1.checkNewVideoFromSubscription)(Object.assign(Object.assign({}, baseParams), { videoName: name, shortUUID, checkType: 'presence' }));
            });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            shared_1.MockSmtpServer.Instance.kill();
            yield (0, server_commands_1.cleanupTests)([server]);
        });
    });
});
//# sourceMappingURL=notifications-api.js.map