// File generated from our OpenAPI spec by Stainless.

package com.openai.models.responses

import com.fasterxml.jackson.module.kotlin.jacksonTypeRef
import com.openai.core.JsonValue
import com.openai.core.jsonMapper
import com.openai.models.ChatModel
import com.openai.models.Reasoning
import com.openai.models.ReasoningEffort
import com.openai.models.ResponseFormatText
import com.openai.models.ResponsesModel
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test

internal class ResponseTest {

    @Test
    fun create() {
        val response =
            Response.builder()
                .id("id")
                .createdAt(0.0)
                .error(
                    ResponseError.builder()
                        .code(ResponseError.Code.SERVER_ERROR)
                        .message("message")
                        .build()
                )
                .incompleteDetails(
                    Response.IncompleteDetails.builder()
                        .reason(Response.IncompleteDetails.Reason.MAX_OUTPUT_TOKENS)
                        .build()
                )
                .instructions("string")
                .metadata(
                    Response.Metadata.builder()
                        .putAdditionalProperty("foo", JsonValue.from("string"))
                        .build()
                )
                .model(ChatModel.GPT_5_1)
                .addOutput(
                    ResponseOutputMessage.builder()
                        .id("id")
                        .addContent(
                            ResponseOutputText.builder()
                                .addAnnotation(
                                    ResponseOutputText.Annotation.FileCitation.builder()
                                        .fileId("file_id")
                                        .filename("filename")
                                        .index(0L)
                                        .build()
                                )
                                .text("text")
                                .addLogprob(
                                    ResponseOutputText.Logprob.builder()
                                        .token("token")
                                        .addByte(0L)
                                        .logprob(0.0)
                                        .addTopLogprob(
                                            ResponseOutputText.Logprob.TopLogprob.builder()
                                                .token("token")
                                                .addByte(0L)
                                                .logprob(0.0)
                                                .build()
                                        )
                                        .build()
                                )
                                .build()
                        )
                        .status(ResponseOutputMessage.Status.IN_PROGRESS)
                        .phase(ResponseOutputMessage.Phase.COMMENTARY)
                        .build()
                )
                .parallelToolCalls(true)
                .temperature(1.0)
                .toolChoice(ToolChoiceOptions.NONE)
                .addTool(
                    FunctionTool.builder()
                        .name("name")
                        .parameters(
                            FunctionTool.Parameters.builder()
                                .putAdditionalProperty("foo", JsonValue.from("bar"))
                                .build()
                        )
                        .strict(true)
                        .deferLoading(true)
                        .description("description")
                        .build()
                )
                .topP(1.0)
                .background(true)
                .completedAt(0.0)
                .conversation(Response.Conversation.builder().id("id").build())
                .maxOutputTokens(0L)
                .maxToolCalls(0L)
                .moderation(
                    Response.Moderation.builder()
                        .input(
                            Response.Moderation.Input.ModerationResult.builder()
                                .categories(
                                    Response.Moderation.Input.ModerationResult.Categories.builder()
                                        .putAdditionalProperty("foo", JsonValue.from(true))
                                        .build()
                                )
                                .categoryAppliedInputTypes(
                                    Response.Moderation.Input.ModerationResult
                                        .CategoryAppliedInputTypes
                                        .builder()
                                        .putAdditionalProperty(
                                            "foo",
                                            JsonValue.from(listOf("text")),
                                        )
                                        .build()
                                )
                                .categoryScores(
                                    Response.Moderation.Input.ModerationResult.CategoryScores
                                        .builder()
                                        .putAdditionalProperty("foo", JsonValue.from(0))
                                        .build()
                                )
                                .flagged(true)
                                .model("model")
                                .build()
                        )
                        .output(
                            Response.Moderation.Output.ModerationResult.builder()
                                .categories(
                                    Response.Moderation.Output.ModerationResult.Categories.builder()
                                        .putAdditionalProperty("foo", JsonValue.from(true))
                                        .build()
                                )
                                .categoryAppliedInputTypes(
                                    Response.Moderation.Output.ModerationResult
                                        .CategoryAppliedInputTypes
                                        .builder()
                                        .putAdditionalProperty(
                                            "foo",
                                            JsonValue.from(listOf("text")),
                                        )
                                        .build()
                                )
                                .categoryScores(
                                    Response.Moderation.Output.ModerationResult.CategoryScores
                                        .builder()
                                        .putAdditionalProperty("foo", JsonValue.from(0))
                                        .build()
                                )
                                .flagged(true)
                                .model("model")
                                .build()
                        )
                        .build()
                )
                .previousResponseId("previous_response_id")
                .prompt(
                    ResponsePrompt.builder()
                        .id("id")
                        .variables(
                            ResponsePrompt.Variables.builder()
                                .putAdditionalProperty("foo", JsonValue.from("string"))
                                .build()
                        )
                        .version("version")
                        .build()
                )
                .promptCacheKey("prompt-cache-key-1234")
                .promptCacheRetention(Response.PromptCacheRetention.IN_MEMORY)
                .reasoning(
                    Reasoning.builder()
                        .context(Reasoning.Context.AUTO)
                        .effort(ReasoningEffort.NONE)
                        .generateSummary(Reasoning.GenerateSummary.AUTO)
                        .summary(Reasoning.Summary.AUTO)
                        .build()
                )
                .safetyIdentifier("safety-identifier-1234")
                .serviceTier(Response.ServiceTier.AUTO)
                .status(ResponseStatus.COMPLETED)
                .text(
                    ResponseTextConfig.builder()
                        .format(ResponseFormatText.builder().build())
                        .verbosity(ResponseTextConfig.Verbosity.LOW)
                        .build()
                )
                .topLogprobs(0L)
                .truncation(Response.Truncation.AUTO)
                .usage(
                    ResponseUsage.builder()
                        .inputTokens(0L)
                        .inputTokensDetails(
                            ResponseUsage.InputTokensDetails.builder().cachedTokens(0L).build()
                        )
                        .outputTokens(0L)
                        .outputTokensDetails(
                            ResponseUsage.OutputTokensDetails.builder().reasoningTokens(0L).build()
                        )
                        .totalTokens(0L)
                        .build()
                )
                .user("user-1234")
                .build()

        assertThat(response.id()).isEqualTo("id")
        assertThat(response.createdAt()).isEqualTo(0.0)
        assertThat(response.error())
            .contains(
                ResponseError.builder()
                    .code(ResponseError.Code.SERVER_ERROR)
                    .message("message")
                    .build()
            )
        assertThat(response.incompleteDetails())
            .contains(
                Response.IncompleteDetails.builder()
                    .reason(Response.IncompleteDetails.Reason.MAX_OUTPUT_TOKENS)
                    .build()
            )
        assertThat(response.instructions()).contains(Response.Instructions.ofString("string"))
        assertThat(response.metadata())
            .contains(
                Response.Metadata.builder()
                    .putAdditionalProperty("foo", JsonValue.from("string"))
                    .build()
            )
        assertThat(response.model()).isEqualTo(ResponsesModel.ofChat(ChatModel.GPT_5_1))
        assertThat(response.output())
            .containsExactly(
                ResponseOutputItem.ofMessage(
                    ResponseOutputMessage.builder()
                        .id("id")
                        .addContent(
                            ResponseOutputText.builder()
                                .addAnnotation(
                                    ResponseOutputText.Annotation.FileCitation.builder()
                                        .fileId("file_id")
                                        .filename("filename")
                                        .index(0L)
                                        .build()
                                )
                                .text("text")
                                .addLogprob(
                                    ResponseOutputText.Logprob.builder()
                                        .token("token")
                                        .addByte(0L)
                                        .logprob(0.0)
                                        .addTopLogprob(
                                            ResponseOutputText.Logprob.TopLogprob.builder()
                                                .token("token")
                                                .addByte(0L)
                                                .logprob(0.0)
                                                .build()
                                        )
                                        .build()
                                )
                                .build()
                        )
                        .status(ResponseOutputMessage.Status.IN_PROGRESS)
                        .phase(ResponseOutputMessage.Phase.COMMENTARY)
                        .build()
                )
            )
        assertThat(response.parallelToolCalls()).isEqualTo(true)
        assertThat(response.temperature()).contains(1.0)
        assertThat(response.toolChoice())
            .isEqualTo(Response.ToolChoice.ofOptions(ToolChoiceOptions.NONE))
        assertThat(response.tools())
            .containsExactly(
                Tool.ofFunction(
                    FunctionTool.builder()
                        .name("name")
                        .parameters(
                            FunctionTool.Parameters.builder()
                                .putAdditionalProperty("foo", JsonValue.from("bar"))
                                .build()
                        )
                        .strict(true)
                        .deferLoading(true)
                        .description("description")
                        .build()
                )
            )
        assertThat(response.topP()).contains(1.0)
        assertThat(response.background()).contains(true)
        assertThat(response.completedAt()).contains(0.0)
        assertThat(response.conversation())
            .contains(Response.Conversation.builder().id("id").build())
        assertThat(response.maxOutputTokens()).contains(0L)
        assertThat(response.maxToolCalls()).contains(0L)
        assertThat(response.moderation())
            .contains(
                Response.Moderation.builder()
                    .input(
                        Response.Moderation.Input.ModerationResult.builder()
                            .categories(
                                Response.Moderation.Input.ModerationResult.Categories.builder()
                                    .putAdditionalProperty("foo", JsonValue.from(true))
                                    .build()
                            )
                            .categoryAppliedInputTypes(
                                Response.Moderation.Input.ModerationResult.CategoryAppliedInputTypes
                                    .builder()
                                    .putAdditionalProperty("foo", JsonValue.from(listOf("text")))
                                    .build()
                            )
                            .categoryScores(
                                Response.Moderation.Input.ModerationResult.CategoryScores.builder()
                                    .putAdditionalProperty("foo", JsonValue.from(0))
                                    .build()
                            )
                            .flagged(true)
                            .model("model")
                            .build()
                    )
                    .output(
                        Response.Moderation.Output.ModerationResult.builder()
                            .categories(
                                Response.Moderation.Output.ModerationResult.Categories.builder()
                                    .putAdditionalProperty("foo", JsonValue.from(true))
                                    .build()
                            )
                            .categoryAppliedInputTypes(
                                Response.Moderation.Output.ModerationResult
                                    .CategoryAppliedInputTypes
                                    .builder()
                                    .putAdditionalProperty("foo", JsonValue.from(listOf("text")))
                                    .build()
                            )
                            .categoryScores(
                                Response.Moderation.Output.ModerationResult.CategoryScores.builder()
                                    .putAdditionalProperty("foo", JsonValue.from(0))
                                    .build()
                            )
                            .flagged(true)
                            .model("model")
                            .build()
                    )
                    .build()
            )
        assertThat(response.previousResponseId()).contains("previous_response_id")
        assertThat(response.prompt())
            .contains(
                ResponsePrompt.builder()
                    .id("id")
                    .variables(
                        ResponsePrompt.Variables.builder()
                            .putAdditionalProperty("foo", JsonValue.from("string"))
                            .build()
                    )
                    .version("version")
                    .build()
            )
        assertThat(response.promptCacheKey()).contains("prompt-cache-key-1234")
        assertThat(response.promptCacheRetention())
            .contains(Response.PromptCacheRetention.IN_MEMORY)
        assertThat(response.reasoning())
            .contains(
                Reasoning.builder()
                    .context(Reasoning.Context.AUTO)
                    .effort(ReasoningEffort.NONE)
                    .generateSummary(Reasoning.GenerateSummary.AUTO)
                    .summary(Reasoning.Summary.AUTO)
                    .build()
            )
        assertThat(response.safetyIdentifier()).contains("safety-identifier-1234")
        assertThat(response.serviceTier()).contains(Response.ServiceTier.AUTO)
        assertThat(response.status()).contains(ResponseStatus.COMPLETED)
        assertThat(response.text())
            .contains(
                ResponseTextConfig.builder()
                    .format(ResponseFormatText.builder().build())
                    .verbosity(ResponseTextConfig.Verbosity.LOW)
                    .build()
            )
        assertThat(response.topLogprobs()).contains(0L)
        assertThat(response.truncation()).contains(Response.Truncation.AUTO)
        assertThat(response.usage())
            .contains(
                ResponseUsage.builder()
                    .inputTokens(0L)
                    .inputTokensDetails(
                        ResponseUsage.InputTokensDetails.builder().cachedTokens(0L).build()
                    )
                    .outputTokens(0L)
                    .outputTokensDetails(
                        ResponseUsage.OutputTokensDetails.builder().reasoningTokens(0L).build()
                    )
                    .totalTokens(0L)
                    .build()
            )
        assertThat(response.user()).contains("user-1234")
    }

    @Test
    fun roundtrip() {
        val jsonMapper = jsonMapper()
        val response =
            Response.builder()
                .id("id")
                .createdAt(0.0)
                .error(
                    ResponseError.builder()
                        .code(ResponseError.Code.SERVER_ERROR)
                        .message("message")
                        .build()
                )
                .incompleteDetails(
                    Response.IncompleteDetails.builder()
                        .reason(Response.IncompleteDetails.Reason.MAX_OUTPUT_TOKENS)
                        .build()
                )
                .instructions("string")
                .metadata(
                    Response.Metadata.builder()
                        .putAdditionalProperty("foo", JsonValue.from("string"))
                        .build()
                )
                .model(ChatModel.GPT_5_1)
                .addOutput(
                    ResponseOutputMessage.builder()
                        .id("id")
                        .addContent(
                            ResponseOutputText.builder()
                                .addAnnotation(
                                    ResponseOutputText.Annotation.FileCitation.builder()
                                        .fileId("file_id")
                                        .filename("filename")
                                        .index(0L)
                                        .build()
                                )
                                .text("text")
                                .addLogprob(
                                    ResponseOutputText.Logprob.builder()
                                        .token("token")
                                        .addByte(0L)
                                        .logprob(0.0)
                                        .addTopLogprob(
                                            ResponseOutputText.Logprob.TopLogprob.builder()
                                                .token("token")
                                                .addByte(0L)
                                                .logprob(0.0)
                                                .build()
                                        )
                                        .build()
                                )
                                .build()
                        )
                        .status(ResponseOutputMessage.Status.IN_PROGRESS)
                        .phase(ResponseOutputMessage.Phase.COMMENTARY)
                        .build()
                )
                .parallelToolCalls(true)
                .temperature(1.0)
                .toolChoice(ToolChoiceOptions.NONE)
                .addTool(
                    FunctionTool.builder()
                        .name("name")
                        .parameters(
                            FunctionTool.Parameters.builder()
                                .putAdditionalProperty("foo", JsonValue.from("bar"))
                                .build()
                        )
                        .strict(true)
                        .deferLoading(true)
                        .description("description")
                        .build()
                )
                .topP(1.0)
                .background(true)
                .completedAt(0.0)
                .conversation(Response.Conversation.builder().id("id").build())
                .maxOutputTokens(0L)
                .maxToolCalls(0L)
                .moderation(
                    Response.Moderation.builder()
                        .input(
                            Response.Moderation.Input.ModerationResult.builder()
                                .categories(
                                    Response.Moderation.Input.ModerationResult.Categories.builder()
                                        .putAdditionalProperty("foo", JsonValue.from(true))
                                        .build()
                                )
                                .categoryAppliedInputTypes(
                                    Response.Moderation.Input.ModerationResult
                                        .CategoryAppliedInputTypes
                                        .builder()
                                        .putAdditionalProperty(
                                            "foo",
                                            JsonValue.from(listOf("text")),
                                        )
                                        .build()
                                )
                                .categoryScores(
                                    Response.Moderation.Input.ModerationResult.CategoryScores
                                        .builder()
                                        .putAdditionalProperty("foo", JsonValue.from(0))
                                        .build()
                                )
                                .flagged(true)
                                .model("model")
                                .build()
                        )
                        .output(
                            Response.Moderation.Output.ModerationResult.builder()
                                .categories(
                                    Response.Moderation.Output.ModerationResult.Categories.builder()
                                        .putAdditionalProperty("foo", JsonValue.from(true))
                                        .build()
                                )
                                .categoryAppliedInputTypes(
                                    Response.Moderation.Output.ModerationResult
                                        .CategoryAppliedInputTypes
                                        .builder()
                                        .putAdditionalProperty(
                                            "foo",
                                            JsonValue.from(listOf("text")),
                                        )
                                        .build()
                                )
                                .categoryScores(
                                    Response.Moderation.Output.ModerationResult.CategoryScores
                                        .builder()
                                        .putAdditionalProperty("foo", JsonValue.from(0))
                                        .build()
                                )
                                .flagged(true)
                                .model("model")
                                .build()
                        )
                        .build()
                )
                .previousResponseId("previous_response_id")
                .prompt(
                    ResponsePrompt.builder()
                        .id("id")
                        .variables(
                            ResponsePrompt.Variables.builder()
                                .putAdditionalProperty("foo", JsonValue.from("string"))
                                .build()
                        )
                        .version("version")
                        .build()
                )
                .promptCacheKey("prompt-cache-key-1234")
                .promptCacheRetention(Response.PromptCacheRetention.IN_MEMORY)
                .reasoning(
                    Reasoning.builder()
                        .context(Reasoning.Context.AUTO)
                        .effort(ReasoningEffort.NONE)
                        .generateSummary(Reasoning.GenerateSummary.AUTO)
                        .summary(Reasoning.Summary.AUTO)
                        .build()
                )
                .safetyIdentifier("safety-identifier-1234")
                .serviceTier(Response.ServiceTier.AUTO)
                .status(ResponseStatus.COMPLETED)
                .text(
                    ResponseTextConfig.builder()
                        .format(ResponseFormatText.builder().build())
                        .verbosity(ResponseTextConfig.Verbosity.LOW)
                        .build()
                )
                .topLogprobs(0L)
                .truncation(Response.Truncation.AUTO)
                .usage(
                    ResponseUsage.builder()
                        .inputTokens(0L)
                        .inputTokensDetails(
                            ResponseUsage.InputTokensDetails.builder().cachedTokens(0L).build()
                        )
                        .outputTokens(0L)
                        .outputTokensDetails(
                            ResponseUsage.OutputTokensDetails.builder().reasoningTokens(0L).build()
                        )
                        .totalTokens(0L)
                        .build()
                )
                .user("user-1234")
                .build()

        val roundtrippedResponse =
            jsonMapper.readValue(
                jsonMapper.writeValueAsString(response),
                jacksonTypeRef<Response>(),
            )

        assertThat(roundtrippedResponse).isEqualTo(response)
    }
}
