import supertest from 'supertest';
import path from 'path';
import fsp from 'fs/promises';

import { TestHelpers } from '@squiz/render-runtime-lib';
import { startDevelopmentRender } from './component-dev';
import { Server } from 'http';

jest.setTimeout(20_000);

describe('component-dev', () => {
  describe('production style folder structure', () => {
    let server: Server;
    let request: supertest.SuperTest<supertest.Test>;
    beforeAll(async () => {
      server = startDevelopmentRender(TestHelpers.getTestComponentFolder(), { port: 0, noBrowser: true });
      request = supertest(server);
    });

    afterAll(async () => {
      server.close();
    });

    it('should find the component', async () => {
      const response = await request.get(
        `/r/unit-test-components/test-component/1.0.10?_componentSet=set&something=hello`,
      );
      expect(response.text).toEqual('<div>hello - {}</div>');
    });
  });

  describe('local developer style folders', () => {
    let version: string;
    let componentName: string;
    let fixtureDirectory: string;
    let server: Server;
    let request: supertest.SuperTest<supertest.Test>;

    beforeAll(async () => {
      const {
        fixtureDirectory: createdFixtureDirectory,
        version: createdVersion,
        componentName: createdName,
      } = await TestHelpers.ComponentFixture.setupFullComponentDirectory('<h1>Hello World</h1>');

      version = createdVersion;
      fixtureDirectory = createdFixtureDirectory;
      componentName = createdName;

      server = startDevelopmentRender(fixtureDirectory, { port: 0, noBrowser: true });
      request = supertest(server);
    });
    afterAll(async () => {
      server.close();
    });

    afterAll(async () => {
      await fsp.rm(path.join(fixtureDirectory, componentName), { force: true, recursive: true });
    });

    it('should handle recursing folders to find the component', async () => {
      const response = await request.get(`/r/some-namespace/${componentName}/${version}?_componentSet=set`);
      expect(response.text).toEqual('<h1>Hello World</h1>');
    }, 30_000);

    it('should handle serving static files from single component directory', async () => {
      const response = await request.get(
        `/s/some-namespace/${componentName}/${version}/static.txt?_componentSet=set-x`,
      );
      expect(response.text).toEqual('hello');
    });
  });

  describe('It should pass a simple test and not launch the browser if the -nb flag is present', () => {
    let server: Server;
    let request: supertest.SuperTest<supertest.Test>;
    beforeAll(async () => {
      server = startDevelopmentRender(TestHelpers.getTestComponentFolder(), { port: 0, noBrowser: true });
      request = supertest(server);
    });
    afterAll(async () => {
      server.close();
    });
    it('should find the component', async () => {
      const response = await request.get(
        `/r/unit-test-components/test-component/1.0.10?_componentSet=set&something=hello`,
      );
      expect(response.text).toEqual('<div>hello - {}</div>');
    });
  });
});
