/*
 * Copyright 2019 Adobe. All rights reserved.
 * This file is licensed to you 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 http://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 REPRESENTATIONS
 * OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */

/* eslint-env mocha */
import assert from 'assert';
import { Request } from '@adobe/fetch';
import { main } from '../src/index.js';
import { eloquaForm } from './resources/eloqua.js';
import claim from '../src/cache/claim.js';
import { volvotrucks, volvotrucksWithoutLabels } from './resources/volvo.js';

const endpoint = 'https://localhost/';
const headers = { 'content-type': 'application/json' };
const url = 'https://www.volvotrucks.us/blocks/eloqua-form/forms/event-from.html';

describe('API Tests', () => {
  it('Invalid URL for importer', async () => {
    const result = await main(new Request('https://localhost/'), {});
    assert.strictEqual(result.status, 400);
    assert.strictEqual(await result.text(), 'Missing or invalid url');
  });

  it('importer without labels', async () => {
    const body = { url };
    const result = await main(new Request(endpoint, { method: 'POST', headers, body: JSON.stringify(body) }), {});
    assert.strictEqual(result.status, 200);
    const forms = await result.json();
    assert.strictEqual(forms.length, 1);
    const deedForm = forms[0];
    assert.deepEqual(deedForm, volvotrucksWithoutLabels);
  }).timeout(5000);

  it('Import with labels', async () => {
    const body = { url, options: { includePlainText: true } };
    const result = await main(new Request(endpoint, { method: 'POST', headers, body: JSON.stringify(body) }), {});
    assert.strictEqual(result.status, 200);
    const forms = await result.json();
    assert.strictEqual(forms.length, 1);
    const deedForm = forms[0];
    assert.deepEqual(deedForm, volvotrucks);
  }).timeout(5000);

  it('Import with user agent', async () => {
    const body = { url, options: { includePlainText: true, userAgent: 'Helix-Forms-Importer' } };
    const result = await main(new Request(endpoint, { method: 'POST', headers, body: JSON.stringify(body) }), {});
    assert.strictEqual(result.status, 200);
    const forms = await result.json();
    assert.strictEqual(forms.length, 1);
    const deedForm = forms[0];
    assert.deepEqual(deedForm, volvotrucks);
  }).timeout(5000);

  it('Import eloqua Form', async () => {
    const vovloUrl = 'https://www.volvotrucks.us/blocks/eloqua-form/forms/event-from.html';
    const body = { url: vovloUrl, options: { includePlainText: true } };
    const result = await main(new Request(endpoint, { method: 'POST', headers, body: JSON.stringify(body) }), {});
    assert.strictEqual(result.status, 200);
    const forms = await result.json();
    assert.strictEqual(forms.length, 1);
    const resultForm = forms[0];
    assert.deepEqual(resultForm, eloquaForm);
  }).timeout(5000);

  it('Import cached claim Form', async () => {
    const claimUrl = 'https://www.stewart.com/en/contact-us/submit-a-claim.html';
    const body = { url: claimUrl, options: { includePlainText: true, groupBySelector: '.contentcontainer' } };
    const result = await main(new Request(endpoint, { method: 'POST', headers, body: JSON.stringify(body) }), {});
    assert.strictEqual(result.status, 200);
    const forms = await result.json();
    assert.strictEqual(forms.length, 3);
    assert.deepEqual(forms, claim);
  }).timeout(5000);
});
