|
| 1 | +package com.microsoft.copilot.eclipse.ui.chat; |
| 2 | + |
| 3 | +import static org.mockito.Mockito.mock; |
| 4 | +import static org.mockito.Mockito.mockStatic; |
| 5 | +import static org.mockito.Mockito.verify; |
| 6 | +import static org.mockito.Mockito.when; |
| 7 | + |
| 8 | +import java.lang.reflect.Field; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +import org.eclipse.lsp4j.WorkDoneProgressKind; |
| 13 | +import org.eclipse.swt.SWT; |
| 14 | +import org.eclipse.swt.widgets.Display; |
| 15 | +import org.eclipse.swt.widgets.Shell; |
| 16 | +import org.junit.jupiter.api.AfterEach; |
| 17 | +import org.junit.jupiter.api.BeforeEach; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 20 | +import org.mockito.Mock; |
| 21 | +import org.mockito.MockedStatic; |
| 22 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 23 | + |
| 24 | +import com.google.gson.Gson; |
| 25 | +import com.microsoft.copilot.eclipse.core.lsp.protocol.AgentRound; |
| 26 | +import com.microsoft.copilot.eclipse.core.lsp.protocol.ChatProgressValue; |
| 27 | +import com.microsoft.copilot.eclipse.ui.CopilotUi; |
| 28 | +import com.microsoft.copilot.eclipse.ui.chat.services.ChatServiceManager; |
| 29 | +import com.microsoft.copilot.eclipse.ui.utils.SwtUtils; |
| 30 | + |
| 31 | +@ExtendWith(MockitoExtension.class) |
| 32 | +class ChatContentViewerTest { |
| 33 | + |
| 34 | + private static final String TURN_ID = "239ca6fd-f4c9-440d-869e-eb93eef7c522"; |
| 35 | + private static final String CONVERSATION_ID = "8d8f3177-6ebd-4cec-9ae2-81d2c52979b4"; |
| 36 | + private static final String EXPECTED_REPLY = |
| 37 | + "Hello! How can I assist you with your project today?"; |
| 38 | + |
| 39 | + private Shell shell; |
| 40 | + private ChatContentViewer viewer; |
| 41 | + |
| 42 | + @Mock |
| 43 | + private ChatServiceManager mockChatServiceManager; |
| 44 | + |
| 45 | + @BeforeEach |
| 46 | + void setUp() { |
| 47 | + SwtUtils.invokeOnDisplayThread(() -> { |
| 48 | + shell = new Shell(Display.getDefault()); |
| 49 | + viewer = new ChatContentViewer(shell, SWT.NONE, mockChatServiceManager); |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + @AfterEach |
| 54 | + void tearDown() { |
| 55 | + SwtUtils.invokeOnDisplayThread(() -> { |
| 56 | + if (shell != null && !shell.isDisposed()) { |
| 57 | + shell.dispose(); |
| 58 | + } |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void testProcessTurnEvent_agentRoundReplyIsAppendedToTurnWidget() { |
| 64 | + SwtUtils.invokeOnDisplayThread(() -> { |
| 65 | + // Create a mock turn widget and inject into the turns map |
| 66 | + BaseTurnWidget mockTurnWidget = mock(BaseTurnWidget.class); |
| 67 | + getTurnsMap(viewer).put(TURN_ID, mockTurnWidget); |
| 68 | + |
| 69 | + // Build a ChatProgressValue matching the LSP response |
| 70 | + ChatProgressValue value = buildAgentRoundProgressValue(); |
| 71 | + |
| 72 | + try (MockedStatic<CopilotUi> copilotUiMock = mockStatic(CopilotUi.class)) { |
| 73 | + CopilotUi mockPlugin = mock(CopilotUi.class); |
| 74 | + copilotUiMock.when(CopilotUi::getPlugin).thenReturn(mockPlugin); |
| 75 | + when(mockPlugin.getChatServiceManager()).thenReturn(mockChatServiceManager); |
| 76 | + |
| 77 | + viewer.processTurnEvent(value); |
| 78 | + } |
| 79 | + |
| 80 | + verify(mockTurnWidget).appendMessage(EXPECTED_REPLY); |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + private ChatProgressValue buildAgentRoundProgressValue() { |
| 85 | + ChatProgressValue value = new ChatProgressValue(); |
| 86 | + value.setKind(WorkDoneProgressKind.report); |
| 87 | + value.setTurnId(TURN_ID); |
| 88 | + value.setConversationId(CONVERSATION_ID); |
| 89 | + |
| 90 | + // AgentRound has no setters, use Gson to construct it |
| 91 | + String agentRoundJson = new Gson().toJson(Map.of("roundId", 1, "reply", EXPECTED_REPLY)); |
| 92 | + AgentRound agentRound = new Gson().fromJson(agentRoundJson, AgentRound.class); |
| 93 | + |
| 94 | + // ChatProgressValue uses 'editAgentRounds' field (no setter), set via reflection |
| 95 | + setFieldValue(value, "editAgentRounds", List.of(agentRound)); |
| 96 | + return value; |
| 97 | + } |
| 98 | + |
| 99 | + @SuppressWarnings("unchecked") |
| 100 | + private Map<String, BaseTurnWidget> getTurnsMap(ChatContentViewer viewer) { |
| 101 | + return (Map<String, BaseTurnWidget>) getFieldValue(viewer, "turns"); |
| 102 | + } |
| 103 | + |
| 104 | + private Object getFieldValue(Object target, String fieldName) { |
| 105 | + try { |
| 106 | + Field field = target.getClass().getDeclaredField(fieldName); |
| 107 | + field.setAccessible(true); |
| 108 | + return field.get(target); |
| 109 | + } catch (Exception e) { |
| 110 | + throw new RuntimeException("Failed to get field " + fieldName, e); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private void setFieldValue(Object target, String fieldName, Object value) { |
| 115 | + try { |
| 116 | + Field field = target.getClass().getDeclaredField(fieldName); |
| 117 | + field.setAccessible(true); |
| 118 | + field.set(target, value); |
| 119 | + } catch (Exception e) { |
| 120 | + throw new RuntimeException("Failed to set field " + fieldName, e); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments