Skip to content

Commit 7e2e46a

Browse files
committed
make codewhispererservice spy on testBase
1 parent 3d75a4f commit 7e2e46a

File tree

5 files changed

+17
-29
lines changed

5 files changed

+17
-29
lines changed

plugins/amazonq/codewhisperer/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/codewhisperer/CodeWhispererAcceptTest.kt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,13 @@ import com.intellij.openapi.actionSystem.IdeActions
1212
import com.intellij.openapi.actionSystem.IdeActions.ACTION_EDITOR_MOVE_CARET_LEFT
1313
import com.intellij.openapi.actionSystem.IdeActions.ACTION_EDITOR_MOVE_CARET_RIGHT
1414
import com.intellij.openapi.actionSystem.IdeActions.ACTION_EDITOR_TAB
15-
import com.intellij.openapi.application.ApplicationManager
1615
import com.intellij.openapi.editor.actionSystem.EditorActionManager
17-
import com.intellij.testFramework.replaceService
1816
import com.intellij.testFramework.runInEdtAndWait
1917
import org.assertj.core.api.Assertions.assertThat
2018
import org.junit.Before
2119
import org.junit.Test
2220
import org.mockito.kotlin.any
2321
import org.mockito.kotlin.doAnswer
24-
import org.mockito.kotlin.spy
2522
import org.mockito.kotlin.stub
2623
import software.amazon.awssdk.services.codewhispererruntime.model.GenerateCompletionsRequest
2724
import software.amazon.awssdk.services.codewhispererruntime.model.GenerateCompletionsResponse
@@ -32,20 +29,13 @@ import software.aws.toolkits.jetbrains.services.codewhisperer.CodeWhispererTestU
3229
import software.aws.toolkits.jetbrains.services.codewhisperer.CodeWhispererTestUtil.metadata
3330
import software.aws.toolkits.jetbrains.services.codewhisperer.CodeWhispererTestUtil.sdkHttpResponse
3431
import software.aws.toolkits.jetbrains.services.codewhisperer.actions.CodeWhispererActionPromoter
35-
import software.aws.toolkits.jetbrains.services.codewhisperer.service.CodeWhispererService
3632
import kotlin.test.fail
37-
import org.mockito.kotlin.doNothing
3833

3934
class CodeWhispererAcceptTest : CodeWhispererTestBase() {
4035

41-
private lateinit var codewhispererServiceSpy: CodeWhispererService
42-
4336
@Before
4437
override fun setUp() {
4538
super.setUp()
46-
codewhispererServiceSpy = spy(codewhispererService)
47-
ApplicationManager.getApplication().replaceService(CodeWhispererService::class.java, codewhispererServiceSpy, disposableRule.disposable)
48-
doNothing().`when`(codewhispererServiceSpy).promoteNextInvocationIfAvailable()
4939

5040
// Use java code to test curly braces behavior
5141
projectRule.fixture.configureByText(javaFileName, javaTestContext)

plugins/amazonq/codewhisperer/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/codewhisperer/CodeWhispererSettingsTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ import kotlin.test.fail
3434

3535
class CodeWhispererSettingsTest : CodeWhispererTestBase() {
3636

37-
private lateinit var codewhispererServiceSpy: CodeWhispererService
37+
// private lateinit var codewhispererServiceSpy: CodeWhispererService
3838
private lateinit var toolWindowHeadlessManager: ToolWindowHeadlessManagerImpl
3939

4040
@Before
4141
override fun setUp() {
4242
super.setUp()
43-
codewhispererServiceSpy = spy(codewhispererService)
44-
ApplicationManager.getApplication().replaceService(CodeWhispererService::class.java, codewhispererServiceSpy, disposableRule.disposable)
43+
// codewhispererServiceSpy = spy(codewhispererService)
44+
// ApplicationManager.getApplication().replaceService(CodeWhispererService::class.java, codewhispererServiceSpy, disposableRule.disposable)
4545

4646
// Create a mock ToolWindowManager with working implementation of setAvailable() and isAvailable()
4747
toolWindowHeadlessManager = object : ToolWindowHeadlessManagerImpl(projectRule.project) {
@@ -83,7 +83,7 @@ class CodeWhispererSettingsTest : CodeWhispererTestBase() {
8383
whenever(stateManager.checkActiveCodeWhispererConnectionType(projectRule.project)).thenReturn(CodeWhispererLoginType.Logout)
8484
assertThat(isCodeWhispererEnabled(projectRule.project)).isFalse
8585
invokeCodeWhispererService()
86-
verify(codewhispererServiceSpy, never()).showRecommendationsInPopup(any(), any(), any())
86+
verify(codewhispererService, never()).showRecommendationsInPopup(any(), any(), any())
8787
}
8888

8989
@Test
@@ -92,7 +92,7 @@ class CodeWhispererSettingsTest : CodeWhispererTestBase() {
9292
assertThat(stateManager.isAutoEnabled()).isFalse
9393
runInEdtAndWait {
9494
projectRule.fixture.type(':')
95-
verify(codewhispererServiceSpy, never()).showRecommendationsInPopup(any(), any(), any())
95+
verify(codewhispererService, never()).showRecommendationsInPopup(any(), any(), any())
9696
}
9797
}
9898

plugins/amazonq/codewhisperer/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/codewhisperer/CodeWhispererTelemetryTest.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,9 @@ class CodeWhispererTelemetryTest : CodeWhispererTestBase() {
105105

106106
@Test
107107
fun `test pre-setup failure will send service invocation event with failed status`() {
108-
val codewhispererServiceSpy = spy(codewhispererService) {
109-
onGeneric { getRequestContext(any(), any(), any(), any(), any()) }
110-
.doAnswer { throw Exception() }
111-
}
112-
ApplicationManager.getApplication().replaceService(CodeWhispererService::class.java, codewhispererServiceSpy, disposableRule.disposable)
108+
doAnswer { throw Exception() }
109+
.`when`(codewhispererService)
110+
.getRequestContext(any(), any(), any(), any(), any())
113111

114112
invokeCodeWhispererService()
115113

@@ -164,7 +162,8 @@ class CodeWhispererTelemetryTest : CodeWhispererTestBase() {
164162
// 1 serviceInvocation + 1 userModification + 1 userDecision for accepted +
165163
// (count - 1) userDecisions for ignored
166164
verify(batcher, atLeast(2 + count)).enqueue(capture())
167-
assertEventsContainsFieldsAndCount(allValues, serviceInvocation, 3)
165+
// one service invocation for current recommendation and one for next recommendation
166+
assertEventsContainsFieldsAndCount(allValues, serviceInvocation, 2)
168167
assertEventsContainsFieldsAndCount(
169168
allValues,
170169
userModification,
@@ -411,15 +410,13 @@ class CodeWhispererTelemetryTest : CodeWhispererTestBase() {
411410

412411
@Test
413412
fun `test showing IntelliSense after triggering CodeWhisperer will send userDecision events of state Discard`() {
414-
val codewhispererServiceSpy = spy(codewhispererService)
415-
codewhispererServiceSpy.stub {
413+
codewhispererService.stub {
416414
onGeneric {
417415
canDoInvocation(any(), any())
418416
} doAnswer {
419417
true
420418
}
421419
}
422-
ApplicationManager.getApplication().replaceService(CodeWhispererService::class.java, codewhispererServiceSpy, disposableRule.disposable)
423420
popupManagerSpy.stub {
424421
onGeneric {
425422
hasConflictingPopups(any())

plugins/amazonq/codewhisperer/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/codewhisperer/CodeWhispererTestBase.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ open class CodeWhispererTestBase {
114114

115115
stateManager = spy(CodeWhispererExplorerActionManager.getInstance())
116116
recommendationManager = CodeWhispererRecommendationManager.getInstance()
117-
codewhispererService = CodeWhispererService.getInstance()
117+
codewhispererService = spy(CodeWhispererService.getInstance())
118+
ApplicationManager.getApplication().replaceService(CodeWhispererService::class.java, codewhispererService, disposableRule.disposable)
119+
doNothing().`when`(codewhispererService).promoteNextInvocationIfAvailable()
120+
118121
editorManager = CodeWhispererEditorManager.getInstance()
119122
settingsManager = CodeWhispererSettings.getInstance()
120123

plugins/amazonq/codewhisperer/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/codewhisperer/CodeWhispererUserInputTest.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,12 @@ class CodeWhispererUserInputTest : CodeWhispererTestBase() {
100100
}
101101

102102
private fun addUserInputAfterInvocation(userInput: String) {
103-
val codewhispererServiceSpy = spy(codewhispererService)
104103
val triggerTypeCaptor = argumentCaptor<TriggerTypeInfo>()
105104
val editorCaptor = argumentCaptor<Editor>()
106105
val projectCaptor = argumentCaptor<Project>()
107106
val psiFileCaptor = argumentCaptor<PsiFile>()
108107
val latencyContextCaptor = argumentCaptor<LatencyContext>()
109-
codewhispererServiceSpy.stub {
108+
codewhispererService.stub {
110109
onGeneric {
111110
getRequestContext(
112111
triggerTypeCaptor.capture(),
@@ -116,7 +115,7 @@ class CodeWhispererUserInputTest : CodeWhispererTestBase() {
116115
latencyContextCaptor.capture()
117116
)
118117
}.doAnswer {
119-
val requestContext = codewhispererServiceSpy.getRequestContext(
118+
val requestContext = codewhispererService.getRequestContext(
120119
triggerTypeCaptor.firstValue,
121120
editorCaptor.firstValue,
122121
projectCaptor.firstValue,
@@ -127,6 +126,5 @@ class CodeWhispererUserInputTest : CodeWhispererTestBase() {
127126
requestContext
128127
}.thenCallRealMethod()
129128
}
130-
ApplicationManager.getApplication().replaceService(CodeWhispererService::class.java, codewhispererServiceSpy, disposableRule.disposable)
131129
}
132130
}

0 commit comments

Comments
 (0)