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

import nock from 'nock'
import fields from '../query'
import r from '../../../../../db/connect'
import factory from '../../../../../test/factories'
import {withDBCleanup, runGraphQLQuery, useFixture} from '../../../../../test/helpers'

describe(testContext(__filename), function () {
  withDBCleanup()
  useFixture.buildSurvey()

  beforeEach('Setup Survey Data', async function () {
    try {
      const teamQuestion = await factory.create('question', {
        responseType: 'relativeContribution',
        subjectType: 'team'
      })
      const playerQuestion = await factory.create('question', {
        body: 'What is one thing {{subject}} did well?',
        responseType: 'text',
        subjectType: 'player'
      })
      await this.buildSurvey([
        {questionId: teamQuestion.id, subject: () => this.teamPlayerIds},
        {questionId: playerQuestion.id, subject: () => this.teamPlayerIds[1]},
      ])
      this.currentUser = await factory.build('user', {id: this.teamPlayerIds[0]})

      const idmUsers = await Promise.all(this.teamPlayerIds.map(id => factory.build('user', {id})))
      nock(process.env.IDM_BASE_URL)
        .post('/graphql')
        .reply(200, JSON.stringify({data: {getUsersByIds: idmUsers}}))
    } catch (e) {
      throw (e)
    }
  })

  afterEach(function () {
    nock.cleanAll()
  })

  describe('getRetrosoectiveSurveyQuestion', function () {
    it('gets a single question from the survey by index', function () {
      const questionNumber = 2 // <-- 1-based arg
      const questionIndex = 1 // <-- 0-based index

      return runGraphQLQuery(
        `query($questionNumber: Int!) {
          getRetrospectiveSurveyQuestion(questionNumber: $questionNumber) {
            ... on SurveyQuestionInterface {
              id subjectType responseType body
            }
            ... on SinglePartSubjectSurveyQuestion {
              subject { id name handle }
            }
            ... on MultiPartSubjectSurveyQuestion {
              subject { id name handle }
            }
          }
        }
        `,
        fields,
        {questionNumber},
        {currentUser: this.currentUser}
      ).then(result =>
        expect(result.data.getRetrospectiveSurveyQuestion)
          .to.have.property('id', this.survey.questionRefs[questionIndex].questionId)
      )
    })
  })

  describe('getRetrospectiveSurvey', function () {
    it('returns the survey for the correct cycle and project for the current user', function () {
      return runGraphQLQuery(
        `query {
          getRetrospectiveSurvey {
            id
            cycle { id }
            project { id }
            questions {
              ... on SurveyQuestionInterface {
                id subjectType responseType body
              }
              ... on SinglePartSubjectSurveyQuestion {
                subject { id name handle }
                response { value }
              }
              ... on MultiPartSubjectSurveyQuestion {
                subject { id name handle }
                response { value }
              }
            }
          }
        }
        `,
        fields,
        undefined,
        {currentUser: this.currentUser}
      ).then(result =>
        expect(result.data.getRetrospectiveSurvey.id).to.eq(this.survey.id)
      )
    })

    it('renders the question body as a template', function () {
      return runGraphQLQuery(
        `query {
          getRetrospectiveSurvey {
            questions {
              ... on SurveyQuestionInterface { body }
              ... on SinglePartSubjectSurveyQuestion { subject { handle } }
            }
          }
        }
        `,
        fields,
        undefined,
        {currentUser: this.currentUser}
      )
      .then(result =>
        expect(result.data.getRetrospectiveSurvey.questions[1].body)
          .to.contain(result.data.getRetrospectiveSurvey.questions[1].subject.handle)
      )
    })

    it('returns a meaningful error when lookup fails', function () {
      return r.table('surveys').get(this.survey.id).delete()
        .then(() => expect(
          runGraphQLQuery('query { getRetrospectiveSurvey { id } }',
            fields,
            undefined,
            {currentUser: this.currentUser}
          )
        ).to.be.rejectedWith(/no retrospective survey/))
    })
  })
})<commit_msg>checking that handle is prefixed by @ in question body<commit_after>/* eslint-env mocha */
/* global expect, testContext */
/* eslint-disable prefer-arrow-callback, no-unused-expressions */

import nock from 'nock'
import fields from '../query'
import r from '../../../../../db/connect'
import factory from '../../../../../test/factories'
import {withDBCleanup, runGraphQLQuery, useFixture} from '../../../../../test/helpers'

describe(testContext(__filename), function () {
  withDBCleanup()
  useFixture.buildSurvey()

  beforeEach('Setup Survey Data', async function () {
    try {
      const teamQuestion = await factory.create('question', {
        responseType: 'relativeContribution',
        subjectType: 'team'
      })
      const playerQuestion = await factory.create('question', {
        body: 'What is one thing {{subject}} did well?',
        responseType: 'text',
        subjectType: 'player'
      })
      await this.buildSurvey([
        {questionId: teamQuestion.id, subject: () => this.teamPlayerIds},
        {questionId: playerQuestion.id, subject: () => this.teamPlayerIds[1]},
      ])
      this.currentUser = await factory.build('user', {id: this.teamPlayerIds[0]})

      const idmUsers = await Promise.all(this.teamPlayerIds.map(id => factory.build('user', {id})))
      nock(process.env.IDM_BASE_URL)
        .post('/graphql')
        .reply(200, JSON.stringify({data: {getUsersByIds: idmUsers}}))
    } catch (e) {
      throw (e)
    }
  })

  afterEach(function () {
    nock.cleanAll()
  })

  describe('getRetrosoectiveSurveyQuestion', function () {
    it('gets a single question from the survey by index', function () {
      const questionNumber = 2 // <-- 1-based arg
      const questionIndex = 1 // <-- 0-based index

      return runGraphQLQuery(
        `query($questionNumber: Int!) {
          getRetrospectiveSurveyQuestion(questionNumber: $questionNumber) {
            ... on SurveyQuestionInterface {
              id subjectType responseType body
            }
            ... on SinglePartSubjectSurveyQuestion {
              subject { id name handle }
            }
            ... on MultiPartSubjectSurveyQuestion {
              subject { id name handle }
            }
          }
        }
        `,
        fields,
        {questionNumber},
        {currentUser: this.currentUser}
      ).then(result =>
        expect(result.data.getRetrospectiveSurveyQuestion)
          .to.have.property('id', this.survey.questionRefs[questionIndex].questionId)
      )
    })
  })

  describe('getRetrospectiveSurvey', function () {
    it('returns the survey for the correct cycle and project for the current user', function () {
      return runGraphQLQuery(
        `query {
          getRetrospectiveSurvey {
            id
            cycle { id }
            project { id }
            questions {
              ... on SurveyQuestionInterface {
                id subjectType responseType body
              }
              ... on SinglePartSubjectSurveyQuestion {
                subject { id name handle }
                response { value }
              }
              ... on MultiPartSubjectSurveyQuestion {
                subject { id name handle }
                response { value }
              }
            }
          }
        }
        `,
        fields,
        undefined,
        {currentUser: this.currentUser}
      ).then(result =>
        expect(result.data.getRetrospectiveSurvey.id).to.eq(this.survey.id)
      )
    })

    it('renders the question body as a template', function () {
      return runGraphQLQuery(
        `query {
          getRetrospectiveSurvey {
            questions {
              ... on SurveyQuestionInterface { body }
              ... on SinglePartSubjectSurveyQuestion { subject { handle } }
            }
          }
        }
        `,
        fields,
        undefined,
        {currentUser: this.currentUser}
      )
      .then(result => {
        const question = result.data.getRetrospectiveSurvey.questions[1]
        expect(question.body).to.contain(`@${question.subject.handle}`)
      })
    })

    it('returns a meaningful error when lookup fails', function () {
      return r.table('surveys').get(this.survey.id).delete()
        .then(() => expect(
          runGraphQLQuery('query { getRetrospectiveSurvey { id } }',
            fields,
            undefined,
            {currentUser: this.currentUser}
          )
        ).to.be.rejectedWith(/no retrospective survey/))
    })
  })
})
{'fraction_non_alphanumeric': 0.12221975088967972, 'fraction_numerical': 0.002335409252669039, 'mean_word_length': 1.9019038399483705, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, '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': '6266657', 'n_tokens_mistral': 2436, 'n_tokens_neox': 2382, 'n_words': 520}