#!/usr/bin/env node

'use strict';

const rpcEmulatorModel = require('../orwpt-list-all').rpcEmulatorModel;
const unitTestUtils = require('./unitTestUtils');

describe('ORWPT LIST ALL asynchronous specs', () => {
    let rpc;

    beforeAll(() => {
        const res = unitTestUtils.beforeAllScript(rpcEmulatorModel);
        rpc = res;
    });

    const testReturnsArray = (res) => {
        expect(res).toBeDefined();
        expect(res).toEqual(jasmine.any(Array));
        if (res.length > 0) {
            const record = res[0].split('^');
            expect(record.length >= 3).toEqual(true);
            expect(parseInt(record[0], 10)).toEqual(jasmine.any(Number));
            expect(record[1]).toEqual(jasmine.any(String));
        }
    };

    const TEST_INPUT = [{
        name: 'term search',
        serviceOutput: [
            { id: 'urn:vista:999:2:11', name: 'TWENTY,PATIENT T' },
            { id: 'urn:vista:999:2:12', name: 'TWENTYFOUR,PATIENT X' },
            { id: 'urn:vista:999:2:21', name: 'TWENTYONE,PATIENT U' },
            { id: 'urn:vista:999:2:13', name: 'TWENTYTHREE,PATIENT W' },
            { id: 'urn:vista:999:2:7', name: 'TWENTYTWO,PATIENT V' },
            { id: 'urn:vista:999:2:10', name: 'TWO,PATIENT B' },
        ],
        results: [
            '11^TWENTY,PATIENT T^^^^TWENTY,PATIENT T',
            '12^TWENTYFOUR,PATIENT X^^^^TWENTYFOUR,PATIENT X',
            '21^TWENTYONE,PATIENT U^^^^TWENTYONE,PATIENT U',
            '13^TWENTYTHREE,PATIENT W^^^^TWENTYTHREE,PATIENT W',
            '7^TWENTYTWO,PATIENT V^^^^TWENTYTWO,PATIENT V',
            '10^TWO,PATIENT B^^^^TWO,PATIENT B',
        ],
    }, {
        name: 'empty search term',
        serviceOutput: [
            { id: 'urn:vista:999:2:25', name: 'CARTER,DAVID' },
            { id: 'urn:vista:999:2:17', name: 'EIGHT,PATIENT H' },
            { id: 'urn:vista:999:2:22', name: 'EIGHTEEN,PATIENT R' },
            { id: 'urn:vista:999:2:5', name: 'ELEVEN,PATIENT K' },
            { id: 'urn:vista:999:2:18', name: 'FIFTEEN,PATIENT O' },
            { id: 'urn:vista:999:2:20', name: 'FIVE,PATIENT E' },
            { id: 'urn:vista:999:2:24', name: 'FOUR,PATIENT D' },
            { id: 'urn:vista:999:2:1', name: 'FOURTEEN,PATIENT N' },
            { id: 'urn:vista:999:2:4', name: 'NINE,PATIENT I' },
            { id: 'urn:vista:999:2:14', name: 'NINETEEN,PATIENT S' },
            { id: 'urn:vista:999:2:3', name: 'ONE,PATIENT A' },
            { id: 'urn:vista:999:2:2', name: 'SEVEN,PATIENT G' },
            { id: 'urn:vista:999:2:15', name: 'SEVENTEEN,PATIENT Q' },
            { id: 'urn:vista:999:2:6', name: 'SIX,PATIENT F' },
            { id: 'urn:vista:999:2:16', name: 'SIXTEEN,PATIENT P' },
            { id: 'urn:vista:999:2:8', name: 'TEN,PATIENT J' },
            { id: 'urn:vista:999:2:23', name: 'THIRTEEN,PATIENT M' },
            { id: 'urn:vista:999:2:9', name: 'THREE,PATIENT C' },
            { id: 'urn:vista:999:2:19', name: 'TWELVE,PATIENT L' },
            { id: 'urn:vista:999:2:11', name: 'TWENTY,PATIENT T' },
            { id: 'urn:vista:999:2:12', name: 'TWENTYFOUR,PATIENT X' },
            { id: 'urn:vista:999:2:21', name: 'TWENTYONE,PATIENT U' },
            { id: 'urn:vista:999:2:13', name: 'TWENTYTHREE,PATIENT W' },
            { id: 'urn:vista:999:2:7', name: 'TWENTYTWO,PATIENT V' },
            { id: 'urn:vista:999:2:10', name: 'TWO,PATIENT B' },
        ],
        results: [
            '25^CARTER,DAVID^^^^CARTER,DAVID',
            '17^EIGHT,PATIENT H^^^^EIGHT,PATIENT H',
            '22^EIGHTEEN,PATIENT R^^^^EIGHTEEN,PATIENT R',
            '5^ELEVEN,PATIENT K^^^^ELEVEN,PATIENT K',
            '18^FIFTEEN,PATIENT O^^^^FIFTEEN,PATIENT O',
            '20^FIVE,PATIENT E^^^^FIVE,PATIENT E',
            '24^FOUR,PATIENT D^^^^FOUR,PATIENT D',
            '1^FOURTEEN,PATIENT N^^^^FOURTEEN,PATIENT N',
            '4^NINE,PATIENT I^^^^NINE,PATIENT I',
            '14^NINETEEN,PATIENT S^^^^NINETEEN,PATIENT S',
            '3^ONE,PATIENT A^^^^ONE,PATIENT A',
            '2^SEVEN,PATIENT G^^^^SEVEN,PATIENT G',
            '15^SEVENTEEN,PATIENT Q^^^^SEVENTEEN,PATIENT Q',
            '6^SIX,PATIENT F^^^^SIX,PATIENT F',
            '16^SIXTEEN,PATIENT P^^^^SIXTEEN,PATIENT P',
            '8^TEN,PATIENT J^^^^TEN,PATIENT J',
            '23^THIRTEEN,PATIENT M^^^^THIRTEEN,PATIENT M',
            '9^THREE,PATIENT C^^^^THREE,PATIENT C',
            '19^TWELVE,PATIENT L^^^^TWELVE,PATIENT L',
            '11^TWENTY,PATIENT T^^^^TWENTY,PATIENT T',
            '12^TWENTYFOUR,PATIENT X^^^^TWENTYFOUR,PATIENT X',
            '21^TWENTYONE,PATIENT U^^^^TWENTYONE,PATIENT U',
            '13^TWENTYTHREE,PATIENT W^^^^TWENTYTHREE,PATIENT W',
            '7^TWENTYTWO,PATIENT V^^^^TWENTYTWO,PATIENT V',
            '10^TWO,PATIENT B^^^^TWO,PATIENT B',
        ],
    }, {
        name: 'no results',
        serviceOutput: [],
        results: [],
    }];

    TEST_INPUT.forEach((testCase) => {
        it(`formatResults: should return an appropriately formatted list of results for ${testCase.name}`, () => {
            const res = rpc.formatResult(testCase.serviceOutput);
            testReturnsArray(res);
            res.forEach((line, index) => {
                expect(line).toEqual(testCase.results[index]);
            });
        });
    });
});
