#include <gtest/gtest.h>
#include <rclcpp/rclcpp.hpp>
#include <behaviortree_cpp/bt_factory.h>
#include "brain/bt_registry.hpp"

TEST(BTRegistry, BuildTreeFromSequence)
{
  rclcpp::init(0, nullptr);
  auto node = std::make_shared<rclcpp::Node>("test_bt_registry_node");
  BT::BehaviorTreeFactory factory;
  BTRegistry reg(factory, node.get());

  BTActionHandlers handlers;
  bool start_called = false;
  handlers.on_start = [&](rclcpp::Node *, BT::TreeNode &) {
      start_called = true; return BT::NodeStatus::SUCCESS;
    };
  handlers.on_running = [&](rclcpp::Node *, BT::TreeNode &) {return BT::NodeStatus::SUCCESS;};
  handlers.on_halted = [&](rclcpp::Node *, BT::TreeNode &) {};
  reg.register_bt_action("TestAction", handlers);

  BTSequenceSpec seq = {{"TestAction", "a1", {}}};
  reg.register_bt_sequence("Seq1", seq);

  BT::Tree tree;
  ASSERT_TRUE(reg.build_tree_from_sequence("Seq1", tree));

  // Tick once; it should call on_start.
  tree.tickWhileRunning();
  EXPECT_TRUE(start_called);

  rclcpp::shutdown();
}
