/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

/**
* @file
* @copyright 2022 MPLv2
*/

/**
* @defgroup tests
*/



#include <string>
#define CATCH_CONFIG_MAIN

#include <catch2/catch.hpp>

#include <DirectCommunicationClient.hpp>
#include <DirectCommunicationServer.hpp>
#include <loguru.hpp>

#include <memory>

/**
* @brief brief test description
* @ingroup group
*/
SCENARIO("A test scenario","[keywords]")
{
  GIVEN("Preliminaries")
  {
        DirectCommunication::DirectCommunicationServer server(5555);
        // std::this_thread::sleep_for(std::chrono::milliseconds(1000));

        DirectCommunication::DirectCommunicationClient client(5555,"127.0.0.1");
    
    std::string msg1 = "hello client";
    std::string msg2 = "hello client again";


    WHEN("doing something")
    { 
      client.sendMessage("hello server");  
      std::this_thread::sleep_for(std::chrono::milliseconds(1000));

      auto msg1FromClient = server.getLatestMessage();  
      LOG_S(INFO)<< msg1FromClient;
      LOG_S(INFO)<< server.countClients();
      THEN("expecting something to happen")
      {
        REQUIRE(server.countClients() == 1 );
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));

        REQUIRE(msg1FromClient == "hello server");
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));

        server.sendMessage(msg1);
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
        REQUIRE(client.getLatestMessage() == msg1);

        server.sendMessage(msg2);
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
        REQUIRE(client.getLatestMessage() == msg2);


      } // THEN
    } // WHEN
  } // GIVEN
} // SCENARIO
