<commit_before>/* eslint-disable no-unused-expressions */
const nock = require('nock');
const cheerio = require('cheerio');
const chai = require('chai');
const chaiHttp = require('chai-http');
const server = require('../../server');
const getSampleResponse = require('../unit/lib/getSampleResponse');

const expect = chai.expect;

chai.use(chaiHttp);

function checkHtmlResponse(err, res) {
  expect(err).to.be.null;
  expect(res).to.have.status(200);
  expect(res).to.be.html;
}

describe('The default page', () => {
  it('should return the link for stomach ache', (done) => {
    chai.request(server)
      .get('/')
      .end((err, res) => {
        checkHtmlResponse(err, res);

        const $ = cheerio.load(res.text);

        // Hardly robust but the page is in question of even existing
        expect($('ul.list-bullet li a').text())
          .to.equal('Stomach Ache');
        done();
      });
  });
});

describe('The stomach ache page', () => {
  it('should contain content for stomach ache', (done) => {
    chai.request(server)
      .get('/stomach-ache')
      .end((err, res) => {
        checkHtmlResponse(err, res);

        const $ = cheerio.load(res.text);

        expect($('.heading-xlarge').text())
          .to.equal('Stomach ache');
        done();
      });
  });
});

describe('The search page', () => {
  it('should provide a prompt to enter a postcode', (done) => {
    chai.request(server)
      .get('/search')
      .query({ able: 'true' })
      .end((err, res) => {
        checkHtmlResponse(err, res);

        const $ = cheerio.load(res.text);

        expect($('.heading-xlarge').text())
          .to.equal('Enter a postcode');
        done();
      });
  });

  it('should return a call 111 page when people are not able to get there',
    (done) => {
      chai.request(server)
        .get('/search')
        .query({ able: 'false' })
        .end((err, res) => {
          checkHtmlResponse(err, res);

          const $ = cheerio.load(res.text);

          expect($('.heading-xlarge').text())
            .to.equal('Call NHS 111');
          done();
        });
    });
});

describe('The results routes', () => {
  let originalUrl = '';
  let originalApikey = '';
  const baseUrl = 'http://web.site';
  const apikey = 'secret';
  const validPostcode = 'AB123CD';
  const postcodeSearchPath =
    new RegExp(`/organisations/pharmacies/postcode/${validPostcode}`);

  before('setup environment variables', () => {
    originalUrl = process.env.NHSCHOICES_SYNDICATION_BASEURL;
    originalApikey = process.env.NHSCHOICES_SYNDICATION_APIKEY;

    process.env.NHSCHOICES_SYNDICATION_BASEURL = baseUrl;
    process.env.NHSCHOICES_SYNDICATION_APIKEY = apikey;
  });

  after('reset environemnt variables', () => {
    process.env.NHSCHOICES_SYNDICATION_BASEURL = originalUrl;
    process.env.NHSCHOICES_SYNDICATION_APIKEY = originalApikey;
  });


  describe('The results route', () => {
    it('should return 10 results', (done) => {
      const postcodeSearchResponse = getSampleResponse('paged_pharmacies_postcode_search');
      const overviewResponse = getSampleResponse('gp_overview');
      const allResultsRoute = '/results';

      const postcodeSearchScope =
        nock(baseUrl)
          .get(postcodeSearchPath)
          .query(true)
          .times(10)
          .reply(200, postcodeSearchResponse);

      const overviewScope =
        nock(/.*nhschoices.*/)
          .get(/organisations\/pharmacies\/\d+\/overview\.xml/)
          .query(true)
          .times(10)
          .reply(200, overviewResponse);

      chai.request(server)
        .get(allResultsRoute)
        .query({ location: validPostcode })
        .end((err, res) => {
          checkHtmlResponse(err, res);

          const $ = cheerio.load(res.text);

          // Some arbitary element to suggest there are 2 results<commit_msg>:page_facing_up: Use correct pharmacy xml resource<commit_after>/* eslint-disable no-unused-expressions */
const nock = require('nock');
const cheerio = require('cheerio');
const chai = require('chai');
const chaiHttp = require('chai-http');
const server = require('../../server');
const getSampleResponse = require('../unit/lib/getSampleResponse');

const expect = chai.expect;

chai.use(chaiHttp);

function checkHtmlResponse(err, res) {
  expect(err).to.be.null;
  expect(res).to.have.status(200);
  expect(res).to.be.html;
}

describe('The default page', () => {
  it('should return the link for stomach ache', (done) => {
    chai.request(server)
      .get('/')
      .end((err, res) => {
        checkHtmlResponse(err, res);

        const $ = cheerio.load(res.text);

        // Hardly robust but the page is in question of even existing
        expect($('ul.list-bullet li a').text())
          .to.equal('Stomach Ache');
        done();
      });
  });
});

describe('The stomach ache page', () => {
  it('should contain content for stomach ache', (done) => {
    chai.request(server)
      .get('/stomach-ache')
      .end((err, res) => {
        checkHtmlResponse(err, res);

        const $ = cheerio.load(res.text);

        expect($('.heading-xlarge').text())
          .to.equal('Stomach ache');
        done();
      });
  });
});

describe('The search page', () => {
  it('should provide a prompt to enter a postcode', (done) => {
    chai.request(server)
      .get('/search')
      .query({ able: 'true' })
      .end((err, res) => {
        checkHtmlResponse(err, res);

        const $ = cheerio.load(res.text);

        expect($('.heading-xlarge').text())
          .to.equal('Enter a postcode');
        done();
      });
  });

  it('should return a call 111 page when people are not able to get there',
    (done) => {
      chai.request(server)
        .get('/search')
        .query({ able: 'false' })
        .end((err, res) => {
          checkHtmlResponse(err, res);

          const $ = cheerio.load(res.text);

          expect($('.heading-xlarge').text())
            .to.equal('Call NHS 111');
          done();
        });
    });
});

describe('The results routes', () => {
  let originalUrl = '';
  let originalApikey = '';
  const baseUrl = 'http://web.site';
  const apikey = 'secret';
  const validPostcode = 'AB123CD';
  const postcodeSearchPath =
    new RegExp(`/organisations/pharmacies/postcode/${validPostcode}`);

  before('setup environment variables', () => {
    originalUrl = process.env.NHSCHOICES_SYNDICATION_BASEURL;
    originalApikey = process.env.NHSCHOICES_SYNDICATION_APIKEY;

    process.env.NHSCHOICES_SYNDICATION_BASEURL = baseUrl;
    process.env.NHSCHOICES_SYNDICATION_APIKEY = apikey;
  });

  after('reset environemnt variables', () => {
    process.env.NHSCHOICES_SYNDICATION_BASEURL = originalUrl;
    process.env.NHSCHOICES_SYNDICATION_APIKEY = originalApikey;
  });


  describe('The results route', () => {
    it('should return 10 results', (done) => {
      const postcodeSearchResponse = getSampleResponse('paged_pharmacies_postcode_search');
      const overviewResponse = getSampleResponse('pharmacy_opening_times');
      const allResultsRoute = '/results';

      const postcodeSearchScope =
        nock(baseUrl)
          .get(postcodeSearchPath)
          .query(true)
          .times(10)
          .reply(200, postcodeSearchResponse);

      const overviewScope =
        nock(/.*nhschoices.*/)
          .get(/organisations\/pharmacies\/\d+\/overview\.xml/)
          .query(true)
          .times(10)
          .reply(200, overviewResponse);

      chai.request(server)
        .get(allResultsRoute)
        .query({ location: validPostcode })
        .end((err, res) => {
          checkHtmlResponse(err, res);

          const $ = cheerio.load(res.text);

          // Some arbitary element to suggest there are 2 results
{'fraction_non_alphanumeric': 0.15958815958815958, 'fraction_numerical': 0.006435006435006435, 'mean_word_length': 2.6724952741020793, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 3}, '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': '321494', 'n_tokens_mistral': 2489, 'n_tokens_neox': 2367, 'n_words': 484}