|
| 1 | +package com.stackup.stackup.session.application; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.mockito.ArgumentMatchers.any; |
| 5 | +import static org.mockito.ArgumentMatchers.eq; |
| 6 | +import static org.mockito.Mockito.never; |
| 7 | +import static org.mockito.Mockito.verify; |
| 8 | +import static org.mockito.Mockito.when; |
| 9 | + |
| 10 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 11 | +import com.stackup.stackup.common.messaging.domain.ProcessedMessage; |
| 12 | +import com.stackup.stackup.common.messaging.domain.ProcessedMessageRepository; |
| 13 | +import com.stackup.stackup.common.sse.SseEventPublisher; |
| 14 | +import com.stackup.stackup.common.sse.SseEventType; |
| 15 | +import com.stackup.stackup.session.application.dto.MessageResult; |
| 16 | +import com.stackup.stackup.session.application.dto.TtsCallbackEnvelope; |
| 17 | +import com.stackup.stackup.session.application.dto.TtsCallbackPayload; |
| 18 | +import com.stackup.stackup.session.domain.InterviewMessage; |
| 19 | +import com.stackup.stackup.session.domain.InterviewMessageRepository; |
| 20 | +import com.stackup.stackup.session.domain.InterviewSession; |
| 21 | +import com.stackup.stackup.session.domain.JobCategory; |
| 22 | +import com.stackup.stackup.session.domain.SessionMode; |
| 23 | +import com.stackup.stackup.session.domain.TtsStatus; |
| 24 | +import com.stackup.stackup.user.domain.User; |
| 25 | +import java.util.Optional; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 28 | +import org.mockito.InjectMocks; |
| 29 | +import org.mockito.Mock; |
| 30 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 31 | +import org.springframework.test.util.ReflectionTestUtils; |
| 32 | + |
| 33 | +@ExtendWith(MockitoExtension.class) |
| 34 | +class TtsCallbackServiceTest { |
| 35 | + |
| 36 | + private static final ObjectMapper JSON = new ObjectMapper(); |
| 37 | + |
| 38 | + @Mock InterviewMessageRepository messageRepository; |
| 39 | + @Mock ProcessedMessageRepository processedMessageRepository; |
| 40 | + @Mock SseEventPublisher sseEventPublisher; |
| 41 | + @InjectMocks TtsCallbackService service; |
| 42 | + |
| 43 | + @Test |
| 44 | + void apply_successStoresTtsFieldsWithoutChangingMessageStatus() { |
| 45 | + InterviewSession session = sessionFixture(50L); |
| 46 | + InterviewMessage question = InterviewMessage.interviewer(session, 1, "Q?"); |
| 47 | + ReflectionTestUtils.setField(question, "id", 100L); |
| 48 | + |
| 49 | + when(processedMessageRepository.existsById("tts-1")).thenReturn(false); |
| 50 | + when(messageRepository.findById(100L)).thenReturn(Optional.of(question)); |
| 51 | + |
| 52 | + service.apply(envelope("tts-1", new TtsCallbackPayload( |
| 53 | + 50L, 100L, "SUCCEEDED", "interview/tts/50/100.mp3", 2.4, null |
| 54 | + ))); |
| 55 | + |
| 56 | + assertThat(question.getTtsStatus()).isEqualTo(TtsStatus.SUCCEEDED); |
| 57 | + assertThat(question.getTtsAudioPath()).isEqualTo("interview/tts/50/100.mp3"); |
| 58 | + assertThat(question.getTtsDurationSec()).isEqualTo(2.4); |
| 59 | + assertThat(MessageResult.of(question).ttsAudioPath()).isEqualTo("interview/tts/50/100.mp3"); |
| 60 | + verify(sseEventPublisher).publishToSession(eq(50L), eq(SseEventType.SESSION_MESSAGE), any()); |
| 61 | + verify(sseEventPublisher).publishToUser(eq(1L), eq(SseEventType.SESSION_MESSAGE), any()); |
| 62 | + verify(processedMessageRepository).save(any(ProcessedMessage.class)); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void apply_failedMarksOnlyTtsFailedAndLeavesQuestionUsable() { |
| 67 | + InterviewSession session = sessionFixture(50L); |
| 68 | + InterviewMessage question = InterviewMessage.interviewer(session, 1, "Q?"); |
| 69 | + ReflectionTestUtils.setField(question, "id", 100L); |
| 70 | + |
| 71 | + when(processedMessageRepository.existsById("tts-fail")).thenReturn(false); |
| 72 | + when(messageRepository.findById(100L)).thenReturn(Optional.of(question)); |
| 73 | + |
| 74 | + service.apply(envelope("tts-fail", new TtsCallbackPayload( |
| 75 | + 50L, 100L, "FAILED", null, null, "TTS_PROVIDER_DOWN" |
| 76 | + ))); |
| 77 | + |
| 78 | + assertThat(question.getTtsStatus()).isEqualTo(TtsStatus.FAILED); |
| 79 | + assertThat(question.getContent()).isEqualTo("Q?"); |
| 80 | + assertThat(question.getTtsAudioPath()).isNull(); |
| 81 | + verify(sseEventPublisher).publishToSession(eq(50L), eq(SseEventType.SESSION_MESSAGE), any()); |
| 82 | + verify(processedMessageRepository).save(any(ProcessedMessage.class)); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void apply_ignoresIntervieweeMessages() { |
| 87 | + InterviewSession session = sessionFixture(50L); |
| 88 | + InterviewMessage answer = InterviewMessage.interviewee(session, 2, "A", null, null); |
| 89 | + ReflectionTestUtils.setField(answer, "id", 200L); |
| 90 | + |
| 91 | + when(processedMessageRepository.existsById("tts-answer")).thenReturn(false); |
| 92 | + when(messageRepository.findById(200L)).thenReturn(Optional.of(answer)); |
| 93 | + |
| 94 | + service.apply(envelope("tts-answer", new TtsCallbackPayload( |
| 95 | + 50L, 200L, "SUCCEEDED", "interview/tts/50/200.mp3", 1.1, null |
| 96 | + ))); |
| 97 | + |
| 98 | + assertThat(answer.getTtsStatus()).isEqualTo(TtsStatus.NOT_REQUESTED); |
| 99 | + verify(sseEventPublisher, never()).publishToSession(any(), any(), any()); |
| 100 | + verify(processedMessageRepository).save(any(ProcessedMessage.class)); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void apply_skipsDuplicateMessageId() { |
| 105 | + when(processedMessageRepository.existsById("dup")).thenReturn(true); |
| 106 | + |
| 107 | + service.apply(envelope("dup", new TtsCallbackPayload( |
| 108 | + 50L, 100L, "SUCCEEDED", "interview/tts/50/100.mp3", 2.4, null |
| 109 | + ))); |
| 110 | + |
| 111 | + verify(messageRepository, never()).findById(any()); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + void payload_acceptsAudioUrlAliasAndLowercaseStatus() throws Exception { |
| 116 | + TtsCallbackPayload payload = JSON.readValue(""" |
| 117 | + { |
| 118 | + "sessionId": 50, |
| 119 | + "messageId": 100, |
| 120 | + "status": "succeeded", |
| 121 | + "audioUrl": "interview/tts/50/100.mp3", |
| 122 | + "durationSec": 2.4 |
| 123 | + } |
| 124 | + """, TtsCallbackPayload.class); |
| 125 | + |
| 126 | + assertThat(payload.isSucceeded()).isTrue(); |
| 127 | + assertThat(payload.audioKey()).isEqualTo("interview/tts/50/100.mp3"); |
| 128 | + } |
| 129 | + |
| 130 | + private TtsCallbackEnvelope envelope(String messageId, TtsCallbackPayload payload) { |
| 131 | + return new TtsCallbackEnvelope(messageId, "callback.tts", "1", "t", |
| 132 | + null, "ai", payload, null); |
| 133 | + } |
| 134 | + |
| 135 | + private InterviewSession sessionFixture(Long id) { |
| 136 | + User user = User.createGithubUser(1L, "u", null, null, "t"); |
| 137 | + ReflectionTestUtils.setField(user, "id", 1L); |
| 138 | + InterviewSession s = InterviewSession.create(user, "t", null, SessionMode.TECHNICAL, JobCategory.BACKEND, 5, 30); |
| 139 | + ReflectionTestUtils.setField(s, "id", id); |
| 140 | + return s; |
| 141 | + } |
| 142 | +} |
0 commit comments