/* Copyright 2026 The OpenXLA Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "xprof/convert/unified_memory_profile_processor.h"

#include <cstddef>
#include <memory>
#include <string>

#include "testing/base/public/gmock.h"
#include "<gtest/gtest.h>"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/arena.h"
#include "tsl/profiler/protobuf/xplane.pb.h"
#include "xprof/convert/tool_options.h"
#include "xprof/convert/unified_profile_processor.h"
#include "xprof/convert/unified_profile_processor_factory.h"
#include "xprof/convert/unified_session_snapshot.h"
#include "xprof/convert/unified_tools_registration.h"

namespace xprof {
namespace {

using ::testing::IsEmpty;
using ::testing::Not;

class MockXprofSessionSnapshot : public XprofSessionSnapshot {
 public:
  MOCK_METHOD(size_t, XSpaceSize, (), (const, override));
  MOCK_METHOD((absl::StatusOr<tensorflow::profiler::XSpace*>), GetXSpace,
              (size_t index, google::protobuf::Arena* arena), (const, override));
  MOCK_METHOD(std::string, GetHostname, (size_t index), (const, override));
  MOCK_METHOD(absl::string_view, GetSessionRunDir, (), (const, override));
  MOCK_METHOD(absl::StatusOr<std::string>, GetHostDataFileName,
              (tensorflow::profiler::StoredDataType data_type,
               absl::string_view host),
              (const, override));
};

TEST(UnifiedMemoryProfileProcessorTest, ProcessSessionTest) {
  RegisterUnifiedToolRegistrations();
  tensorflow::profiler::ToolOptions options;
  std::unique_ptr<UnifiedProfileProcessor> processor =
      UnifiedProfileProcessorFactory::GetInstance().Create("memory_profile",
                                                           options);
  ASSERT_NE(processor, nullptr);

  auto* memory_profile_processor =
      dynamic_cast<UnifiedMemoryProfileProcessor*>(processor.get());
  ASSERT_NE(memory_profile_processor, nullptr);

  tensorflow::profiler::XSpace xspace_obj;
  auto* plane = xspace_obj.add_planes();
  plane->set_name("/host:CPU");
  auto* xspace = &xspace_obj;

  testing::NiceMock<MockXprofSessionSnapshot> session_snapshot;
  EXPECT_CALL(session_snapshot, XSpaceSize())
      .WillRepeatedly(testing::Return(1));
  EXPECT_CALL(session_snapshot, GetHostname(0))
      .WillRepeatedly(testing::Return("test_host"));
  EXPECT_CALL(session_snapshot, GetXSpace(0, testing::_))
      .WillRepeatedly(testing::Return(xspace));

  ASSERT_OK(
      memory_profile_processor->ProcessSession(session_snapshot, options));

  EXPECT_EQ(memory_profile_processor->GetContentType(), "application/json");
  EXPECT_THAT(memory_profile_processor->GetData(), Not(IsEmpty()));
}

}  // namespace
}  // namespace xprof
