feat: initial commit
This commit is contained in:
99
internal/application/usecase/handle_voice_message_test.go
Normal file
99
internal/application/usecase/handle_voice_message_test.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package usecase_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/paramah/gw_telegram/internal/application/dto"
|
||||
"github.com/paramah/gw_telegram/internal/application/usecase"
|
||||
"github.com/paramah/gw_telegram/internal/domain/entity"
|
||||
"github.com/paramah/gw_telegram/test/testutil"
|
||||
)
|
||||
|
||||
func validVoiceDTO() dto.IncomingMessageDTO {
|
||||
return dto.IncomingMessageDTO{
|
||||
MessageID: 2,
|
||||
ChatID: 100,
|
||||
UserID: 200,
|
||||
Username: "testuser",
|
||||
VoiceFileID: "file_abc123",
|
||||
IsVoice: true,
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleVoiceMessage_Execute_HappyPath(t *testing.T) {
|
||||
downloader := &testutil.FakeFileDownloader{
|
||||
Data: []byte("fake-ogg-audio"),
|
||||
MimeType: "audio/ogg",
|
||||
}
|
||||
converter := &testutil.FakeAudioConverter{Output: []byte("fake-wav-audio")}
|
||||
transcriber := &testutil.FakeSpeechTranscriber{Transcript: "I need help with my order"}
|
||||
router := &testutil.FakeIntentRouter{
|
||||
RouteResult: entity.Route{IntentName: "order_inquiry"},
|
||||
}
|
||||
dispatcher := &testutil.FakeWorkflowDispatcher{
|
||||
Response: entity.WorkflowResponse{ReplyText: "Order status: shipped"},
|
||||
}
|
||||
sessions := &testutil.FakeSessionStore{}
|
||||
gateway := &testutil.FakeMessageGateway{}
|
||||
|
||||
textUC := usecase.NewHandleTextMessage(router, dispatcher, sessions, gateway, newTestLogger())
|
||||
uc := usecase.NewHandleVoiceMessage(downloader, converter, transcriber, textUC, gateway, newTestLogger())
|
||||
|
||||
err := uc.Execute(context.Background(), validVoiceDTO())
|
||||
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 1, transcriber.CallCount)
|
||||
assert.Equal(t, 1, dispatcher.CallCount)
|
||||
assert.Equal(t, "I need help with my order", dispatcher.LastRequest.MessageText)
|
||||
assert.Equal(t, "Order status: shipped", gateway.LastSentText)
|
||||
}
|
||||
|
||||
func TestHandleVoiceMessage_Execute_DownloadFails(t *testing.T) {
|
||||
downloader := &testutil.FakeFileDownloader{Error: errors.New("download error")}
|
||||
transcriber := &testutil.FakeSpeechTranscriber{}
|
||||
gateway := &testutil.FakeMessageGateway{}
|
||||
|
||||
textUC := usecase.NewHandleTextMessage(
|
||||
&testutil.FakeIntentRouter{},
|
||||
&testutil.FakeWorkflowDispatcher{},
|
||||
&testutil.FakeSessionStore{},
|
||||
gateway,
|
||||
newTestLogger(),
|
||||
)
|
||||
uc := usecase.NewHandleVoiceMessage(downloader, &testutil.FakeAudioConverter{}, transcriber, textUC, gateway, newTestLogger())
|
||||
|
||||
err := uc.Execute(context.Background(), validVoiceDTO())
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, 0, transcriber.CallCount)
|
||||
assert.NotEmpty(t, gateway.LastSentText) // error message sent
|
||||
}
|
||||
|
||||
func TestHandleVoiceMessage_Execute_TranscriptionFails(t *testing.T) {
|
||||
downloader := &testutil.FakeFileDownloader{
|
||||
Data: []byte("audio"),
|
||||
MimeType: "audio/ogg",
|
||||
}
|
||||
transcriber := &testutil.FakeSpeechTranscriber{Error: errors.New("whisper error")}
|
||||
gateway := &testutil.FakeMessageGateway{}
|
||||
dispatcher := &testutil.FakeWorkflowDispatcher{}
|
||||
|
||||
textUC := usecase.NewHandleTextMessage(
|
||||
&testutil.FakeIntentRouter{},
|
||||
dispatcher,
|
||||
&testutil.FakeSessionStore{},
|
||||
gateway,
|
||||
newTestLogger(),
|
||||
)
|
||||
uc := usecase.NewHandleVoiceMessage(downloader, &testutil.FakeAudioConverter{}, transcriber, textUC, gateway, newTestLogger())
|
||||
|
||||
err := uc.Execute(context.Background(), validVoiceDTO())
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, 0, dispatcher.CallCount)
|
||||
assert.NotEmpty(t, gateway.LastSentText)
|
||||
}
|
||||
Reference in New Issue
Block a user