import { tap, expect } from '@git.zone/tstest/tapbundle';

import * as uiPublicServer from '../ts/npm-publicserver.classes.uipublicserver.js';
import * as smartnetwork from '@push.rocks/smartnetwork';
import { SmartRequest } from '@push.rocks/smartrequest';

const plugins = {
  smartnetwork,
};

let testserverInstance: uiPublicServer.UiPublicServer;

tap.test('should create an instance of PublicServer', async () => {
  testserverInstance = new uiPublicServer.UiPublicServer({
    npmRegistryUrl: 'https://registry.npmjs.org',
    port: 3000,
    allowedPackages: ['@pushrocks/smartfile'],
    mode: 'prod',
  });
  expect(testserverInstance).toBeInstanceOf(uiPublicServer.UiPublicServer);
});

tap.test('should start the server', async () => {
  const smartnetworkInstance = new plugins.smartnetwork.SmartNetwork();
  const result1 = await smartnetworkInstance.isLocalPortUnused(3000);
  await testserverInstance.startServer();
  const result2 = await smartnetworkInstance.isLocalPortUnused(3000);
  expect(result1).toBeTrue();
  expect(result2).toBeFalse();
});

tap.skip.test('optional manual testing', async (toolsArg) => {
  await toolsArg.delayFor(30000);
});

tap.test('should NOT deliver a file for a malformed org', async () => {
  const response = await SmartRequest.create()
    .url('http://localhost:3000/someorg/somemodule')
    .get();
  const body = await response.text();
  console.log(body);
  expect(body).toEqual('npmorg "someorg" must start with @');
});

tap.test('should NOT deliver a file for a nonexisting file', async () => {
  const response = await SmartRequest.create()
    .url('http://localhost:3000/@pushrocks/smartfile/readme2.md')
    .get();
  const body = await response.text();
  console.log(body);
  expect(body.includes('@pushrocks/smartfile@ does not have a file at')).toBeTrue();
});

tap.test('should deliver a file for an existing file', async () => {
  const response = await SmartRequest.create()
    .url('http://localhost:3000/@pushrocks/smartfile/readme.md')
    .get();
  const body = await response.text();
  expect(body.startsWith('# @pushrocks/smartfile')).toBeTrue();
});

tap.test('should deliver different versions', async () => {
  const response = await SmartRequest.create()
    .url('http://localhost:3000/@pushrocks/smartfile/package.json?version=7')
    .get();
  const packageJson = await response.json();
  expect(packageJson.version.startsWith('7')).toBeTrue();

  const response2 = await SmartRequest.create()
    .url('http://localhost:3000/@pushrocks/smartfile/package.json?version=8.x.x')
    .get();
  const packageJson2 = await response2.json();
  expect(packageJson2.version.startsWith('8')).toBeTrue();

  const response3 = await SmartRequest.create()
    .url('http://localhost:3000/@pushrocks/smartfile/package.json?version=6.0.6')
    .get();
  const packageJson3 = await response3.json();
  expect(packageJson3.version).toEqual('6.0.6');
});

tap.test('should stop the server', async () => {
  await testserverInstance.stopServer();
});

export default tap.start();
