<commit_before>/**!
 *
 * Copyright (c) 2015-2016 Cisco Systems, Inc. See LICENSE file.
 */

import '../..';

import {assert} from '@ciscospark/test-helper-chai';
import CiscoSpark from '@ciscospark/spark-core';
import testUsers from '@ciscospark/test-helper-test-users';
import {flaky} from '@ciscospark/test-helper-mocha';
import fh from '@ciscospark/test-helper-file';
import {find, map} from 'lodash';
import uuid from 'uuid';

function generateTonsOfContents(numOfContents) {
  const contents = [];

  for (let i = 0; i < numOfContents; i++) {
    contents.push({
      type: `curve`,
      payload: JSON.stringify({id: i, type: `curve`})
    });
  }
  return contents;
}

function boardChannelToMercuryBinding(channelId) {
  // make channelId mercury compatible replace `-` with `.` and `_` with `#`
  return channelId.replace(/-/g, `.`).replace(/_/g, `#`);
}

describe(`plugin-board`, function() {
  this.timeout(30000);

  let board, conversation, fixture, participants;
  const mercuryBindingsPrefix = `board.`;

  // create users
  before(() => testUsers.create({count: 3})
    .then((users) => {
      participants = users;

      return Promise.all(map(participants, (participant) => {
        participant.spark = new CiscoSpark({
          credentials: {
            authorization: participant.token
          }
        });
        return participant.spark.device.register();
      }));
    })

    // create conversation
    .then(() => participants[0].spark.conversation.create({
      displayName: `Test Board Conversation`,
      participants
    }))
    .then((c) => {
      conversation = c;
      return conversation;
    })

    // create channel (board)
    .then(() => participants[0].spark.board.createChannel({aclUrl: conversation.id}))
    .then((channel) => {
      board = channel;
      return channel;
    })

    // connect to realtime channel
    .then(() => {
      const mercuryBindingId = boardChannelToMercuryBinding(board.channelId);
      const bindingStr = [mercuryBindingsPrefix + mercuryBindingId];
      const bindingObj = {bindings: bindingStr};

      return Promise.all(map(participants, (participant) => {
        return participant.spark.board.register(bindingObj)
          .then((url) => {
            participant.spark.board.realtime.set({
              boardWebSocketUrl: url.webSocketUrl,
              boardBindings: bindingStr
            });
            return participant.spark.board.realtime.connect();
          });
      }));
    }));

  // load fixture image
  before(() => fh.fetch(`sample-image-small-one.png`)
    .then((fetchedFixture) => {
      fixture = fetchedFixture;
      return fetchedFixture;
    }));

  // disconnect realtime
  after(() => Promise.all(map(participants, (participant) => {

    if (participant.spark.board.realtime.connected) {
      return participant.spark.board.realtime.disconnect()
        .then(() => {
          participant.spark.board.realtime.set({boardWebSocketUrl: ``});
          participant.spark.board.realtime.set({boardBindings: []});
        });
    }
    return true;
  })));

  describe(`#_uploadImage()`, () => {

    it(`uploads image to spark files`, () => {
      return participants[0].spark.board._uploadImage(conversation, fixture)
        .then((scr) => {
          return participants[0].spark.encryption.download(scr);
        })<commit_msg>refactor(plugin-board): use multiple begin blocks for integration setup<commit_after>/**!
 *
 * Copyright (c) 2015-2016 Cisco Systems, Inc. See LICENSE file.
 */

import '../..';

import {assert} from '@ciscospark/test-helper-chai';
import CiscoSpark from '@ciscospark/spark-core';
import testUsers from '@ciscospark/test-helper-test-users';
import {flaky} from '@ciscospark/test-helper-mocha';
import fh from '@ciscospark/test-helper-file';
import {find, map} from 'lodash';
import uuid from 'uuid';

function generateTonsOfContents(numOfContents) {
  const contents = [];

  for (let i = 0; i < numOfContents; i++) {
    contents.push({
      type: `curve`,
      payload: JSON.stringify({id: i, type: `curve`})
    });
  }
  return contents;
}

function boardChannelToMercuryBinding(channelId) {
  // make channelId mercury compatible replace `-` with `.` and `_` with `#`
  return channelId.replace(/-/g, `.`).replace(/_/g, `#`);
}

describe(`plugin-board`, function() {
  this.timeout(30000);

  let board, conversation, fixture, participants;
  const mercuryBindingsPrefix = `board.`;

  // create users
  before(() => testUsers.create({count: 3})
    .then((users) => {
      participants = users;

      return Promise.all(map(participants, (participant) => {
        participant.spark = new CiscoSpark({
          credentials: {
            authorization: participant.token
          }
        });
        return participant.spark.device.register();
      }));
    }));

  // create conversation
  before(() => participants[0].spark.conversation.create({
    displayName: `Test Board Conversation`,
    participants
  })
    .then((c) => {
      conversation = c;
      return conversation;
    }));

  // create channel (board)
  before(() => participants[0].spark.board.createChannel({aclUrl: conversation.id})
    .then((channel) => {
      board = channel;
      return channel;
    }));

  // connect to realtime channel
  before(() => {
    const mercuryBindingId = boardChannelToMercuryBinding(board.channelId);
    const bindingStr = [mercuryBindingsPrefix + mercuryBindingId];
    const bindingObj = {bindings: bindingStr};

    return Promise.all(map(participants, (participant) => {
      return participant.spark.board.register(bindingObj)
        .then((url) => {
          participant.spark.board.realtime.set({
            boardWebSocketUrl: url.webSocketUrl,
            boardBindings: bindingStr
          });
          return participant.spark.board.realtime.connect();
        });
    }));
  });

  // load fixture image
  before(() => fh.fetch(`sample-image-small-one.png`)
    .then((fetchedFixture) => {
      fixture = fetchedFixture;
      return fetchedFixture;
    }));

  // disconnect realtime
  after(() => Promise.all(map(participants, (participant) => {

    if (participant.spark.board.realtime.connected) {
      return participant.spark.board.realtime.disconnect()
        .then(() => {
          participant.spark.board.realtime.set({boardWebSocketUrl: ``});
          participant.spark.board.realtime.set({boardBindings: []});
        });
    }
    return true;
  })));

  describe(`#_uploadImage()`, () => {

    it(`uploads image to spark files`, () => {
      return participants[0].spark.board._uploadImage(conversation, fixture)
        .then((scr) => {
          return participants[0].spark.encryption.download(scr);
        })
{'fraction_non_alphanumeric': 0.1537091988130564, 'fraction_numerical': 0.005637982195845697, 'mean_word_length': 2.9329054842473745, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '367461', 'n_tokens_mistral': 2076, 'n_tokens_neox': 1988, 'n_words': 395}