const models = require('vn-loopback/server/server').models;
const got = require('got');
const stream = require('stream');

describe('docuware download()', () => {
    const userId = 9;
    const ticketId = 1;
    const ctx = {
        req: {

            accessToken: {userId: userId},
            headers: {origin: 'http://localhost:5000'},
        }
    };

    it('should return the downloaded file name', async() => {
        const fileCabinetName = 'deliveryClient';
        const dialogDisplayName = 'find';
        const dialogName = 'findTicket';
        const gotGetResponse = {
            body: JSON.stringify(
                {
                    FileCabinet: [
                        {Id: 12, Name: fileCabinetName}
                    ],
                    Dialog: [
                        {Id: 34, DisplayName: dialogDisplayName}
                    ]
                })
        };

        const gotPostResponse = {
            body: JSON.stringify(
                {
                    Items: [
                        {Id: 56}
                    ],
                })
        };

        spyOn(got, 'get').and.returnValue(new Promise(resolve => resolve(gotGetResponse)));
        spyOn(got, 'post').and.returnValue(new Promise(resolve => resolve(gotPostResponse)));
        spyOn(got, 'stream').and.returnValue(new stream.PassThrough({objectMode: true}));

        const result = await models.Docuware.download(ctx, ticketId, fileCabinetName, dialogName);

        expect(result[1]).toEqual('application/pdf');
        expect(result[2]).toEqual(`filename="${ticketId}.pdf"`);
    });
});