<commit_before>'use strict';

const validation = require('../validation');

describe('aggregation validation', () => {
  it('should throw on wrong config object', async () => {
    const err = /^aggregation configuration must be an object$/;
    await expect(() => validation.aggregation()).toThrow(err);
    await expect(() => validation.aggregation(42)).toThrow(err);
    await expect(() => validation.aggregation(null)).toThrow(err);
  });

  it('should throw if collection is not a string', () => {
    return expect(() => validation.aggregation({ collection: 42 })).toThrow(
      /^config\.collection must be a string$/
    );
  });

  it('should throw if sources is not an object', async () => {
    const err = /^config\.sources must be an object$/;
    await expect(() =>
      validation.aggregation({ collection: 'dummy', sources: null })
    ).toThrow(err);
    await expect(() =>
      validation.aggregation({ collection: 'dummy', sources: 42 })
    ).toThrow(err);
  });

  it('should throw if chunkSize is not an integer', async () => {
    const err = /^config\.chunkSize must be a positive integer$/;
    await expect(() =>
      validation.aggregation({
        collection: 'dummy',
        sources: { a: 1 },
        chunkSize: 0.1
      })
    ).toThrow(err);
    await expect(() =>
      validation.aggregation({
        collection: 'dummy',
        sources: { a: 1 },
        chunkSize: -1
      })
    ).toThrow(err);
    await expect(() =>
      validation.aggregation({
        collection: 'dummy',
        sources: { a: 1 },
        chunkSize: 0
      })
    ).toThrow(err);
  });

  it('should throw if sources is an empty object', () => {
    return expect(() =>
      validation.aggregation({ collection: 'dummy', sources: {} })
    ).toThrow(/^config\.sources must have at least one source$/);
  });

  it('should throw if sources properties are not functions', () => {
    return expect(() =>
      validation.aggregation({
        collection: 'dummy',
        sources: {
          source1: 'abc'
        }
      })
    ).toThrow(
      'all sources in the aggregation config should be functions (source1)'
    );
  });
});<commit_msg>add test: aggregation config validation should return new object<commit_after>'use strict';

const validation = require('../validation');

describe('aggregation validation', () => {
  it('should throw on wrong config object', async () => {
    const err = /^aggregation configuration must be an object$/;
    await expect(() => validation.aggregation()).toThrow(err);
    await expect(() => validation.aggregation(42)).toThrow(err);
    await expect(() => validation.aggregation(null)).toThrow(err);
  });

  it('should throw if collection is not a string', () => {
    return expect(() => validation.aggregation({ collection: 42 })).toThrow(
      /^config\.collection must be a string$/
    );
  });

  it('should throw if sources is not an object', async () => {
    const err = /^config\.sources must be an object$/;
    await expect(() =>
      validation.aggregation({ collection: 'dummy', sources: null })
    ).toThrow(err);
    await expect(() =>
      validation.aggregation({ collection: 'dummy', sources: 42 })
    ).toThrow(err);
  });

  it('should throw if chunkSize is not an integer', async () => {
    const err = /^config\.chunkSize must be a positive integer$/;
    await expect(() =>
      validation.aggregation({
        collection: 'dummy',
        sources: { a: 1 },
        chunkSize: 0.1
      })
    ).toThrow(err);
    await expect(() =>
      validation.aggregation({
        collection: 'dummy',
        sources: { a: 1 },
        chunkSize: -1
      })
    ).toThrow(err);
    await expect(() =>
      validation.aggregation({
        collection: 'dummy',
        sources: { a: 1 },
        chunkSize: 0
      })
    ).toThrow(err);
  });

  it('should throw if sources is an empty object', () => {
    return expect(() =>
      validation.aggregation({ collection: 'dummy', sources: {} })
    ).toThrow(/^config\.sources must have at least one source$/);
  });

  it('should throw if sources properties are not functions', () => {
    return expect(() =>
      validation.aggregation({
        collection: 'dummy',
        sources: {
          source1: 'abc'
        }
      })
    ).toThrow(
      'all sources in the aggregation config should be functions (source1)'
    );
  });

  it('should return a new object', () => {
    const aggConfig = {
      collection: 'dummy',
      sources: {
        source1: () => {
          // noop
        }
      }
    };
    const validatedConfig = validation.aggregation(aggConfig);
    expect(validatedConfig).toBeDefined();
    return expect(validatedConfig).not.toStrictEqual(aggConfig);
  });
});
{'fraction_non_alphanumeric': 0.16090257275411218, 'fraction_numerical': 0.0065373260227752005, 'mean_word_length': 2.6456571867794003, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 41, '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': '504476', 'n_tokens_mistral': 1350, 'n_tokens_neox': 1309, 'n_words': 386}