// File generated from our OpenAPI spec by Stainless.

package com.openai.models.beta.assistants

import com.fasterxml.jackson.module.kotlin.jacksonTypeRef
import com.openai.core.JsonValue
import com.openai.core.jsonMapper
import com.openai.errors.OpenAIInvalidDataException
import com.openai.models.beta.threads.AssistantToolChoiceOption
import com.openai.models.beta.threads.runs.RequiredActionFunctionToolCall
import com.openai.models.beta.threads.runs.Run
import com.openai.models.beta.threads.runs.RunStatus
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.EnumSource

internal class RunStreamEventTest {

    @Test
    fun ofThreadRunCreated() {
        val threadRunCreated =
            RunStreamEvent.ThreadRunCreated.builder()
                .data(
                    Run.builder()
                        .id("id")
                        .assistantId("assistant_id")
                        .cancelledAt(0L)
                        .completedAt(0L)
                        .createdAt(0L)
                        .expiresAt(0L)
                        .failedAt(0L)
                        .incompleteDetails(
                            Run.IncompleteDetails.builder()
                                .reason(Run.IncompleteDetails.Reason.MAX_COMPLETION_TOKENS)
                                .build()
                        )
                        .instructions("instructions")
                        .lastError(
                            Run.LastError.builder()
                                .code(Run.LastError.Code.SERVER_ERROR)
                                .message("message")
                                .build()
                        )
                        .maxCompletionTokens(256L)
                        .maxPromptTokens(256L)
                        .metadata(
                            Run.Metadata.builder()
                                .putAdditionalProperty("foo", JsonValue.from("string"))
                                .build()
                        )
                        .model("model")
                        .parallelToolCalls(true)
                        .requiredAction(
                            Run.RequiredAction.builder()
                                .submitToolOutputs(
                                    Run.RequiredAction.SubmitToolOutputs.builder()
                                        .addToolCall(
                                            RequiredActionFunctionToolCall.builder()
                                                .id("id")
                                                .function(
                                                    RequiredActionFunctionToolCall.Function
                                                        .builder()
                                                        .arguments("arguments")
                                                        .name("name")
                                                        .build()
                                                )
                                                .build()
                                        )
                                        .build()
                                )
                                .build()
                        )
                        .responseFormatAuto()
                        .startedAt(0L)
                        .status(RunStatus.QUEUED)
                        .threadId("thread_id")
                        .toolChoice(AssistantToolChoiceOption.Auto.NONE)
                        .addTool(CodeInterpreterTool.builder().build())
                        .truncationStrategy(
                            Run.TruncationStrategy.builder()
                                .type(Run.TruncationStrategy.Type.AUTO)
                                .lastMessages(1L)
                                .build()
                        )
                        .usage(
                            Run.Usage.builder()
                                .completionTokens(0L)
                                .promptTokens(0L)
                                .totalTokens(0L)
                                .build()
                        )
                        .temperature(0.0)
                        .topP(0.0)
                        .build()
                )
                .build()

        val runStreamEvent = RunStreamEvent.ofThreadRunCreated(threadRunCreated)

        assertThat(runStreamEvent.threadRunCreated()).contains(threadRunCreated)
        assertThat(runStreamEvent.threadRunQueued()).isEmpty
        assertThat(runStreamEvent.threadRunInProgress()).isEmpty
        assertThat(runStreamEvent.threadRunRequiresAction()).isEmpty
        assertThat(runStreamEvent.threadRunCompleted()).isEmpty
        assertThat(runStreamEvent.threadRunIncomplete()).isEmpty
        assertThat(runStreamEvent.threadRunFailed()).isEmpty
        assertThat(runStreamEvent.threadRunCancelling()).isEmpty
        assertThat(runStreamEvent.threadRunCancelled()).isEmpty
        assertThat(runStreamEvent.threadRunExpired()).isEmpty
    }

    @Test
    fun ofThreadRunCreatedRoundtrip() {
        val jsonMapper = jsonMapper()
        val runStreamEvent =
            RunStreamEvent.ofThreadRunCreated(
                RunStreamEvent.ThreadRunCreated.builder()
                    .data(
                        Run.builder()
                            .id("id")
                            .assistantId("assistant_id")
                            .cancelledAt(0L)
                            .completedAt(0L)
                            .createdAt(0L)
                            .expiresAt(0L)
                            .failedAt(0L)
                            .incompleteDetails(
                                Run.IncompleteDetails.builder()
                                    .reason(Run.IncompleteDetails.Reason.MAX_COMPLETION_TOKENS)
                                    .build()
                            )
                            .instructions("instructions")
                            .lastError(
                                Run.LastError.builder()
                                    .code(Run.LastError.Code.SERVER_ERROR)
                                    .message("message")
                                    .build()
                            )
                            .maxCompletionTokens(256L)
                            .maxPromptTokens(256L)
                            .metadata(
                                Run.Metadata.builder()
                                    .putAdditionalProperty("foo", JsonValue.from("string"))
                                    .build()
                            )
                            .model("model")
                            .parallelToolCalls(true)
                            .requiredAction(
                                Run.RequiredAction.builder()
                                    .submitToolOutputs(
                                        Run.RequiredAction.SubmitToolOutputs.builder()
                                            .addToolCall(
                                                RequiredActionFunctionToolCall.builder()
                                                    .id("id")
                                                    .function(
                                                        RequiredActionFunctionToolCall.Function
                                                            .builder()
                                                            .arguments("arguments")
                                                            .name("name")
                                                            .build()
                                                    )
                                                    .build()
                                            )
                                            .build()
                                    )
                                    .build()
                            )
                            .responseFormatAuto()
                            .startedAt(0L)
                            .status(RunStatus.QUEUED)
                            .threadId("thread_id")
                            .toolChoice(AssistantToolChoiceOption.Auto.NONE)
                            .addTool(CodeInterpreterTool.builder().build())
                            .truncationStrategy(
                                Run.TruncationStrategy.builder()
                                    .type(Run.TruncationStrategy.Type.AUTO)
                                    .lastMessages(1L)
                                    .build()
                            )
                            .usage(
                                Run.Usage.builder()
                                    .completionTokens(0L)
                                    .promptTokens(0L)
                                    .totalTokens(0L)
                                    .build()
                            )
                            .temperature(0.0)
                            .topP(0.0)
                            .build()
                    )
                    .build()
            )

        val roundtrippedRunStreamEvent =
            jsonMapper.readValue(
                jsonMapper.writeValueAsString(runStreamEvent),
                jacksonTypeRef<RunStreamEvent>(),
            )

        assertThat(roundtrippedRunStreamEvent).isEqualTo(runStreamEvent)
    }

    @Test
    fun ofThreadRunQueued() {
        val threadRunQueued =
            RunStreamEvent.ThreadRunQueued.builder()
                .data(
                    Run.builder()
                        .id("id")
                        .assistantId("assistant_id")
                        .cancelledAt(0L)
                        .completedAt(0L)
                        .createdAt(0L)
                        .expiresAt(0L)
                        .failedAt(0L)
                        .incompleteDetails(
                            Run.IncompleteDetails.builder()
                                .reason(Run.IncompleteDetails.Reason.MAX_COMPLETION_TOKENS)
                                .build()
                        )
                        .instructions("instructions")
                        .lastError(
                            Run.LastError.builder()
                                .code(Run.LastError.Code.SERVER_ERROR)
                                .message("message")
                                .build()
                        )
                        .maxCompletionTokens(256L)
                        .maxPromptTokens(256L)
                        .metadata(
                            Run.Metadata.builder()
                                .putAdditionalProperty("foo", JsonValue.from("string"))
                                .build()
                        )
                        .model("model")
                        .parallelToolCalls(true)
                        .requiredAction(
                            Run.RequiredAction.builder()
                                .submitToolOutputs(
                                    Run.RequiredAction.SubmitToolOutputs.builder()
                                        .addToolCall(
                                            RequiredActionFunctionToolCall.builder()
                                                .id("id")
                                                .function(
                                                    RequiredActionFunctionToolCall.Function
                                                        .builder()
                                                        .arguments("arguments")
                                                        .name("name")
                                                        .build()
                                                )
                                                .build()
                                        )
                                        .build()
                                )
                                .build()
                        )
                        .responseFormatAuto()
                        .startedAt(0L)
                        .status(RunStatus.QUEUED)
                        .threadId("thread_id")
                        .toolChoice(AssistantToolChoiceOption.Auto.NONE)
                        .addTool(CodeInterpreterTool.builder().build())
                        .truncationStrategy(
                            Run.TruncationStrategy.builder()
                                .type(Run.TruncationStrategy.Type.AUTO)
                                .lastMessages(1L)
                                .build()
                        )
                        .usage(
                            Run.Usage.builder()
                                .completionTokens(0L)
                                .promptTokens(0L)
                                .totalTokens(0L)
                                .build()
                        )
                        .temperature(0.0)
                        .topP(0.0)
                        .build()
                )
                .build()

        val runStreamEvent = RunStreamEvent.ofThreadRunQueued(threadRunQueued)

        assertThat(runStreamEvent.threadRunCreated()).isEmpty
        assertThat(runStreamEvent.threadRunQueued()).contains(threadRunQueued)
        assertThat(runStreamEvent.threadRunInProgress()).isEmpty
        assertThat(runStreamEvent.threadRunRequiresAction()).isEmpty
        assertThat(runStreamEvent.threadRunCompleted()).isEmpty
        assertThat(runStreamEvent.threadRunIncomplete()).isEmpty
        assertThat(runStreamEvent.threadRunFailed()).isEmpty
        assertThat(runStreamEvent.threadRunCancelling()).isEmpty
        assertThat(runStreamEvent.threadRunCancelled()).isEmpty
        assertThat(runStreamEvent.threadRunExpired()).isEmpty
    }

    @Test
    fun ofThreadRunQueuedRoundtrip() {
        val jsonMapper = jsonMapper()
        val runStreamEvent =
            RunStreamEvent.ofThreadRunQueued(
                RunStreamEvent.ThreadRunQueued.builder()
                    .data(
                        Run.builder()
                            .id("id")
                            .assistantId("assistant_id")
                            .cancelledAt(0L)
                            .completedAt(0L)
                            .createdAt(0L)
                            .expiresAt(0L)
                            .failedAt(0L)
                            .incompleteDetails(
                                Run.IncompleteDetails.builder()
                                    .reason(Run.IncompleteDetails.Reason.MAX_COMPLETION_TOKENS)
                                    .build()
                            )
                            .instructions("instructions")
                            .lastError(
                                Run.LastError.builder()
                                    .code(Run.LastError.Code.SERVER_ERROR)
                                    .message("message")
                                    .build()
                            )
                            .maxCompletionTokens(256L)
                            .maxPromptTokens(256L)
                            .metadata(
                                Run.Metadata.builder()
                                    .putAdditionalProperty("foo", JsonValue.from("string"))
                                    .build()
                            )
                            .model("model")
                            .parallelToolCalls(true)
                            .requiredAction(
                                Run.RequiredAction.builder()
                                    .submitToolOutputs(
                                        Run.RequiredAction.SubmitToolOutputs.builder()
                                            .addToolCall(
                                                RequiredActionFunctionToolCall.builder()
                                                    .id("id")
                                                    .function(
                                                        RequiredActionFunctionToolCall.Function
                                                            .builder()
                                                            .arguments("arguments")
                                                            .name("name")
                                                            .build()
                                                    )
                                                    .build()
                                            )
                                            .build()
                                    )
                                    .build()
                            )
                            .responseFormatAuto()
                            .startedAt(0L)
                            .status(RunStatus.QUEUED)
                            .threadId("thread_id")
                            .toolChoice(AssistantToolChoiceOption.Auto.NONE)
                            .addTool(CodeInterpreterTool.builder().build())
                            .truncationStrategy(
                                Run.TruncationStrategy.builder()
                                    .type(Run.TruncationStrategy.Type.AUTO)
                                    .lastMessages(1L)
                                    .build()
                            )
                            .usage(
                                Run.Usage.builder()
                                    .completionTokens(0L)
                                    .promptTokens(0L)
                                    .totalTokens(0L)
                                    .build()
                            )
                            .temperature(0.0)
                            .topP(0.0)
                            .build()
                    )
                    .build()
            )

        val roundtrippedRunStreamEvent =
            jsonMapper.readValue(
                jsonMapper.writeValueAsString(runStreamEvent),
                jacksonTypeRef<RunStreamEvent>(),
            )

        assertThat(roundtrippedRunStreamEvent).isEqualTo(runStreamEvent)
    }

    @Test
    fun ofThreadRunInProgress() {
        val threadRunInProgress =
            RunStreamEvent.ThreadRunInProgress.builder()
                .data(
                    Run.builder()
                        .id("id")
                        .assistantId("assistant_id")
                        .cancelledAt(0L)
                        .completedAt(0L)
                        .createdAt(0L)
                        .expiresAt(0L)
                        .failedAt(0L)
                        .incompleteDetails(
                            Run.IncompleteDetails.builder()
                                .reason(Run.IncompleteDetails.Reason.MAX_COMPLETION_TOKENS)
                                .build()
                        )
                        .instructions("instructions")
                        .lastError(
                            Run.LastError.builder()
                                .code(Run.LastError.Code.SERVER_ERROR)
                                .message("message")
                                .build()
                        )
                        .maxCompletionTokens(256L)
                        .maxPromptTokens(256L)
                        .metadata(
                            Run.Metadata.builder()
                                .putAdditionalProperty("foo", JsonValue.from("string"))
                                .build()
                        )
                        .model("model")
                        .parallelToolCalls(true)
                        .requiredAction(
                            Run.RequiredAction.builder()
                                .submitToolOutputs(
                                    Run.RequiredAction.SubmitToolOutputs.builder()
                                        .addToolCall(
                                            RequiredActionFunctionToolCall.builder()
                                                .id("id")
                                                .function(
                                                    RequiredActionFunctionToolCall.Function
                                                        .builder()
                                                        .arguments("arguments")
                                                        .name("name")
                                                        .build()
                                                )
                                                .build()
                                        )
                                        .build()
                                )
                                .build()
                        )
                        .responseFormatAuto()
                        .startedAt(0L)
                        .status(RunStatus.QUEUED)
                        .threadId("thread_id")
                        .toolChoice(AssistantToolChoiceOption.Auto.NONE)
                        .addTool(CodeInterpreterTool.builder().build())
                        .truncationStrategy(
                            Run.TruncationStrategy.builder()
                                .type(Run.TruncationStrategy.Type.AUTO)
                                .lastMessages(1L)
                                .build()
                        )
                        .usage(
                            Run.Usage.builder()
                                .completionTokens(0L)
                                .promptTokens(0L)
                                .totalTokens(0L)
                                .build()
                        )
                        .temperature(0.0)
                        .topP(0.0)
                        .build()
                )
                .build()

        val runStreamEvent = RunStreamEvent.ofThreadRunInProgress(threadRunInProgress)

        assertThat(runStreamEvent.threadRunCreated()).isEmpty
        assertThat(runStreamEvent.threadRunQueued()).isEmpty
        assertThat(runStreamEvent.threadRunInProgress()).contains(threadRunInProgress)
        assertThat(runStreamEvent.threadRunRequiresAction()).isEmpty
        assertThat(runStreamEvent.threadRunCompleted()).isEmpty
        assertThat(runStreamEvent.threadRunIncomplete()).isEmpty
        assertThat(runStreamEvent.threadRunFailed()).isEmpty
        assertThat(runStreamEvent.threadRunCancelling()).isEmpty
        assertThat(runStreamEvent.threadRunCancelled()).isEmpty
        assertThat(runStreamEvent.threadRunExpired()).isEmpty
    }

    @Test
    fun ofThreadRunInProgressRoundtrip() {
        val jsonMapper = jsonMapper()
        val runStreamEvent =
            RunStreamEvent.ofThreadRunInProgress(
                RunStreamEvent.ThreadRunInProgress.builder()
                    .data(
                        Run.builder()
                            .id("id")
                            .assistantId("assistant_id")
                            .cancelledAt(0L)
                            .completedAt(0L)
                            .createdAt(0L)
                            .expiresAt(0L)
                            .failedAt(0L)
                            .incompleteDetails(
                                Run.IncompleteDetails.builder()
                                    .reason(Run.IncompleteDetails.Reason.MAX_COMPLETION_TOKENS)
                                    .build()
                            )
                            .instructions("instructions")
                            .lastError(
                                Run.LastError.builder()
                                    .code(Run.LastError.Code.SERVER_ERROR)
                                    .message("message")
                                    .build()
                            )
                            .maxCompletionTokens(256L)
                            .maxPromptTokens(256L)
                            .metadata(
                                Run.Metadata.builder()
                                    .putAdditionalProperty("foo", JsonValue.from("string"))
                                    .build()
                            )
                            .model("model")
                            .parallelToolCalls(true)
                            .requiredAction(
                                Run.RequiredAction.builder()
                                    .submitToolOutputs(
                                        Run.RequiredAction.SubmitToolOutputs.builder()
                                            .addToolCall(
                                                RequiredActionFunctionToolCall.builder()
                                                    .id("id")
                                                    .function(
                                                        RequiredActionFunctionToolCall.Function
                                                            .builder()
                                                            .arguments("arguments")
                                                            .name("name")
                                                            .build()
                                                    )
                                                    .build()
                                            )
                                            .build()
                                    )
                                    .build()
                            )
                            .responseFormatAuto()
                            .startedAt(0L)
                            .status(RunStatus.QUEUED)
                            .threadId("thread_id")
                            .toolChoice(AssistantToolChoiceOption.Auto.NONE)
                            .addTool(CodeInterpreterTool.builder().build())
                            .truncationStrategy(
                                Run.TruncationStrategy.builder()
                                    .type(Run.TruncationStrategy.Type.AUTO)
                                    .lastMessages(1L)
                                    .build()
                            )
                            .usage(
                                Run.Usage.builder()
                                    .completionTokens(0L)
                                    .promptTokens(0L)
                                    .totalTokens(0L)
                                    .build()
                            )
                            .temperature(0.0)
                            .topP(0.0)
                            .build()
                    )
                    .build()
            )

        val roundtrippedRunStreamEvent =
            jsonMapper.readValue(
                jsonMapper.writeValueAsString(runStreamEvent),
                jacksonTypeRef<RunStreamEvent>(),
            )

        assertThat(roundtrippedRunStreamEvent).isEqualTo(runStreamEvent)
    }

    @Test
    fun ofThreadRunRequiresAction() {
        val threadRunRequiresAction =
            RunStreamEvent.ThreadRunRequiresAction.builder()
                .data(
                    Run.builder()
                        .id("id")
                        .assistantId("assistant_id")
                        .cancelledAt(0L)
                        .completedAt(0L)
                        .createdAt(0L)
                        .expiresAt(0L)
                        .failedAt(0L)
                        .incompleteDetails(
                            Run.IncompleteDetails.builder()
                                .reason(Run.IncompleteDetails.Reason.MAX_COMPLETION_TOKENS)
                                .build()
                        )
                        .instructions("instructions")
                        .lastError(
                            Run.LastError.builder()
                                .code(Run.LastError.Code.SERVER_ERROR)
                                .message("message")
                                .build()
                        )
                        .maxCompletionTokens(256L)
                        .maxPromptTokens(256L)
                        .metadata(
                            Run.Metadata.builder()
                                .putAdditionalProperty("foo", JsonValue.from("string"))
                                .build()
                        )
                        .model("model")
                        .parallelToolCalls(true)
                        .requiredAction(
                            Run.RequiredAction.builder()
                                .submitToolOutputs(
                                    Run.RequiredAction.SubmitToolOutputs.builder()
                                        .addToolCall(
                                            RequiredActionFunctionToolCall.builder()
                                                .id("id")
                                                .function(
                                                    RequiredActionFunctionToolCall.Function
                                                        .builder()
                                                        .arguments("arguments")
                                                        .name("name")
                                                        .build()
                                                )
                                                .build()
                                        )
                                        .build()
                                )
                                .build()
                        )
                        .responseFormatAuto()
                        .startedAt(0L)
                        .status(RunStatus.QUEUED)
                        .threadId("thread_id")
                        .toolChoice(AssistantToolChoiceOption.Auto.NONE)
                        .addTool(CodeInterpreterTool.builder().build())
                        .truncationStrategy(
                            Run.TruncationStrategy.builder()
                                .type(Run.TruncationStrategy.Type.AUTO)
                                .lastMessages(1L)
                                .build()
                        )
                        .usage(
                            Run.Usage.builder()
                                .completionTokens(0L)
                                .promptTokens(0L)
                                .totalTokens(0L)
                                .build()
                        )
                        .temperature(0.0)
                        .topP(0.0)
                        .build()
                )
                .build()

        val runStreamEvent = RunStreamEvent.ofThreadRunRequiresAction(threadRunRequiresAction)

        assertThat(runStreamEvent.threadRunCreated()).isEmpty
        assertThat(runStreamEvent.threadRunQueued()).isEmpty
        assertThat(runStreamEvent.threadRunInProgress()).isEmpty
        assertThat(runStreamEvent.threadRunRequiresAction()).contains(threadRunRequiresAction)
        assertThat(runStreamEvent.threadRunCompleted()).isEmpty
        assertThat(runStreamEvent.threadRunIncomplete()).isEmpty
        assertThat(runStreamEvent.threadRunFailed()).isEmpty
        assertThat(runStreamEvent.threadRunCancelling()).isEmpty
        assertThat(runStreamEvent.threadRunCancelled()).isEmpty
        assertThat(runStreamEvent.threadRunExpired()).isEmpty
    }

    @Test
    fun ofThreadRunRequiresActionRoundtrip() {
        val jsonMapper = jsonMapper()
        val runStreamEvent =
            RunStreamEvent.ofThreadRunRequiresAction(
                RunStreamEvent.ThreadRunRequiresAction.builder()
                    .data(
                        Run.builder()
                            .id("id")
                            .assistantId("assistant_id")
                            .cancelledAt(0L)
                            .completedAt(0L)
                            .createdAt(0L)
                            .expiresAt(0L)
                            .failedAt(0L)
                            .incompleteDetails(
                                Run.IncompleteDetails.builder()
                                    .reason(Run.IncompleteDetails.Reason.MAX_COMPLETION_TOKENS)
                                    .build()
                            )
                            .instructions("instructions")
                            .lastError(
                                Run.LastError.builder()
                                    .code(Run.LastError.Code.SERVER_ERROR)
                                    .message("message")
                                    .build()
                            )
                            .maxCompletionTokens(256L)
                            .maxPromptTokens(256L)
                            .metadata(
                                Run.Metadata.builder()
                                    .putAdditionalProperty("foo", JsonValue.from("string"))
                                    .build()
                            )
                            .model("model")
                            .parallelToolCalls(true)
                            .requiredAction(
                                Run.RequiredAction.builder()
                                    .submitToolOutputs(
                                        Run.RequiredAction.SubmitToolOutputs.builder()
                                            .addToolCall(
                                                RequiredActionFunctionToolCall.builder()
                                                    .id("id")
                                                    .function(
                                                        RequiredActionFunctionToolCall.Function
                                                            .builder()
                                                            .arguments("arguments")
                                                            .name("name")
                                                            .build()
                                                    )
                                                    .build()
                                            )
                                            .build()
                                    )
                                    .build()
                            )
                            .responseFormatAuto()
                            .startedAt(0L)
                            .status(RunStatus.QUEUED)
                            .threadId("thread_id")
                            .toolChoice(AssistantToolChoiceOption.Auto.NONE)
                            .addTool(CodeInterpreterTool.builder().build())
                            .truncationStrategy(
                                Run.TruncationStrategy.builder()
                                    .type(Run.TruncationStrategy.Type.AUTO)
                                    .lastMessages(1L)
                                    .build()
                            )
                            .usage(
                                Run.Usage.builder()
                                    .completionTokens(0L)
                                    .promptTokens(0L)
                                    .totalTokens(0L)
                                    .build()
                            )
                            .temperature(0.0)
                            .topP(0.0)
                            .build()
                    )
                    .build()
            )

        val roundtrippedRunStreamEvent =
            jsonMapper.readValue(
                jsonMapper.writeValueAsString(runStreamEvent),
                jacksonTypeRef<RunStreamEvent>(),
            )

        assertThat(roundtrippedRunStreamEvent).isEqualTo(runStreamEvent)
    }

    @Test
    fun ofThreadRunCompleted() {
        val threadRunCompleted =
            RunStreamEvent.ThreadRunCompleted.builder()
                .data(
                    Run.builder()
                        .id("id")
                        .assistantId("assistant_id")
                        .cancelledAt(0L)
                        .completedAt(0L)
                        .createdAt(0L)
                        .expiresAt(0L)
                        .failedAt(0L)
                        .incompleteDetails(
                            Run.IncompleteDetails.builder()
                                .reason(Run.IncompleteDetails.Reason.MAX_COMPLETION_TOKENS)
                                .build()
                        )
                        .instructions("instructions")
                        .lastError(
                            Run.LastError.builder()
                                .code(Run.LastError.Code.SERVER_ERROR)
                                .message("message")
                                .build()
                        )
                        .maxCompletionTokens(256L)
                        .maxPromptTokens(256L)
                        .metadata(
                            Run.Metadata.builder()
                                .putAdditionalProperty("foo", JsonValue.from("string"))
                                .build()
                        )
                        .model("model")
                        .parallelToolCalls(true)
                        .requiredAction(
                            Run.RequiredAction.builder()
                                .submitToolOutputs(
                                    Run.RequiredAction.SubmitToolOutputs.builder()
                                        .addToolCall(
                                            RequiredActionFunctionToolCall.builder()
                                                .id("id")
                                                .function(
                                                    RequiredActionFunctionToolCall.Function
                                                        .builder()
                                                        .arguments("arguments")
                                                        .name("name")
                                                        .build()
                                                )
                                                .build()
                                        )
                                        .build()
                                )
                                .build()
                        )
                        .responseFormatAuto()
                        .startedAt(0L)
                        .status(RunStatus.QUEUED)
                        .threadId("thread_id")
                        .toolChoice(AssistantToolChoiceOption.Auto.NONE)
                        .addTool(CodeInterpreterTool.builder().build())
                        .truncationStrategy(
                            Run.TruncationStrategy.builder()
                                .type(Run.TruncationStrategy.Type.AUTO)
                                .lastMessages(1L)
                                .build()
                        )
                        .usage(
                            Run.Usage.builder()
                                .completionTokens(0L)
                                .promptTokens(0L)
                                .totalTokens(0L)
                                .build()
                        )
                        .temperature(0.0)
                        .topP(0.0)
                        .build()
                )
                .build()

        val runStreamEvent = RunStreamEvent.ofThreadRunCompleted(threadRunCompleted)

        assertThat(runStreamEvent.threadRunCreated()).isEmpty
        assertThat(runStreamEvent.threadRunQueued()).isEmpty
        assertThat(runStreamEvent.threadRunInProgress()).isEmpty
        assertThat(runStreamEvent.threadRunRequiresAction()).isEmpty
        assertThat(runStreamEvent.threadRunCompleted()).contains(threadRunCompleted)
        assertThat(runStreamEvent.threadRunIncomplete()).isEmpty
        assertThat(runStreamEvent.threadRunFailed()).isEmpty
        assertThat(runStreamEvent.threadRunCancelling()).isEmpty
        assertThat(runStreamEvent.threadRunCancelled()).isEmpty
        assertThat(runStreamEvent.threadRunExpired()).isEmpty
    }

    @Test
    fun ofThreadRunCompletedRoundtrip() {
        val jsonMapper = jsonMapper()
        val runStreamEvent =
            RunStreamEvent.ofThreadRunCompleted(
                RunStreamEvent.ThreadRunCompleted.builder()
                    .data(
                        Run.builder()
                            .id("id")
                            .assistantId("assistant_id")
                            .cancelledAt(0L)
                            .completedAt(0L)
                            .createdAt(0L)
                            .expiresAt(0L)
                            .failedAt(0L)
                            .incompleteDetails(
                                Run.IncompleteDetails.builder()
                                    .reason(Run.IncompleteDetails.Reason.MAX_COMPLETION_TOKENS)
                                    .build()
                            )
                            .instructions("instructions")
                            .lastError(
                                Run.LastError.builder()
                                    .code(Run.LastError.Code.SERVER_ERROR)
                                    .message("message")
                                    .build()
                            )
                            .maxCompletionTokens(256L)
                            .maxPromptTokens(256L)
                            .metadata(
                                Run.Metadata.builder()
                                    .putAdditionalProperty("foo", JsonValue.from("string"))
                                    .build()
                            )
                            .model("model")
                            .parallelToolCalls(true)
                            .requiredAction(
                                Run.RequiredAction.builder()
                                    .submitToolOutputs(
                                        Run.RequiredAction.SubmitToolOutputs.builder()
                                            .addToolCall(
                                                RequiredActionFunctionToolCall.builder()
                                                    .id("id")
                                                    .function(
                                                        RequiredActionFunctionToolCall.Function
                                                            .builder()
                                                            .arguments("arguments")
                                                            .name("name")
                                                            .build()
                                                    )
                                                    .build()
                                            )
                                            .build()
                                    )
                                    .build()
                            )
                            .responseFormatAuto()
                            .startedAt(0L)
                            .status(RunStatus.QUEUED)
                            .threadId("thread_id")
                            .toolChoice(AssistantToolChoiceOption.Auto.NONE)
                            .addTool(CodeInterpreterTool.builder().build())
                            .truncationStrategy(
                                Run.TruncationStrategy.builder()
                                    .type(Run.TruncationStrategy.Type.AUTO)
                                    .lastMessages(1L)
                                    .build()
                            )
                            .usage(
                                Run.Usage.builder()
                                    .completionTokens(0L)
                                    .promptTokens(0L)
                                    .totalTokens(0L)
                                    .build()
                            )
                            .temperature(0.0)
                            .topP(0.0)
                            .build()
                    )
                    .build()
            )

        val roundtrippedRunStreamEvent =
            jsonMapper.readValue(
                jsonMapper.writeValueAsString(runStreamEvent),
                jacksonTypeRef<RunStreamEvent>(),
            )

        assertThat(roundtrippedRunStreamEvent).isEqualTo(runStreamEvent)
    }

    @Test
    fun ofThreadRunIncomplete() {
        val threadRunIncomplete =
            RunStreamEvent.ThreadRunIncomplete.builder()
                .data(
                    Run.builder()
                        .id("id")
                        .assistantId("assistant_id")
                        .cancelledAt(0L)
                        .completedAt(0L)
                        .createdAt(0L)
                        .expiresAt(0L)
                        .failedAt(0L)
                        .incompleteDetails(
                            Run.IncompleteDetails.builder()
                                .reason(Run.IncompleteDetails.Reason.MAX_COMPLETION_TOKENS)
                                .build()
                        )
                        .instructions("instructions")
                        .lastError(
                            Run.LastError.builder()
                                .code(Run.LastError.Code.SERVER_ERROR)
                                .message("message")
                                .build()
                        )
                        .maxCompletionTokens(256L)
                        .maxPromptTokens(256L)
                        .metadata(
                            Run.Metadata.builder()
                                .putAdditionalProperty("foo", JsonValue.from("string"))
                                .build()
                        )
                        .model("model")
                        .parallelToolCalls(true)
                        .requiredAction(
                            Run.RequiredAction.builder()
                                .submitToolOutputs(
                                    Run.RequiredAction.SubmitToolOutputs.builder()
                                        .addToolCall(
                                            RequiredActionFunctionToolCall.builder()
                                                .id("id")
                                                .function(
                                                    RequiredActionFunctionToolCall.Function
                                                        .builder()
                                                        .arguments("arguments")
                                                        .name("name")
                                                        .build()
                                                )
                                                .build()
                                        )
                                        .build()
                                )
                                .build()
                        )
                        .responseFormatAuto()
                        .startedAt(0L)
                        .status(RunStatus.QUEUED)
                        .threadId("thread_id")
                        .toolChoice(AssistantToolChoiceOption.Auto.NONE)
                        .addTool(CodeInterpreterTool.builder().build())
                        .truncationStrategy(
                            Run.TruncationStrategy.builder()
                                .type(Run.TruncationStrategy.Type.AUTO)
                                .lastMessages(1L)
                                .build()
                        )
                        .usage(
                            Run.Usage.builder()
                                .completionTokens(0L)
                                .promptTokens(0L)
                                .totalTokens(0L)
                                .build()
                        )
                        .temperature(0.0)
                        .topP(0.0)
                        .build()
                )
                .build()

        val runStreamEvent = RunStreamEvent.ofThreadRunIncomplete(threadRunIncomplete)

        assertThat(runStreamEvent.threadRunCreated()).isEmpty
        assertThat(runStreamEvent.threadRunQueued()).isEmpty
        assertThat(runStreamEvent.threadRunInProgress()).isEmpty
        assertThat(runStreamEvent.threadRunRequiresAction()).isEmpty
        assertThat(runStreamEvent.threadRunCompleted()).isEmpty
        assertThat(runStreamEvent.threadRunIncomplete()).contains(threadRunIncomplete)
        assertThat(runStreamEvent.threadRunFailed()).isEmpty
        assertThat(runStreamEvent.threadRunCancelling()).isEmpty
        assertThat(runStreamEvent.threadRunCancelled()).isEmpty
        assertThat(runStreamEvent.threadRunExpired()).isEmpty
    }

    @Test
    fun ofThreadRunIncompleteRoundtrip() {
        val jsonMapper = jsonMapper()
        val runStreamEvent =
            RunStreamEvent.ofThreadRunIncomplete(
                RunStreamEvent.ThreadRunIncomplete.builder()
                    .data(
                        Run.builder()
                            .id("id")
                            .assistantId("assistant_id")
                            .cancelledAt(0L)
                            .completedAt(0L)
                            .createdAt(0L)
                            .expiresAt(0L)
                            .failedAt(0L)
                            .incompleteDetails(
                                Run.IncompleteDetails.builder()
                                    .reason(Run.IncompleteDetails.Reason.MAX_COMPLETION_TOKENS)
                                    .build()
                            )
                            .instructions("instructions")
                            .lastError(
                                Run.LastError.builder()
                                    .code(Run.LastError.Code.SERVER_ERROR)
                                    .message("message")
                                    .build()
                            )
                            .maxCompletionTokens(256L)
                            .maxPromptTokens(256L)
                            .metadata(
                                Run.Metadata.builder()
                                    .putAdditionalProperty("foo", JsonValue.from("string"))
                                    .build()
                            )
                            .model("model")
                            .parallelToolCalls(true)
                            .requiredAction(
                                Run.RequiredAction.builder()
                                    .submitToolOutputs(
                                        Run.RequiredAction.SubmitToolOutputs.builder()
                                            .addToolCall(
                                                RequiredActionFunctionToolCall.builder()
                                                    .id("id")
                                                    .function(
                                                        RequiredActionFunctionToolCall.Function
                                                            .builder()
                                                            .arguments("arguments")
                                                            .name("name")
                                                            .build()
                                                    )
                                                    .build()
                                            )
                                            .build()
                                    )
                                    .build()
                            )
                            .responseFormatAuto()
                            .startedAt(0L)
                            .status(RunStatus.QUEUED)
                            .threadId("thread_id")
                            .toolChoice(AssistantToolChoiceOption.Auto.NONE)
                            .addTool(CodeInterpreterTool.builder().build())
                            .truncationStrategy(
                                Run.TruncationStrategy.builder()
                                    .type(Run.TruncationStrategy.Type.AUTO)
                                    .lastMessages(1L)
                                    .build()
                            )
                            .usage(
                                Run.Usage.builder()
                                    .completionTokens(0L)
                                    .promptTokens(0L)
                                    .totalTokens(0L)
                                    .build()
                            )
                            .temperature(0.0)
                            .topP(0.0)
                            .build()
                    )
                    .build()
            )

        val roundtrippedRunStreamEvent =
            jsonMapper.readValue(
                jsonMapper.writeValueAsString(runStreamEvent),
                jacksonTypeRef<RunStreamEvent>(),
            )

        assertThat(roundtrippedRunStreamEvent).isEqualTo(runStreamEvent)
    }

    @Test
    fun ofThreadRunFailed() {
        val threadRunFailed =
            RunStreamEvent.ThreadRunFailed.builder()
                .data(
                    Run.builder()
                        .id("id")
                        .assistantId("assistant_id")
                        .cancelledAt(0L)
                        .completedAt(0L)
                        .createdAt(0L)
                        .expiresAt(0L)
                        .failedAt(0L)
                        .incompleteDetails(
                            Run.IncompleteDetails.builder()
                                .reason(Run.IncompleteDetails.Reason.MAX_COMPLETION_TOKENS)
                                .build()
                        )
                        .instructions("instructions")
                        .lastError(
                            Run.LastError.builder()
                                .code(Run.LastError.Code.SERVER_ERROR)
                                .message("message")
                                .build()
                        )
                        .maxCompletionTokens(256L)
                        .maxPromptTokens(256L)
                        .metadata(
                            Run.Metadata.builder()
                                .putAdditionalProperty("foo", JsonValue.from("string"))
                                .build()
                        )
                        .model("model")
                        .parallelToolCalls(true)
                        .requiredAction(
                            Run.RequiredAction.builder()
                                .submitToolOutputs(
                                    Run.RequiredAction.SubmitToolOutputs.builder()
                                        .addToolCall(
                                            RequiredActionFunctionToolCall.builder()
                                                .id("id")
                                                .function(
                                                    RequiredActionFunctionToolCall.Function
                                                        .builder()
                                                        .arguments("arguments")
                                                        .name("name")
                                                        .build()
                                                )
                                                .build()
                                        )
                                        .build()
                                )
                                .build()
                        )
                        .responseFormatAuto()
                        .startedAt(0L)
                        .status(RunStatus.QUEUED)
                        .threadId("thread_id")
                        .toolChoice(AssistantToolChoiceOption.Auto.NONE)
                        .addTool(CodeInterpreterTool.builder().build())
                        .truncationStrategy(
                            Run.TruncationStrategy.builder()
                                .type(Run.TruncationStrategy.Type.AUTO)
                                .lastMessages(1L)
                                .build()
                        )
                        .usage(
                            Run.Usage.builder()
                                .completionTokens(0L)
                                .promptTokens(0L)
                                .totalTokens(0L)
                                .build()
                        )
                        .temperature(0.0)
                        .topP(0.0)
                        .build()
                )
                .build()

        val runStreamEvent = RunStreamEvent.ofThreadRunFailed(threadRunFailed)

        assertThat(runStreamEvent.threadRunCreated()).isEmpty
        assertThat(runStreamEvent.threadRunQueued()).isEmpty
        assertThat(runStreamEvent.threadRunInProgress()).isEmpty
        assertThat(runStreamEvent.threadRunRequiresAction()).isEmpty
        assertThat(runStreamEvent.threadRunCompleted()).isEmpty
        assertThat(runStreamEvent.threadRunIncomplete()).isEmpty
        assertThat(runStreamEvent.threadRunFailed()).contains(threadRunFailed)
        assertThat(runStreamEvent.threadRunCancelling()).isEmpty
        assertThat(runStreamEvent.threadRunCancelled()).isEmpty
        assertThat(runStreamEvent.threadRunExpired()).isEmpty
    }

    @Test
    fun ofThreadRunFailedRoundtrip() {
        val jsonMapper = jsonMapper()
        val runStreamEvent =
            RunStreamEvent.ofThreadRunFailed(
                RunStreamEvent.ThreadRunFailed.builder()
                    .data(
                        Run.builder()
                            .id("id")
                            .assistantId("assistant_id")
                            .cancelledAt(0L)
                            .completedAt(0L)
                            .createdAt(0L)
                            .expiresAt(0L)
                            .failedAt(0L)
                            .incompleteDetails(
                                Run.IncompleteDetails.builder()
                                    .reason(Run.IncompleteDetails.Reason.MAX_COMPLETION_TOKENS)
                                    .build()
                            )
                            .instructions("instructions")
                            .lastError(
                                Run.LastError.builder()
                                    .code(Run.LastError.Code.SERVER_ERROR)
                                    .message("message")
                                    .build()
                            )
                            .maxCompletionTokens(256L)
                            .maxPromptTokens(256L)
                            .metadata(
                                Run.Metadata.builder()
                                    .putAdditionalProperty("foo", JsonValue.from("string"))
                                    .build()
                            )
                            .model("model")
                            .parallelToolCalls(true)
                            .requiredAction(
                                Run.RequiredAction.builder()
                                    .submitToolOutputs(
                                        Run.RequiredAction.SubmitToolOutputs.builder()
                                            .addToolCall(
                                                RequiredActionFunctionToolCall.builder()
                                                    .id("id")
                                                    .function(
                                                        RequiredActionFunctionToolCall.Function
                                                            .builder()
                                                            .arguments("arguments")
                                                            .name("name")
                                                            .build()
                                                    )
                                                    .build()
                                            )
                                            .build()
                                    )
                                    .build()
                            )
                            .responseFormatAuto()
                            .startedAt(0L)
                            .status(RunStatus.QUEUED)
                            .threadId("thread_id")
                            .toolChoice(AssistantToolChoiceOption.Auto.NONE)
                            .addTool(CodeInterpreterTool.builder().build())
                            .truncationStrategy(
                                Run.TruncationStrategy.builder()
                                    .type(Run.TruncationStrategy.Type.AUTO)
                                    .lastMessages(1L)
                                    .build()
                            )
                            .usage(
                                Run.Usage.builder()
                                    .completionTokens(0L)
                                    .promptTokens(0L)
                                    .totalTokens(0L)
                                    .build()
                            )
                            .temperature(0.0)
                            .topP(0.0)
                            .build()
                    )
                    .build()
            )

        val roundtrippedRunStreamEvent =
            jsonMapper.readValue(
                jsonMapper.writeValueAsString(runStreamEvent),
                jacksonTypeRef<RunStreamEvent>(),
            )

        assertThat(roundtrippedRunStreamEvent).isEqualTo(runStreamEvent)
    }

    @Test
    fun ofThreadRunCancelling() {
        val threadRunCancelling =
            RunStreamEvent.ThreadRunCancelling.builder()
                .data(
                    Run.builder()
                        .id("id")
                        .assistantId("assistant_id")
                        .cancelledAt(0L)
                        .completedAt(0L)
                        .createdAt(0L)
                        .expiresAt(0L)
                        .failedAt(0L)
                        .incompleteDetails(
                            Run.IncompleteDetails.builder()
                                .reason(Run.IncompleteDetails.Reason.MAX_COMPLETION_TOKENS)
                                .build()
                        )
                        .instructions("instructions")
                        .lastError(
                            Run.LastError.builder()
                                .code(Run.LastError.Code.SERVER_ERROR)
                                .message("message")
                                .build()
                        )
                        .maxCompletionTokens(256L)
                        .maxPromptTokens(256L)
                        .metadata(
                            Run.Metadata.builder()
                                .putAdditionalProperty("foo", JsonValue.from("string"))
                                .build()
                        )
                        .model("model")
                        .parallelToolCalls(true)
                        .requiredAction(
                            Run.RequiredAction.builder()
                                .submitToolOutputs(
                                    Run.RequiredAction.SubmitToolOutputs.builder()
                                        .addToolCall(
                                            RequiredActionFunctionToolCall.builder()
                                                .id("id")
                                                .function(
                                                    RequiredActionFunctionToolCall.Function
                                                        .builder()
                                                        .arguments("arguments")
                                                        .name("name")
                                                        .build()
                                                )
                                                .build()
                                        )
                                        .build()
                                )
                                .build()
                        )
                        .responseFormatAuto()
                        .startedAt(0L)
                        .status(RunStatus.QUEUED)
                        .threadId("thread_id")
                        .toolChoice(AssistantToolChoiceOption.Auto.NONE)
                        .addTool(CodeInterpreterTool.builder().build())
                        .truncationStrategy(
                            Run.TruncationStrategy.builder()
                                .type(Run.TruncationStrategy.Type.AUTO)
                                .lastMessages(1L)
                                .build()
                        )
                        .usage(
                            Run.Usage.builder()
                                .completionTokens(0L)
                                .promptTokens(0L)
                                .totalTokens(0L)
                                .build()
                        )
                        .temperature(0.0)
                        .topP(0.0)
                        .build()
                )
                .build()

        val runStreamEvent = RunStreamEvent.ofThreadRunCancelling(threadRunCancelling)

        assertThat(runStreamEvent.threadRunCreated()).isEmpty
        assertThat(runStreamEvent.threadRunQueued()).isEmpty
        assertThat(runStreamEvent.threadRunInProgress()).isEmpty
        assertThat(runStreamEvent.threadRunRequiresAction()).isEmpty
        assertThat(runStreamEvent.threadRunCompleted()).isEmpty
        assertThat(runStreamEvent.threadRunIncomplete()).isEmpty
        assertThat(runStreamEvent.threadRunFailed()).isEmpty
        assertThat(runStreamEvent.threadRunCancelling()).contains(threadRunCancelling)
        assertThat(runStreamEvent.threadRunCancelled()).isEmpty
        assertThat(runStreamEvent.threadRunExpired()).isEmpty
    }

    @Test
    fun ofThreadRunCancellingRoundtrip() {
        val jsonMapper = jsonMapper()
        val runStreamEvent =
            RunStreamEvent.ofThreadRunCancelling(
                RunStreamEvent.ThreadRunCancelling.builder()
                    .data(
                        Run.builder()
                            .id("id")
                            .assistantId("assistant_id")
                            .cancelledAt(0L)
                            .completedAt(0L)
                            .createdAt(0L)
                            .expiresAt(0L)
                            .failedAt(0L)
                            .incompleteDetails(
                                Run.IncompleteDetails.builder()
                                    .reason(Run.IncompleteDetails.Reason.MAX_COMPLETION_TOKENS)
                                    .build()
                            )
                            .instructions("instructions")
                            .lastError(
                                Run.LastError.builder()
                                    .code(Run.LastError.Code.SERVER_ERROR)
                                    .message("message")
                                    .build()
                            )
                            .maxCompletionTokens(256L)
                            .maxPromptTokens(256L)
                            .metadata(
                                Run.Metadata.builder()
                                    .putAdditionalProperty("foo", JsonValue.from("string"))
                                    .build()
                            )
                            .model("model")
                            .parallelToolCalls(true)
                            .requiredAction(
                                Run.RequiredAction.builder()
                                    .submitToolOutputs(
                                        Run.RequiredAction.SubmitToolOutputs.builder()
                                            .addToolCall(
                                                RequiredActionFunctionToolCall.builder()
                                                    .id("id")
                                                    .function(
                                                        RequiredActionFunctionToolCall.Function
                                                            .builder()
                                                            .arguments("arguments")
                                                            .name("name")
                                                            .build()
                                                    )
                                                    .build()
                                            )
                                            .build()
                                    )
                                    .build()
                            )
                            .responseFormatAuto()
                            .startedAt(0L)
                            .status(RunStatus.QUEUED)
                            .threadId("thread_id")
                            .toolChoice(AssistantToolChoiceOption.Auto.NONE)
                            .addTool(CodeInterpreterTool.builder().build())
                            .truncationStrategy(
                                Run.TruncationStrategy.builder()
                                    .type(Run.TruncationStrategy.Type.AUTO)
                                    .lastMessages(1L)
                                    .build()
                            )
                            .usage(
                                Run.Usage.builder()
                                    .completionTokens(0L)
                                    .promptTokens(0L)
                                    .totalTokens(0L)
                                    .build()
                            )
                            .temperature(0.0)
                            .topP(0.0)
                            .build()
                    )
                    .build()
            )

        val roundtrippedRunStreamEvent =
            jsonMapper.readValue(
                jsonMapper.writeValueAsString(runStreamEvent),
                jacksonTypeRef<RunStreamEvent>(),
            )

        assertThat(roundtrippedRunStreamEvent).isEqualTo(runStreamEvent)
    }

    @Test
    fun ofThreadRunCancelled() {
        val threadRunCancelled =
            RunStreamEvent.ThreadRunCancelled.builder()
                .data(
                    Run.builder()
                        .id("id")
                        .assistantId("assistant_id")
                        .cancelledAt(0L)
                        .completedAt(0L)
                        .createdAt(0L)
                        .expiresAt(0L)
                        .failedAt(0L)
                        .incompleteDetails(
                            Run.IncompleteDetails.builder()
                                .reason(Run.IncompleteDetails.Reason.MAX_COMPLETION_TOKENS)
                                .build()
                        )
                        .instructions("instructions")
                        .lastError(
                            Run.LastError.builder()
                                .code(Run.LastError.Code.SERVER_ERROR)
                                .message("message")
                                .build()
                        )
                        .maxCompletionTokens(256L)
                        .maxPromptTokens(256L)
                        .metadata(
                            Run.Metadata.builder()
                                .putAdditionalProperty("foo", JsonValue.from("string"))
                                .build()
                        )
                        .model("model")
                        .parallelToolCalls(true)
                        .requiredAction(
                            Run.RequiredAction.builder()
                                .submitToolOutputs(
                                    Run.RequiredAction.SubmitToolOutputs.builder()
                                        .addToolCall(
                                            RequiredActionFunctionToolCall.builder()
                                                .id("id")
                                                .function(
                                                    RequiredActionFunctionToolCall.Function
                                                        .builder()
                                                        .arguments("arguments")
                                                        .name("name")
                                                        .build()
                                                )
                                                .build()
                                        )
                                        .build()
                                )
                                .build()
                        )
                        .responseFormatAuto()
                        .startedAt(0L)
                        .status(RunStatus.QUEUED)
                        .threadId("thread_id")
                        .toolChoice(AssistantToolChoiceOption.Auto.NONE)
                        .addTool(CodeInterpreterTool.builder().build())
                        .truncationStrategy(
                            Run.TruncationStrategy.builder()
                                .type(Run.TruncationStrategy.Type.AUTO)
                                .lastMessages(1L)
                                .build()
                        )
                        .usage(
                            Run.Usage.builder()
                                .completionTokens(0L)
                                .promptTokens(0L)
                                .totalTokens(0L)
                                .build()
                        )
                        .temperature(0.0)
                        .topP(0.0)
                        .build()
                )
                .build()

        val runStreamEvent = RunStreamEvent.ofThreadRunCancelled(threadRunCancelled)

        assertThat(runStreamEvent.threadRunCreated()).isEmpty
        assertThat(runStreamEvent.threadRunQueued()).isEmpty
        assertThat(runStreamEvent.threadRunInProgress()).isEmpty
        assertThat(runStreamEvent.threadRunRequiresAction()).isEmpty
        assertThat(runStreamEvent.threadRunCompleted()).isEmpty
        assertThat(runStreamEvent.threadRunIncomplete()).isEmpty
        assertThat(runStreamEvent.threadRunFailed()).isEmpty
        assertThat(runStreamEvent.threadRunCancelling()).isEmpty
        assertThat(runStreamEvent.threadRunCancelled()).contains(threadRunCancelled)
        assertThat(runStreamEvent.threadRunExpired()).isEmpty
    }

    @Test
    fun ofThreadRunCancelledRoundtrip() {
        val jsonMapper = jsonMapper()
        val runStreamEvent =
            RunStreamEvent.ofThreadRunCancelled(
                RunStreamEvent.ThreadRunCancelled.builder()
                    .data(
                        Run.builder()
                            .id("id")
                            .assistantId("assistant_id")
                            .cancelledAt(0L)
                            .completedAt(0L)
                            .createdAt(0L)
                            .expiresAt(0L)
                            .failedAt(0L)
                            .incompleteDetails(
                                Run.IncompleteDetails.builder()
                                    .reason(Run.IncompleteDetails.Reason.MAX_COMPLETION_TOKENS)
                                    .build()
                            )
                            .instructions("instructions")
                            .lastError(
                                Run.LastError.builder()
                                    .code(Run.LastError.Code.SERVER_ERROR)
                                    .message("message")
                                    .build()
                            )
                            .maxCompletionTokens(256L)
                            .maxPromptTokens(256L)
                            .metadata(
                                Run.Metadata.builder()
                                    .putAdditionalProperty("foo", JsonValue.from("string"))
                                    .build()
                            )
                            .model("model")
                            .parallelToolCalls(true)
                            .requiredAction(
                                Run.RequiredAction.builder()
                                    .submitToolOutputs(
                                        Run.RequiredAction.SubmitToolOutputs.builder()
                                            .addToolCall(
                                                RequiredActionFunctionToolCall.builder()
                                                    .id("id")
                                                    .function(
                                                        RequiredActionFunctionToolCall.Function
                                                            .builder()
                                                            .arguments("arguments")
                                                            .name("name")
                                                            .build()
                                                    )
                                                    .build()
                                            )
                                            .build()
                                    )
                                    .build()
                            )
                            .responseFormatAuto()
                            .startedAt(0L)
                            .status(RunStatus.QUEUED)
                            .threadId("thread_id")
                            .toolChoice(AssistantToolChoiceOption.Auto.NONE)
                            .addTool(CodeInterpreterTool.builder().build())
                            .truncationStrategy(
                                Run.TruncationStrategy.builder()
                                    .type(Run.TruncationStrategy.Type.AUTO)
                                    .lastMessages(1L)
                                    .build()
                            )
                            .usage(
                                Run.Usage.builder()
                                    .completionTokens(0L)
                                    .promptTokens(0L)
                                    .totalTokens(0L)
                                    .build()
                            )
                            .temperature(0.0)
                            .topP(0.0)
                            .build()
                    )
                    .build()
            )

        val roundtrippedRunStreamEvent =
            jsonMapper.readValue(
                jsonMapper.writeValueAsString(runStreamEvent),
                jacksonTypeRef<RunStreamEvent>(),
            )

        assertThat(roundtrippedRunStreamEvent).isEqualTo(runStreamEvent)
    }

    @Test
    fun ofThreadRunExpired() {
        val threadRunExpired =
            RunStreamEvent.ThreadRunExpired.builder()
                .data(
                    Run.builder()
                        .id("id")
                        .assistantId("assistant_id")
                        .cancelledAt(0L)
                        .completedAt(0L)
                        .createdAt(0L)
                        .expiresAt(0L)
                        .failedAt(0L)
                        .incompleteDetails(
                            Run.IncompleteDetails.builder()
                                .reason(Run.IncompleteDetails.Reason.MAX_COMPLETION_TOKENS)
                                .build()
                        )
                        .instructions("instructions")
                        .lastError(
                            Run.LastError.builder()
                                .code(Run.LastError.Code.SERVER_ERROR)
                                .message("message")
                                .build()
                        )
                        .maxCompletionTokens(256L)
                        .maxPromptTokens(256L)
                        .metadata(
                            Run.Metadata.builder()
                                .putAdditionalProperty("foo", JsonValue.from("string"))
                                .build()
                        )
                        .model("model")
                        .parallelToolCalls(true)
                        .requiredAction(
                            Run.RequiredAction.builder()
                                .submitToolOutputs(
                                    Run.RequiredAction.SubmitToolOutputs.builder()
                                        .addToolCall(
                                            RequiredActionFunctionToolCall.builder()
                                                .id("id")
                                                .function(
                                                    RequiredActionFunctionToolCall.Function
                                                        .builder()
                                                        .arguments("arguments")
                                                        .name("name")
                                                        .build()
                                                )
                                                .build()
                                        )
                                        .build()
                                )
                                .build()
                        )
                        .responseFormatAuto()
                        .startedAt(0L)
                        .status(RunStatus.QUEUED)
                        .threadId("thread_id")
                        .toolChoice(AssistantToolChoiceOption.Auto.NONE)
                        .addTool(CodeInterpreterTool.builder().build())
                        .truncationStrategy(
                            Run.TruncationStrategy.builder()
                                .type(Run.TruncationStrategy.Type.AUTO)
                                .lastMessages(1L)
                                .build()
                        )
                        .usage(
                            Run.Usage.builder()
                                .completionTokens(0L)
                                .promptTokens(0L)
                                .totalTokens(0L)
                                .build()
                        )
                        .temperature(0.0)
                        .topP(0.0)
                        .build()
                )
                .build()

        val runStreamEvent = RunStreamEvent.ofThreadRunExpired(threadRunExpired)

        assertThat(runStreamEvent.threadRunCreated()).isEmpty
        assertThat(runStreamEvent.threadRunQueued()).isEmpty
        assertThat(runStreamEvent.threadRunInProgress()).isEmpty
        assertThat(runStreamEvent.threadRunRequiresAction()).isEmpty
        assertThat(runStreamEvent.threadRunCompleted()).isEmpty
        assertThat(runStreamEvent.threadRunIncomplete()).isEmpty
        assertThat(runStreamEvent.threadRunFailed()).isEmpty
        assertThat(runStreamEvent.threadRunCancelling()).isEmpty
        assertThat(runStreamEvent.threadRunCancelled()).isEmpty
        assertThat(runStreamEvent.threadRunExpired()).contains(threadRunExpired)
    }

    @Test
    fun ofThreadRunExpiredRoundtrip() {
        val jsonMapper = jsonMapper()
        val runStreamEvent =
            RunStreamEvent.ofThreadRunExpired(
                RunStreamEvent.ThreadRunExpired.builder()
                    .data(
                        Run.builder()
                            .id("id")
                            .assistantId("assistant_id")
                            .cancelledAt(0L)
                            .completedAt(0L)
                            .createdAt(0L)
                            .expiresAt(0L)
                            .failedAt(0L)
                            .incompleteDetails(
                                Run.IncompleteDetails.builder()
                                    .reason(Run.IncompleteDetails.Reason.MAX_COMPLETION_TOKENS)
                                    .build()
                            )
                            .instructions("instructions")
                            .lastError(
                                Run.LastError.builder()
                                    .code(Run.LastError.Code.SERVER_ERROR)
                                    .message("message")
                                    .build()
                            )
                            .maxCompletionTokens(256L)
                            .maxPromptTokens(256L)
                            .metadata(
                                Run.Metadata.builder()
                                    .putAdditionalProperty("foo", JsonValue.from("string"))
                                    .build()
                            )
                            .model("model")
                            .parallelToolCalls(true)
                            .requiredAction(
                                Run.RequiredAction.builder()
                                    .submitToolOutputs(
                                        Run.RequiredAction.SubmitToolOutputs.builder()
                                            .addToolCall(
                                                RequiredActionFunctionToolCall.builder()
                                                    .id("id")
                                                    .function(
                                                        RequiredActionFunctionToolCall.Function
                                                            .builder()
                                                            .arguments("arguments")
                                                            .name("name")
                                                            .build()
                                                    )
                                                    .build()
                                            )
                                            .build()
                                    )
                                    .build()
                            )
                            .responseFormatAuto()
                            .startedAt(0L)
                            .status(RunStatus.QUEUED)
                            .threadId("thread_id")
                            .toolChoice(AssistantToolChoiceOption.Auto.NONE)
                            .addTool(CodeInterpreterTool.builder().build())
                            .truncationStrategy(
                                Run.TruncationStrategy.builder()
                                    .type(Run.TruncationStrategy.Type.AUTO)
                                    .lastMessages(1L)
                                    .build()
                            )
                            .usage(
                                Run.Usage.builder()
                                    .completionTokens(0L)
                                    .promptTokens(0L)
                                    .totalTokens(0L)
                                    .build()
                            )
                            .temperature(0.0)
                            .topP(0.0)
                            .build()
                    )
                    .build()
            )

        val roundtrippedRunStreamEvent =
            jsonMapper.readValue(
                jsonMapper.writeValueAsString(runStreamEvent),
                jacksonTypeRef<RunStreamEvent>(),
            )

        assertThat(roundtrippedRunStreamEvent).isEqualTo(runStreamEvent)
    }

    enum class IncompatibleJsonShapeTestCase(val value: JsonValue) {
        BOOLEAN(JsonValue.from(false)),
        STRING(JsonValue.from("invalid")),
        INTEGER(JsonValue.from(-1)),
        FLOAT(JsonValue.from(3.14)),
        ARRAY(JsonValue.from(listOf("invalid", "array"))),
    }

    @ParameterizedTest
    @EnumSource
    fun incompatibleJsonShapeDeserializesToUnknown(testCase: IncompatibleJsonShapeTestCase) {
        val runStreamEvent =
            jsonMapper().convertValue(testCase.value, jacksonTypeRef<RunStreamEvent>())

        val e = assertThrows<OpenAIInvalidDataException> { runStreamEvent.validate() }
        assertThat(e).hasMessageStartingWith("Unknown ")
    }
}
