Skip to content

Commit eaf4932

Browse files
committed
tst
1 parent b150cb8 commit eaf4932

File tree

6 files changed

+232
-14
lines changed

6 files changed

+232
-14
lines changed

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/editor/context/project/LspMessage.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ sealed interface LspMessage {
2222
override val endpoint: String = "query"
2323
}
2424

25-
data object QueryInlineCompletion: LspMessage {
25+
data object QueryInlineCompletion : LspMessage {
2626
override val endpoint: String = "queryInlineProjectContext"
2727
}
2828

@@ -37,22 +37,22 @@ data class IndexRequest(
3737
val filePaths: List<String>,
3838
val projectRoot: String,
3939
val config: String,
40-
val language: String = ""
41-
): LspRequest
40+
val language: String = "",
41+
) : LspRequest
4242

4343
data class UpdateIndexRequest(
4444
val filePaths: List<String>,
4545
val mode: String,
46-
): LspRequest
46+
) : LspRequest
4747

4848
data class QueryChatRequest(
4949
val query: String,
50-
): LspRequest
50+
) : LspRequest
5151

5252
data class QueryInlineCompletionRequest(
5353
val query: String,
54-
val filePath: String
55-
): LspRequest
54+
val filePath: String,
55+
) : LspRequest
5656

5757
data class LspResponse(
5858
val responseCode: Int,

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/editor/context/project/ProjectContextController.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ class ProjectContextController(private val project: Project, private val cs: Cor
4545
)
4646
}
4747

48-
4948
fun getProjectContextIndexComplete() = projectContextProvider.isIndexComplete.get()
5049

5150
fun query(prompt: String): List<RelevantDocument> {

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/editor/context/project/ProjectContextEditorListener.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33

44
package software.aws.toolkits.jetbrains.services.cwc.editor.context.project
55
import com.intellij.openapi.fileEditor.FileDocumentManager
6-
import com.intellij.openapi.fileEditor.FileEditorManager
76
import com.intellij.openapi.fileEditor.FileEditorManagerEvent
87
import com.intellij.openapi.fileEditor.FileEditorManagerListener
9-
import com.intellij.openapi.vfs.VirtualFile
10-
import software.aws.toolkits.jetbrains.services.codewhisperer.settings.CodeWhispererSettings
118

129
class ProjectContextEditorListener : FileEditorManagerListener {
1310
override fun selectionChanged(event: FileEditorManagerEvent) {

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/editor/context/project/ProjectContextProvider.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,11 @@ class ProjectContextProvider(val project: Project, private val encoderServer: En
176176
mapper.readValue<List<InlineBm25Chunk>>(response.responseBody)
177177
} catch (e: Exception) {
178178
logger.warn { "error parsing query response ${e.message}" }
179-
throw e
179+
emptyList()
180180
}
181181
}
182182

183-
private fun getUsage(): Usage? {
183+
fun getUsage(): Usage? {
184184
val response = sendMsgToLsp(LspMessage.GetUsageMetrics, request = null)
185185
return try {
186186
val parsedResponse = mapper.readValue<Usage>(response.responseBody)

plugins/amazonq/chat/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/amazonq/workspace/context/ProjectContextProviderTest.kt

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,31 @@
33

44
package software.aws.toolkits.jetbrains.services.amazonq.workspace.context
55

6+
import com.github.tomakehurst.wiremock.client.WireMock.aResponse
7+
import com.github.tomakehurst.wiremock.client.WireMock.any
8+
import com.github.tomakehurst.wiremock.client.WireMock.stubFor
9+
import com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo
10+
import com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig
11+
import com.github.tomakehurst.wiremock.http.Body
12+
import com.github.tomakehurst.wiremock.junit.WireMockRule
613
import com.intellij.openapi.project.Project
714
import kotlinx.coroutines.test.TestScope
815
import kotlinx.coroutines.test.runTest
16+
import org.assertj.core.api.Assertions.assertThat
917
import org.junit.Before
1018
import org.junit.Rule
1119
import org.mockito.kotlin.any
20+
import org.mockito.kotlin.doReturn
1221
import org.mockito.kotlin.mock
22+
import org.mockito.kotlin.stub
1323
import org.mockito.kotlin.times
1424
import org.mockito.kotlin.verify
1525
import org.mockito.kotlin.whenever
1626
import software.aws.toolkits.jetbrains.services.cwc.editor.context.project.EncoderServer
27+
import software.aws.toolkits.jetbrains.services.cwc.editor.context.project.InlineBm25Chunk
28+
import software.aws.toolkits.jetbrains.services.cwc.editor.context.project.LspMessage
1729
import software.aws.toolkits.jetbrains.services.cwc.editor.context.project.ProjectContextProvider
30+
import software.aws.toolkits.jetbrains.services.cwc.editor.context.project.RelevantDocument
1831
import software.aws.toolkits.jetbrains.utils.rules.CodeInsightTestFixtureRule
1932
import software.aws.toolkits.jetbrains.utils.rules.JavaCodeInsightTestFixtureRule
2033
import java.net.ConnectException
@@ -25,6 +38,10 @@ class ProjectContextProviderTest {
2538
@JvmField
2639
val projectRule: CodeInsightTestFixtureRule = JavaCodeInsightTestFixtureRule()
2740

41+
@Rule
42+
@JvmField
43+
val wireMock: WireMockRule = createMockServer()
44+
2845
private val project: Project
2946
get() = projectRule.project
3047

@@ -34,6 +51,208 @@ class ProjectContextProviderTest {
3451
@Before
3552
fun setup() {
3653
sut = ProjectContextProvider(project, encoderServer, TestScope())
54+
encoderServer.stub { on { port } doReturn wireMock.port() }
55+
// initialization
56+
stubFor(any(urlPathEqualTo("/initialize")).willReturn(aResponse().withStatus(200).withResponseBody(Body("initialize response"))))
57+
58+
// build index
59+
stubFor(any(urlPathEqualTo("/indexFiles")).willReturn(aResponse().withStatus(200).withResponseBody(Body("initialize response"))))
60+
stubFor(any(urlPathEqualTo("/buildIndex")).willReturn(aResponse().withStatus(200).withResponseBody(Body("initialize response"))))
61+
62+
// update index
63+
stubFor(any(urlPathEqualTo("/updateIndex")).willReturn(aResponse().withStatus(200).withResponseBody(Body("initialize response"))))
64+
stubFor(any(urlPathEqualTo("/updateIndexV2")).willReturn(aResponse().withStatus(200).withResponseBody(Body("initialize response"))))
65+
66+
// query
67+
stubFor(
68+
any(urlPathEqualTo("/query")).willReturn(
69+
aResponse().withStatus(200).withResponseBody(
70+
Body(
71+
"""
72+
[
73+
{
74+
"filePath": "file1",
75+
"content": "content1",
76+
"id": "id1",
77+
"index": "index1",
78+
"vec": [
79+
"vec_1-1",
80+
"vec_1-2",
81+
"vec_1-3"
82+
],
83+
"context": "context1",
84+
"prev": "prev1",
85+
"next": "next1",
86+
"relativePath": "relativeFilePath1",
87+
"programmingLanguage": "language1"
88+
},
89+
{
90+
"filePath": "file2",
91+
"content": "content2",
92+
"id": "id2",
93+
"index": "index2",
94+
"vec": [
95+
"vec_2-1",
96+
"vec_2-2",
97+
"vec_2-3"
98+
],
99+
"context": "context2",
100+
"prev": "prev2",
101+
"next": "next2",
102+
"relativePath": "relativeFilePath2",
103+
"programmingLanguage": "language2"
104+
}
105+
]
106+
""".trimIndent()
107+
)
108+
)
109+
)
110+
)
111+
stubFor(
112+
any(urlPathEqualTo("/queryInlineProjectContext")).willReturn(
113+
aResponse().withStatus(200).withResponseBody(
114+
Body(
115+
"""
116+
[
117+
{
118+
"content": "content1",
119+
"filePath": "file1",
120+
"score": 0.1
121+
},
122+
{
123+
"content": "content2",
124+
"filePath": "file2",
125+
"score": 0.2
126+
},
127+
{
128+
"content": "content3",
129+
"filePath": "file3",
130+
"score": 0.3
131+
}
132+
]
133+
""".trimIndent()
134+
)
135+
)
136+
)
137+
)
138+
139+
stubFor(
140+
any(urlPathEqualTo("/getUsage"))
141+
.willReturn(
142+
aResponse()
143+
.withStatus(200)
144+
.withResponseBody(
145+
Body(
146+
"""
147+
{
148+
"memoryUsage":123,
149+
"cpuUsage":456
150+
}
151+
""".trimIndent()
152+
)
153+
)
154+
)
155+
)
156+
}
157+
158+
@Test
159+
fun `Lsp endpoint correctness`() {
160+
assertThat(LspMessage.Initialize.endpoint).isEqualTo("initialize")
161+
assertThat(LspMessage.Index.endpoint).isEqualTo("buildIndex")
162+
assertThat(LspMessage.UpdateIndex.endpoint).isEqualTo("updateIndexV2")
163+
assertThat(LspMessage.QueryChat.endpoint).isEqualTo("query")
164+
assertThat(LspMessage.QueryInlineCompletion.endpoint).isEqualTo("queryInlineProjectContext")
165+
assertThat(LspMessage.GetUsageMetrics.endpoint).isEqualTo("getUsage")
166+
}
167+
168+
@Test
169+
fun `query chat should return empty if resultset non deserializable`() = runTest {
170+
stubFor(
171+
any(urlPathEqualTo("/query")).willReturn(
172+
aResponse().withStatus(200).withResponseBody(
173+
Body(
174+
"""
175+
[
176+
"foo", "bar"
177+
]
178+
""".trimIndent()
179+
)
180+
)
181+
)
182+
)
183+
val r = sut.query("foo")
184+
assertThat(r).isEmpty()
185+
}
186+
187+
@Test
188+
fun `query chat should return deserialized relevantDocument`() = runTest {
189+
val r = sut.query("foo")
190+
assertThat(r).hasSize(2)
191+
assertThat(r[0]).isEqualTo(
192+
RelevantDocument(
193+
"relativeFilePath1",
194+
"context1"
195+
)
196+
)
197+
assertThat(r[1]).isEqualTo(
198+
RelevantDocument(
199+
"relativeFilePath2",
200+
"context2"
201+
)
202+
)
203+
}
204+
205+
@Test
206+
fun `query inline should return empty if resultset not deserializable`() {
207+
stubFor(
208+
any(urlPathEqualTo("/queryInlineProjectContext")).willReturn(
209+
aResponse().withStatus(200).withResponseBody(
210+
Body(
211+
"""
212+
[
213+
"foo", "bar"
214+
]
215+
""".trimIndent()
216+
)
217+
)
218+
)
219+
)
220+
221+
val r = sut.queryInline("foo", "filepath")
222+
assertThat(r).isEmpty()
223+
}
224+
225+
@Test
226+
fun `query inline should return deserialized bm25 chunks`() = runTest {
227+
val r = sut.queryInline("foo", "filepath")
228+
assertThat(r).hasSize(3)
229+
assertThat(r[0]).isEqualTo(
230+
InlineBm25Chunk(
231+
"content1",
232+
"file1",
233+
0.1
234+
)
235+
)
236+
assertThat(r[1]).isEqualTo(
237+
InlineBm25Chunk(
238+
"content2",
239+
"file2",
240+
0.2
241+
)
242+
)
243+
assertThat(r[2]).isEqualTo(
244+
InlineBm25Chunk(
245+
"content3",
246+
"file3",
247+
0.3
248+
)
249+
)
250+
}
251+
252+
@Test
253+
fun `get usage should return memory, cpu usage`() = runTest {
254+
val r = sut.getUsage()
255+
assertThat(r).isEqualTo(ProjectContextProvider.Usage(123, 456))
37256
}
38257

39258
@Test
@@ -57,4 +276,6 @@ class ProjectContextProviderTest {
57276
}
58277
verify(encoderServer, times(1)).encrypt(any())
59278
}
279+
280+
private fun createMockServer() = WireMockRule(wireMockConfig().dynamicPort())
60281
}

plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/service/CodeWhispererFeatureConfigService.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class CodeWhispererFeatureConfigService {
9696
fun getNewAutoTriggerUX(): Boolean = getFeatureValueForKey(NEW_AUTO_TRIGGER_UX).boolValue()
9797

9898
// TODO: remove dev mode flag
99-
fun getInlineCompletion(): Boolean = if(isDeveloperMode()) true else getFeatureValueForKey(INLINE_COMPLETION).boolValue()
99+
fun getInlineCompletion(): Boolean = if (isDeveloperMode()) true else getFeatureValueForKey(INLINE_COMPLETION).boolValue()
100100

101101
// Get the feature value for the given key.
102102
// In case of a misconfiguration, it will return a default feature value of Boolean false.
@@ -107,6 +107,7 @@ class CodeWhispererFeatureConfigService {
107107
companion object {
108108
fun getInstance(): CodeWhispererFeatureConfigService = service()
109109
private const val TEST_FEATURE_NAME = "testFeature"
110+
110111
// TODO: update
111112
private const val INLINE_COMPLETION = "inlineCompletion"
112113
private const val DATA_COLLECTION_FEATURE = "IDEProjectContextDataCollection"

0 commit comments

Comments
 (0)