// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

import assert from 'assert';
import supertest from 'supertest';
import app, {buildRenderedHtml} from '../app.js';

describe('Editor unit tests', () => {
  describe('Initialize app', () => {
    it('should successfully load the index page', async () => {
      const request = supertest(app);
      await request.get('/').retry(3).expect(200);
    });
  });

  describe('Handlebars compiler', async () => {
    let template;

    before(async () => {
      template = await buildRenderedHtml();
    });

    it('includes HTML from the templates', () => {
      const htmlString = template.includes('<title>Markdown Editor</title>');
      assert.equal(htmlString, true);
    });
  });
});

describe('Integration tests', () => {
  describe('Render request', () => {
    let request;

    before(async () => {
      process.env.EDITOR_UPSTREAM_RENDER_URL = 'https://www.example.com/';
      request = supertest(app);
    });

    it('responds 404 Not Found on "GET /render"', async () => {
      await request.get('/render').retry(3).expect(404);
    });

    // SKIP: this test is trying to call out to the RENDER_URL, which does not
    // accept POST requests. TODO: setup correct mocking/workaround.
    it.skip('responds 200 OK on "POST /render" with valid JSON', async () => {
      // A valid type will make a request to the /render endpoint.
      // TODO: This test outputs a JSON parsing SyntaxError from supertest but does not fail the assert.
      await request
        .post('/render')
        .type('json')
        .set('Accept', 'text/html')
        .send({data: 'markdown'})
        .retry(3)
        .expect(200)
        .expect('content-type', 'text/html; charset=utf-8');
    });

    it('responds 400 Bad Request on "POST /render" without valid JSON', async () => {
      // An incorrect type will not successfully make a request and will print an error in the console.
      await request
        .post('/render')
        .type('json')
        .send('string: incorrect data type')
        .retry(3)
        .expect(400);
    });
  });
});
