<commit_before>import 'jest-extended';
import { listPackages } from '../src/helpers/packages';
import { filterBySuite, groupBySuite } from '../src/helpers/test-runner/utils';
import { PackageInfo } from '../src/helpers/interfaces';
import { getAvailableTestSuites } from '../src/helpers/misc';
import { TestOptions } from '../src/helpers/test-runner/interfaces';

describe('Test Runner Helpers', () => {
    const availableSuites = getAvailableTestSuites();
    const packages = listPackages();

    const defaultsOptions: TestOptions = {
        bail: false,
        debug: false,
        watch: false,
        trace: false,
        all: true,
        keepOpen: false,
        reportCoverage: false,
        useExistingServices: false,
        elasticsearchAPIVersion: '',
        elasticsearchVersion: '',
        kafkaVersion: '',
        minioVersion: '',
        rabbitmqVersion: '',
        opensearchVersion: '',
        ignoreMount: true
    };

    function makeTestOptions(input: Partial<TestOptions>): TestOptions {
        return Object.assign({}, defaultsOptions, input);
    }

    describe('->filterBySuite', () => {
        it('should be able to filter by a suite', () => {
            const options = makeTestOptions({
                all: false,
                suite: ['unit-a'],
            });
            const filtered = filterBySuite(packages, options);
            const suites = filtered.map((pkgInfo) => pkgInfo.terascope.testSuite);

            expect(suites).not.toContain('e2e');
            expect(suites).not.toContain('elasticsearch');
            expect(suites).not.toContain('disabled');
            expect(suites).not.toContain('kafka');
        });
    });

    describe('->groupBySuite', () => {
        describe('when running all tests', () => {
            it('should be able to group by suite', () => {
                const grouped = groupBySuite(packages, availableSuites, {
                    all: true,
                } as any);

                for (const [suite, group] of Object.entries(grouped)) {
                    const filtered = filterBySuite(packages, makeTestOptions({
                        all: false,
                        suite: [suite],
                    }));

                    expect(mapInfo(group)).toEqual(mapInfo(filtered));
                }
            });
        });

        describe('when running all tests in watch mode', () => {
            it('should be able group elasticsearch and unit together', () => {
                const elasticsearchTests = filterBySuite(packages, makeTestOptions({
                    all: true,
                    suite: ['elasticsearch'],
                }));

                const opensearchTests = filterBySuite(packages, makeTestOptions({
                    all: true,
                    suite: ['search'],
                }));

                const unitATests = filterBySuite(packages, makeTestOptions({
                    all: true,
                    suite: ['unit-a'],
                }));

                const unitBTests = filterBySuite(packages, makeTestOptions({
                    all: true,
                    suite: ['unit-b'],
                }));

                const unitAndESPackages = [
                    ...unitBTests,
                    ...unitATests,
                    ...opensearchTests,
                    ...elasticsearchTests,
                ];

                const grouped = groupBySuite(packages, availableSuites, makeTestOptions({
                    all: true,
                    watch: true
                }));

                expect(mapInfo(grouped['unit-a'])).toBeArrayOfSize(0);
                expect(mapInfo(grouped['unit-b'])).toBeArrayOfSize(0);
                expect(mapInfo(grouped.elasticsearch)).toEqual(
                    mapInfo(unitAndESPackages)
                );
            });
        });

        describe('when a unit-a test and none-unit-a test, it should group them together', () => {
            it('should be able group elasticsearch and unit-a together', () => {
                const elasticsearchTests = filterBySuite(packages, makeTestOptions({
                    all: false,<commit_msg>make test less prone to failure<commit_after>import 'jest-extended';
import { listPackages } from '../src/helpers/packages';
import { filterBySuite, groupBySuite } from '../src/helpers/test-runner/utils';
import { PackageInfo } from '../src/helpers/interfaces';
import { getAvailableTestSuites } from '../src/helpers/misc';
import { TestOptions } from '../src/helpers/test-runner/interfaces';

describe('Test Runner Helpers', () => {
    const availableSuites = getAvailableTestSuites();
    const packages = listPackages();

    const defaultsOptions: TestOptions = {
        bail: false,
        debug: false,
        watch: false,
        trace: false,
        all: true,
        keepOpen: false,
        reportCoverage: false,
        useExistingServices: false,
        elasticsearchAPIVersion: '',
        elasticsearchVersion: '',
        kafkaVersion: '',
        minioVersion: '',
        rabbitmqVersion: '',
        opensearchVersion: '',
        ignoreMount: true
    };

    function makeTestOptions(input: Partial<TestOptions>): TestOptions {
        return Object.assign({}, defaultsOptions, input);
    }

    describe('->filterBySuite', () => {
        it('should be able to filter by a suite', () => {
            const options = makeTestOptions({
                all: false,
                suite: ['unit-a'],
            });
            const filtered = filterBySuite(packages, options);
            const suites = filtered.map((pkgInfo) => pkgInfo.terascope.testSuite);

            expect(suites).not.toContain('e2e');
            expect(suites).not.toContain('elasticsearch');
            expect(suites).not.toContain('disabled');
            expect(suites).not.toContain('kafka');
        });
    });

    describe('->groupBySuite', () => {
        describe('when running all tests', () => {
            it('should be able to group by suite', () => {
                const grouped = groupBySuite(packages, availableSuites, {
                    all: true,
                } as any);

                for (const [suite, group] of Object.entries(grouped)) {
                    const filtered = filterBySuite(packages, makeTestOptions({
                        all: false,
                        suite: [suite],
                    }));

                    expect(mapInfo(group)).toEqual(mapInfo(filtered));
                }
            });
        });

        describe('when running all tests in watch mode', () => {
            it('should be able group elasticsearch and unit together', () => {
                const elasticsearchTests = filterBySuite(packages, makeTestOptions({
                    all: true,
                    suite: ['elasticsearch'],
                }));

                const opensearchTests = filterBySuite(packages, makeTestOptions({
                    all: true,
                    suite: ['search'],
                }));

                const restrainedTests = filterBySuite(packages, makeTestOptions({
                    all: true,
                    suite: ['restrained'],
                }));

                const unitATests = filterBySuite(packages, makeTestOptions({
                    all: true,
                    suite: ['unit-a'],
                }));

                const unitBTests = filterBySuite(packages, makeTestOptions({
                    all: true,
                    suite: ['unit-b'],
                }));

                const unitAndESPackages = [
                    ...unitBTests,
                    ...unitATests,
                    ...restrainedTests,
                    ...opensearchTests,
                    ...elasticsearchTests,
                ];

                const grouped = groupBySuite(packages, availableSuites, makeTestOptions({
                    all: true,
                    watch: true
                }));

                expect(mapInfo(grouped['unit-a'])).toBeArrayOfSize(0);
                expect(mapInfo(grouped['unit-b'])).toBeArrayOfSize(0);
                expect(mapInfo(grouped.elasticsearch).length).toEqual(
                    mapInfo(unitAndESPackages).length
                );
            });
        });

        describe('when a unit-a test and none-unit-a test, it should group them together', () => {
            it('should be able group elasticsearch and unit-a together', () => {
                const elasticsearchTests = filterBySuite(packages, makeTestOptions({
                    all: false,
{'fraction_non_alphanumeric': 0.12039397450753186, 'fraction_numerical': 0.0006952491309385864, 'mean_word_length': 1.6257986005476117, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, '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': '2877273', 'n_tokens_mistral': 2163, 'n_tokens_neox': 2059, 'n_words': 457}