/* eslint-disable func-names, prefer-arrow-callback */

const Flint = require('../../../index.js');
const request = require('supertest');
const expect = require('chai').expect;
const populateDB = require('../../populatedb');
const mongoose = require('mongoose');
const mocks = require('../../mocks');
const fs = require('fs');
const { promisify } = require('util');
const path = require('path');

const readFile = promisify(fs.readFile);

describe('Compile templates', function () {
  let server;

  before('Creates a server and populates the db', async function () {
    const flintServer = new Flint({ templatePath: 'test/fixtures' });
    server = await flintServer.startServer();
    return populateDB();
  });

  it('returns the index.njk template for the homepage', async function () {
    const res = await request(server).get('/');
    const pathToFile = path.join(__dirname, '..', '..', 'fixtures', 'index.txt');
    const file = await readFile(pathToFile, 'utf-8');
    expect(res.text).to.not.equal('no-template');
    expect(res.text).to.equal(file);
  });

  it('returns the `no-template` when the requested template does not exist', async function () {
    const res = await request(server).get('/no-template');
    expect(res.status).to.equal(302);
    expect(res.text).to.equal('Found. Redirecting to /admin/error?r=no-template&p=/no-template&t=template-no-exist');
  });

  it('returns 404 when a page does not exist', async function () {
    const res = await request(server).get('/pizza');
    const pathToFile = path.join(__dirname, '..', '..', 'fixtures', '404.txt');
    const file = await readFile(pathToFile, 'utf-8');
    expect(res.status).to.equal(404);
    expect(res.text).to.equal(file);
  });

  it('returns a page with variables', async function () {
    const res = await request(server).get('/page-with-vars');
    const pathToFile = path.join(__dirname, '..', '..', 'fixtures', 'page-with-vars.txt');
    const file = await readFile(pathToFile, 'utf-8');
    expect(res.status).to.equal(200);
    expect(res.text).to.equal(file);
  });

  it('returns an entry in a section', async function () {
    const url = `/${mocks.sections[0].slug}/${mocks.entries[3].slug}`;
    const res = await request(server).get(url);
    const pathToFile = path.join(__dirname, '..', '..', 'fixtures', 'entry.txt');
    let file = await readFile(pathToFile, 'utf-8');
    file = file.replace(new RegExp('{{ this.title }}', 'g'), mocks.entries[3].title);
    expect(res.status).to.equal(200);
    expect(res.text).to.equal(file);
  });

  after((done) => {
    mongoose.disconnect();
    done();
  });
});

@@ -629,14 +629,24 @@
 ures
+/templates
 ' %7D);%0A
-
     
@@ -910,16 +910,29 @@
 xtures',
+ 'templates',
  'index.
@@ -1592,16 +1592,29 @@
 xtures',
+ 'templates',
  '404.tx
@@ -1941,16 +1941,29 @@
 xtures',
+ 'templates',
  'page-w
@@ -2358,16 +2358,29 @@
 xtures',
+ 'templates',
  'entry.
@@ -2654,20 +2654,12 @@
 ect(
-);%0A    
 done
-(
 );%0A 

Update compiler tests