content: /**
   * Copyright (C) 2024 AIDC-AI
   *
   * 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.
   */
  package com.alibaba.langengine.stackoverflow.service.impl;
  
  import com.alibaba.langengine.stackoverflow.model.StackOverflowApiResponse;
  import com.alibaba.langengine.stackoverflow.model.StackOverflowSearchRequest;
  import com.alibaba.langengine.stackoverflow.model.StackOverflowSearchResult;
  import org.junit.jupiter.api.BeforeEach;
  import org.junit.jupiter.api.Test;
  import org.junit.jupiter.api.extension.ExtendWith;
  import org.mockito.junit.jupiter.MockitoExtension;
  
  import java.util.Arrays;
  import java.util.List;
  
  import static org.junit.jupiter.api.Assertions.*;
  
  
  @ExtendWith(MockitoExtension.class)
  class StackOverflowApiServiceImplTest {
      
      private StackOverflowApiServiceImpl apiService;
      
      @BeforeEach
      void setUp() {
          apiService = new StackOverflowApiServiceImpl();
      }
      
      @Test
      void testSearchQuestionsBasic() {
          // Arrange
          StackOverflowSearchRequest request = StackOverflowSearchRequest.builder()
                  .query("Java streams")
                  .site("stackoverflow")
                  .pageSize(5)
                  .sort("votes")
                  .order("desc")
                  .build();
          
          // Act & Assert
          assertDoesNotThrow(() -> {
              List<StackOverflowSearchResult> results = apiService.searchQuestions(request);
              assertNotNull(results);
              // Note: This will either use API or scraping depending on availability
          });
      }
      
      @Test
      void testSearchQuestionsWithTags() {
          // Arrange
          StackOverflowSearchRequest request = StackOverflowSearchRequest.builder()
                  .query("concurrency")
                  .tags(Arrays.asList("java", "concurrency"))
                  .site("stackoverflow")
                  .pageSize(3)
                  .sort("votes")
                  .order("desc")
                  .minScore(5)
                  .answeredOnly(true)
                  .build();
          
          // Act & Assert
          assertDoesNotThrow(() -> {
              List<StackOverflowSearchResult> results = apiService.searchQuestions(request);
              assertNotNull(results);
              // In case of rate limiting, we still pass if we get any non-null result
              // The important thing is that the method doesn't throw an exception
          });
      }
      
      @Test
      void testSearchWithInvalidSite() {
          // Arrange
          StackOverflowSearchRequest request = StackOverflowSearchRequest.builder()
                  .query("test")
                  .site("invalid-site")
                  .pageSize(5)
                  .build();
          
          // Act & Assert
          assertThrows(Exception.class, () -> {
              apiService.searchQuestions(request);
          });
      }
      
      @Test
      void testGetQuestion() {
          // Act & Assert
          assertDoesNotThrow(() -> {
              // Using a known Stack Overflow question ID (this is a real question)
              // Note: This test might fail if the API is not available
              try {
                  StackOverflowSearchResult result = apiService.getQuestion(3106136L, true);
                  if (result != null) {
                      assertNotNull(result.getQuestionId());
                      assertNotNull(result.getTitle());
                  }
              } catch (Exception e) {
                  // API might not be available, which is acceptable for testing
                  assertTrue(e.getMessage().contains("API") || e.getMessage().contains("quota") || 
                            e.getMessage().contains("unavailable"));
              }
          });
      }
      
      @Test
      void testIsApiAvailable() {
          // Act
          boolean isAvailable = apiService.isApiAvailable();
          
          // Assert
          // This is just checking that the method doesn't throw an exception
          // The actual availability depends on network and API status
          assertTrue(isAvailable || !isAvailable); // Always true, just testing execution
      }
      
      @Test
      void testGetRemainingQuota() {
          // Act
          int quota = apiService.getRemainingQuota();
          
          // Assert
          // Quota can be -1 (unknown) or any positive number
          assertTrue(quota >= -1);
      }
      
      @Test
      void testSearchWithScrapingFallback() {
          // Arrange
          StackOverflowSearchRequest request = StackOverflowSearchRequest.builder()
                  .query("python list comprehension")
                  .pageSize(3)
                  .build();
          
          // Act & Assert
          assertDoesNotThrow(() -> {
              try {
                  List<StackOverflowSearchResult> results = apiService.searchWithScraping(request);
                  assertNotNull(results);
                  // Results might be empty if the search doesn't match anything
              } catch (Exception e) {
                  // Scraping might fail due to network issues or site changes
                  // Just verify that some kind of reasonable exception was thrown
                  assertNotNull(e.getMessage());
                  assertTrue(e.getMessage().length() > 0, "Exception message should not be empty");
              }
          });
      }
      
      @Test
      void testSearchRequestValidation() {
          // Arrange
          StackOverflowSearchRequest emptyRequest = StackOverflowSearchRequest.builder()
                  .build();
          
          // Act & Assert
          assertDoesNotThrow(() -> {
              List<StackOverflowSearchResult> results = apiService.searchQuestions(emptyRequest);
              assertNotNull(results);
              // Empty request should still work, might return general results
          });
      }
      
      @Test
      void testSearchWithSpecialCharacters() {
          // Arrange
          StackOverflowSearchRequest request = StackOverflowSearchRequest.builder()
                  .query("C++ memory management & smart pointers")
                  .tags(Arrays.asList("c++", "memory-management"))
                  .pageSize(2)
                  .build();
          
          // Act & Assert
          assertDoesNotThrow(() -> {
              List<StackOverflowSearchResult> results = apiService.searchQuestions(request);
              assertNotNull(results);
          });
      }
      
      @Test
      void testSearchWithDateRange() {
          // Arrange - search for questions from last year
          long oneYearAgo = System.currentTimeMillis() / 1000 - (365 * 24 * 60 * 60);
          long now = System.currentTimeMillis() / 1000;
          
          StackOverflowSearchRequest request = StackOverflowSearchRequest.builder()
                  .query("Spring Boot")
                  .fromDate(oneYearAgo)
                  .toDate(now)
                  .pageSize(3)
                  .sort("creation")
                  .order("desc")
                  .build();
          
          // Act & Assert
          assertDoesNotThrow(() -> {
              List<StackOverflowSearchResult> results = apiService.searchQuestions(request);
              assertNotNull(results);
          });
      }
      
      @Test
      void testSearchWithAcceptedAnswersOnly() {
          // Arrange
          StackOverflowSearchRequest request = StackOverflowSearchRequest.builder()
                  .query("algorithm optimization")
                  .acceptedAnswerOnly(true)
                  .minScore(10)
                  .pageSize(3)
                  .sort("votes")
                  .build();
          
          // Act & Assert
          assertDoesNotThrow(() -> {
              List<StackOverflowSearchResult> results = apiService.searchQuestions(request);
              assertNotNull(results);
              // All results should have accepted answers if API works correctly
              for (StackOverflowSearchResult result : results) {
                  if (result.getAcceptedAnswerId() != null) {
                      assertNotNull(result.getAcceptedAnswerId());
                  }
              }
          });
      }
  }
  