|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +package com.microsoft.copilot.eclipse.ui.chat.tools; |
| 5 | + |
| 6 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 8 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 9 | + |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.concurrent.CompletableFuture; |
| 13 | +import java.util.concurrent.ExecutionException; |
| 14 | + |
| 15 | +import org.eclipse.swt.widgets.Display; |
| 16 | +import org.eclipse.swt.widgets.Shell; |
| 17 | +import org.junit.jupiter.api.AfterEach; |
| 18 | +import org.junit.jupiter.api.BeforeEach; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import com.microsoft.copilot.eclipse.core.lsp.protocol.LanguageModelToolInformation; |
| 22 | +import com.microsoft.copilot.eclipse.core.lsp.protocol.LanguageModelToolResult; |
| 23 | +import com.microsoft.copilot.eclipse.core.lsp.protocol.LanguageModelToolResult.ToolInvocationStatus; |
| 24 | +import com.microsoft.copilot.eclipse.ui.utils.SwtUtils; |
| 25 | + |
| 26 | +class RunInTerminalToolAdapterTest { |
| 27 | + |
| 28 | + private RunInTerminalToolAdapter runInTerminalToolAdapter; |
| 29 | + private Shell shell; |
| 30 | + |
| 31 | + @BeforeEach |
| 32 | + void setUp() { |
| 33 | + // Setup SWT components |
| 34 | + SwtUtils.invokeOnDisplayThread(() -> { |
| 35 | + shell = new Shell(Display.getDefault()); |
| 36 | + }); |
| 37 | + |
| 38 | + runInTerminalToolAdapter = new RunInTerminalToolAdapter(); |
| 39 | + } |
| 40 | + |
| 41 | + @AfterEach |
| 42 | + void tearDown() { |
| 43 | + SwtUtils.invokeOnDisplayThread(() -> { |
| 44 | + if (shell != null && !shell.isDisposed()) { |
| 45 | + shell.dispose(); |
| 46 | + } |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void testGetToolInformation() { |
| 52 | + // Act |
| 53 | + LanguageModelToolInformation toolInfo = runInTerminalToolAdapter.getToolInformation(); |
| 54 | + |
| 55 | + // Assert |
| 56 | + assertNotNull(toolInfo); |
| 57 | + assertEquals("run_in_terminal", toolInfo.getName()); |
| 58 | + assertNotNull(toolInfo.getDescription()); |
| 59 | + assertNotNull(toolInfo.getInputSchema()); |
| 60 | + assertTrue(toolInfo.getInputSchema().getRequired().contains("command")); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void testInvokeWithEmptyCommandReturnsErrorStatus() throws InterruptedException, ExecutionException { |
| 65 | + // Arrange |
| 66 | + Map<String, Object> input = new HashMap<>(); |
| 67 | + input.put("command", ""); |
| 68 | + input.put("isBackground", false); |
| 69 | + |
| 70 | + // Act |
| 71 | + CompletableFuture<LanguageModelToolResult[]> future = runInTerminalToolAdapter.invoke(input, null); |
| 72 | + LanguageModelToolResult[] results = future.get(); |
| 73 | + |
| 74 | + // Assert |
| 75 | + // In test environment with no terminal service, the "no terminal" error is returned |
| 76 | + // before command validation. This is the actual behavior of the implementation. |
| 77 | + assertNotNull(results); |
| 78 | + assertEquals(1, results.length); |
| 79 | + assertEquals(ToolInvocationStatus.error, results[0].getStatus()); |
| 80 | + assertEquals("No terminal implementation available. Terminal service not yet loaded or failed to load.", |
| 81 | + results[0].getContent().get(0).getValue()); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void testInvokeWithNoTerminalServiceReturnsErrorStatus() throws InterruptedException, ExecutionException { |
| 86 | + // Arrange - Since we can't mock the singleton, this test verifies error handling |
| 87 | + // when no terminal service is available (which is the typical case in unit tests) |
| 88 | + Map<String, Object> input = new HashMap<>(); |
| 89 | + input.put("command", "echo test"); |
| 90 | + input.put("isBackground", false); |
| 91 | + |
| 92 | + // Act |
| 93 | + CompletableFuture<LanguageModelToolResult[]> future = runInTerminalToolAdapter.invoke(input, null); |
| 94 | + LanguageModelToolResult[] results = future.get(); |
| 95 | + |
| 96 | + // Assert - When no terminal service is available, we expect an error |
| 97 | + assertNotNull(results); |
| 98 | + assertEquals(1, results.length); |
| 99 | + // The result will be either error (no terminal service) or success (if service is available) |
| 100 | + // We just verify the result is properly formed |
| 101 | + assertNotNull(results[0].getStatus()); |
| 102 | + assertNotNull(results[0].getContent()); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void testNeedConfirmation() { |
| 107 | + // Act & Assert |
| 108 | + assertEquals(true, runInTerminalToolAdapter.needConfirmation()); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void testToolName() { |
| 113 | + // Act & Assert |
| 114 | + assertEquals("run_in_terminal", runInTerminalToolAdapter.getToolName()); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + void testGetTerminalOutputToolWithNoTerminalServiceReturnsErrorStatus() |
| 119 | + throws InterruptedException, ExecutionException { |
| 120 | + // Arrange |
| 121 | + RunInTerminalToolAdapter.GetTerminalOutputTool getTool = new RunInTerminalToolAdapter.GetTerminalOutputTool(); |
| 122 | + Map<String, Object> input = new HashMap<>(); |
| 123 | + input.put("id", "terminal-123"); |
| 124 | + |
| 125 | + // Act |
| 126 | + CompletableFuture<LanguageModelToolResult[]> future = getTool.invoke(input, null); |
| 127 | + LanguageModelToolResult[] results = future.get(); |
| 128 | + |
| 129 | + // Assert - When no terminal service is available, we expect an error |
| 130 | + assertNotNull(results); |
| 131 | + assertEquals(1, results.length); |
| 132 | + // The result will be either error (no terminal service or invalid ID) |
| 133 | + // We just verify the result is properly formed |
| 134 | + assertNotNull(results[0].getStatus()); |
| 135 | + assertNotNull(results[0].getContent()); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + void testGetTerminalOutputToolGetToolInformation() { |
| 140 | + // Arrange |
| 141 | + RunInTerminalToolAdapter.GetTerminalOutputTool getTool = new RunInTerminalToolAdapter.GetTerminalOutputTool(); |
| 142 | + |
| 143 | + // Act |
| 144 | + LanguageModelToolInformation toolInfo = getTool.getToolInformation(); |
| 145 | + |
| 146 | + // Assert |
| 147 | + assertNotNull(toolInfo); |
| 148 | + assertEquals("get_terminal_output", toolInfo.getName()); |
| 149 | + assertNotNull(toolInfo.getDescription()); |
| 150 | + assertNotNull(toolInfo.getInputSchema()); |
| 151 | + assertTrue(toolInfo.getInputSchema().getRequired().contains("id")); |
| 152 | + } |
| 153 | +} |
0 commit comments