#include "validation/sensor_validator.hpp"

#include <gtest/gtest.h>

TEST(SensorValidator, LidarRequiresNameIpPort)
{
  std::string err;
  EXPECT_FALSE(lm::SensorValidator::validateLidarPayload(nlohmann::json::object(), err));
  EXPECT_FALSE(err.empty());

  err.clear();
  EXPECT_TRUE(lm::SensorValidator::validateLidarPayload(
      {{"name", "front"}, {"ip", "192.168.1.10"}, {"port", 2112}}, err))
      << err;
}

TEST(SensorValidator, LidarPortRange)
{
  std::string err;
  EXPECT_FALSE(lm::SensorValidator::validateLidarPayload(
      {{"name", "front"}, {"ip", "192.168.1.10"}, {"port", 70000}}, err));
}

TEST(SensorValidator, ImuRequiresFrameAndTopic)
{
  std::string err;
  EXPECT_FALSE(lm::SensorValidator::validateImuPayload({{"name", "imu1"}}, err));

  err.clear();
  EXPECT_TRUE(lm::SensorValidator::validateImuPayload(
      {{"name", "imu1"}, {"frame_id", "imu_link"}, {"topic", "/imu/data"}}, err))
      << err;
}

TEST(SensorValidator, ImuInvalidSource)
{
  std::string err;
  EXPECT_FALSE(lm::SensorValidator::validateImuPayload(
      {{"name", "imu1"},
       {"frame_id", "imu_link"},
       {"topic", "/imu/data"},
       {"source", "invalid_source"}},
      err));
}

TEST(SensorValidator, LidarTripletDuplicateDetection)
{
  const nlohmann::json state = {
      {"lidars",
       nlohmann::json::array({{{"id", "l1"}, {"name", "front"}, {"ip", "10.0.0.1"}, {"port", 2112}}})}};
  EXPECT_TRUE(lm::SensorValidator::lidarTripletExists(state, "front", "10.0.0.1", 2112));
  const std::string exclude = "l1";
  EXPECT_FALSE(lm::SensorValidator::lidarTripletExists(state, "front", "10.0.0.1", 2112, &exclude));
}

TEST(SensorValidator, ImuFrameDuplicateDetection)
{
  const nlohmann::json state = {
      {"imus", nlohmann::json::array({{{"id", "i1"}, {"frame_id", "base_imu"}}})}};
  EXPECT_TRUE(lm::SensorValidator::imuFrameExists(state, "base_imu"));
  const std::string exclude = "i1";
  EXPECT_FALSE(lm::SensorValidator::imuFrameExists(state, "base_imu", &exclude));
}
