forked from microsoft/copilot-for-eclipse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReferencedFileTest.java
More file actions
196 lines (171 loc) · 7.72 KB
/
Copy pathReferencedFileTest.java
File metadata and controls
196 lines (171 loc) · 7.72 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package com.microsoft.copilot.eclipse.ui.chat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import org.eclipse.core.resources.IFile;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
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.microsoft.copilot.eclipse.core.lsp.protocol.ChatMode;
import com.microsoft.copilot.eclipse.core.lsp.protocol.CopilotModel;
import com.microsoft.copilot.eclipse.ui.CopilotUi;
import com.microsoft.copilot.eclipse.ui.chat.services.AgentToolService;
import com.microsoft.copilot.eclipse.ui.chat.services.ChatFontService;
import com.microsoft.copilot.eclipse.ui.chat.services.ChatServiceManager;
import com.microsoft.copilot.eclipse.ui.chat.services.McpConfigService;
import com.microsoft.copilot.eclipse.ui.chat.services.ModelService;
import com.microsoft.copilot.eclipse.ui.chat.services.ReferencedFileService;
import com.microsoft.copilot.eclipse.ui.chat.services.UserPreferenceService;
import com.microsoft.copilot.eclipse.ui.chat.tools.JavaDebuggerToolAdapter;
import com.microsoft.copilot.eclipse.ui.utils.SwtUtils;
@ExtendWith(MockitoExtension.class)
class ReferencedFileTest {
@Mock
private UserPreferenceService mockUserPreferenceService;
@Mock
private ModelService mockModelService;
@Mock
private ChatServiceManager mockChatServiceManager;
@Mock
private ReferencedFileService mockReferencedFileService;
@Mock
private IFile mockImageFile;
@Mock
private IFile mockTextFile;
@Mock
private CopilotUi mockCopilotUi;
@Mock
private CopilotModel mockModel;
@Mock
private McpConfigService mockMcpConfigService;
@Mock
private AgentToolService mockAgentToolService;
@Mock
private ChatFontService mockChatFontService;
private Shell shell;
private ActionBar actionBar;
private MockedStatic<CopilotUi> mockedCopilotUi;
@BeforeEach
void setUp() {
SwtUtils.invokeOnDisplayThread(() -> {
setupSwtComponents();
setupMockFiles();
setupMockServices();
});
}
@AfterEach
void tearDown() {
SwtUtils.invokeOnDisplayThread(() -> {
if (actionBar != null && !actionBar.isDisposed()) {
actionBar.dispose();
}
if (shell != null && !shell.isDisposed()) {
shell.dispose();
}
});
if (mockedCopilotUi != null) {
mockedCopilotUi.close();
}
}
private void setupSwtComponents() {
shell = new Shell(Display.getDefault());
}
private void setupMockFiles() {
lenient().when(mockImageFile.getName()).thenReturn("test-image.png");
lenient().when(mockImageFile.getFileExtension()).thenReturn("png");
lenient().when(mockTextFile.getName()).thenReturn("test-file.txt");
lenient().when(mockTextFile.getFileExtension()).thenReturn("txt");
}
private void setupMockServices() {
mockedCopilotUi = mockStatic(CopilotUi.class);
mockedCopilotUi.when(CopilotUi::getPlugin).thenReturn(mockCopilotUi);
lenient().when(mockModel.getModelName()).thenReturn("test-model");
lenient().when(mockModelService.getActiveModel()).thenReturn(mockModel);
lenient().when(mockChatServiceManager.getUserPreferenceService()).thenReturn(mockUserPreferenceService);
lenient().when(mockChatServiceManager.getModelService()).thenReturn(mockModelService);
lenient().when(mockChatServiceManager.getReferencedFileService()).thenReturn(mockReferencedFileService);
lenient().when(mockChatServiceManager.getMcpConfigService()).thenReturn(mockMcpConfigService);
lenient().when(mockChatServiceManager.getAgentToolService()).thenReturn(mockAgentToolService);
lenient().when(mockChatServiceManager.getChatFontService()).thenReturn(mockChatFontService);
lenient().when(mockAgentToolService.getTool(JavaDebuggerToolAdapter.TOOL_NAME)).thenReturn(null);
lenient().when(mockUserPreferenceService.getActiveChatMode()).thenReturn(ChatMode.Ask);
lenient().when(mockCopilotUi.getChatServiceManager()).thenReturn(mockChatServiceManager);
actionBar = spy(new ActionBar(shell, SWT.NONE, mockChatServiceManager));
}
/**
* Test ActionBar creates ReferencedFiles with correct strikeThrough state based on model vision support.
*/
@Test
void testStrikeThroughBehavior() {
SwtUtils.invokeOnDisplayThread(() -> {
List<IFile> testFiles = Arrays.asList(mockImageFile, mockTextFile);
callUpdateReferencedFilesInternal(actionBar, testFiles, true);
ReferencedFile imageWithVisionWidget = getReferencedFileWidgetByReflection(actionBar, mockImageFile);
assertNotNull(imageWithVisionWidget, "Image widget should be created");
assertFalse(imageWithVisionWidget.isFileUnSupported(),
"Image file should not have strikethrough when vision is supported");
ReferencedFile textWithVisionWidget = getReferencedFileWidgetByReflection(actionBar, mockTextFile);
assertNotNull(textWithVisionWidget, "Text widget should be created");
assertFalse(textWithVisionWidget.isFileUnSupported(),
"Text file should not have strikethrough when vision is supported");
callUpdateReferencedFilesInternal(actionBar, testFiles, false);
ReferencedFile imageWithoutVisionWidget = getReferencedFileWidgetByReflection(actionBar, mockImageFile);
assertNotNull(imageWithoutVisionWidget, "Image widget should be created");
assertTrue(imageWithoutVisionWidget.isFileUnSupported(),
"Image file should have strikethrough when vision is not supported");
ReferencedFile textWithoutVisionWidget = getReferencedFileWidgetByReflection(actionBar, mockTextFile);
assertNotNull(textWithoutVisionWidget, "Text widget should be created");
assertFalse(textWithoutVisionWidget.isFileUnSupported(),
"Text file should not have strikethrough regardless of vision support");
});
}
/**
* Helper method to call private updateReferencedFilesInternal method using reflection.
*/
private void callUpdateReferencedFilesInternal(ActionBar actionBar, List<IFile> files, boolean supportVision) {
try {
Method method = ActionBar.class.getDeclaredMethod("updateReferencedFilesInternal", List.class, boolean.class);
method.setAccessible(true);
method.invoke(actionBar, files, supportVision);
} catch (Exception e) {
throw new RuntimeException("Failed to call updateReferencedFilesInternal", e);
}
}
private ReferencedFile getReferencedFileWidgetByReflection(ActionBar actionBar, IFile file) {
try {
Field cmpFileRefField = ActionBar.class.getDeclaredField("cmpFileRef");
cmpFileRefField.setAccessible(true);
Composite cmpFileRef = (Composite) cmpFileRefField.get(actionBar);
for (Control child : cmpFileRef.getChildren()) {
if (child instanceof ReferencedFile) {
ReferencedFile refFile = (ReferencedFile) child;
if (file.equals(refFile.getFile())) {
return refFile;
}
}
}
} catch (Exception e) {
throw new RuntimeException("Failed to get ReferencedFile widget by reflection", e);
}
return null;
}
}