import { TableColumn } from '@/interfaces';
import { ScopeAccess, WithScopeAccess } from '@interfaces/entities/Access';
import { AccessController } from '@/utils/base/AccessController';
import { USER_SCOPES } from '@constants/entities/Scope';
import { getReversedScopes } from '@/utils/entities/UserScope';

describe('ScopeAccessController', () => {
  let userScopes: string, dataToCheck: WithScopeAccess<TableColumn>[];
  beforeEach(() => {
    userScopes = getUserScopes();
    dataToCheck = getData();
  });

  test('ScopeAccessController should throw all data without access info', () => {
    const accessController = new AccessController({ scope: userScopes });
    const access: ScopeAccess = { openedTo: [] };
    dataToCheck = dataToCheck.map((el) => ({ ...el, access }));
    const data = accessController.scopesSift(dataToCheck);
    expect(data.length).toBe(dataToCheck.length);
  });

  test('ScopeAccessController should lock data by "getReversedScopes" to our user scope', () => {
    const accessController = new AccessController({ scope: userScopes });
    const access: ScopeAccess = {
      openedTo: getReversedScopes([USER_SCOPES.CLIENT_SCOPE_1]),
    };
    dataToCheck = dataToCheck.map((el) => ({ ...el, access }));
    const data = accessController.scopesSift(dataToCheck);
    expect(data.length).toBe(0);
  });

  test('ScopeAccessController should lock data, coz user scope is not in "openedTo"', () => {
    const accessController = new AccessController({ scope: userScopes });
    const access: ScopeAccess = { openedTo: [USER_SCOPES.UPR_DIR_ACCESS] };
    dataToCheck = dataToCheck.map((el) => ({ ...el, access }));
    const data = accessController.scopesSift(dataToCheck);
    expect(data.length).toBe(0);
  });
});

function getUserScopes() {
  return 'ClientScope1';
}
function getData(): WithScopeAccess<TableColumn>[] {
  return [
    {
      data: {
        name: 'column',
      },
    },
  ];
}
