<commit_before>/* @flow */
'use strict'

const path = require('path')

const test = require('ava')

const fs = require('../lib/fs.js')
const { loadTasks } = require('../lib/tasks.js')

const TASKS_DIR = path.join(__dirname, '..', 'lib', 'tasks')

test('loadTasks()', (t) => {
  return fs.readdir(TASKS_DIR)
    .then((files) => {
      t.true(Array.isArray(files))

      const tasks = loadTasks()
      t.is(files.length, tasks.length)
    })
})

test('all tasks have unique "id" and "label"', (t) => {
  const tasks = loadTasks()

  const labels = tasks.map((task) => task.label)
  const uniqueLabels = Array.from(new Set(labels))
  t.is(tasks.length, uniqueLabels.length)

  const ids = tasks.map((task) => task.id)
  const uniqueIds = Array.from(new Set(ids))
  t.is(tasks.length, uniqueIds.length)
})

test('all tasks "fn" function', (t) => {
  const tasks = loadTasks()

  tasks.forEach((task) => {
    t.is(typeof task.fn, 'function', task.id)
  })
})

function assertTaskBefore (
  t /* : any */,
  before /* : string */,
  after /* : string[] */
) {
  const ids = [ before, ...after ]
  const tasks = loadTasks()
  const taskIds = tasks.map((task) => task.id)

  const theseTasks = tasks.filter((task) => ids.includes(task.id))
  const indices = theseTasks.map((task) => tasks.indexOf(task))
  t.is(Math.min(...indices), taskIds.indexOf(before))
}

test('npm-name.js before tasks that use package.json:name', (t) => {
  assertTaskBefore(t, 'npm-name.js', [
    'badge-npm.js'
  ])
})

test('git-init.js before tasks that use `git` or .git/', (t) => {
  assertTaskBefore(t, 'git-init.js', [
    'appveyor.yml.js',
    'badge-appveyor.js',
    'badge-travis.js',
    'gitignore.js',
    'npm-init.js',
    'travis.yml'
  ])
})

test('npm-init.js before tasks that use package.json', (t) => {
  assertTaskBefore(t, 'npm-init.js', [
    'appveyor.yml.js',
    'badge-appveyor.js',
    'badge-npm.js',
    'badge-travis.js',
    'eslint.js',
    'eslintrc.json.js',
    'fixpack.js',
    'flowtype.js',
    'npm-dev-deps.js',
    'npm-engines.js',<commit_msg>test accuracy: gitignore.js has no deps<commit_after>/* @flow */
'use strict'

const path = require('path')

const test = require('ava')

const fs = require('../lib/fs.js')
const { loadTasks } = require('../lib/tasks.js')

const TASKS_DIR = path.join(__dirname, '..', 'lib', 'tasks')

test('loadTasks()', (t) => {
  return fs.readdir(TASKS_DIR)
    .then((files) => {
      t.true(Array.isArray(files))

      const tasks = loadTasks()
      t.is(files.length, tasks.length)
    })
})

test('all tasks have unique "id" and "label"', (t) => {
  const tasks = loadTasks()

  const labels = tasks.map((task) => task.label)
  const uniqueLabels = Array.from(new Set(labels))
  t.is(tasks.length, uniqueLabels.length)

  const ids = tasks.map((task) => task.id)
  const uniqueIds = Array.from(new Set(ids))
  t.is(tasks.length, uniqueIds.length)
})

test('all tasks "fn" function', (t) => {
  const tasks = loadTasks()

  tasks.forEach((task) => {
    t.is(typeof task.fn, 'function', task.id)
  })
})

function assertTaskBefore (
  t /* : any */,
  before /* : string */,
  after /* : string[] */
) {
  const ids = [ before, ...after ]
  const tasks = loadTasks()
  const taskIds = tasks.map((task) => task.id)

  const theseTasks = tasks.filter((task) => ids.includes(task.id))
  const indices = theseTasks.map((task) => tasks.indexOf(task))
  t.is(Math.min(...indices), taskIds.indexOf(before))
}

test('npm-name.js before tasks that use package.json:name', (t) => {
  assertTaskBefore(t, 'npm-name.js', [
    'badge-npm.js'
  ])
})

test('git-init.js before tasks that use `git` or .git/', (t) => {
  assertTaskBefore(t, 'git-init.js', [
    'appveyor.yml.js',
    'badge-appveyor.js',
    'badge-travis.js',
    'npm-init.js',
    'travis.yml'
  ])
})

test('npm-init.js before tasks that use package.json', (t) => {
  assertTaskBefore(t, 'npm-init.js', [
    'appveyor.yml.js',
    'badge-appveyor.js',
    'badge-npm.js',
    'badge-travis.js',
    'eslint.js',
    'eslintrc.json.js',
    'fixpack.js',
    'flowtype.js',
    'npm-dev-deps.js',
    'npm-engines.js',
{'fraction_non_alphanumeric': 0.20280735721200388, 'fraction_numerical': 0.0, 'mean_word_length': 3.2216547497446375, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, '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': '6674797', 'n_tokens_mistral': 1587, 'n_tokens_neox': 1556, 'n_words': 305}