<commit_before>const fixtures = require('../../fixtures')
const largeCapitalOpportunity = require('../../../../sandbox/fixtures/v4/investment/large-capital-opportunity-complete.json')
const selectors = require('../../../../selectors')
const {
  assertSummaryTable,
  assertBreadcrumbs,
} = require('../../support/assertions')
const {
  interactions,
  companies,
  investments,
  contacts,
  events,
} = require('../../../../../src/lib/urls')

const {
  interaction: { withReferral: interactionWithReferral },
} = fixtures

const companyObject = {
  href: companies.details(fixtures.company.venusLtd.id),
  name: fixtures.company.venusLtd.name,
}

const contactObjectTheodore = {
  href: contacts.details('71906039-858e-47ba-8016-f3c80da69ace'),
  name: '<NAME>|6e4b048d-5bb5-4868-9455-aa712f4ceffd',
}

describe('Interaction details', () => {
  context('Past draft interaction', () => {
    const params = {}

    before(() => {
      params.companyId = fixtures.company.venusLtd.id
      params.interactionId = fixtures.interaction.draftPastMeeting.id

      cy.visit(
        `/companies/${params.companyId}/interactions/${params.interactionId}`
      )
    })

    assertInteractionBreadcrumbs()
    assertHeader(fixtures.interaction.draftPastMeeting.subject)

    it('should render the details', () => {
      assertSummaryTable({
        dataTest: 'interaction-details-table',
        heading: null,
        showEditLink: false,
        content: {
          Company: companyObject,
          'Contact(s)': contactObjectTheodore,
          'Date of interaction': '20 May 2019',
          'Adviser(s)': '<NAME>, Aberdeen City Council',
        },
      })
    })

    it('should render the "Complete interaction" button', () => {
      cy.get('[data-test=complete-interaction-button]')
        .should('be.visible')
        .should('have.text', 'Complete interaction')
    })

    assertEditButtonNotVisible()
    assertCannotCompleteTextNotVisible()
  })

  context('Future draft interaction', () => {
    const params = {}

    before(() => {
      params.companyId = fixtures.company.venusLtd.id
      params.interactionId = fixtures.interaction.draftFutureMeeting.id

      cy.visit(
        `/companies/${params.companyId}/interactions/${params.interactionId}`
      )
    })

    assertInteractionBreadcrumbs()
    assertHeader(fixtures.interaction.draftFutureMeeting.subject)

    it('should render the details', () => {
      assertSummaryTable({
        dataTest: 'interaction-details-table',
        heading: null,
        showEditLink: false,
        content: {
          Company: companyObject,
          'Contact(s)': contactObjectTheodore,
          'Date of interaction': '20 May 2030',
          'Adviser(s)': '<NAME>, Aberdeen City Council',
        },
      })
    })

    assertCompleteButtonNotVisible()
    assertEditButtonNotVisible()

    it('should render the "Why can I not complete this interaction?" details summary', () => {
      cy.get('[data-test=cannot-complete-interaction]').should('be.visible')
    })
  })

  context('Cancelled interaction', () => {
    const params = {}

    before(() => {
      params.companyId = fixtures.company.venusLtd.id
      params.interactionId = fixtures.interaction.cancelledMeeting.id

      cy.visit(
        `/companies/${params.companyId}/interactions/${params.interactionId}`
      )
    })

    assertInteractionBreadcrumbs()
    assertHeader(fixtures.interaction.cancelledMeeting.subject)

    it('should render the details', () => {
      assertSummaryTable({
        dataTest: 'interaction-details-table',
        heading: null,
        showEditLink: false,
        content: {
          Company: companyObject,
          'Contact(s)': {
            href: contacts.details('e2eee6cd-acf6-454a-a4a8-f6c8fa604fde'),
            name: '<NAME>',
          },
          'Date of interaction': '11 June 2019',
          'Adviser(s)': '<NAME>, Digital Data Hub - Live Service',
        },
      })
    })

    assertCompleteButtonNotVisible()
    assertEditButtonNotVisible()
    assertCannotCompleteTextNotVisible()
  })

  context('Complete service delivery with documents', () => {
    const params = {}

    before(() => {
      params.companyId = fixtures.company.venusLtd.id
      params.interactionId = fixtures.interaction.withLink.id<commit_msg>Add tests for new complete and cancel functionality<commit_after>const fixtures = require('../../fixtures')
const largeCapitalOpportunity = require('../../../../sandbox/fixtures/v4/investment/large-capital-opportunity-complete.json')
const selectors = require('../../../../selectors')
const {
  assertSummaryTable,
  assertBreadcrumbs,
  assertFieldRadios,
  assertErrorSummary,
  assertFieldDate,
  assertFlashMessage,
} = require('../../support/assertions')
const {
  interactions,
  companies,
  investments,
  contacts,
  events,
} = require('../../../../../src/lib/urls')

const {
  interaction: { withReferral: interactionWithReferral },
} = fixtures

const companyObject = {
  href: companies.details(fixtures.company.venusLtd.id),
  name: fixtures.company.venusLtd.name,
}

const contactObjectTheodore = {
  href: contacts.details('71906039-858e-47ba-8016-f3c80da69ace'),
  name: '<NAME>|6e4b048d-5bb5-4868-9455-aa712f4ceffd',
}

describe('Interaction details', () => {
  context('Past draft interaction', () => {
    const params = {}

    before(() => {
      params.companyId = fixtures.company.venusLtd.id
      params.interactionId = fixtures.interaction.draftPastMeeting.id

      cy.visit(
        `/companies/${params.companyId}/interactions/${params.interactionId}`
      )
    })

    assertInteractionBreadcrumbs()
    assertHeader(fixtures.interaction.draftPastMeeting.subject)

    it('should render the details', () => {
      assertSummaryTable({
        dataTest: 'interaction-details-table',
        heading: null,
        showEditLink: false,
        content: {
          Company: companyObject,
          'Contact(s)': contactObjectTheodore,
          'Date of interaction': '20 May 2019',
          'Adviser(s)': '<NAME>, Aberdeen City Council',
        },
      })
    })

    it('should render the "Complete interaction" button', () => {
      cy.get('[data-test=complete-interaction-button]')
        .should('be.visible')
        .should('have.text', 'Complete interaction')
        .should(
          'have.attr',
          'href',
          companies.interactions.edit(params.companyId, params.interactionId)
        )
    })

    assertEditButtonNotVisible()
    assertCannotCompleteTextNotVisible()

    context('when cancelling the interaction', () => {
      it('should render the archive container', () => {
        cy.get('[data-test=archive-interaction-container]').should('exist')
        cy.get('[data-test=archive-button')
          .should('be.visible')
          .should('have.text', 'Cancel')
        cy.get('[data-test=archive-header]')
          .should('be.visible')
          .should('have.text', 'Cancel interaction')
        cy.get('[data-test=archive-hint]')
          .should('be.visible')
          .should(
            'have.text',
            'Cancel this interaction if the meeting did not happen'
          )
        cy.get('[data-test=field-archived_reason]').should('not.exist')
      })

      it('should render the archive reasons', () => {
        cy.get('[data-test="archive-button"]').click()
        cy.get('[data-test=field-archived_reason]')
          .should('exist')
          .then((element) => {
            assertFieldRadios({
              element,
              label: 'Cancellation reason',
              optionsCount: 3,
            })
          })
      })

      it('should not submit if no reason is selected', () => {
        cy.get('[data-test=submit-button]').click()
        assertErrorSummary(['You must select a reason'])
      })

      it('should render the date field when reschedule is selected', () => {
        cy.get('[data-test=archived-reason-meeting-was-rescheduled]').click()
        cy.get('[data-test="field-date"]')
          .should('exist')
          .then((element) => {
            assertFieldDate({
              element,
              label: 'When will the meeting take place?',
              hint: 'This will change the date of the interaction rather than cancelling it.',
              value: { day: '', month: '', year: '' },
            })
          })
      })

      it('should not submit if reschedule is selected and no date is entered', () => {
        cy.get('[data-test=submit-button]').click()
        assertErrorSummary(['You must enter a valid date'])
      })

      it('should submit the form if a reason is seleted', () => {
        cy.get('[data-test=archived-reason-client-cancelled]').click()
        cy.get('[data-test=submit-button]').click()
        assertFlashMessage('The interaction has been updated')
      })
    })
  })

  context('Future draft interaction', () => {
    const params = {}

    before(() => {
      params.companyId = fixtures.company.venusLtd.id
      params.interactionId = fixtures.interaction.draftFutureMeeting.id

      cy.visit(
        `/companies/${params.companyId}/interactions/${params.interactionId}`
      )
    })

    assertInteractionBreadcrumbs()
    assertHeader(fixtures.interaction.draftFutureMeeting.subject)

    it('should render the details', () => {
      assertSummaryTable({
        dataTest: 'interaction-details-table',
        heading: null,
        showEditLink: false,
        content: {
          Company: companyObject,
          'Contact(s)': contactObjectTheodore,
          'Date of interaction': '20 May 2030',
          'Adviser(s)': '<NAME>, Aberdeen City Council',
        },
      })
    })

    assertCompleteButtonNotVisible()
    assertEditButtonNotVisible()

    it('should render the "Why can I not complete this interaction?" details summary', () => {
      cy.get('[data-test=cannot-complete-interaction]').should('be.visible')
    })
  })

  context('Cancelled interaction', () => {
    const params = {}

    before(() => {
      params.companyId = fixtures.company.venusLtd.id
      params.interactionId = fixtures.interaction.cancelledMeeting.id

      cy.visit(
        `/companies/${params.companyId}/interactions/${params.interactionId}`
      )
    })

    assertInteractionBreadcrumbs()
    assertHeader(fixtures.interaction.cancelledMeeting.subject)

    it('should render the archive panel', () => {
      cy.get('[data-test=archive-panel]').should('exist')
      cy.get('[data-test=archive-message]')
        .should('be.visible')
        .should(
          'have.text',
          'This interaction was cancelled on 12 Jun 2019 by <NAME>.'
        )
      cy.get('[data-test=archive-reason]')
        .should('be.visible')
        .should('have.text', 'Reason: Client cancelled')
      cy.get('[data-test=unarchive-link]').should('not.exist')
    })

    it('should render the details', () => {
      assertSummaryTable({
        dataTest: 'interaction-details-table',
        heading: null,
        showEditLink: false,
        content: {
          Company: companyObject,
          'Contact(s)': {
            href: contacts.details('e2eee6cd-acf6-454a-a4a8-f6c8fa604fde'),
            name: '<NAME>',
          },
          'Date of interaction': '11 June 2019',
          'Adviser(s)': '<NAME>, Digital Data Hub - Live Service',
        },
      })
    })

    assertCompleteButtonNotVisible()
    assertEditButtonNotVisible()
    assertCannotCompleteTextNotVisible()
  })

  context('Complete service delivery with documents', () => {
    const params = {}

    before(() => {
      params.companyId = fixtures.company.venusLtd.id
      params.interactionId = fixtures.interaction.withLink.id
{'fraction_non_alphanumeric': 0.14773792280821335, 'fraction_numerical': 0.013035699071312941, 'mean_word_length': 2.6351811706410655, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 52, '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': '7388991', 'n_tokens_mistral': 3561, 'n_tokens_neox': 3412, 'n_words': 597}