|
| 1 | +package org.jabref.gui.ai.components.aichat; |
| 2 | + |
| 3 | +import java.util.concurrent.CountDownLatch; |
| 4 | +import java.util.concurrent.TimeUnit; |
| 5 | + |
| 6 | +import javafx.application.Platform; |
| 7 | +import javafx.beans.property.SimpleObjectProperty; |
| 8 | +import javafx.beans.property.SimpleStringProperty; |
| 9 | +import javafx.collections.FXCollections; |
| 10 | + |
| 11 | +import org.jabref.gui.DialogService; |
| 12 | +import org.jabref.logic.ai.AiPreferences; |
| 13 | +import org.jabref.logic.ai.AiService; |
| 14 | +import org.jabref.logic.ai.chatting.AiChatLogic; |
| 15 | +import org.jabref.logic.ai.chatting.AiChatService; |
| 16 | +import org.jabref.logic.ai.ingestion.IngestionService; |
| 17 | +import org.jabref.logic.l10n.Language; |
| 18 | +import org.jabref.logic.l10n.Localization; |
| 19 | +import org.jabref.logic.util.TaskExecutor; |
| 20 | +import org.jabref.model.ai.AiProvider; |
| 21 | +import org.jabref.model.database.BibDatabaseContext; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 25 | +import org.junit.jupiter.params.ParameterizedTest; |
| 26 | +import org.junit.jupiter.params.provider.EnumSource; |
| 27 | +import org.mockito.Mockito; |
| 28 | +import org.testfx.framework.junit5.ApplicationExtension; |
| 29 | + |
| 30 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 31 | +import static org.mockito.Mockito.mock; |
| 32 | +import static org.mockito.Mockito.when; |
| 33 | + |
| 34 | +@ExtendWith(ApplicationExtension.class) |
| 35 | +class AiChatComponentTest { |
| 36 | + private AiPreferences prefs; |
| 37 | + private AiService aiService; |
| 38 | + private BibDatabaseContext bibDatabaseContext; |
| 39 | + private DialogService dialogService; |
| 40 | + private TaskExecutor taskExecutor; |
| 41 | + |
| 42 | + @BeforeEach |
| 43 | + void setUp() { |
| 44 | + Localization.setLanguage(Language.ENGLISH); |
| 45 | + |
| 46 | + prefs = mock(AiPreferences.class, Mockito.RETURNS_DEEP_STUBS); |
| 47 | + SimpleObjectProperty<AiProvider> providerProp = new SimpleObjectProperty<>(AiProvider.OPEN_AI); |
| 48 | + SimpleStringProperty openAiModelProp = new SimpleStringProperty("gpt-4o"); |
| 49 | + SimpleStringProperty mistralModelProp = new SimpleStringProperty("mistral-large"); |
| 50 | + SimpleStringProperty geminiModelProp = new SimpleStringProperty("gemini-1.5-pro"); |
| 51 | + SimpleStringProperty hfModelProp = new SimpleStringProperty("hf-model"); |
| 52 | + SimpleStringProperty gpt4AllModelProp = new SimpleStringProperty("gpt4all"); |
| 53 | + |
| 54 | + when(prefs.aiProviderProperty()).thenReturn(providerProp); |
| 55 | + when(prefs.openAiChatModelProperty()).thenReturn(openAiModelProp); |
| 56 | + when(prefs.mistralAiChatModelProperty()).thenReturn(mistralModelProp); |
| 57 | + when(prefs.geminiChatModelProperty()).thenReturn(geminiModelProp); |
| 58 | + when(prefs.huggingFaceChatModelProperty()).thenReturn(hfModelProp); |
| 59 | + when(prefs.gpt4AllChatModelProperty()).thenReturn(gpt4AllModelProp); |
| 60 | + |
| 61 | + when(prefs.getAiProvider()).thenAnswer(_ -> providerProp.get()); |
| 62 | + when(prefs.getSelectedChatModel()).thenAnswer(_ -> { |
| 63 | + return switch (prefs.getAiProvider()) { |
| 64 | + case OPEN_AI -> |
| 65 | + openAiModelProp.get(); |
| 66 | + case MISTRAL_AI -> |
| 67 | + mistralModelProp.get(); |
| 68 | + case GEMINI -> |
| 69 | + geminiModelProp.get(); |
| 70 | + case HUGGING_FACE -> |
| 71 | + hfModelProp.get(); |
| 72 | + case GPT4ALL -> |
| 73 | + gpt4AllModelProp.get(); |
| 74 | + }; |
| 75 | + }); |
| 76 | + |
| 77 | + aiService = mock(AiService.class, Mockito.RETURNS_DEEP_STUBS); |
| 78 | + AiChatService chatService = mock(AiChatService.class); |
| 79 | + AiChatLogic chatLogic = mock(AiChatLogic.class, Mockito.RETURNS_DEEP_STUBS); |
| 80 | + when(chatLogic.getChatHistory()).thenReturn(FXCollections.observableArrayList()); |
| 81 | + when(chatService.makeChat(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(chatLogic); |
| 82 | + when(aiService.getAiChatService()).thenReturn(chatService); |
| 83 | + |
| 84 | + IngestionService ingestionService = mock(IngestionService.class, Mockito.RETURNS_DEEP_STUBS); |
| 85 | + when(aiService.getIngestionService()).thenReturn(ingestionService); |
| 86 | + |
| 87 | + bibDatabaseContext = mock(BibDatabaseContext.class); |
| 88 | + dialogService = mock(DialogService.class); |
| 89 | + taskExecutor = mock(TaskExecutor.class); |
| 90 | + } |
| 91 | + |
| 92 | + private AiChatComponent createComponent() throws Exception { |
| 93 | + CountDownLatch latch = new CountDownLatch(1); |
| 94 | + final AiChatComponent[] holder = new AiChatComponent[1]; |
| 95 | + Platform.runLater(() -> { |
| 96 | + holder[0] = new AiChatComponent( |
| 97 | + aiService, |
| 98 | + new SimpleStringProperty("entry"), |
| 99 | + FXCollections.observableArrayList(), |
| 100 | + FXCollections.observableArrayList(), |
| 101 | + bibDatabaseContext, |
| 102 | + prefs, |
| 103 | + dialogService, |
| 104 | + taskExecutor |
| 105 | + ); |
| 106 | + latch.countDown(); |
| 107 | + }); |
| 108 | + latch.await(5, TimeUnit.SECONDS); |
| 109 | + return holder[0]; |
| 110 | + } |
| 111 | + |
| 112 | + @ParameterizedTest |
| 113 | + @EnumSource(AiProvider.class) |
| 114 | + void noticeTextUpdatesWhenProviderChanges(AiProvider provider) throws Exception { |
| 115 | + AiChatComponent component = createComponent(); |
| 116 | + |
| 117 | + // Change provider |
| 118 | + prefs.aiProviderProperty().set(provider); |
| 119 | + |
| 120 | + Thread.sleep(50); |
| 121 | + |
| 122 | + String expected = "Current AI model: " + provider.getLabel() + " " + prefs.getSelectedChatModel() |
| 123 | + + ". The AI may generate inaccurate or inappropriate responses. Please verify any information provided."; |
| 124 | + assertEquals(expected, component.computeNoticeText()); |
| 125 | + } |
| 126 | + |
| 127 | + @ParameterizedTest |
| 128 | + @EnumSource(AiProvider.class) |
| 129 | + void noticeTextUpdatesWhenCurrentModelChangesForSelectedProvider(AiProvider provider) throws Exception { |
| 130 | + AiChatComponent component = createComponent(); |
| 131 | + |
| 132 | + // Select provider |
| 133 | + prefs.aiProviderProperty().set(provider); |
| 134 | + |
| 135 | + // Change current model for all providers; only the selected provider should influence the notice text. |
| 136 | + String newModel = "new-model"; |
| 137 | + prefs.openAiChatModelProperty().set(newModel); |
| 138 | + prefs.mistralAiChatModelProperty().set(newModel); |
| 139 | + prefs.geminiChatModelProperty().set(newModel); |
| 140 | + prefs.huggingFaceChatModelProperty().set(newModel); |
| 141 | + prefs.gpt4AllChatModelProperty().set(newModel); |
| 142 | + |
| 143 | + Thread.sleep(50); |
| 144 | + String expected = "Current AI model: " + provider.getLabel() + " " + newModel |
| 145 | + + ". The AI may generate inaccurate or inappropriate responses. Please verify any information provided."; |
| 146 | + assertEquals(expected, component.computeNoticeText()); |
| 147 | + } |
| 148 | +} |
0 commit comments