package cn.nianxx.thhotel.integrations.ai.superagent.service.impl;

import static org.assertj.core.api.Assertions.assertThat;

import cn.nianxx.thhotel.integrations.ai.superagent.common.result.SuperAgentOpenApiResult;
import org.junit.jupiter.api.Test;

class SuperAgentOpenApiSseParserTest {

    private final SuperAgentOpenApiSseParser parser = new SuperAgentOpenApiSseParser();

    @Test
    void shouldExtractFinalAiAnswerFromValuesEvent() {
        String sse = """
                event: metadata
                data: {"run_id":"run-debug-001","resolved_profile_id":"profile-debug"}

                event: messages
                data: {"type":"ai","content":"partial answer"}

                event: values
                data: {"messages":[{"type":"human","content":"input"},{"type":"ai","content":"{\\"ai_task_results\\":[{\\"task_type\\":\\"New Booking\\"}]}","response_metadata":{"finish_reason":"stop","model_name":"debug-model"},"usage_metadata":{"input_tokens":11,"output_tokens":7,"total_tokens":18}}]}

                event: end
                data: {}

                """;

        SuperAgentOpenApiResult result = parser.parse("session-debug-001", sse);

        assertThat(result.sessionId()).isEqualTo("session-debug-001");
        assertThat(result.runId()).isEqualTo("run-debug-001");
        assertThat(result.profileId()).isEqualTo("profile-debug");
        assertThat(result.rawAnswer()).isEqualTo("{\"ai_task_results\":[{\"task_type\":\"New Booking\"}]}");
        assertThat(result.modelName()).isEqualTo("debug-model");
        assertThat(result.inputTokens()).isEqualTo(11);
        assertThat(result.outputTokens()).isEqualTo(7);
        assertThat(result.totalTokens()).isEqualTo(18);
        assertThat(result.eventTypes()).containsExactly("metadata", "messages", "values", "end");
    }
}
