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

describe('Client Model', () => {
    const activeCtx = {
        accessToken: {userId: 9},
        http: {
            req: {
                headers: {origin: 'http://localhost'},
                [`__`]: value => {
                    return value;
                }
            }
        }
    };
    const ctx = {req: activeCtx};
    const chatModel = app.models.Chat;
    const client = {id: 101, name: 'Bruce Banner'};
    const previousWorkerId = 106; // DavidCharlesHaller
    const currentWorkerId = 107; // HankPym

    beforeEach(() => {
        spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
            active: activeCtx
        });
    });

    describe('notifyAssignment()', () => {
        it('should call to the Chat send() method for both workers', async() => {
            spyOn(chatModel, 'send').and.callThrough();

            await app.models.Client.notifyAssignment(client, previousWorkerId, currentWorkerId);

            expect(chatModel.send).toHaveBeenCalledWith(ctx, '@DavidCharlesHaller', `Client assignment has changed`);
            expect(chatModel.send).toHaveBeenCalledWith(ctx, '@HankPym', `Client assignment has changed`);
        });

        it('should call to the Chat send() method for the previous worker', async() => {
            spyOn(chatModel, 'send').and.callThrough();

            await app.models.Client.notifyAssignment(client, null, currentWorkerId);

            expect(chatModel.send).toHaveBeenCalledWith(ctx, '@HankPym', `Client assignment has changed`);
        });

        it('should call to the Chat send() method for the current worker', async() => {
            spyOn(chatModel, 'send').and.callThrough();

            await app.models.Client.notifyAssignment(client, previousWorkerId, null);

            expect(chatModel.send).toHaveBeenCalledWith(ctx, '@DavidCharlesHaller', `Client assignment has changed`);
        });
    });
});