const {
  NODE: { CAMPUS, USER },
  EDGE: { CONTACT, MEMBER, OWNER, UNIT_RISK_ADMIN },
} = require('../constant/relationship/label.constant');
const Helper = require('./relationship.helper');

describe('Relationship Helper', () => {
  it('should get user by role', () => {
    const relationships = [
      {
        edge: {
          id: '1',
          isActive: true,
          label: MEMBER,
        },
        node: {
          id: 'VUN8MDN8ZHBvdHRlckB1Y2RhdmlzLmVkdQ',
          label: USER,
          firstName: 'Daniel',
          lastName: 'Potter',
        },
      },
      {
        edge: {
          id: '2',
          isActive: true,
          label: OWNER,
        },
        node: {
          id: 'VUN8MDN8ZHBvdHRlckB1Y2RhdmlzLmVkdR',
          label: USER,
          firstName: 'Mark',
          lastName: 'Harry',
        },
      },
    ];
    const ownerRoleUser = Helper.getUsersByRoles(relationships, [OWNER]);
    expect(ownerRoleUser[0].userId).toEqual(relationships[1].node.id);
  });

  it('should get user by role when relationships are loaded from neptune', () => {
    const relationships = [
      {
        node: {
          id: 'VUN8MDN8c2FpZ2FubnVAdWNkYXZpcy5lZHU',
          label: 'USER',
          analyticsId: '2506e61745f86bf7ed8a968283ae5b51',
          authId: 'saigannu@ucdavis.edu',
          firstName: 'Saimatha',
          lastName: 'Gannu',
          email: 'saigannu@ucdavis.edu',
          hash: '2d3359ed7af81c2ba51d7b8c8375ebf718172614',
          idp: 'ucdavis.edu',
          uri: 'urn:mace:incommon:ucdavis.edu',
        },
        edge: {
          id: '06b807c0-85af-6c4f-5139-0279cfc515a6',
          label: 'OWNER',
          type: 'ROLE',
          startDate: '2019-10-10T21:38:35.736Z',
          isActive: true,
        },
      },
    ];
    const ownerRoleUser = Helper.getUsersByRoles(relationships, [OWNER]);
    expect(ownerRoleUser && ownerRoleUser.length).toEqual(1);
  });

  it('should return true if there is a relationship with matching id and role', () => {
    const userId = 'VUN8MDN8ZHBvdHRlckB1Y2RhdmlzLmVkdQ';
    const relationships = [
      {
        edge: {
          id: '1',
          isActive: true,
          label: MEMBER,
          primary: false,
          startDate: '2020-09-09T23:04:59.565Z',
          type: 'ROLE',
        },
        node: {
          id: userId,
          label: USER,
          firstName: 'Daniel',
          lastName: 'Potter',
        },
      },
      {
        edge: {
          id: '2',
          isActive: true,
          label: MEMBER,
          primary: false,
          startDate: '2020-02-19T23:04:50.565Z',
          type: 'ROLE',
        },
        node: {
          id: 'VUN8MDN8ZHBvdHRlckB1Y2RhdmlzLmVkdR',
          label: USER,
          firstName: 'Mark',
          lastName: 'Harry',
        },
      },
    ];
    const hasRole = Helper.hasRelationshipWithRole(userId, relationships, MEMBER);
    expect(hasRole).toBeTruthy();
  });

  it('should return true if there is an active relationship with matching id and matches one of the passed in roles', () => {
    const userId = 'VUN8MDN8ZHBvdHRlckB1Y2RhdmlzLmVkdQ';
    const relationships = [
      {
        edge: {
          id: '1',
          isActive: true,
          label: MEMBER,
          primary: false,
          startDate: '2020-09-09T23:04:59.565Z',
          type: 'ROLE',
        },
        node: {
          id: userId,
          label: USER,
          firstName: 'Daniel',
          lastName: 'Potter',
        },
      },
      {
        edge: {
          id: '2',
          isActive: false,
          label: OWNER,
          primary: false,
          startDate: '2020-02-19T23:04:50.565Z',
          endDate: '12/12/2012',
          type: 'ROLE',
        },
        node: {
          id: 'VUN8MDN8ZHBvdHRlckB1Y2RhdmlzLmVkdR',
          label: USER,
          firstName: 'Mark',
          lastName: 'Harry',
        },
      },
    ];
    const hasRole = Helper.hasRelationshipWithRole(userId, relationships, [CONTACT, MEMBER]);
    expect(hasRole).toBeTruthy();
  });

  it('should return true if there is a relationship with matching id for neptune', () => {
    const id = '03';
    const relationships = [
      {
        node: {
          id: '03',
          label: CAMPUS,
          name: 'University of California, Davis',
        },
        edge: {
          id: '7cb7872f-9b39-7458-14cd-6274c9922585',
          label: UNIT_RISK_ADMIN,
          startDate: '2019-12-16T11:01:58.018Z',
          isActive: true,
        },
      },
    ];
    const hasRole = Helper.hasRelationship(relationships, id);
    expect(hasRole).toBeTruthy();
  });
});
