|
| 1 | +package com.dbaagent.support; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.extension.ConditionEvaluationResult; |
| 4 | +import org.junit.jupiter.api.extension.ExecutionCondition; |
| 5 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 6 | +import org.junit.platform.commons.support.AnnotationSupport; |
| 7 | + |
| 8 | +import java.util.Optional; |
| 9 | + |
| 10 | +/** |
| 11 | + * JUnit 5 ExecutionCondition that checks whether LLM configuration is available. |
| 12 | + * |
| 13 | + * <p>This condition checks the {@code DEEPSQL_CHAT_*} and {@code DEEPSQL_EMBEDDING_*} |
| 14 | + * environment variables to determine if LLM is configured. It uses the same resolution |
| 15 | + * logic as {@code LlmConfigResolver.fromEnvironment()}. |
| 16 | + * |
| 17 | + * <p>For integration tests running within a Spring context, the actual |
| 18 | + * {@code LlmConfigResolver} bean should be used via {@link LlmTestSupport} for |
| 19 | + * more accurate checks that include database-stored configuration. |
| 20 | + */ |
| 21 | +public class LlmAvailabilityCondition implements ExecutionCondition { |
| 22 | + |
| 23 | + private static final String CHAT_PROVIDER_ENV = "DEEPSQL_CHAT_PROVIDER"; |
| 24 | + private static final String CHAT_API_KEY_ENV = "DEEPSQL_CHAT_API_KEY"; |
| 25 | + private static final String EMBEDDING_PROVIDER_ENV = "DEEPSQL_EMBEDDING_PROVIDER"; |
| 26 | + private static final String EMBEDDING_API_KEY_ENV = "DEEPSQL_EMBEDDING_API_KEY"; |
| 27 | + |
| 28 | + @Override |
| 29 | + public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { |
| 30 | + Optional<RequiresLlm> annotation = findAnnotation(context); |
| 31 | + |
| 32 | + if (annotation.isEmpty()) { |
| 33 | + return ConditionEvaluationResult.enabled("No @RequiresLlm annotation present"); |
| 34 | + } |
| 35 | + |
| 36 | + RequiresLlm requiresLlm = annotation.get(); |
| 37 | + boolean requiresChat = requiresLlm.chat(); |
| 38 | + boolean requiresEmbedding = requiresLlm.embedding(); |
| 39 | + |
| 40 | + if (requiresChat && !isChatConfigured()) { |
| 41 | + return ConditionEvaluationResult.disabled( |
| 42 | + "Chat LLM not configured. Set DEEPSQL_CHAT_PROVIDER and DEEPSQL_CHAT_API_KEY " |
| 43 | + + "environment variables, or configure via the setup wizard."); |
| 44 | + } |
| 45 | + |
| 46 | + if (requiresEmbedding && !isEmbeddingConfigured()) { |
| 47 | + return ConditionEvaluationResult.disabled( |
| 48 | + "Embedding LLM not configured. Set DEEPSQL_EMBEDDING_PROVIDER and " |
| 49 | + + "DEEPSQL_EMBEDDING_API_KEY environment variables, or configure via the setup wizard."); |
| 50 | + } |
| 51 | + |
| 52 | + return ConditionEvaluationResult.enabled("LLM configuration available"); |
| 53 | + } |
| 54 | + |
| 55 | + private Optional<RequiresLlm> findAnnotation(ExtensionContext context) { |
| 56 | + Optional<RequiresLlm> methodAnnotation = context.getElement() |
| 57 | + .flatMap(element -> AnnotationSupport.findAnnotation(element, RequiresLlm.class)); |
| 58 | + |
| 59 | + if (methodAnnotation.isPresent()) { |
| 60 | + return methodAnnotation; |
| 61 | + } |
| 62 | + |
| 63 | + return context.getTestClass() |
| 64 | + .flatMap(clazz -> AnnotationSupport.findAnnotation(clazz, RequiresLlm.class)); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Checks if chat LLM is configured via environment variables. |
| 69 | + * |
| 70 | + * <p>This mirrors the logic in {@code LlmConfigResolver.fromEnvironment("CHAT")}. |
| 71 | + */ |
| 72 | + public static boolean isChatConfigured() { |
| 73 | + String provider = System.getenv(CHAT_PROVIDER_ENV); |
| 74 | + String apiKey = System.getenv(CHAT_API_KEY_ENV); |
| 75 | + return isConfigured(provider, apiKey); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Checks if embedding LLM is configured via environment variables. |
| 80 | + * |
| 81 | + * <p>This mirrors the logic in {@code LlmConfigResolver.fromEnvironment("EMBEDDING")}. |
| 82 | + */ |
| 83 | + public static boolean isEmbeddingConfigured() { |
| 84 | + String provider = System.getenv(EMBEDDING_PROVIDER_ENV); |
| 85 | + String apiKey = System.getenv(EMBEDDING_API_KEY_ENV); |
| 86 | + return isConfigured(provider, apiKey); |
| 87 | + } |
| 88 | + |
| 89 | + private static boolean isConfigured(String provider, String apiKey) { |
| 90 | + return provider != null && !provider.isBlank() |
| 91 | + && apiKey != null && !apiKey.isBlank(); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Returns a human-readable description of the current LLM configuration status. |
| 96 | + * Useful for test diagnostic output. |
| 97 | + */ |
| 98 | + public static String describeConfiguration() { |
| 99 | + StringBuilder sb = new StringBuilder(); |
| 100 | + sb.append("LLM Configuration Status:\n"); |
| 101 | + sb.append(" Chat: ").append(isChatConfigured() ? "CONFIGURED" : "NOT CONFIGURED"); |
| 102 | + if (!isChatConfigured()) { |
| 103 | + sb.append(" (set DEEPSQL_CHAT_PROVIDER and DEEPSQL_CHAT_API_KEY)"); |
| 104 | + } |
| 105 | + sb.append("\n"); |
| 106 | + sb.append(" Embedding: ").append(isEmbeddingConfigured() ? "CONFIGURED" : "NOT CONFIGURED"); |
| 107 | + if (!isEmbeddingConfigured()) { |
| 108 | + sb.append(" (set DEEPSQL_EMBEDDING_PROVIDER and DEEPSQL_EMBEDDING_API_KEY)"); |
| 109 | + } |
| 110 | + return sb.toString(); |
| 111 | + } |
| 112 | +} |
0 commit comments