<commit_before>'use strict';

const Assert = require('chai').assert;
const request = require('../support/request');
const { defineSupportCode } = require('cucumber');

// Timeout of 15 seconds
const TIMEOUT = 15 * 1000;

/**
 * Helper function to create a collection
 * @method createCollection
 * @param   {Object}  body    The body of the request
 * @returns {Promise}
 */
function createCollection(body) {
    return this.getJwt(this.apiToken)
        .then((response) => {
            this.jwt = response.body.token;

            return request({
                uri: `${this.instance}/${this.namespace}/collections`,
                method: 'POST',
                auth: {
                    bearer: this.jwt
                },
                body,
                json: true
            });
        });
}

/**
 * Helper function to delete a collection
 * @method deleteCollection
 * @param   {Number} [id]  Id of the collection to delete
 * @returns {Promise}
 */
function deleteCollection(id) {
    if (!id) {
        return Promise.resolve();
    }

    return request({
        uri: `${this.instance}/${this.namespace}/collections/${id}`,
        method: 'DELETE',
        auth: {
            bearer: this.jwt
        },
        json: true
    }).then((response) => {
        Assert.strictEqual(response.statusCode, 204);
    });
}

defineSupportCode(({ Before, Given, Then, When, After }) => {
    Before('@collections', function hook() {
        this.repoOrg = this.testOrg;
        this.repoName = 'functional-collections';
        this.pipelineId = null;
        this.firstCollectionId = null;
        this.secondCollectionId = null;
    });

    When(/^they create a new collection "myCollection" with that pipeline$/,
        { timeout: TIMEOUT }, function step() {
            return this.ensurePipelineExists({ repoName: this.repoName })
                .then(() => {
                    const requestBody = {
                        name: 'myCollection',
                        pipelineIds: [this.pipelineId]
                    };

                    return createCollection.call(this, requestBody);
                })
                .then((response) => {
                    Assert.strictEqual(response.statusCode, 201);
                    this.firstCollectionId = response.body.id;
                });
        });

    Then(/^they can see that collection$/, function step() {
        return request({
            uri: `${this.instance}/${this.namespace}/collections/${this.firstCollectionId}`,
            method: 'GET',
            auth: {
                bearer: this.jwt
            },
            json: true
        }).then((response) => {
            Assert.strictEqual(response.statusCode, 200);
            Assert.strictEqual(response.body.name, 'myCollection');
        });
    });

    Then(/^the collection contains that pipeline$/, function step() {
        const pipelineId = parseInt(this.pipelineId, 10);

        return request({
            uri: `${this.instance}/${this.namespace}/collections/${this.firstCollectionId}`,
            method: 'GET',
            auth: {
                bearer: this.jwt
            },
            json: true
        }).then((response) => {
            Assert.strictEqual(response.statusCode, 200);
            Assert.deepEqual(response.body.pipelineIds, [pipelineId]);
        });
    });

    When(/^they create a new collection "myCollection"$/, function step() {
        return createCollection.call(this, { name: 'myCollection' })
            .then((response) => {
                Assert.strictEqual(response.statusCode, 201);
                this.firstCollectionId = response.body.id;
            });
    });

    Then(/^the collection is empty$/, function step() {
        return request({
            uri: `${this.instance}/${this.namespace}/collections/${this.firstCollectionId}`,
            method: 'GET',
            auth: {
                bearer: this.jwt
            },
            json: true
        }).then((response) => {
            Assert.strictEqual(response.statusCode, 200);
            Assert.deepEqual(response.body.pipelineIds, []);
        });
    });

    When(/^they update the collection "myCollection" with that pipeline$/,
        { timeout: TIMEOUT }, function step() {
            return this.ensurePipelineExists({ repoName: this.repoName })
                .then(() => {
                    const pipelineId = parseInt(this.pipelineId, 10);

                    request({
                        uri: `${this.instance}/${this.namespace}/collections/` +<commit_msg>fix: Fix collection step_definition (#737)<commit_after>'use strict';

const Assert = require('chai').assert;
const request = require('../support/request');
const { defineSupportCode } = require('cucumber');

// Timeout of 15 seconds
const TIMEOUT = 15 * 1000;

/**
 * Helper function to create a collection
 * @method createCollection
 * @param   {Object}  body    The body of the request
 * @returns {Promise}
 */
function createCollection(body) {
    return this.getJwt(this.apiToken)
        .then((response) => {
            this.jwt = response.body.token;

            return request({
                uri: `${this.instance}/${this.namespace}/collections`,
                method: 'POST',
                auth: {
                    bearer: this.jwt
                },
                body,
                json: true
            });
        });
}

/**
 * Helper function to delete a collection
 * @method deleteCollection
 * @param   {Number} [id]  Id of the collection to delete
 * @returns {Promise}
 */
function deleteCollection(id) {
    if (!id) {
        return Promise.resolve();
    }

    return request({
        uri: `${this.instance}/${this.namespace}/collections/${id}`,
        method: 'DELETE',
        auth: {
            bearer: this.jwt
        },
        json: true
    }).then((response) => {
        Assert.strictEqual(response.statusCode, 204);
    });
}

defineSupportCode(({ Before, Given, Then, When, After }) => {
    Before('@collections', function hook() {
        this.repoOrg = this.testOrg;
        this.repoName = 'functional-collections';
        this.pipelineId = null;
        this.firstCollectionId = null;
        this.secondCollectionId = null;
    });

    When(/^they create a new collection "myCollection" with that pipeline$/,
        { timeout: TIMEOUT }, function step() {
            return this.ensurePipelineExists({ repoName: this.repoName })
                .then(() => {
                    const requestBody = {
                        name: 'myCollection',
                        pipelineIds: [this.pipelineId]
                    };

                    return createCollection.call(this, requestBody);
                })
                .then((response) => {
                    Assert.strictEqual(response.statusCode, 201);
                    this.firstCollectionId = response.body.id;
                });
        });

    Then(/^they can see that collection$/, function step() {
        return request({
            uri: `${this.instance}/${this.namespace}/collections/${this.firstCollectionId}`,
            method: 'GET',
            auth: {
                bearer: this.jwt
            },
            json: true
        }).then((response) => {
            Assert.strictEqual(response.statusCode, 200);
            Assert.strictEqual(response.body.name, 'myCollection');
        });
    });

    Then(/^the collection contains that pipeline$/, function step() {
        const pipelineId = parseInt(this.pipelineId, 10);

        return request({
            uri: `${this.instance}/${this.namespace}/collections/${this.firstCollectionId}`,
            method: 'GET',
            auth: {
                bearer: this.jwt
            },
            json: true
        }).then((response) => {
            Assert.strictEqual(response.statusCode, 200);
            Assert.deepEqual(response.body.pipelineIds, [pipelineId]);
        });
    });

    When(/^they create a new collection "myCollection"$/, function step() {
        return createCollection.call(this, { name: 'myCollection' })
            .then((response) => {
                Assert.strictEqual(response.statusCode, 201);
                this.firstCollectionId = response.body.id;
            });
    });

    Then(/^the collection is empty$/, function step() {
        return request({
            uri: `${this.instance}/${this.namespace}/collections/${this.firstCollectionId}`,
            method: 'GET',
            auth: {
                bearer: this.jwt
            },
            json: true
        }).then((response) => {
            Assert.strictEqual(response.statusCode, 200);
            Assert.deepEqual(response.body.pipelineIds, []);
        });
    });

    When(/^they update the collection "myCollection" with that pipeline$/,
        { timeout: TIMEOUT }, function step() {
            return this.ensurePipelineExists({ repoName: this.repoName })
                .then(() => {
                    const pipelineId = parseInt(this.pipelineId, 10);

                    return request({
                        uri: `${this.instance}/${this.namespace}/collections/` +
{'fraction_non_alphanumeric': 0.13436411149825783, 'fraction_numerical': 0.006859756097560976, 'mean_word_length': 1.7884031572556163, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '4997570', 'n_tokens_mistral': 2446, 'n_tokens_neox': 2339, 'n_words': 521}