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