<commit_before>/* eslint-env mocha */
/* global expect, testContext */
/* eslint-disable prefer-arrow-callback, no-unused-expressions */

import r from '../../../db/connect'
import factory from '../../../test/factories'
import {withDBCleanup} from '../../../test/helpers'

import {GOAL_SELECTION} from '../../../common/models/cycle'
const {formProjectTeams, forTesting: {getTeamSizes}} = require('../formProjectTeams')

describe(testContext(__filename), function () {
  withDBCleanup()

  describe('formProjectTeams', function () {
    it('creates teams based on player votes', async function () {
      try {
        const cycle = await factory.create('cycle', {state: GOAL_SELECTION})
        const players = await factory.createMany('player', {chapterId: cycle.chapterId}, 6)
        await factory.createMany('vote',
          [[1, 2], [1, 2], [1, 3], [3, 4], [3, 4], [6, 7]].map(
            ([a, b], i) => ({
              cycleId: cycle.id,
              playerId: players[i].id,
              goals: [
                {url: `http://ex.co/${a}`, title: `Goal ${a}`},
                {url: `http://ex.co/${b}`, title: `Goal ${b}`},
              ]
            })
          ),
          6,
        )

        await formProjectTeams(cycle.id)

        const createdProjects = await r.table('projects').run()
        expect(createdProjects).to.have.length(2)

        const goal1Project = createdProjects.filter(p => p.goalUrl.endsWith('1'))[0]
        expect(goal1Project).to.exist
        expect(goal1Project.name).to.match(/^\w+-\w+(-\d)?$/)

        const goal3Project = createdProjects.filter(p => p.goalUrl.endsWith('3'))[0]
        expect(goal3Project).to.exist
        expect(goal3Project.name).to.match(/^\w+-\w+(-\d)?$/)

        // const goal1ProjectTeam = goal1Project.cycleTeams[cycle.id].playerIds.sort()
        // console.log({goal1ProjectTeam})
        // expect(goal1ProjectTeam).to.deep.equal(players.map(p => p.id).slice(0, 3))

        // const goal3ProjectTeam = goal3Project.cycleTeams[cycle.id].playerIds.sort()
        // console.log({goal3ProjectTeam})
        // expect(goal3ProjectTeam).to.deep.equal(players.map(p => p.id).slice(4, 6))
      } catch (e) {
        throw (e)
      }
    })
    it('players who did not vote are assigned to teams')
    it('each team has at least one highly skilled player')
  })

  describe('getTeamSizes(playerCount, target)', function () {
    it('determines optimal team sizes based on playerCount', function () {
      expect(getTeamSizes(6).sort()).to.deep.equal([3, 3].sort())
      expect(getTeamSizes(9).sort()).to.deep.equal([5, 4].sort())
      expect(getTeamSizes(11).sort()).to.deep.equal([4, 4, 3].sort())<commit_msg>test cleanup based on PR feedback. adding test for non-voting players<commit_after>/* eslint-env mocha */
/* global expect, testContext */
/* eslint-disable prefer-arrow-callback, no-unused-expressions */

import r from '../../../db/connect'
import factory from '../../../test/factories'
import {withDBCleanup} from '../../../test/helpers'

import {GOAL_SELECTION} from '../../../common/models/cycle'
const {formProjectTeams, forTesting: {getTeamSizes}} = require('../formProjectTeams')

describe(testContext(__filename), function () {
  withDBCleanup()

  describe('formProjectTeams', function () {
    beforeEach(async function() {
      try {
        this.cycle = await factory.create('cycle', {state: GOAL_SELECTION})
        this.players = await factory.createMany('player', {chapterId: this.cycle.chapterId}, 6)
        this.mostPopularGoalIssueNums = [1, 3]
        await factory.createMany('vote',
          [
            [this.mostPopularGoalIssueNums[0], 2],
            [this.mostPopularGoalIssueNums[0], 2],
            [this.mostPopularGoalIssueNums[0], this.mostPopularGoalIssueNums[1]],
            [this.mostPopularGoalIssueNums[1], 4],
            [this.mostPopularGoalIssueNums[1], 4],
            [6, 7]
          ].map(
            ([a, b], i) => ({
              cycleId: this.cycle.id,
              playerId: this.players[i].id,
              goals: [
                {url: `http://ex.co/${a}`, title: `Goal ${a}`},
                {url: `http://ex.co/${b}`, title: `Goal ${b}`},
              ]
            })
          ),
          6,
        )
      } catch (e) {
        throw (e)
      }
    })

    it('creates teams based on player votes', async function () {
      try {
        await formProjectTeams(this.cycle.id)

        const createdProjects = await r.table('projects').run()
        expect(createdProjects).to.have.length(2)

        this.mostPopularGoalIssueNums.forEach(i => {
          const project = createdProjects.filter(p => p.goalUrl.endsWith(i))[0]
          expect(project).to.exist
          expect(project.name).to.match(/^\w+-\w+(-\d)?$/)
        })
      } catch (e) {
        throw (e)
      }
    })
    describe('when not everyone voted', function() {
      beforeEach(async function() {
        return factory.create('player', {chapterId: this.cycle.chapterId})
          .then(player => this.players.push(player))
      })
      it.only('places all players in teams', function() {
        return formProjectTeams(this.cycle.id)
          .then(() => r.table('projects'))
          .then(projects => projects.map(p => p.cycleTeams[this.cycle.id].playerIds))
          .then(teams => teams.reduce((a,b) => a.concat(b), []))
          .then(playersInTeams => expect(playersInTeams.length).to.equal(this.players.length))
      })
    })
    it('each team has at least one highly skilled player')
  })

  describe('getTeamSizes(playerCount, target)', function () {
    it('determines optimal team sizes based on playerCount', function () {
      expect(getTeamSizes(6).sort()).to.deep.equal([3, 3].sort())
      expect(getTeamSizes(9).sort()).to.deep.equal([5, 4].sort())
      expect(getTeamSizes(11).sort()).to.deep.equal([4, 4, 3].sort())
{'fraction_non_alphanumeric': 0.17703024125042474, 'fraction_numerical': 0.01308188922867822, 'mean_word_length': 2.5104353011329756, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1209764', 'n_tokens_mistral': 1900, 'n_tokens_neox': 1868, 'n_words': 347}