const express = require("express");
const superRequest = require("supertest");
const chai = require("chai");
const expect = require("chai").expect;
chai.use(require("chai-json"));

function createApp() {
  app = express();

  require("../routes/api.route.ts")(
    app,
    []
  );
  require("../routes/index.route.js")(app);
  require("../routes/error.route.js")(app); // Make sure this is always last
  return app;
}

describe("Main server", function () {
  var app;

  // Called once before any of the tests in this block begin.
  before(function (done) {
    app = createApp();
    app.listen(function (err) {
      if (err) {
        return done(err);
      }
      done();
    });
  });

  it("API 404 page should return valid JSON", function (done) {
    superRequest(app)
      .get("/api/make404")
      .set("Content-Type", "application/json")
      .expect("Content-Type", /json/)
      .expect(404, function (err, res) {
        if (err) {
          return done(err);
        }
        expect(res.body).to.be.jsonObj();
        // Done
        done();
      });
  });
});
