<commit_before>import * as Chatbot from '../src/chatbot/chatbot-service';
import * as fs from 'fs';
import * as discussions from '../doc/flow/discussions.json';
import dialog from '../src/coaching-chatbot/dialog';
import * as Messenger from '../src/facebook-messenger/messenger-service';
import * as Pairs from '../src/util/pairs-service';
import * as path from 'path';
import * as Sessions from '../src/util/sessions-service';
import * as sinon from 'sinon';
import * as states from '../doc/flow/states.json';

const messengerSpy = sinon.spy(Messenger, "send");

process.env.RUN_ENV = 'dev';

require('../src/lib/env-vars').config();

const sessions = new Sessions();
const bot = new Chatbot(dialog, sessions);

sessions.db.load(states);

let lines = [];
let promise = bot.receive('', '');

let messageQueue = {};

function addBotReply(output) {
  for (let botReply of output) {
    for (let message of botReply.message.split('\n')) {
      lines.push('| ' + message + ' | |');
    }
    if (botReply.quickReplies.length > 0) {
      lines.push('| [' +
        botReply.quickReplies
          .map((quickReply) => quickReply.title)
          .join('] [') + '] | |');
    }
  }
}

var clock = sinon.useFakeTimers(new Date().getTime());

for (let scenario in discussions) {
  const messages = discussions[scenario].messages;
  const sessionID = discussions[scenario].session;

  if (discussions[scenario].time) {
    promise = promise.then(() => {
      clock = sinon.useFakeTimers(new Date(discussions[scenario].time).getTime());
    });
  }

  if (discussions[scenario].hide) {
    for (let message of messages) {
      promise = promise.then(() => {
        return bot.receive(sessionID, message);
      });
    }
  } else {
    promise = promise.then(() => {
      lines.push('# ' + scenario);
      lines.push('| Kehitystön vertaisohjausrobotti | ' + discussions[scenario].player + ' |');
      lines.push('|-|-|');
    });

    promise = promise.then(() => {
      if (messageQueue[sessionID] != undefined) {
        addBotReply(messageQueue[sessionID]);
        messageQueue[sessionID] = undefined;
      }
    });

    for (let message of messages) {
      promise = promise.then(() => {
        lines.push('| | ' + message + ' |');
        return bot.receive(sessionID, message);
      }).then((output) => {
        addBotReply(output);
      });
    }
  }

  promise = promise.then(() => {
    if (messengerSpy.callCount > 0) {
      for (let call of messengerSpy.args) {
        if (!messageQueue[call[0]]) {
          messageQueue[call[0]] = [];
        }
        messageQueue[call[0]].push({
          message: call[1],
          quickReplies: call[2] || {}
        });
      }
      messengerSpy.reset();
      clock.reset();
    }
  });
}

promise = promise.then(() => {
  const target = path.resolve(__dirname, '..', 'doc', 'flow', 'discussions.md');
  fs.writeFileSync(target, lines.join('\n'));
});
<commit_msg>Prevented empty table rows in discussions.json<commit_after>import * as Chatbot from '../src/chatbot/chatbot-service';
import * as fs from 'fs';
import * as discussions from '../doc/flow/discussions.json';
import dialog from '../src/coaching-chatbot/dialog';
import * as Messenger from '../src/facebook-messenger/messenger-service';
import * as Pairs from '../src/util/pairs-service';
import * as path from 'path';
import * as Sessions from '../src/util/sessions-service';
import * as sinon from 'sinon';
import * as states from '../doc/flow/states.json';

const messengerSpy = sinon.spy(Messenger, "send");

process.env.RUN_ENV = 'dev';

require('../src/lib/env-vars').config();

const sessions = new Sessions();
const bot = new Chatbot(dialog, sessions);

sessions.db.load(states);

let lines = [];
let promise = bot.receive('', '');

let messageQueue = {};

function addBotReply(output) {
  for (let botReply of output) {
    for (let message of botReply.message.replace('\n\n', '\n').split('\n')) {
      if (message.length > 0) {
        lines.push('| ' + message + ' | |');
      }
    }
    if (botReply.quickReplies.length > 0) {
      lines.push('| [' +
        botReply.quickReplies
          .map((quickReply) => quickReply.title)
          .join('] [') + '] | |');
    }
  }
}

var clock = sinon.useFakeTimers(new Date().getTime());

for (let scenario in discussions) {
  const messages = discussions[scenario].messages;
  const sessionID = discussions[scenario].session;

  if (discussions[scenario].time) {
    promise = promise.then(() => {
      clock = sinon.useFakeTimers(new Date(discussions[scenario].time).getTime());
    });
  }

  if (discussions[scenario].hide) {
    for (let message of messages) {
      promise = promise.then(() => {
        return bot.receive(sessionID, message);
      });
    }
  } else {
    promise = promise.then(() => {
      lines.push('# ' + scenario);
      lines.push('| Kehitystön vertaisohjausrobotti | ' + discussions[scenario].player + ' |');
      lines.push('|-|-|');
    });

    promise = promise.then(() => {
      if (messageQueue[sessionID] != undefined) {
        addBotReply(messageQueue[sessionID]);
        messageQueue[sessionID] = undefined;
      }
    });

    for (let message of messages) {
      promise = promise.then(() => {
        lines.push('| | ' + message + ' |');
        return bot.receive(sessionID, message);
      }).then((output) => {
        addBotReply(output);
      });
    }
  }

  promise = promise.then(() => {
    if (messengerSpy.callCount > 0) {
      for (let call of messengerSpy.args) {
        if (!messageQueue[call[0]]) {
          messageQueue[call[0]] = [];
        }
        messageQueue[call[0]].push({
          message: call[1],
          quickReplies: call[2] || {}
        });
      }
      messengerSpy.reset();
      clock.reset();
    }
  });
}

promise = promise.then(() => {
  const target = path.resolve(__dirname, '..', 'doc', 'flow', 'discussions.md');
  fs.writeFileSync(target, lines.join('\n'));
});

{'fraction_non_alphanumeric': 0.18041323702334958, 'fraction_numerical': 0.0025197379472534855, 'mean_word_length': 2.8240205523442516, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 26, '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': '4698849', 'n_tokens_mistral': 1922, 'n_tokens_neox': 1891, 'n_words': 391}