<commit_before>const _ = require('lodash');
const chai = require('chai');
const requireMock = require('mock-require');
const sinon = require('sinon');

describe('core-transform', () => {
  it('transforms an intent into a specific message', () => {
    // create stubs for openwhisk calls
    const invokeStub = sinon.stub()
      .returns(Promise.resolve({
        statusCode: 200,
        result: []
      }));

    // mock openwhisk action calls to return successful results
    requireMock('openwhisk', () => ({
      actions: {
        invoke: invokeStub
      }
    }));

    const config = {
      messages: [
        {
          '$intent': 'hello',
          value: [
            {
              '$intent': 'hello',
              '$signal2': 'foo',
              value: {
                text: 'Hello {{{output.context.name}}}'
              }
            },
            {
              '$intent': 'blabla',
              value: {
                text: 'Not such a good ranking'
              }
            }
          ]
        },
        {
          '$intent': 'foo',
          value: {
            text: 'Fooo Baar'
          }
        }
      ],
      openwhisk: {
        package: 'testpackage'
      }
    }

    const payload = {
      id: '123456',
      conversationcontext: {
        user: {
          _id: '1234abcd',
          testchannel_id: 'abcdefg'
        }
      },
      output: {
        channel: 'testchannel',
        intent: '$intent:hello $signal2:foo',
        user: 'abcdefg',
        context: {
          name: 'Egon'
        }
      }
    }

    requireMock.reRequire('openwhisk');
    requireMock.reRequire('serverless-botpack-lib');

    return requireMock.reRequire('./index').main({ payload, config })
      .then(result => {
        chai.expect(result.result.output.message).to.equal('Hello Egon');
      });
  });

  it('selects the translation based on channel and locale', () => {
    // create stubs for openwhisk calls
    const invokeStub = sinon.stub()
      .returns(Promise.resolve({
        statusCode: 200,
        result: []
      }));

    // mock openwhisk action calls to return successful results
    requireMock('openwhisk', () => ({
      actions: {
        invoke: invokeStub
      }
    }));

    const config = {
      messages: [
        {
          '$intent': 'hello',
          value: [
            {
              '$signal2': 'foo',
              value: {
                DE_de: {
                  testchannel: {
                    text: 'Hallo {{{output.context.name}}} auf dem Testchannel!'
                  }
                },
                text: 'Hello {{{output.context.name}}}'
              }
            }
          ]
        }
      ],
      openwhisk: {
        package: 'testpackage'
      }
    }

    const payload = {
      id: '123456',
      conversationcontext: {
        user: {
          _id: '1234abcd',
          testchannel_id: 'abcdefg',
          locale: 'DE_de'
        }
      },
      output: {
        channel: 'testchannel',
        user: 'abcdefg',
        intent: '$intent:hello $signal2:foo',
        context: {
          name: 'Egon'
        }
      }
    }

    requireMock.reRequire('openwhisk');
    requireMock.reRequire('serverless-botpack-lib');
<commit_msg>harder test.<commit_after>const _ = require('lodash');
const chai = require('chai');
const requireMock = require('mock-require');
const sinon = require('sinon');

describe('core-transform', () => {
  it('transforms an intent into a specific message', () => {
    // create stubs for openwhisk calls
    const invokeStub = sinon.stub()
      .returns(Promise.resolve({
        statusCode: 200,
        result: []
      }));

    // mock openwhisk action calls to return successful results
    requireMock('openwhisk', () => ({
      actions: {
        invoke: invokeStub
      }
    }));

    const config = {
      messages: [
        {
          '$intent': 'hello',
          value: [
            {
              '$intent': 'hello',
              '$signal2': 'foo',
              value: {
                text: 'Hello {{{output.context.name}}}'
              }
            },
            {
              '$intent': 'blabla',
              value: {
                text: 'Not such a good ranking'
              }
            }
          ]
        },
        {
          '$intent': 'foo',
          value: {
            text: '<NAME>'
          }
        }
      ],
      openwhisk: {
        package: 'testpackage'
      }
    }

    const payload = {
      id: '123456',
      conversationcontext: {
        user: {
          _id: '<KEY>',
          testchannel_id: 'abcdefg'
        }
      },
      output: {
        channel: 'testchannel',
        intent: '$intent:hello $signal2:foo',
        user: 'abcdefg',
        context: {
          name: 'Egon'
        }
      }
    }

    requireMock.reRequire('openwhisk');
    requireMock.reRequire('serverless-botpack-lib');

    return requireMock.reRequire('./index').main({ payload, config })
      .then(result => {
        chai.expect(result.result.output.message).to.equal('Hello Egon');
      });
  });

  it('selects the translation based on channel and locale', () => {
    // create stubs for openwhisk calls
    const invokeStub = sinon.stub()
      .returns(Promise.resolve({
        statusCode: 200,
        result: []
      }));

    // mock openwhisk action calls to return successful results
    requireMock('openwhisk', () => ({
      actions: {
        invoke: invokeStub
      }
    }));

    const config = {
      messages: [
        {
          '$intent': 'general:hello',
          value: [
            {
              '$signal2': 'foo',
              value: {
                DE_de: {
                  testchannel: {
                    text: 'Hallo {{{output.context.name}}} auf dem Testchannel!'
                  }
                },
                text: 'Hello {{{output.context.name}}}'
              }
            }
          ]
        }
      ],
      openwhisk: {
        package: 'testpackage'
      }
    }

    const payload = {
      id: '123456',
      conversationcontext: {
        user: {
          _id: '1234abcd',
          testchannel_id: 'abcdefg',
          locale: 'DE_de'
        }
      },
      output: {
        channel: 'testchannel',
        user: 'abcdefg',
        intent: '$intent:general:hello $signal2:foo',
        context: {
          name: 'Egon'
        }
      }
    }

    requireMock.reRequire('openwhisk');
    requireMock.reRequire('serverless-botpack-lib');

{'fraction_non_alphanumeric': 0.13888464476699772, 'fraction_numerical': 0.008556149732620321, 'mean_word_length': 1.404849375459221, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '7649487', 'n_tokens_mistral': 1906, 'n_tokens_neox': 1850, 'n_words': 370}