// Copyright 2025
// Unit tests for CerebrumNode utility helpers: ParseListString, PickRandomActionSkills, ParseResultDetail

#include <gtest/gtest.h>
#include <rclcpp/rclcpp.hpp>
#include "brain/cerebrum_node.hpp"

using brain::CerebrumNode;

class CerebrumNodeFixture : public ::testing::Test
{
protected:
  void SetUp() override
  {
    rclcpp::init(0, nullptr);
    node_ = std::make_shared<CerebrumNode>();
  }
  void TearDown() override
  {
    if (rclcpp::ok()) {rclcpp::shutdown();}
  }
  std::shared_ptr<CerebrumNode> node_;
};

TEST(CerebrumNodeStandalone, ParseListStringBasic) {
  auto v = CerebrumNode::ParseListString("a,b;c  d\t e,a");
  ASSERT_EQ(v.size(), 5u);
  EXPECT_EQ(v[0], "a");
  EXPECT_EQ(v[1], "b");
  EXPECT_EQ(v[2], "c");
  EXPECT_EQ(v[3], "d");
  EXPECT_EQ(v[4], "e");
}

TEST(CerebrumNodeStandalone, ParseListStringEmpty) {
  auto v = CerebrumNode::ParseListString("   ,, ; ");
  EXPECT_TRUE(v.empty());
}

TEST(CerebrumNodeStandalone, ParseResultDetail) {
  EXPECT_EQ(
    (int)CerebrumNode::ParseResultDetail(
      "OK: done"), (int)CerebrumNode::ExecResultCode::SUCCESS);
  EXPECT_EQ(
    (int)CerebrumNode::ParseResultDetail(
      "FAIL something"), (int)CerebrumNode::ExecResultCode::FAILURE);
  EXPECT_EQ(
    (int)CerebrumNode::ParseResultDetail(
      "CANCEL user"), (int)CerebrumNode::ExecResultCode::CANCELLED);
  EXPECT_EQ(
    (int)CerebrumNode::ParseResultDetail(
      "code=0 all good"), (int)CerebrumNode::ExecResultCode::SUCCESS);
  EXPECT_EQ(
    (int)CerebrumNode::ParseResultDetail(
      "code=cancel requested"), (int)CerebrumNode::ExecResultCode::CANCELLED);
  EXPECT_EQ(
    (int)CerebrumNode::ParseResultDetail(
      "mystery"), (int)CerebrumNode::ExecResultCode::UNKNOWN);
}

// NOTE: Removed PickRandomNoSkills test. The CerebrumNode constructor loads skills, so
// asserting an empty random selection is invalid and caused intermittent failures.
// If needed in the future, inject a mock SkillManager with zero action skills and
// reintroduce a deterministic test around PickRandomActionSkills.
