Skip to content

Commit 5ea9402

Browse files
committed
More tests
1 parent fedbaf1 commit 5ea9402

19 files changed

+1011
-557
lines changed

src/main/java/com/devoxx/genie/service/prompt/cancellation/PromptCancellationService.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,8 @@ private void cleanupCancelledExecution(
154154
PromptTask<?> task = PromptTaskTracker.getInstance().getTaskByContextId(project, contextId);
155155
if (task != null) {
156156
Object userData = task.getUserData(PromptTask.CONTEXT_KEY);
157-
if (userData instanceof ChatMessageContext) {
158-
ChatMessageContext context = (ChatMessageContext) userData;
159-
157+
if (userData instanceof ChatMessageContext context) {
158+
160159
// Remove last user message from memory
161160
ChatMemoryManager.getInstance().removeLastUserMessage(context);
162161

src/main/java/com/devoxx/genie/service/prompt/strategy/AbstractPromptExecutionStrategy.java

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,56 @@
2323
public abstract class AbstractPromptExecutionStrategy implements PromptExecutionStrategy {
2424

2525
protected final Project project;
26-
protected final ChatMemoryManager chatMemoryManager;
27-
protected final ThreadPoolManager threadPoolManager;
26+
protected ChatMemoryManager chatMemoryManager;
27+
protected ThreadPoolManager threadPoolManager;
28+
protected MessageCreationService messageCreationService;
2829

2930
/**
3031
* Constructor for AbstractPromptExecutionStrategy.
3132
*
3233
* @param project The IntelliJ project
3334
*/
34-
public AbstractPromptExecutionStrategy(@NotNull Project project) {
35+
protected AbstractPromptExecutionStrategy(@NotNull Project project) {
3536
this.project = project;
3637
this.chatMemoryManager = ChatMemoryManager.getInstance();
3738
this.threadPoolManager = ThreadPoolManager.getInstance();
39+
this.messageCreationService = MessageCreationService.getInstance();
40+
}
41+
42+
/**
43+
* Constructor for AbstractPromptExecutionStrategy that allows dependency injection.
44+
* Primarily used for testing.
45+
*
46+
* @param project The IntelliJ project
47+
* @param chatMemoryManager The chat memory manager
48+
* @param threadPoolManager The thread pool manager
49+
*/
50+
protected AbstractPromptExecutionStrategy(@NotNull Project project,
51+
@NotNull ChatMemoryManager chatMemoryManager,
52+
@NotNull ThreadPoolManager threadPoolManager) {
53+
this.project = project;
54+
this.chatMemoryManager = chatMemoryManager;
55+
this.threadPoolManager = threadPoolManager;
56+
this.messageCreationService = MessageCreationService.getInstance();
57+
}
58+
59+
/**
60+
* Constructor for AbstractPromptExecutionStrategy that allows full dependency injection.
61+
* Primarily used for testing.
62+
*
63+
* @param project The IntelliJ project
64+
* @param chatMemoryManager The chat memory manager
65+
* @param threadPoolManager The thread pool manager
66+
* @param messageCreationService The message creation service
67+
*/
68+
protected AbstractPromptExecutionStrategy(@NotNull Project project,
69+
@NotNull ChatMemoryManager chatMemoryManager,
70+
@NotNull ThreadPoolManager threadPoolManager,
71+
@NotNull MessageCreationService messageCreationService) {
72+
this.project = project;
73+
this.chatMemoryManager = chatMemoryManager;
74+
this.threadPoolManager = threadPoolManager;
75+
this.messageCreationService = messageCreationService;
3876
}
3977

4078
@Override
@@ -69,10 +107,9 @@ public PromptTask<PromptResult> execute(@NotNull ChatMessageContext context,
69107
* @param panel The UI panel
70108
* @param resultTask The task to complete with results
71109
*/
72-
protected abstract void executeStrategySpecific(
73-
@NotNull ChatMessageContext context,
74-
@NotNull PromptOutputPanel panel,
75-
@NotNull PromptTask<PromptResult> resultTask);
110+
protected abstract void executeStrategySpecific(ChatMessageContext context,
111+
PromptOutputPanel panel,
112+
PromptTask<PromptResult> resultTask);
76113

77114
/**
78115
* Returns the name of the strategy for logging purposes.
@@ -86,12 +123,12 @@ protected abstract void executeStrategySpecific(
86123
*
87124
* @param context The chat message context
88125
*/
89-
protected void prepareMemory(@NotNull ChatMessageContext context) {
126+
public void prepareMemory(ChatMessageContext context) {
90127
// Prepare memory with system message if needed and add user message
91128
log.debug("Before memory preparation - context ID: {}", context.getId());
92129
chatMemoryManager.prepareMemory(context);
93130
// Add context information to the user message before adding to memory
94-
MessageCreationService.getInstance().addUserMessageToContext(context);
131+
messageCreationService.addUserMessageToContext(context);
95132
// Now add the enriched user message to chat memory
96133
chatMemoryManager.addUserMessage(context);
97134
}

src/main/java/com/devoxx/genie/service/prompt/strategy/NonStreamingPromptStrategy.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import com.devoxx.genie.service.prompt.error.ExecutionException;
66
import com.devoxx.genie.service.prompt.error.PromptErrorHandler;
77
import com.devoxx.genie.service.prompt.error.PromptException;
8+
import com.devoxx.genie.service.prompt.memory.ChatMemoryManager;
89
import com.devoxx.genie.service.prompt.nonstreaming.NonStreamingPromptExecutionService;
910
import com.devoxx.genie.service.prompt.result.PromptResult;
1011
import com.devoxx.genie.service.prompt.threading.PromptTask;
12+
import com.devoxx.genie.service.prompt.threading.ThreadPoolManager;
1113
import com.devoxx.genie.service.rag.SearchResult;
1214
import com.devoxx.genie.service.rag.SemanticSearchService;
1315
import com.devoxx.genie.ui.panel.PromptOutputPanel;
@@ -30,12 +32,29 @@
3032
@Slf4j
3133
public class NonStreamingPromptStrategy extends AbstractPromptExecutionStrategy {
3234

33-
private final NonStreamingPromptExecutionService promptExecutionService;
35+
protected NonStreamingPromptExecutionService promptExecutionService;
3436

3537
public NonStreamingPromptStrategy(Project project) {
3638
super(project);
3739
this.promptExecutionService = NonStreamingPromptExecutionService.getInstance();
3840
}
41+
42+
/**
43+
* Constructor for dependency injection, primarily used for testing.
44+
*
45+
* @param project The IntelliJ project
46+
* @param chatMemoryManager The chat memory manager
47+
* @param threadPoolManager The thread pool manager
48+
* @param promptExecutionService The non-streaming prompt execution service
49+
*/
50+
protected NonStreamingPromptStrategy(
51+
@NotNull Project project,
52+
@NotNull ChatMemoryManager chatMemoryManager,
53+
@NotNull ThreadPoolManager threadPoolManager,
54+
@NotNull NonStreamingPromptExecutionService promptExecutionService) {
55+
super(project, chatMemoryManager, threadPoolManager);
56+
this.promptExecutionService = promptExecutionService;
57+
}
3958

4059
@Override
4160
protected String getStrategyName() {

src/main/java/com/devoxx/genie/service/prompt/strategy/StreamingPromptStrategy.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.devoxx.genie.service.prompt.strategy;
22

33
import com.devoxx.genie.model.request.ChatMessageContext;
4+
import com.devoxx.genie.service.MessageCreationService;
45
import com.devoxx.genie.service.prompt.error.ModelException;
6+
import com.devoxx.genie.service.prompt.memory.ChatMemoryManager;
57
import com.devoxx.genie.service.prompt.memory.ChatMemoryService;
68
import com.devoxx.genie.service.prompt.result.PromptResult;
79
import com.devoxx.genie.service.prompt.streaming.StreamingResponseHandler;
810
import com.devoxx.genie.service.prompt.threading.PromptTask;
11+
import com.devoxx.genie.service.prompt.threading.ThreadPoolManager;
912
import com.devoxx.genie.ui.panel.PromptOutputPanel;
1013
import com.devoxx.genie.ui.util.NotificationUtil;
1114
import com.intellij.openapi.project.Project;
@@ -29,6 +32,36 @@ public class StreamingPromptStrategy extends AbstractPromptExecutionStrategy {
2932
public StreamingPromptStrategy(Project project) {
3033
super(project);
3134
}
35+
36+
/**
37+
* Constructor for dependency injection, primarily used for testing.
38+
*
39+
* @param project The IntelliJ project
40+
* @param chatMemoryManager The chat memory manager
41+
* @param threadPoolManager The thread pool manager
42+
*/
43+
protected StreamingPromptStrategy(
44+
@NotNull Project project,
45+
@NotNull ChatMemoryManager chatMemoryManager,
46+
@NotNull ThreadPoolManager threadPoolManager) {
47+
super(project, chatMemoryManager, threadPoolManager);
48+
}
49+
50+
/**
51+
* Constructor for full dependency injection, primarily used for testing.
52+
*
53+
* @param project The IntelliJ project
54+
* @param chatMemoryManager The chat memory manager
55+
* @param threadPoolManager The thread pool manager
56+
* @param messageCreationService The message creation service
57+
*/
58+
protected StreamingPromptStrategy(
59+
@NotNull Project project,
60+
@NotNull ChatMemoryManager chatMemoryManager,
61+
@NotNull ThreadPoolManager threadPoolManager,
62+
@NotNull MessageCreationService messageCreationService) {
63+
super(project, chatMemoryManager, threadPoolManager, messageCreationService);
64+
}
3265

3366
@Override
3467
protected String getStrategyName() {
@@ -51,9 +84,8 @@ protected void executeStrategySpecific(
5184
return;
5285
}
5386

54-
// Prepare memory and add user message
87+
// Prepare memory which already adds the user message
5588
prepareMemory(context);
56-
chatMemoryManager.addUserMessage(context);
5789

5890
// Create the streaming handler that will process chunks of response
5991
StreamingResponseHandler handler = new StreamingResponseHandler(

src/test/java/com/devoxx/genie/service/projectscanner/ContentExtractorTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import static org.junit.jupiter.api.Assertions.*;
1616
import static org.mockito.Mockito.*;
1717

18-
public class ContentExtractorTest {
18+
class ContentExtractorTest {
1919

2020
private ContentExtractor contentExtractor;
2121
private VirtualFile mockFile;
@@ -32,7 +32,7 @@ public void setUp() {
3232
}
3333

3434
@Test
35-
public void testExtractFileContent_Success() throws IOException {
35+
void testExtractFileContent_Success() throws IOException {
3636
try (MockedStatic<ReadAction> readActionMock = mockStatic(ReadAction.class);
3737
MockedStatic<DevoxxGenieStateService> stateServiceMock = mockStatic(DevoxxGenieStateService.class)) {
3838

@@ -55,7 +55,7 @@ public void testExtractFileContent_Success() throws IOException {
5555
}
5656

5757
@Test
58-
public void testExtractFileContent_IOExceptionHandling() throws IOException {
58+
void testExtractFileContent_IOExceptionHandling() throws IOException {
5959
// Setup exception throwing when attempting to read the file
6060
when(mockFile.getInputStream()).thenThrow(new IOException("Test IO exception"));
6161

@@ -68,7 +68,7 @@ public void testExtractFileContent_IOExceptionHandling() throws IOException {
6868
}
6969

7070
@Test
71-
public void testExtractFileContent_WithOtherExceptionHandling() throws IOException {
71+
void testExtractFileContent_WithOtherExceptionHandling() throws IOException {
7272
// Setup a RuntimeException when trying to read the file
7373
when(mockFile.getInputStream()).thenThrow(new RuntimeException("Unexpected error"));
7474

@@ -81,7 +81,7 @@ public void testExtractFileContent_WithOtherExceptionHandling() throws IOExcepti
8181
}
8282

8383
@Test
84-
public void testCombineContent() {
84+
void testCombineContent() {
8585
// Setup test data
8686
String directoryStructure = "src/\n main/\n test/\n";
8787
String fileContents = "--- TestFile.java ---\npublic class TestFile { }";
@@ -97,7 +97,7 @@ public void testCombineContent() {
9797
}
9898

9999
@Test
100-
public void testJavadocRemoval_WhenEnabled() throws IOException {
100+
void testJavadocRemoval_WhenEnabled() throws IOException {
101101
// Setup content with Javadoc
102102
String fileContent = "/**\n * Test Javadoc comment\n */\npublic class Test {\n // Regular comment\n private String field;\n}";
103103
InputStream contentStream = new ByteArrayInputStream(fileContent.getBytes(StandardCharsets.UTF_8));
@@ -119,7 +119,7 @@ public void testJavadocRemoval_WhenEnabled() throws IOException {
119119
}
120120

121121
@Test
122-
public void testJavadocRetention_WhenDisabled() throws IOException {
122+
void testJavadocRetention_WhenDisabled() throws IOException {
123123
// Setup content with Javadoc
124124
String fileContent = "/**\n * Test Javadoc comment\n */\npublic class Test {\n // Regular comment\n private String field;\n}";
125125
InputStream contentStream = new ByteArrayInputStream(fileContent.getBytes(StandardCharsets.UTF_8));

src/test/java/com/devoxx/genie/service/projectscanner/DirectoryScannerServiceTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import static org.junit.jupiter.api.Assertions.*;
1414
import static org.mockito.Mockito.*;
1515

16-
public class DirectoryScannerServiceTest {
16+
class DirectoryScannerServiceTest {
1717

1818
private DirectoryScannerService scannerService;
1919

@@ -38,7 +38,7 @@ public void setUp() {
3838
}
3939

4040
@Test
41-
public void testAddDirectory_FirstAddition() {
41+
void testAddDirectory_FirstAddition() {
4242
// Test adding a directory for the first time
4343
scannerService.addDirectory(mockFileShort);
4444

@@ -48,7 +48,7 @@ public void testAddDirectory_FirstAddition() {
4848
}
4949

5050
@Test
51-
public void testAddDirectory_DuplicateAddition() {
51+
void testAddDirectory_DuplicateAddition() {
5252
// Add a directory first time
5353
scannerService.addDirectory(mockFileShort);
5454

@@ -59,7 +59,7 @@ public void testAddDirectory_DuplicateAddition() {
5959
}
6060

6161
@Test
62-
public void testAddDirectory_MultipleDifferentDirectories() {
62+
void testAddDirectory_MultipleDifferentDirectories() {
6363
// Add several different directories
6464
scannerService.addDirectory(mockFileShort);
6565
scannerService.addDirectory(mockFileMiddle);
@@ -79,7 +79,7 @@ public void testAddDirectory_MultipleDifferentDirectories() {
7979
}
8080

8181
@Test
82-
public void testGetHighestCommonRoot_EmptyMap() {
82+
void testGetHighestCommonRoot_EmptyMap() {
8383
// No directories added yet
8484
Optional<VirtualFile> result = scannerService.getHighestCommonRoot();
8585

@@ -88,7 +88,7 @@ public void testGetHighestCommonRoot_EmptyMap() {
8888
}
8989

9090
@Test
91-
public void testGetHighestCommonRoot_SingleDirectory() {
91+
void testGetHighestCommonRoot_SingleDirectory() {
9292
// Add only one directory
9393
scannerService.addDirectory(mockFileLong);
9494

@@ -100,7 +100,7 @@ public void testGetHighestCommonRoot_SingleDirectory() {
100100
}
101101

102102
@Test
103-
public void testGetHighestCommonRoot_MultipleDirectories() {
103+
void testGetHighestCommonRoot_MultipleDirectories() {
104104
// Add directories with paths of different lengths
105105
scannerService.addDirectory(mockFileShort); // Shortest path
106106
scannerService.addDirectory(mockFileMiddle);
@@ -114,7 +114,7 @@ public void testGetHighestCommonRoot_MultipleDirectories() {
114114
}
115115

116116
@Test
117-
public void testGetHighestCommonRoot_DirectoriesInReverseOrder() {
117+
void testGetHighestCommonRoot_DirectoriesInReverseOrder() {
118118
// Add directories in reverse order (longest path first)
119119
scannerService.addDirectory(mockFileLong); // Longest path
120120
scannerService.addDirectory(mockFileMiddle);
@@ -128,7 +128,7 @@ public void testGetHighestCommonRoot_DirectoriesInReverseOrder() {
128128
}
129129

130130
@Test
131-
public void testNormalizationOfPaths() {
131+
void testNormalizationOfPaths() {
132132
// Create a mock with a path containing unnecessary components
133133
VirtualFile mockFileWithDots = mock(VirtualFile.class);
134134
when(mockFileWithDots.getPath()).thenReturn("/project/./unnecessary/../project");
@@ -141,7 +141,7 @@ public void testNormalizationOfPaths() {
141141
}
142142

143143
@Test
144-
public void testAddDirectory_NullInput() {
144+
void testAddDirectory_NullInput() {
145145
// Test behavior with null input
146146
assertThrows(IllegalArgumentException.class, () -> scannerService.addDirectory(null));
147147
}

0 commit comments

Comments
 (0)