<commit_before>'use strict';

const clientFactory = require('../utils/client-factory');
const cloneDeep = require('lodash/cloneDeep');
const coinstacCommon = require('coinstac-common');
const EventEmitter = require('events');
const fs = require('fs');
const fileStats = require('../../src/utils/file-stats');
const noop = require('lodash/noop');
const path = require('path');
const projectFactory = require('../utils/project-factory');
const ProjectService = require('../../src/sub-api/project-service');
const sinon = require('sinon');
const test = require('tape');

function getNextTick(callback) {
  return new Promise((resolve, reject) => {
    process.nextTick(() => {
      try {
        callback();
        resolve();
      } catch (error) {
        reject(error);
      }
    });
  });
}

test('ProjectService#getCSV', t => {
  const readFileStub = sinon.stub(fs, 'readFile');
  const filename = './path/to/random.csv';
  const data = [
    ['filename', 'random', 'column'],
    ['M100.txt', '44', 'true'],
    ['M101.txt', '18', 'false'],
    ['M102.txt', '65', 'true'],
  ];
  readFileStub.yields(null, new Buffer(data.map(r => r.join(',')).join('\n')));

  t.plan(2);

  ProjectService.prototype.getCSV(filename)
    .then(csvString => {
      t.ok(
        readFileStub.calledWith(filename),
        'reads file via passed arg'
      );
      t.deepEqual(
        JSON.parse(csvString),
        data,
        'parses and returns CSV string'
      );
    })
    .then(readFileStub.restore, readFileStub.restore)
    .catch(t.end);
});

test('ProjectService#setMetaContents errors', t => {
  const consortiumId = 'test-consortium';
  const projectId = 'test-project';

  const dbGetSpy = sinon.spy();

  // Sinon doesn't support dynamic stub responses based on args. Do it manually.
  function dbGetStub(name) {
    let doc;

    if (name === 'computations') {
      doc = {
        inputs: dbGetSpy.callCount <= 1 ?
          [[]] :
          [[
            'wat',
            'wat',
            {
              type: 'covariates',
            },
          ]],
      };
    } else {
      doc = {
        _id: consortiumId,
        activeComputationInputs: dbGetSpy.callCount <= 2 ?
          [['wat', 'wat', 'wat']] :
          [[
            'wat',
            'wat',
            [{
              name: 'Is Control',
              type: 'boolean',
            }],
          ]],
      };
    }

    return {
      get(...args) {
        dbGetSpy.apply(dbGetSpy, args);
        return Promise.resolve(doc);
      },
    };
  }

  const project = {
    dbRegistry: {
      get: dbGetStub,
    },
    get: sinon.stub(),
  };

  const setMetaContents = ProjectService.prototype.setMetaContents.bind(
    project
  );
  const badFiles = [{
    filename: path.join(__dirname, 'baddies.txt'),<commit_msg>Fix linting error.<commit_after>'use strict';

const clientFactory = require('../utils/client-factory');
const cloneDeep = require('lodash/cloneDeep');
const coinstacCommon = require('coinstac-common');
const EventEmitter = require('events');
const fs = require('fs');
const fileStats = require('../../src/utils/file-stats');
const noop = require('lodash/noop');
const path = require('path');
const projectFactory = require('../utils/project-factory');
const ProjectService = require('../../src/sub-api/project-service');
const sinon = require('sinon');
const test = require('tape');

function getNextTick(callback) {
  return new Promise((resolve, reject) => {
    process.nextTick(() => {
      try {
        callback();
        resolve();
      } catch (error) {
        reject(error);
      }
    });
  });
}

test('ProjectService#getCSV', t => {
  const readFileStub = sinon.stub(fs, 'readFile');
  const filename = './path/to/random.csv';
  const data = [
    ['filename', 'random', 'column'],
    ['M100.txt', '44', 'true'],
    ['M101.txt', '18', 'false'],
    ['M102.txt', '65', 'true'],
  ];
  readFileStub.yields(null, new Buffer(data.map(r => r.join(',')).join('\n')));

  t.plan(2);

  ProjectService.prototype.getCSV(filename)
    .then(csvString => {
      t.ok(
        readFileStub.calledWith(filename),
        'reads file via passed arg'
      );
      t.deepEqual(
        JSON.parse(csvString),
        data,
        'parses and returns CSV string'
      );
    })
    .then(readFileStub.restore, readFileStub.restore)
    .catch(t.end);
});

test('ProjectService#setMetaContents errors', t => {
  const consortiumId = 'test-consortium';
  const projectId = 'test-project';

  const dbGetSpy = sinon.spy();

  // Sinon doesn't support dynamic stub responses based on args. Do it manually.
  function dbGetStub(name) {
    let doc;

    if (name === 'computations') {
      doc = {
        inputs: dbGetSpy.callCount <= 1 ?
          [[]] :
        [[
          'wat',
          'wat',
          {
            type: 'covariates',
          },
        ]],
      };
    } else {
      doc = {
        _id: consortiumId,
        activeComputationInputs: dbGetSpy.callCount <= 2 ?
          [['wat', 'wat', 'wat']] :
        [[
          'wat',
          'wat',
          [{
            name: 'Is Control',
            type: 'boolean',
          }],
        ]],
      };
    }

    return {
      get(...args) {
        dbGetSpy.apply(dbGetSpy, args);
        return Promise.resolve(doc);
      },
    };
  }

  const project = {
    dbRegistry: {
      get: dbGetStub,
    },
    get: sinon.stub(),
  };

  const setMetaContents = ProjectService.prototype.setMetaContents.bind(
    project
  );
  const badFiles = [{
    filename: path.join(__dirname, 'baddies.txt'),
{'fraction_non_alphanumeric': 0.17074915072411945, 'fraction_numerical': 0.006436617200071518, 'mean_word_length': 2.2905882352941176, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '5830236', 'n_tokens_mistral': 1817, 'n_tokens_neox': 1762, 'n_words': 355}