const app = require('vn-loopback/server/server');

describe('Chat sendCheckingPresence()', () => {
    const today = new Date();
    today.setHours(6, 0);
    const ctx = {req: {accessToken: {userId: 1}}};
    const chatModel = app.models.Chat;
    const departmentId = 23;
    const workerId = 107;

    it(`should call send() method with the worker name if he's currently working then return a response`, async() => {
        spyOn(chatModel, 'send').and.callThrough();

        const timeEntry = await app.models.WorkerTimeControl.create({
            userFk: workerId,
            timed: today,
            manual: false,
            direction: 'in'
        });

        const response = await chatModel.sendCheckingPresence(ctx, workerId, 'I changed something');

        expect(response.statusCode).toEqual(200);
        expect(response.message).toEqual('Fake notification sent');
        expect(chatModel.send).toHaveBeenCalledWith(ctx, '@HankPym', 'I changed something');

        // restores
        await app.models.WorkerTimeControl.destroyById(timeEntry.id);
    });

    it(`should call to send() method with the worker department channel if he's not currently working then return a response`, async() => {
        spyOn(chatModel, 'send').and.callThrough();

        const department = await app.models.Department.findById(departmentId);
        await department.updateAttribute('chatName', 'cooler');

        const response = await chatModel.sendCheckingPresence(ctx, workerId, 'I changed something');

        expect(response.statusCode).toEqual(200);
        expect(response.message).toEqual('Fake notification sent');
        expect(chatModel.send).toHaveBeenCalledWith(ctx, '#cooler', '@HankPym ➔ I changed something');

        // restores
        await department.updateAttribute('chatName', null);
    });
});