fun recordAddMessageTest()

in plugins/amazonq/chat/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/amazonq/TelemetryHelperTest.kt [218:318]


    fun recordAddMessageTest() {
        mockClient.stub {
            on {
                sendChatAddMessageTelemetry(any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), any())
            } doReturn mockSteResponse
        }

        // set up request data
        val responseLength = 10
        val statusCode = 400
        val numberOfCodeBlocks = 1

        sut.recordAddMessage(
            data = data,
            response = response,
            responseLength = responseLength,
            statusCode = statusCode,
            numberOfCodeBlocks = numberOfCodeBlocks
        )

        // Q STE
        verify(mockClient).sendChatAddMessageTelemetry(
            sessionId = eq(conversationId),
            requestId = eq(messageId),
            userIntent = eq(software.amazon.awssdk.services.codewhispererruntime.model.UserIntent.fromValue(data.userIntent?.name)),
            hasCodeSnippet = any(),
            programmingLanguage = eq(lang),
            activeEditorTotalCharacters = eq(data.activeFileContext.focusAreaContext?.codeSelection?.length),
            timeToFirstChunkMilliseconds = eq(sut.getResponseStreamTimeToFirstChunk(tabId)),
            timeBetweenChunks = eq(sut.getResponseStreamTimeBetweenChunks(tabId)),
            fullResponselatency = any(), // TODO
            requestLength = eq(data.message.length),
            responseLength = eq(responseLength),
            numberOfCodeBlocks = eq(numberOfCodeBlocks),
            hasProjectLevelContext = eq(CodeWhispererSettings.getInstance().isProjectContextEnabled()),
            customization = eq(mockCustomization)
        )

        // Toolkit telemetry
        argumentCaptor<MetricEvent> {
            verify(mockBatcher).enqueue(capture())
            val event = firstValue.data.find { it.name == "amazonq_addMessage" }
            assertNotNull(event)
            assertThat(event)
                .matches({ it.metadata["cwsprChatConversationId"] == conversationId }, "conversation id doesn't match")
                .matches({ it.metadata["cwsprChatMessageId"] == "messageId" }, "message id doesn't match")
                .matches(
                    { it.metadata["cwsprChatTriggerInteraction"] == CwsprChatTriggerInteraction.ContextMenu.toString() },
                    "trigger type doesn't match"
                )
                .matches({ it.metadata["cwsprChatUserIntent"] == CwsprChatUserIntent.ImproveCode.toString() }, "user intent doesn't match")
                .matches({
                    it.metadata["cwsprChatHasCodeSnippet"] == (
                        data.activeFileContext.focusAreaContext?.codeSelection?.isNotEmpty()
                            ?: false
                        ).toString()
                }, "has code snippet doesn't match")
                .matches({ it.metadata["cwsprChatProgrammingLanguage"] == "java" }, "language doesn't match")
                .matches(
                    { it.metadata["cwsprChatActiveEditorTotalCharacters"] == data.activeFileContext.focusAreaContext?.codeSelection?.length?.toString() },
                    "total characters doesn't match"
                )
                .matches(
                    {
                        it.metadata["cwsprChatActiveEditorImportCount"] ==
                            data.activeFileContext.focusAreaContext?.codeNames?.fullyQualifiedNames?.used?.size?.toString()
                    },
                    "import count doesn't match"
                )
                .matches(
                    { it.metadata["cwsprChatResponseCodeSnippetCount"] == numberOfCodeBlocks.toString() },
                    "number of code blocks doesn't match"
                )
                .matches({ it.metadata["cwsprChatResponseCode"] == statusCode.toString() }, "response code doesn't match")
                .matches(
                    { it.metadata["cwsprChatSourceLinkCount"] == response.relatedSuggestions?.size?.toString() },
                    "source link count doesn't match"
                )
                .matches({ it.metadata["cwsprChatFollowUpCount"] == response.followUps?.size?.toString() }, "follow up count doesn't match")
                .matches(
                    { it.metadata["cwsprChatTimeToFirstChunk"] == sut.getResponseStreamTimeToFirstChunk(response.tabId).toInt().toString() },
                    "time to first chunk doesn't match"
                )
                .matches({
                    it.metadata["cwsprChatTimeBetweenChunks"] == "[${
                        sut.getResponseStreamTimeBetweenChunks(response.tabId).joinToString(", ")
                    }]"
                }, "time between chunks doesn't match")
                .matches({ it.metadata["cwsprChatRequestLength"] == data.message.length.toString() }, "request length doesn't match")
                .matches({ it.metadata["cwsprChatResponseLength"] == responseLength.toString() }, "response length doesn't match")
                .matches(
                    { it.metadata["cwsprChatConversationType"] == CwsprChatConversationType.Chat.toString() },
                    "conversation type doesn't match"
                )
                .matches({ it.metadata["codewhispererCustomizationArn"] == "customizationArn" }, "user intent doesn't match")
                .matches({
                    it.metadata["cwsprChatHasProjectContext"] == CodeWhispererSettings.getInstance().isProjectContextEnabled().toString()
                }, "customization description doesn't match")
//                .matches({  it.metadata["cwsprChatFullResponseLatency"] == "" }, "latency") TODO
        }
    }