|
| 1 | +package com.langfuse.client.deserialization; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import com.langfuse.client.core.ObjectMappers; |
| 6 | +import com.langfuse.client.resources.prompts.types.ChatMessage; |
| 7 | +import com.langfuse.client.resources.prompts.types.ChatMessageWithPlaceholders; |
| 8 | +import com.langfuse.client.resources.prompts.types.ChatPrompt; |
| 9 | +import com.langfuse.client.resources.prompts.types.Prompt; |
| 10 | +import java.io.InputStream; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Optional; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +/** |
| 17 | + * Unit tests for chat prompt deserialization through the same code path as |
| 18 | + * {@code PromptsClient.get()} — i.e. {@code ObjectMappers.JSON_MAPPER.readValue(..., Prompt.class)}. |
| 19 | + * |
| 20 | + * <p>These tests use a hardcoded JSON fixture and require no credentials or network access. |
| 21 | + * They exist primarily to catch the regression where {@code ChatMessage.getRole()} and |
| 22 | + * {@code ChatMessage.getContent()} return {@code null} after deserialization. |
| 23 | + */ |
| 24 | +class ChatPromptDeserializationTest { |
| 25 | + |
| 26 | + private static final String FIXTURE = "/fixtures/chat_prompt_response.json"; |
| 27 | + |
| 28 | + @Test |
| 29 | + void deserializeChatPrompt_hasCorrectTopLevelFields() throws Exception { |
| 30 | + Prompt prompt = deserializeFixture(); |
| 31 | + |
| 32 | + assertThat(prompt.isChat()).isTrue(); |
| 33 | + assertThat(prompt.isText()).isFalse(); |
| 34 | + |
| 35 | + ChatPrompt chatPrompt = prompt.getChat().orElseThrow(); |
| 36 | + assertThat(chatPrompt.getName()).isEqualTo("test-chat-prompt"); |
| 37 | + assertThat(chatPrompt.getVersion()).isEqualTo(1); |
| 38 | + assertThat(chatPrompt.getLabels()).contains("production"); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + void deserializeChatPrompt_chatMessageHasNonNullRoleAndContent() throws Exception { |
| 43 | + Prompt prompt = deserializeFixture(); |
| 44 | + ChatPrompt chatPrompt = prompt.getChat().orElseThrow(); |
| 45 | + List<ChatMessageWithPlaceholders> messages = chatPrompt.getPrompt(); |
| 46 | + |
| 47 | + assertThat(messages).hasSize(2); |
| 48 | + |
| 49 | + // First entry — chat message (no "type" discriminator in real API response) |
| 50 | + assertThat(messages.get(0).isChatmessage()).isTrue(); |
| 51 | + Optional<ChatMessage> system = messages.get(0).getChatmessage(); |
| 52 | + assertThat(system).isPresent(); |
| 53 | + assertThat(system.get().getRole()).isNotNull().isEqualTo("system"); |
| 54 | + assertThat(system.get().getContent()).isNotNull().isEqualTo("Hello World"); |
| 55 | + |
| 56 | + // Second entry — placeholder (has "type": "placeholder") |
| 57 | + assertThat(messages.get(1).isPlaceholder()).isTrue(); |
| 58 | + assertThat(messages.get(1).getPlaceholder()).isPresent(); |
| 59 | + assertThat(messages.get(1).getPlaceholder().get().getName()).isEqualTo("username"); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void chatMessageRoundTripSerialization() throws Exception { |
| 64 | + Prompt prompt = deserializeFixture(); |
| 65 | + ChatPrompt chatPrompt = prompt.getChat().orElseThrow(); |
| 66 | + ChatMessage original = chatPrompt.getPrompt().get(0).getChatmessage().orElseThrow(); |
| 67 | + |
| 68 | + // Serialize to JSON and back |
| 69 | + String json = ObjectMappers.JSON_MAPPER.writeValueAsString(original); |
| 70 | + ChatMessage roundTripped = ObjectMappers.JSON_MAPPER.readValue(json, ChatMessage.class); |
| 71 | + |
| 72 | + assertThat(roundTripped.getRole()).isEqualTo(original.getRole()); |
| 73 | + assertThat(roundTripped.getContent()).isEqualTo(original.getContent()); |
| 74 | + } |
| 75 | + |
| 76 | + private Prompt deserializeFixture() throws Exception { |
| 77 | + try (InputStream is = getClass().getResourceAsStream(FIXTURE)) { |
| 78 | + assertThat(is).as("Fixture %s must be on the classpath", FIXTURE).isNotNull(); |
| 79 | + String json = new String(is.readAllBytes(), StandardCharsets.UTF_8); |
| 80 | + return ObjectMappers.JSON_MAPPER.readValue(json, Prompt.class); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments