Skip to content

Commit

Permalink
Fixed unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanj committed Dec 17, 2024
1 parent 1791625 commit ffbd974
Showing 1 changed file with 29 additions and 58 deletions.
87 changes: 29 additions & 58 deletions src/test/java/com/devoxx/genie/service/ChatPromptExecutorIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import com.devoxx.genie.model.request.ChatMessageContext;
import com.devoxx.genie.ui.component.input.PromptInputArea;
import com.devoxx.genie.ui.panel.PromptOutputPanel;
import com.devoxx.genie.ui.settings.DevoxxGenieStateService;
import com.intellij.openapi.application.ex.ApplicationEx;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.extensions.ExtensionsArea;
import com.intellij.util.messages.MessageBusConnection;
import dev.langchain4j.data.message.UserMessage;

import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.util.messages.MessageBus;

Expand All @@ -31,85 +32,55 @@ public class ChatPromptExecutorIT extends AbstractLightPlatformTestCase {

ChatPromptExecutor chatPromptExecutor;

@Override
@BeforeEach
public void setUp() throws Exception {
super.setUp();
Application application = mock(Application.class);
ApplicationEx applicationEx = mock(ApplicationEx.class);
MessageBus messageBus = mock(MessageBus.class);
when(application.getMessageBus()).thenReturn(messageBus);
when(applicationEx.getMessageBus()).thenReturn(messageBus);

// Mockito.doNothing().when(messageBus).syncPublisher(Mockito.any()); // If needed
MessageBusConnection messageBusConnection = mock(MessageBusConnection.class);
when(messageBus.connect()).thenReturn(messageBusConnection);

ExtensionsArea extensionsArea = mock(ExtensionsArea.class);
when(application.getExtensionArea()).thenReturn(extensionsArea);
when(applicationEx.getExtensionArea()).thenReturn(extensionsArea);
when(extensionsArea.hasExtensionPoint(Mockito.any(ExtensionPointName.class))).thenReturn(true);

// Use reflection or a dedicated method to set the application mock
setApplication(application);
DevoxxGenieStateService stateService = mock(DevoxxGenieStateService.class);
when(applicationEx.getService(DevoxxGenieStateService.class)).thenReturn(stateService);

MessageCreationService messageCreationService = mock(MessageCreationService.class);
when(applicationEx.getService(MessageCreationService.class)).thenReturn(messageCreationService);

PromptExecutionService promptExecutionService = mock(PromptExecutionService.class);
when(applicationEx.getService(PromptExecutionService.class)).thenReturn(promptExecutionService);

setApplication(applicationEx);

promptInputArea = new PromptInputArea(getProject(), resourceBundle);
promptOutputPanel = new PromptOutputPanel(getProject(), resourceBundle);
chatPromptExecutor = new ChatPromptExecutor(promptInputArea);
}

@Test
public void testGetCommandFromPrompt_validInput() {
private void setApplication(ApplicationEx applicationEx) throws Exception {
java.lang.reflect.Field applicationField = ApplicationManager.class.getDeclaredField("ourApplication");
applicationField.setAccessible(true);
applicationField.set(null, applicationEx);
}

@Test
public void test_GetCommandFromPrompt_validInput() {
String command = "/review";
var chatMessageContext = ChatMessageContext
.builder()
.userMessage(UserMessage.from("/review"))
.userMessage(UserMessage.from(command))
.build();

Optional<String> command = chatPromptExecutor.getCommandFromPrompt(chatMessageContext, promptOutputPanel);

assertEquals("add", command.get());
}
chatMessageContext.setUserPrompt(command);
Optional<String> aCommand = chatPromptExecutor.getCommandFromPrompt(chatMessageContext, promptOutputPanel);

private void setApplication(Application application) throws Exception {
java.lang.reflect.Field applicationField = ApplicationManager.class.getDeclaredField("ourApplication");
applicationField.setAccessible(true);
applicationField.set(null, application);
assertTrue(aCommand.isPresent());
assertEquals(command, aCommand.get());
}

//
// @Test
// public void testGetCommandFromPrompt_emptyInput() {
// // Simulate empty user input (just pressing Enter)
// String input = "\n";
// InputStream in = new ByteArrayInputStream(input.getBytes());
// System.setIn(in);
//
// YourClass yourClassInstance = new YourClass();
// String command = yourClassInstance.getCommandFromPrompt();
//
// assertEquals("", command); // Or assertNull(command) if that's the expected behavior for empty input
// }
//
// @Test
// public void testGetCommandFromPrompt_multipleSpaces() {
// // Simulate user input with multiple spaces
// String input = " delete \n";
// InputStream in = new ByteArrayInputStream(input.getBytes());
// System.setIn(in);
//
// YourClass yourClassInstance = new YourClass();
// String command = yourClassInstance.getCommandFromPrompt();
//
// assertEquals("delete", command); // Assuming the method trims whitespace
// }
//
// @Test
// public void testGetCommandFromPrompt_mixedCase() {
// // Simulate user input with mixed case
// String input = "UpDaTe\n";
// InputStream in = new ByteArrayInputStream(input.getBytes());
// System.setIn(in);
//
// YourClass yourClassInstance = new YourClass();
// String command = yourClassInstance.getCommandFromPrompt();
//
// assertEquals("update", command); // Assuming the method converts to lowercase
// }
}

0 comments on commit ffbd974

Please sign in to comment.