import {
  createEvent,
  createTagsByInvestigationId,
  getEventCountByInvestigationId,
  getEventsByInvestigationId,
  getFileByEventId,
  getPDF,
  getTagsByInvestigationId,
  updateEvent,
  updateTagsByInvestigationId,
  uploadFile,
} from '../../api/icat/icatPlus';
import ICATPLUS from '../../config/icat/icatPlus';

describe('Tests URL generation used to hit ICAT+', () => {
  describe('Events', () => {
    it('getEventsByInvestigationId()', () => {
      expect(getEventsByInvestigationId('mySessionId', '0123456')).toEqual(
        `${ICATPLUS.server}/logbook/mySessionId/investigation/id/0123456/event/query`
      );
    });

    it('getEventCountByInvestigationId()', () => {
      expect(getEventCountByInvestigationId('mySessionId', '0123456')).toEqual(
        `${ICATPLUS.server}/logbook/mySessionId/investigation/id/0123456/event/count`
      );
    });

    it('updateEvent()', () => {
      expect(updateEvent('mySessionId', '0123456')).toEqual(
        `${ICATPLUS.server}/logbook/mySessionId/investigation/id/0123456/event/update`
      );
    });

    it('createEvent()', () => {
      expect(createEvent('mySessionId', '0123456')).toEqual(
        `${ICATPLUS.server}/logbook/mySessionId/investigation/id/0123456/event/create`
      );
    });

    it('uploadFile()', () => {
      expect(uploadFile('mySessionId', '0123456')).toEqual(
        `${ICATPLUS.server}/resource/mySessionId/file/investigation/id/0123456/upload`
      );
    });

    it('getFileByEventId', () => {
      expect(getFileByEventId('mySessionId', '0123456', 'myEventId')).toEqual(
        `${ICATPLUS.server}/resource/mySessionId/file/id/myEventId/investigation/id/0123456/download`
      );
    });

    it('getPDF()', () => {
      const actualSessionId = 'mySessionId';
      const actualInvestigationId = '0123456';
      const actualSelectionFilter = {
        find: {
          $and: [
            {
              $or: [{ type: 'annotation' }, { type: 'notification' }],
            },
          ],
        },
        sort: { creationDate: -1 },
        skip: 0,
        limit: 400,
      };

      const expectedURL =
        `${ICATPLUS.server}/logbook/mySessionId/investigation/id/0123456/event/pdf` +
        `?find=${JSON.stringify(
          actualSelectionFilter.find
        )}&sort=${JSON.stringify(
          actualSelectionFilter.sort
        )}&skip=${JSON.stringify(
          actualSelectionFilter.skip
        )}&limit=${JSON.stringify(actualSelectionFilter.limit)}`;
      expect(
        getPDF(actualSessionId, actualInvestigationId, actualSelectionFilter)
      ).toEqual(expectedURL);
    });
  });

  describe('Tags', () => {
    it('getTagsByInvestigationId()', () => {
      expect(getTagsByInvestigationId('sessionIdTest', '123456789')).toEqual(
        `${ICATPLUS.server}/logbook/sessionIdTest/investigation/id/123456789/tag`
      );
    });

    it('createTagsByInvestigationId()', () => {
      expect(createTagsByInvestigationId('sessionIdTest', '123456789')).toEqual(
        `${ICATPLUS.server}/logbook/sessionIdTest/tag/create`
      );
    });

    it('updateTagsByInvestigationId()', () => {
      expect(
        updateTagsByInvestigationId('sessionIdTest', '123456789', 'tagId')
      ).toEqual(
        `${ICATPLUS.server}/logbook/sessionIdTest/investigation/id/123456789/tag/id/tagId/tag/update`
      );
    });
  });
});
