Skip to content

Commit 5016751

Browse files
committed
test fix
1 parent 68880af commit 5016751

File tree

4 files changed

+62
-92
lines changed

4 files changed

+62
-92
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class CodeWhispererFeatureConfigService {
3333
}
3434

3535
// Only apply new auto-trigger UX to BID users
36-
val isNewAutoTriggerUX = getIsNewAutoTriggerUX()
36+
val isNewAutoTriggerUX = getNewAutoTriggerUX()
3737
if (isNewAutoTriggerUX) {
3838
calculateIfIamIdentityCenterConnection(project) {
3939
featureConfigs.remove(NEW_AUTO_TRIGGER_UX)
@@ -92,7 +92,7 @@ class CodeWhispererFeatureConfigService {
9292

9393
fun getCustomizationArnOverride(): String = getFeatureValueForKey(CUSTOMIZATION_ARN_OVERRIDE_NAME).stringValue()
9494

95-
fun getIsNewAutoTriggerUX(): Boolean = getFeatureValueForKey(NEW_AUTO_TRIGGER_UX).boolValue()
95+
fun getNewAutoTriggerUX(): Boolean = getFeatureValueForKey(NEW_AUTO_TRIGGER_UX).boolValue()
9696

9797
// Get the feature value for the given key.
9898
// In case of a misconfiguration, it will return a default feature value of Boolean false.

plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/telemetry/CodeWhispererCodeCoverageTracker.kt

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,22 @@ abstract class CodeWhispererCodeCoverageTracker(
5151
private val myServiceInvocationCount: AtomicInteger,
5252
) : Disposable {
5353
val percentage: Long?
54-
get() = if (totalTokensSize != 0L) calculatePercentage(rawAcceptedTokenSize, totalTokensSize) else null
55-
val acceptedTokensSize: Long
54+
get() = if (totalCharsCount != 0L) calculatePercentage(acceptedCharsCount, totalCharsCount) else null
55+
val unmodifiedAcceptedCharsCount: Long
5656
get() = fileToTokens.map {
57-
it.value.acceptedTokens.get()
57+
it.value.unmodifiedAcceptedChars.get()
5858
}.fold(0) { acc, next ->
5959
acc + next
6060
}
61-
val totalTokensSize: Long
61+
val totalCharsCount: Long
6262
get() = fileToTokens.map {
63-
it.value.totalTokens.get()
63+
it.value.totalChars.get()
6464
}.fold(0) { acc, next ->
6565
acc + next
6666
}
67-
private val rawAcceptedTokenSize: Long
67+
private val acceptedCharsCount: Long
6868
get() = fileToTokens.map {
69-
it.value.rawAcceptedTokens.get()
69+
it.value.acceptedChars.get()
7070
}.fold(0) { acc, next ->
7171
acc + next
7272
}
@@ -95,10 +95,10 @@ abstract class CodeWhispererCodeCoverageTracker(
9595
rangeMarker.putUserData(KEY_REMAINING_RECOMMENDATION, originalRecommendation)
9696
runReadAction {
9797
// also increment total tokens because accepted tokens are part of it
98-
incrementTotalTokens(rangeMarker.document, originalRecommendation.length)
98+
incrementTotalCharsCount(rangeMarker.document, originalRecommendation.length)
9999
// avoid counting CodeWhisperer inserted suggestion twice in total tokens
100100
if (rangeMarker.textRange.length in 2..49 && originalRecommendation.trim().isNotEmpty()) {
101-
incrementTotalTokens(rangeMarker.document, -rangeMarker.textRange.length)
101+
incrementTotalCharsCount(rangeMarker.document, -rangeMarker.textRange.length)
102102
}
103103
}
104104
}
@@ -135,12 +135,12 @@ abstract class CodeWhispererCodeCoverageTracker(
135135
// when event is auto closing [{(', there will be 2 separated events, both count as 1 char increase in total chars
136136
val text = event.newFragment.toString()
137137
if ((event.newLength == 1 && event.oldLength == 0) || (text.startsWith('\n') && text.trim().isEmpty())) {
138-
incrementTotalTokens(event.document, 1)
138+
incrementTotalCharsCount(event.document, 1)
139139
return
140140
} else if (event.newLength < 50 && text.trim().isNotEmpty()) {
141141
// count doc changes from <50 multi character input as total user written code
142142
// ignore all white space changes, this usually comes from IntelliJ formatting
143-
incrementTotalTokens(event.document, event.newLength)
143+
incrementTotalCharsCount(event.document, event.newLength)
144144
}
145145
}
146146

@@ -165,19 +165,19 @@ abstract class CodeWhispererCodeCoverageTracker(
165165

166166
private fun incrementUnmodifiedAcceptedCharsCount(document: Document, delta: Int) {
167167
val tokens = fileToTokens.getOrPut(document) { CodeCoverageTokens() }
168-
tokens.acceptedTokens.addAndGet(delta)
168+
tokens.unmodifiedAcceptedChars.addAndGet(delta)
169169
}
170170

171171
private fun incrementAcceptedCharsCount(document: Document, delta: Int) {
172172
val tokens = fileToTokens.getOrPut(document) { CodeCoverageTokens() }
173-
tokens.rawAcceptedTokens.addAndGet(delta)
173+
tokens.acceptedChars.addAndGet(delta)
174174
}
175175

176-
private fun incrementTotalTokens(document: Document, delta: Int) {
176+
private fun incrementTotalCharsCount(document: Document, delta: Int) {
177177
val tokens = fileToTokens.getOrPut(document) { CodeCoverageTokens() }
178178
tokens.apply {
179-
totalTokens.addAndGet(delta)
180-
if (totalTokens.get() < 0) totalTokens.set(0)
179+
totalChars.addAndGet(delta)
180+
if (totalChars.get() < 0) totalChars.set(0)
181181
}
182182
}
183183

@@ -220,9 +220,9 @@ abstract class CodeWhispererCodeCoverageTracker(
220220
val response = CodeWhispererClientAdaptor.getInstance(project).sendCodePercentageTelemetry(
221221
language,
222222
customizationArn,
223-
rawAcceptedTokenSize,
224-
totalTokensSize,
225-
acceptedTokensSize
223+
acceptedCharsCount,
224+
totalCharsCount,
225+
unmodifiedAcceptedCharsCount
226226
)
227227
LOG.debug { "Successfully sent code percentage telemetry. RequestId: ${response.responseMetadata().requestId()}" }
228228
} catch (e: Exception) {
@@ -237,11 +237,11 @@ abstract class CodeWhispererCodeCoverageTracker(
237237
percentage?.let { percentage ->
238238
CodewhispererTelemetry.codePercentage(
239239
project = null,
240-
codewhispererAcceptedTokens = acceptedTokensSize,
241-
codewhispererSuggestedTokens = rawAcceptedTokenSize,
240+
codewhispererAcceptedTokens = unmodifiedAcceptedCharsCount,
241+
codewhispererSuggestedTokens = acceptedCharsCount,
242242
codewhispererLanguage = language.toTelemetryType(),
243243
codewhispererPercentage = percentage,
244-
codewhispererTotalTokens = totalTokensSize,
244+
codewhispererTotalTokens = totalCharsCount,
245245
successCount = myServiceInvocationCount.get().toLong(),
246246
codewhispererCustomizationArn = customizationArn,
247247
credentialStartUrl = getCodeWhispererStartUrl(project)
@@ -300,14 +300,8 @@ class DefaultCodeWhispererCodeCoverageTracker(project: Project, language: CodeWh
300300
AtomicInteger(0)
301301
)
302302

303-
class CodeCoverageTokens(totalTokens: Int = 0, acceptedTokens: Int = 0, rawAcceptedTokens: Int = 0) {
304-
val totalTokens: AtomicInteger
305-
val acceptedTokens: AtomicInteger
306-
val rawAcceptedTokens: AtomicInteger
307-
308-
init {
309-
this.totalTokens = AtomicInteger(totalTokens)
310-
this.acceptedTokens = AtomicInteger(acceptedTokens)
311-
this.rawAcceptedTokens = AtomicInteger(rawAcceptedTokens)
312-
}
303+
class CodeCoverageTokens(totalChars: Int = 0, unmodifiedAcceptedChars: Int = 0, acceptedChars: Int = 0) {
304+
val totalChars = AtomicInteger(totalChars)
305+
val unmodifiedAcceptedChars = AtomicInteger(unmodifiedAcceptedChars)
306+
val acceptedChars = AtomicInteger(acceptedChars)
313307
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ class CodeWhispererClientAdaptorTest {
402402
@Test
403403
fun `sendTelemetryEvent for userModification respects telemetry optin status`() {
404404
sendTelemetryEventOptOutCheckHelper {
405-
sut.sendUserModificationTelemetry(aString(), aString(), aProgrammingLanguage(), aString(), 0.0)
405+
sut.sendUserModificationTelemetry(aString(), aString(), aProgrammingLanguage(), aString(), 0, 0)
406406
}
407407
}
408408

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

Lines changed: 33 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import software.aws.toolkits.jetbrains.services.codewhisperer.telemetry.CodeCove
6464
import software.aws.toolkits.jetbrains.services.codewhisperer.telemetry.CodeWhispererCodeCoverageTracker
6565
import software.aws.toolkits.jetbrains.services.codewhisperer.toolwindow.CodeWhispererCodeReferenceManager
6666
import software.aws.toolkits.jetbrains.services.codewhisperer.util.CodeWhispererConstants.TOTAL_SECONDS_IN_MINUTE
67+
import software.aws.toolkits.jetbrains.services.codewhisperer.util.CodeWhispererUtil.getUnmodifiedAcceptedCharsCount
6768
import software.aws.toolkits.jetbrains.services.codewhisperer.util.CrossFileStrategy
6869
import software.aws.toolkits.jetbrains.services.telemetry.NoOpPublisher
6970
import software.aws.toolkits.jetbrains.services.telemetry.TelemetryService
@@ -253,7 +254,7 @@ internal class CodeWhispererCodeCoverageTrackerTestPython : CodeWhispererCodeCov
253254
val captor = argumentCaptor<DocumentEvent>()
254255
verify(sut, Times(1)).documentChanged(captor.capture())
255256
assertThat(captor.firstValue.newFragment.toString()).isEqualTo(keystrokeInput)
256-
assertThat(sut.totalTokensSize).isEqualTo(keystrokeInput.length.toLong())
257+
assertThat(sut.totalCharsCount).isEqualTo(keystrokeInput.length.toLong())
257258
}
258259

259260
@Test
@@ -271,15 +272,15 @@ internal class CodeWhispererCodeCoverageTrackerTestPython : CodeWhispererCodeCov
271272
val captor = argumentCaptor<DocumentEvent>()
272273
verify(sut, Times(1)).documentChanged(captor.capture())
273274
assertThat(captor.firstValue.newFragment.toString()).isEqualTo(pythonTestLeftContext)
274-
assertThat(sut.totalTokensSize).isEqualTo(pythonTestLeftContext.length.toLong())
275+
assertThat(sut.totalCharsCount).isEqualTo(pythonTestLeftContext.length.toLong())
275276

276277
val anotherCode = "(x, y):".repeat(8)
277278
runInEdtAndWait {
278279
WriteCommandAction.runWriteCommandAction(project) {
279280
fixture.editor.appendString(anotherCode)
280281
}
281282
}
282-
assertThat(sut.totalTokensSize).isEqualTo(pythonTestLeftContext.length.toLong())
283+
assertThat(sut.totalCharsCount).isEqualTo(pythonTestLeftContext.length.toLong())
283284
}
284285

285286
@Test
@@ -293,7 +294,7 @@ internal class CodeWhispererCodeCoverageTrackerTestPython : CodeWhispererCodeCov
293294

294295
CodeWhispererCodeCoverageTracker.getInstancesMap()[CodeWhispererPython.INSTANCE] = sut
295296
sut.activateTrackerIfNotActive()
296-
assertThat(sut.totalTokensSize).isEqualTo(pythonTestLeftContext.length.toLong())
297+
assertThat(sut.totalCharsCount).isEqualTo(pythonTestLeftContext.length.toLong())
297298

298299
runInEdtAndWait {
299300
fixture.editor.caretModel.primaryCaret.moveToOffset(fixture.editor.document.textLength)
@@ -302,7 +303,7 @@ internal class CodeWhispererCodeCoverageTrackerTestPython : CodeWhispererCodeCov
302303
}
303304
}
304305

305-
assertThat(sut.totalTokensSize).isEqualTo(pythonTestLeftContext.length.toLong())
306+
assertThat(sut.totalCharsCount).isEqualTo(pythonTestLeftContext.length.toLong())
306307
}
307308

308309
@Test
@@ -315,15 +316,15 @@ internal class CodeWhispererCodeCoverageTrackerTestPython : CodeWhispererCodeCov
315316
)
316317
CodeWhispererCodeCoverageTracker.getInstancesMap()[CodeWhispererPython.INSTANCE] = sut
317318
sut.activateTrackerIfNotActive()
318-
assertThat(sut.totalTokensSize).isEqualTo(pythonTestLeftContext.length.toLong())
319+
assertThat(sut.totalCharsCount).isEqualTo(pythonTestLeftContext.length.toLong())
319320

320321
runInEdtAndWait {
321322
WriteCommandAction.runWriteCommandAction(project) {
322323
fixture.editor.document.insertString(fixture.editor.caretModel.offset, "\t")
323324
}
324325
}
325326

326-
assertThat(sut.totalTokensSize).isEqualTo(pythonTestLeftContext.length + 1L)
327+
assertThat(sut.totalCharsCount).isEqualTo(pythonTestLeftContext.length + 1L)
327328
}
328329

329330
@Test
@@ -366,36 +367,14 @@ internal class CodeWhispererCodeCoverageTrackerTestPython : CodeWhispererCodeCov
366367

367368
sut.activateTrackerIfNotActive()
368369
assertThat(sut.activeRequestCount()).isEqualTo(1)
369-
assertThat(sut.acceptedTokensSize).isEqualTo("bar".length.toLong())
370-
assertThat(sut.totalTokensSize).isEqualTo("foobar".length.toLong())
370+
assertThat(sut.unmodifiedAcceptedCharsCount).isEqualTo("bar".length.toLong())
371+
assertThat(sut.totalCharsCount).isEqualTo("foobar".length.toLong())
371372

372373
sut.forceTrackerFlush()
373374

374375
assertThat(sut.activeRequestCount()).isEqualTo(1)
375-
assertThat(sut.acceptedTokensSize).isEqualTo(0)
376-
assertThat(sut.totalTokensSize).isEqualTo(0)
377-
}
378-
379-
@Test
380-
fun `test when rangeMarker is not vaild, acceptedToken will not be updated`() {
381-
// when user delete whole recommendation, rangeMarker will be isValid = false
382-
val rangeMarkerMock: RangeMarker = mock()
383-
whenever(rangeMarkerMock.isValid).thenReturn(false)
384-
sut = spy(
385-
TestCodePercentageTracker(
386-
project,
387-
TOTAL_SECONDS_IN_MINUTE,
388-
CodeWhispererPython.INSTANCE,
389-
mutableListOf(rangeMarkerMock),
390-
)
391-
) {
392-
onGeneric { getUnmodifiedAcceptedCharsCount(any(), any()) } doReturn 100
393-
}
394-
395-
sut.activateTrackerIfNotActive()
396-
sut.forceTrackerFlush()
397-
398-
verify(sut, Times(0)).getUnmodifiedAcceptedCharsCount(any(), any())
376+
assertThat(sut.unmodifiedAcceptedCharsCount).isEqualTo(0)
377+
assertThat(sut.totalCharsCount).isEqualTo(0)
399378
}
400379

401380
@Test
@@ -430,12 +409,11 @@ internal class CodeWhispererCodeCoverageTrackerTestPython : CodeWhispererCodeCov
430409
TOTAL_SECONDS_IN_MINUTE,
431410
CodeWhispererPython.INSTANCE,
432411
mutableListOf(rangeMarkerMock1),
433-
mutableMapOf(fixture.editor.document to CodeCoverageTokens(totalTokens = 100, acceptedTokens = 0, rawAcceptedTokens = 0)),
412+
mutableMapOf(fixture.editor.document to CodeCoverageTokens(totalChars = 100, unmodifiedAcceptedChars = 0, acceptedChars = 0)),
434413
1
435414
)
436415
) {
437416
onGeneric { extractRangeMarkerString(any()) } doReturn "fou"
438-
onGeneric { getUnmodifiedAcceptedCharsCount(any(), any()) } doReturn 1
439417
}
440418
sut.emitCodeWhispererCodeContribution()
441419

@@ -445,8 +423,7 @@ internal class CodeWhispererCodeCoverageTrackerTestPython : CodeWhispererCodeCov
445423
metricCaptor.allValues,
446424
CODE_PERCENTAGE,
447425
1,
448-
CWSPR_PERCENTAGE to "3",
449-
CWSPR_ACCEPTED_TOKENS to "1",
426+
CWSPR_ACCEPTED_TOKENS to "2",
450427
CWSPR_RAW_ACCEPTED_TOKENS to "3",
451428
CWSPR_TOTAL_TOKENS to "100",
452429
)
@@ -465,48 +442,47 @@ internal class CodeWhispererCodeCoverageTrackerTestPython : CodeWhispererCodeCov
465442
}
466443

467444
@Test
468-
fun `test getAcceptedTokensDelta()`() {
469-
val tracker = TestCodePercentageTracker(project, TOTAL_SECONDS_IN_MINUTE, CodeWhispererPython.INSTANCE)
445+
fun `test getUnmodifiedAcceptedCharsCount()`() {
470446
var originalRecommendation = "foo"
471447
var modifiedRecommendation = "fou"
472-
var delta = tracker.getUnmodifiedAcceptedCharsCount(originalRecommendation, modifiedRecommendation)
473-
assertThat(delta).isEqualTo(2)
448+
var unmodifiedCharsCount = getUnmodifiedAcceptedCharsCount(originalRecommendation, modifiedRecommendation)
449+
assertThat(unmodifiedCharsCount).isEqualTo(2)
474450

475451
originalRecommendation = "foo"
476452
modifiedRecommendation = "f11111oo"
477-
delta = tracker.getUnmodifiedAcceptedCharsCount(originalRecommendation, modifiedRecommendation)
478-
assertThat(delta).isEqualTo(3)
453+
unmodifiedCharsCount = getUnmodifiedAcceptedCharsCount(originalRecommendation, modifiedRecommendation)
454+
assertThat(unmodifiedCharsCount).isEqualTo(3)
479455

480456
originalRecommendation = "foo"
481457
modifiedRecommendation = "fo"
482-
delta = tracker.getUnmodifiedAcceptedCharsCount(originalRecommendation, modifiedRecommendation)
483-
assertThat(delta).isEqualTo(2)
458+
unmodifiedCharsCount = getUnmodifiedAcceptedCharsCount(originalRecommendation, modifiedRecommendation)
459+
assertThat(unmodifiedCharsCount).isEqualTo(2)
484460

485461
originalRecommendation = "helloworld"
486462
modifiedRecommendation = "HelloWorld"
487-
delta = tracker.getUnmodifiedAcceptedCharsCount(originalRecommendation, modifiedRecommendation)
488-
assertThat(delta).isEqualTo("helloworld".length - 2)
463+
unmodifiedCharsCount = getUnmodifiedAcceptedCharsCount(originalRecommendation, modifiedRecommendation)
464+
assertThat(unmodifiedCharsCount).isEqualTo("helloworld".length - 2)
489465

490466
originalRecommendation = "helloworld"
491467
modifiedRecommendation = "World"
492-
delta = tracker.getUnmodifiedAcceptedCharsCount(originalRecommendation, modifiedRecommendation)
493-
assertThat(delta).isEqualTo("helloworld".length - "hello".length - 1)
468+
unmodifiedCharsCount = getUnmodifiedAcceptedCharsCount(originalRecommendation, modifiedRecommendation)
469+
assertThat(unmodifiedCharsCount).isEqualTo("helloworld".length - "hello".length - 1)
494470

495471
originalRecommendation = "CodeWhisperer"
496472
modifiedRecommendation = "CODE"
497-
delta = tracker.getUnmodifiedAcceptedCharsCount(originalRecommendation, modifiedRecommendation)
498-
assertThat(delta).isEqualTo(1)
473+
unmodifiedCharsCount = getUnmodifiedAcceptedCharsCount(originalRecommendation, modifiedRecommendation)
474+
assertThat(unmodifiedCharsCount).isEqualTo(1)
499475

500476
originalRecommendation = "CodeWhisperer"
501477
modifiedRecommendation = "codewhispererISBEST"
502-
delta = tracker.getUnmodifiedAcceptedCharsCount(originalRecommendation, modifiedRecommendation)
503-
assertThat(delta).isEqualTo("CodeWhisperer".length - 2)
478+
unmodifiedCharsCount = getUnmodifiedAcceptedCharsCount(originalRecommendation, modifiedRecommendation)
479+
assertThat(unmodifiedCharsCount).isEqualTo("CodeWhisperer".length - 2)
504480

505481
val pythonCommentAddedByUser = "\"\"\"we don't count this comment as generated by CodeWhisperer\"\"\"\n"
506482
originalRecommendation = "x, y):\n\treturn x + y"
507483
modifiedRecommendation = "x, y):\n$pythonCommentAddedByUser\treturn x + y"
508-
delta = tracker.getUnmodifiedAcceptedCharsCount(originalRecommendation, modifiedRecommendation)
509-
assertThat(delta).isEqualTo(originalRecommendation.length)
484+
unmodifiedCharsCount = getUnmodifiedAcceptedCharsCount(originalRecommendation, modifiedRecommendation)
485+
assertThat(unmodifiedCharsCount).isEqualTo(originalRecommendation.length)
510486
}
511487

512488
@Test
@@ -557,7 +533,7 @@ internal class CodeWhispererCodeCoverageTrackerTestJava : CodeWhispererCodeCover
557533
project,
558534
TOTAL_SECONDS_IN_MINUTE,
559535
language = CodeWhispererJava.INSTANCE,
560-
codeCoverageTokens = mutableMapOf(fixture.editor.document to CodeCoverageTokens(totalTokens = codeNeedToBeReformatted.length))
536+
codeCoverageTokens = mutableMapOf(fixture.editor.document to CodeCoverageTokens(totalChars = codeNeedToBeReformatted.length))
561537
)
562538
)
563539
CodeWhispererCodeCoverageTracker.getInstancesMap()[CodeWhispererJava.INSTANCE] = sut
@@ -568,7 +544,7 @@ internal class CodeWhispererCodeCoverageTrackerTestJava : CodeWhispererCodeCover
568544
}
569545
// reformat should fire documentChanged events, but tracker should not update token from these events
570546
verify(sut, atLeastOnce()).documentChanged(any())
571-
assertThat(sut.totalTokensSize).isEqualTo(codeNeedToBeReformatted.length.toLong())
547+
assertThat(sut.totalCharsCount).isEqualTo(codeNeedToBeReformatted.length.toLong())
572548

573549
val formatted = """
574550
class Answer {

0 commit comments

Comments
 (0)