<commit_before>/* global QUnit */
'use strict';

const fetch = require('node-fetch');
const Ajv = require('ajv');
const LOCAL_API = 'http://localhost:8080/api';
const TEST_API = 'http://jacekkopecky.myvm.port.ac.uk/api';
const TEST_USER = '<EMAIL>';
const TEST_METAANALYSES = 'MisinformationEffect';
const TEST_PAPER = 'LiveVideo';

/* --------------------------------- Schemas -------------------------------- */

const profileSchema = require('../schemas/profile');
const userPapersSchema = require('../schemas/user-papers');
const paperTitleSchema = require('../schemas/paper-title');
const ajv = new Ajv();

/* -------------------------------------------------------------------------- */
/*                                  test data                                 */
/* -------------------------------------------------------------------------- */

const profileTestData = require('./snapshots/profile.json');
// const commentTestData = require('./snapshots/comments.json');
const paperTestData = require('./snapshots/user_papers.json');
const paperTitleTestData = require('./snapshots/paper-title.json');
const metaanalysesTestData = require('./snapshots/metaanalyses.json');
const metaanalysesTitleTestData = require('./snapshots/metaanalyses-title.json');


/* -------------------------------------------------------------------------- */
/*                               Fetch Functions                              */
/* -------------------------------------------------------------------------- */

async function getProfile(url, username) {
  const req = await fetch(`${url}/profile/${username}`);
  const res = await req.json();
  return res;
}

async function getPapers(url, username) {
  const req = await fetch(`${url}/papers/${username}`);
  const res = await req.json();
  return res;
}

async function getPaperByTitle(url, username, title) {
  const req = await fetch(`${url}/papers/${username}/${title}`);
  const res = await req.json();
  return res;
}

async function getMetaanalyses(url, username) {
  const req = await fetch(`${url}/metaanalyses/${username}`);
  const res = await req.json();
  return res;
}

async function getMetaanlysisByTitle(url, username, title) {
  const req = await fetch(`${url}/metaanalyses/${username}/${title}`);
  const res = await req.json();
  return res;
}

/* -------------------------------------------------------------------------- */
/*                                Test Helpers                                */
/* -------------------------------------------------------------------------- */

function testSinglePaper(assert, paper) {
  // todo: maybe check data types?
  assert.ok(paper.id, 'Check that the paper has an id');
  assert.ok(paper.title, 'Check that the paper has a title');
  assert.ok(paper.enteredBy, 'Check that the paper has a enteredBy field');
  assert.ok(paper.ctime, 'Check that the paper has a ctime');
  assert.ok(paper.mtime, 'Check that the paper has a mtime');
  // assert.ok(paper.reference, 'Check that the paper has a reference');
  assert.ok(Array.isArray(paper.tags), 'Check that the paper has a an array of tags');
  assert.ok(paper.apiurl, 'Check that the paper has a id');
}

function testSingleMetaanalysis(assert, metaanalysis) {
  // console.log(metaanalysis)
  // todo: maybe check data types?
  assert.ok(metaanalysis.id, 'Check the metaanlysis has an ID');
  assert.ok(metaanalysis.title, 'Check the metaanlysis has a title');
  assert.ok(metaanalysis.enteredBy, 'Check the metaanlysis has an entered by name');
  assert.ok(metaanalysis.ctime, 'Check the metaanlysis has a ctime');
  assert.ok(metaanalysis.mtime, 'Check the metaanlysis has an mtime');
  // assert.ok(metaanalysis.published, 'Check the metaanlysis has a published field');
  // assert.ok(metaanalysis.description, 'Check the metaanlysis has a description');
  assert.ok(metaanalysis.columns, 'Check the metaanlysis has columns');
  assert.ok(metaanalysis.paperOrder, 'Check the metaanlysis has a paper order');
  assert.ok(metaanalysis.hiddenCols, 'Check the metaanlysis has hidden columns');
  // assert.ok(metaanalysis.hiddenExperiments, 'Check the metaanlysis has hidden experiments');
  assert.ok(metaanalysis.excludedExperiments, 'Check the metaanlysis has exclued experiments');
  assert.ok(metaanalysis.aggregates, 'Check the metaanlysis has aggregates');
}
/* -------------------------------------------------------------------------- */
/*                     Tests to check against old version                     */
/* -------------------------------------------------------------------------- */

QUnit.module('API Comparison')

QUnit.test('Get user profile', async assert => {
  const req1 = await getProfile(LOCAL_API, TEST_USER);
  assert.deepEqual(req1, profileTestData, 'The body should be the same');
});

QUnit.test('Get papers from a specific user', async assert => {
  const req1 = await getPapers(LOCAL_API, TEST_USER);
  assert.deepEqual(req1, paperTestData, 'The body should be the same');
});

QUnit.test('Get a paper by title', async assert => {
  const req1 = await getPaperByTitle(LOCAL_API, TEST_USER, TEST_PAPER);
  assert.deepEqual(req1, paperTitleTestData, 'The body should be the same');
});

QUnit.test('Get all metaanalysis by a user', async assert => {
  const req1 = await getMetaanalyses(LOCAL_API, TEST_USER);
  const req2 = await getMetaanalyses(TEST_API, TEST_USER);
  assert.deepEqual(req1, metaanalysesTestData, 'The body should be the same');
});

QUnit.test('Get a metaanalysis by title', async assert => {
  const req1 = await getMetaanlysisByTitle(LOCAL_API, TEST_USER, TEST_METAANALYSES);
  assert.deepEqual(req1, metaanalysesTitleTestData, 'The body should be the same');
});


/* -------------------------------------------------------------------------- */
/*                       Test to check structure of data                      */
/* -------------------------------------------------------------------------- */
QUnit.module('Structure Test');

QUnit.test('Check the structure of the user', async assert => {
  const user = await getProfile(LOCAL_API, TEST_USER);
  const validate = ajv.compile(profileSchema);
  const valid = validate(user);
  assert.ok(valid, 'Check the structure of the user matches the schema');
});

QUnit.test('Check the structure of the papers', async assert => {
  const papers = await getPapers(LOCAL_API, TEST_USER);
  const validate = ajv.compile(userPapersSchema);
  const valid = validate(papers);
  assert.ok(valid, 'Check the structure of the papers matches the schema')
});

QUnit.test('Check the structure of a specific paper', async assert => {
  const paper = await getPaperByTitle(LOCAL_API, TEST_USER, TEST_PAPER);
  testSinglePaper(assert, paper);
});

QUnit.test('Check the strucutre of the metaanalysis', async assert => {
  const metaanalyses = await getMetaanalyses(LOCAL_API, TEST_USER);
  metaanalyses.forEach(meta => testSingleMetaanalysis(assert, meta));
});

QUnit.test('Check the strucutre of the metaanalysis for specific title', async assert => {
  const metaanalysis = await getMetaanlysisByTitle(LOCAL_API, TEST_USER, TEST_METAANALYSES);  
  testSingleMetaanalysis(assert, metaanalysis);
});


// { "id": "/id/p/1591873074600", "title": "paper-new", "enteredBy": "<EMAIL>", "ctime": 1591873074600, "mtime": 1591873074600, "tags": [], "apiurl": "/api/papers/up932006/paper-new", "experiments": [{ "title": "exp", "data": { "1": { "value": "10", "enteredBy": "<EMAIL>", "ctime": 1591873074602 } }, "enteredBy": "<EMAIL>", "ctime": 1591873074601 }], "columns": [{ "id": "1", "title": "col", "type": "characteristic" }], "hiddenCols": [] }
// { "id": "/id/ma/1591873074809", "title": "testing123", "enteredBy": "<EMAIL>", "ctime": 1591873074809, "mtime": 1591873074809, "tags": [], "columns": [{ "id": "1", "title": "col", "type": "characteristic", "sourceColumnMap": { "new_paper_1591873012770": "1" } }], "paperOrder": ["/id/p/1591873074600"], "hiddenCols": [], "hiddenExperiments": [], "excludedExperiments": [], "aggregates": [], "groupingAggregates": [], "graphs": [], "apiurl": "/api/metaanalyses/up932006/testing123" }<commit_msg>:white_check_mark: updating tests to use new schemas<commit_after>/* global QUnit */
'use strict';

const requireDir = require('require-directory');

const fetch = require('node-fetch');
const Ajv = require('ajv');
const SCHEMA_URI = 'https://lima.soc.port.ac.uk/schemas';
const LOCAL_API = 'http://localhost:8080/api';
const TEST_USER = '<EMAIL>';
const TEST_METAANALYSES = 'MisinformationEffect';
const TEST_PAPER = 'LiveVideo';

/* --------------------------------- Schemas -------------------------------- */

const schemas = requireDir(module, '../schemas');

const ajv = new Ajv({ schemas: Object.values(schemas), allErrors: true, jsonPointers: true });
require('ajv-errors')(ajv);
require('ajv-merge-patch')(ajv);


/* -------------------------------------------------------------------------- */
/*                                  test data                                 */
/* -------------------------------------------------------------------------- */

const profileTestData = require('./snapshots/profile.json');
// const commentTestData = require('./snapshots/comments.json');
const paperTestData = require('./snapshots/user_papers.json');
const paperTitleTestData = require('./snapshots/paper-title.json');
const metaanalysesTestData = require('./snapshots/metaanalyses.json');
const metaanalysesTitleTestData = require('./snapshots/metaanalyses-title.json');


/* -------------------------------------------------------------------------- */
/*                               Fetch Functions                              */
/* -------------------------------------------------------------------------- */

async function getProfile(url, username) {
  const req = await fetch(`${url}/profile/${username}`);
  const res = await req.json();
  return res;
}

async function getPapers(url, username) {
  const req = await fetch(`${url}/papers/${username}`);
  const res = await req.json();
  return res;
}

async function getPaperByTitle(url, username, title) {
  const req = await fetch(`${url}/papers/${username}/${title}`);
  const res = await req.json();
  return res;
}

async function getMetaanalyses(url, username) {
  const req = await fetch(`${url}/metaanalyses/${username}`);
  const res = await req.json();
  return res;
}

async function getMetaanlysisByTitle(url, username, title) {
  const req = await fetch(`${url}/metaanalyses/${username}/${title}`);
  const res = await req.json();
  return res;
}


/* -------------------------------------------------------------------------- */
/*                     Tests to check against old version                     */
/* -------------------------------------------------------------------------- */

QUnit.module('API Comparison');

QUnit.test('Get user profile', async assert => {
  const req1 = await getProfile(LOCAL_API, TEST_USER);
  assert.deepEqual(req1, profileTestData, 'The body should be the same');
});

QUnit.test('Get papers from a specific user', async assert => {
  const req1 = await getPapers(LOCAL_API, TEST_USER);
  assert.deepEqual(req1, paperTestData, 'The body should be the same');
});

QUnit.test('Get a paper by title', async assert => {
  const req1 = await getPaperByTitle(LOCAL_API, TEST_USER, TEST_PAPER);
  assert.deepEqual(req1, paperTitleTestData, 'The body should be the same');
});

QUnit.test('Get all metaanalysis by a user', async assert => {
  const req1 = await getMetaanalyses(LOCAL_API, TEST_USER);
  assert.deepEqual(req1, metaanalysesTestData, 'The body should be the same');
});

QUnit.test('Get a metaanalysis by title', async assert => {
  const req1 = await getMetaanlysisByTitle(LOCAL_API, TEST_USER, TEST_METAANALYSES);
  assert.deepEqual(req1, metaanalysesTitleTestData, 'The body should be the same');
});


/* -------------------------------------------------------------------------- */
/*                       Test to check structure of data                      */
/* -------------------------------------------------------------------------- */
QUnit.module('Structure Test');

QUnit.test('Check the structure of the user', async assert => {
  const user = await getProfile(LOCAL_API, TEST_USER);
  const validate = ajv.getSchema(`${SCHEMA_URI}/profile`);
  const valid = validate(user);
  assert.ok(valid, 'Check the structure of the user matches the schema');
});

QUnit.test('Check the structure of the papers', async assert => {
  const papers = await getPapers(LOCAL_API, TEST_USER);
  const validate = ajv.getSchema(`${SCHEMA_URI}/user-papers`);
  const valid = validate(papers);
  assert.ok(valid, 'Check the structure of the papers matches the schema');
});

QUnit.test('Check the structure of a specific paper', async assert => {
  const paper = await getPaperByTitle(LOCAL_API, TEST_USER, TEST_PAPER);
  const validate = ajv.getSchema(`${SCHEMA_URI}/paper-title`);
  const valid = validate(paper);
  assert.ok(valid, 'Check the structure of the paper matches the schema');
});

QUnit.test('Check the strucutre of the metaanalysis', async assert => {
  const metaanalyses = await getMetaanalyses(LOCAL_API, TEST_USER);
  const validate = ajv.getSchema(`${SCHEMA_URI}/metaanalyses`);
  const valid = validate(metaanalyses);
  assert.ok(valid, 'Check the structure of the metaanalyses matches the schema');
});

QUnit.test('Check the strucutre of the metaanalysis for specific title', async assert => {
  const metaanalysis = await getMetaanlysisByTitle(LOCAL_API, TEST_USER, TEST_METAANALYSES);
  const validate = ajv.getSchema(`${SCHEMA_URI}/metaanalysis-title`);
  const valid = validate(metaanalysis);
  assert.ok(valid, 'Check the structure of the metaanalysis matches the schema');
});

QUnit.test('Creating a new blank metanalyses', async assert => {
  const request = await fetch(`${LOCAL_API}/metaanalyses/test-account/test-metanalyses`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': 'Fake test',
    },
    body: JSON.stringify({
      title: 'test-metaanalyses',
      enteredBy: '<EMAIL>',
    }),
  });

  const response = await request.text();

  try {
    const obj = JSON.parse(response);
    const validate = ajv.getSchema(`${SCHEMA_URI}/metaanalysis-base`);
    const valid = validate(obj);
    assert.ok(valid, 'Check the structure of the saved metaanalysis matches the schema');
  } catch (error) {
    assert.ok(false, response);
  }
});
{'fraction_non_alphanumeric': 0.2320182408622953, 'fraction_numerical': 0.012644234091066122, 'mean_word_length': 2.583560287199802, 'pattern_counts': {'":': 41, '<': 10, '<?xml version=': 0, '>': 32, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '307191', 'n_tokens_mistral': 4135, 'n_tokens_neox': 3917, 'n_words': 1083}