<commit_before>const fs = require('fs');
const should = require('should');
const async = require('async');
const sinon = require('sinon');
const Octokit = require('@octokit/rest');
const lib = require('./index.js');

const base64Build = '<KEY>';
const nbCommonFields = 2;
const MS_PER_MINUTE = 60000;

describe('eventToBuild', () => {
  it('should transform a base64 build to an object', () => {
    const build = lib.eventToBuild(base64Build);
    should.exist(build.projectId);
    build.projectId.should.equal('node-example-gke');
  });
});

describe('createSlackMessage', () => {
  it('should create a slack message', async () => {
    const build = {
      id: 'build-id',
      logUrl: 'https://logurl.com',
      status: 'SUCCESS',
      finishTime: '2017-03-19T00:08:12.220502Z',
    };
    const message = await lib.createSlackMessage(build);

    message.text.should.equal('Build `build-id` finished');
    should.exist(message.attachments);
    message.attachments.should.have.length(1);
    const attachment = message.attachments[0];
    attachment.title_link.should.equal(build.logUrl);
    attachment.ts.should.equal(1489882092);
    attachment.fields.should.have.length(nbCommonFields);
    attachment.fields[0].value.should.equal(build.status);
  });

  it('should create a slack message saying the build started, with start timestamp if status WORKING', async () => {
    const build = {
      id: 'build-id',
      logUrl: 'https://logurl.com',
      status: 'WORKING',
      startTime: '2017-03-19T00:08:12.220502Z',
      finishTime: null,
    };

    const message = await lib.createSlackMessage(build);

    message.text.should.equal('Build `build-id` started');
    should.exist(message.attachments);
    message.attachments.should.have.length(1);
    const attachment = message.attachments[0];
    attachment.title_link.should.equal(build.logUrl);
    attachment.ts.should.equal(1489882092);
    attachment.fields.should.have.length(nbCommonFields - 1);
    attachment.fields[0].value.should.equal(build.status);
  });

  it('should include the build duration as a field', async () => {
    const now = Date.now();
    const deltaInMinutes = 11;
    const build = {
      id: 'build-id',
      logUrl: 'https://logurl.com',
      status: 'SUCCESS',
      startTime: new Date(now - deltaInMinutes * MS_PER_MINUTE),
      finishTime: now,
    };
    const message = await lib.createSlackMessage(build);

    const attachment = message.attachments[0];
    attachment.fields[1].value.should.equal(`${deltaInMinutes} minutes`);
  });

  it('should not include build duration as a field for start notifications', async () => {
    const now = Date.now();
    const deltaInMinutes = 11;
    const build = {
      id: 'build-id',
      logUrl: 'https://logurl.com',
      status: 'WORKING',
      startTime: new Date(now - deltaInMinutes * MS_PER_MINUTE),
      finishTime: null,
    };
    const message = await lib.createSlackMessage(build);

    const attachment = message.attachments[0];
    attachment.fields.should.not.containEql({ title: 'Duration', value: `${deltaInMinutes} minutes` });
  });

  it('should create a slack message with images', async () => {
    const build = {
      id: 'build-id',
      logUrl: 'https://logurl.com',
      status: 'SUCCESS',
      finishTime: Date.now(),
      images: ['image-1', 'image-2'],
    };
    const message = await lib.createSlackMessage(build);

    const attachment = message.attachments[0];
    attachment.fields.should.have.length(nbCommonFields + build.images.length);
    attachment.fields[nbCommonFields].value.should.equal('image-1');
    attachment.fields[nbCommonFields + 1].value.should.equal('image-2');
  });

  it('should include the source info in the message', async () => {<commit_msg>Fix tests<commit_after>const fs = require('fs');
const should = require('should');
const async = require('async');
const sinon = require('sinon');
const Octokit = require('@octokit/rest');
const lib = require('./index.js');

const base64Build = '<KEY>';
const nbCommonFields = 2;
const MS_PER_MINUTE = 60000;

describe('eventToBuild', () => {
  it('should transform a base64 build to an object', () => {
    const build = lib.eventToBuild(base64Build);
    should.exist(build.projectId);
    build.projectId.should.equal('node-example-gke');
  });
});

describe('createSlackMessage', () => {
  it('should create a slack message', async () => {
    const build = {
      id: 'build-id',
      logUrl: 'https://logurl.com',
      status: 'SUCCESS',
      finishTime: '2017-03-19T00:08:12.220502Z',
      source: {},
    };
    // const build = lib.eventToBuild(base64Build);
    const message = await lib.createSlackMessage(build);

    message.text.should.equal(`Build \`${build.id}\` finished`);
    should.exist(message.attachments);
    message.attachments.should.have.length(1);
    const attachment = message.attachments[0];
    attachment.title_link.should.equal(build.logUrl);
    attachment.ts.should.equal(1489882092);
    attachment.fields.should.have.length(nbCommonFields);
    attachment.fields[0].value.should.equal(build.status);
  });

  it('should create a slack message saying the build started, with start timestamp if status WORKING', async () => {
    const build = {
      id: 'build-id',
      logUrl: 'https://logurl.com',
      status: 'WORKING',
      startTime: '2017-03-19T00:08:12.220502Z',
      finishTime: null,
      source: {},
    };

    const message = await lib.createSlackMessage(build);

    message.text.should.equal('Build `build-id` started');
    should.exist(message.attachments);
    message.attachments.should.have.length(1);
    const attachment = message.attachments[0];
    attachment.title_link.should.equal(build.logUrl);
    attachment.ts.should.equal(1489882092);
    attachment.fields.should.have.length(nbCommonFields - 1);
    attachment.fields[0].value.should.equal(build.status);
  });

  it('should include the build duration as a field', async () => {
    const now = Date.now();
    const deltaInMinutes = 11;
    const build = {
      id: 'build-id',
      logUrl: 'https://logurl.com',
      status: 'SUCCESS',
      startTime: new Date(now - deltaInMinutes * MS_PER_MINUTE),
      finishTime: now,
      source: {},
    };
    const message = await lib.createSlackMessage(build);

    const attachment = message.attachments[0];
    attachment.fields[1].value.should.equal(`${deltaInMinutes} minutes`);
  });

  it('should not include build duration as a field for start notifications', async () => {
    const now = Date.now();
    const deltaInMinutes = 11;
    const build = {
      id: 'build-id',
      logUrl: 'https://logurl.com',
      status: 'WORKING',
      startTime: new Date(now - deltaInMinutes * MS_PER_MINUTE),
      finishTime: null,
      source: {},
    };
    const message = await lib.createSlackMessage(build);

    const attachment = message.attachments[0];
    attachment.fields.should.not.containEql({ title: 'Duration', value: `${deltaInMinutes} minutes` });
  });

  it('should create a slack message with images', async () => {
    const build = {
      id: 'build-id',
      logUrl: 'https://logurl.com',
      status: 'SUCCESS',
      finishTime: Date.now(),
      images: ['image-1', 'image-2'],
      source: {},
    };
    const message = await lib.createSlackMessage(build);

    const attachment = message.attachments[0];
    attachment.fields.should.have.length(nbCommonFields + 1);
    attachment.fields[nbCommonFields].value.should.equal('image-1\nimage-2');
  });

  it('should include the source info in the message', async () => {
{'fraction_non_alphanumeric': 0.1531081613878302, 'fraction_numerical': 0.024444736496254435, 'mean_word_length': 3.4870283018867925, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 10, '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': '4947568', 'n_tokens_mistral': 2456, 'n_tokens_neox': 2327, 'n_words': 499}