<commit_before>const Promise = require('bluebird');
const { strict: assert } = require('assert');
const { expect } = require('chai');
const ld = require('lodash');
const redisKey = require('../../../src/utils/key');

const getUserName = (audience) => (data) => data.metadata[audience].username;

const sortByCaseInsensitive = (getMember) => (list) => list
  .sort((a, b) => getMember(a).toLowerCase() < getMember(b).toLowerCase());

describe('Redis Search: list', function listSuite() {
  this.timeout(50000);

  const ctx = {
    redisSearch: {
      enabled: true,
    },
  };

  const totalUsers = 105;
  const { faker } = require('@faker-js/faker');

  beforeEach(async function startService() {
    await global.startService.call(this, ctx);
  });
  afterEach('reset redis', global.clearRedis);

  beforeEach('populate redis', function populateRedis() {
    const audience = this.users.config.jwt.defaultAudience;
    const promises = [];
    const { USERS_INDEX, USERS_METADATA } = require('../../../src/constants');

    ld.times(totalUsers, () => {
      const user = {
        id: this.users.flake.next(),
        metadata: {
          username: faker.internet.email(),
          firstName: faker.name.firstName(),
          lastName: faker.name.lastName(),
        },
      };

      promises.push((
        this.users.redis
          .pipeline()
          .sadd(USERS_INDEX, user.id)
          .hmset(
            redisKey(user.id, USERS_METADATA, audience),
            ld.mapValues(user.metadata, JSON.stringify.bind(JSON))
          )
          .exec()
      ));
    });

    this.audience = audience;
    this.userStubs = Promise.all(promises);
    return this.userStubs;
  });

  it('responds with error when index not created', async function test() {
    const query = {
      params: {
        audience: 'not-existing-audience',
      },
    };

    await assert.rejects(
      this.users.dispatch('list', query),
      /Search index does not exist/
    );
  });

  it('list by username', function test() {
    return this
      .users<commit_msg>test: correct expected error message<commit_after>const Promise = require('bluebird');
const { strict: assert } = require('assert');
const { expect } = require('chai');
const ld = require('lodash');
const redisKey = require('../../../src/utils/key');

const getUserName = (audience) => (data) => data.metadata[audience].username;

const sortByCaseInsensitive = (getMember) => (list) => list
  .sort((a, b) => getMember(a).toLowerCase() < getMember(b).toLowerCase());

describe('Redis Search: list', function listSuite() {
  this.timeout(50000);

  const ctx = {
    redisSearch: {
      enabled: true,
    },
  };

  const totalUsers = 105;
  const { faker } = require('@faker-js/faker');

  beforeEach(async function startService() {
    await global.startService.call(this, ctx);
  });
  afterEach('reset redis', global.clearRedis);

  beforeEach('populate redis', function populateRedis() {
    const audience = this.users.config.jwt.defaultAudience;
    const promises = [];
    const { USERS_INDEX, USERS_METADATA } = require('../../../src/constants');

    ld.times(totalUsers, () => {
      const user = {
        id: this.users.flake.next(),
        metadata: {
          username: faker.internet.email(),
          firstName: faker.name.firstName(),
          lastName: faker.name.lastName(),
        },
      };

      promises.push((
        this.users.redis
          .pipeline()
          .sadd(USERS_INDEX, user.id)
          .hmset(
            redisKey(user.id, USERS_METADATA, audience),
            ld.mapValues(user.metadata, JSON.stringify.bind(JSON))
          )
          .exec()
      ));
    });

    this.audience = audience;
    this.userStubs = Promise.all(promises);
    return this.userStubs;
  });

  it('responds with error when index not created', async function test() {
    const query = {
      params: {
        audience: 'not-existing-audience',
      },
    };

    await assert.rejects(
      this.users.dispatch('list', query),
      /Search index does not registered for/
    );
  });

  it('list by username', function test() {
    return this
      .users
{'fraction_non_alphanumeric': 0.1558597502401537, 'fraction_numerical': 0.0038424591738712775, 'mean_word_length': 2.640734265734266, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3812188', 'n_tokens_mistral': 1309, 'n_tokens_neox': 1282, 'n_words': 266}