134 lines
4.5 KiB
Go
134 lines
4.5 KiB
Go
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/internal/domain/port"
|
|
"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 newVoiceUC(
|
|
downloader *testutil.FakeFileDownloader,
|
|
transcriber *testutil.FakeSpeechTranscriber,
|
|
fileStore *testutil.FakeVoiceFileStore,
|
|
router *testutil.FakeIntentRouter,
|
|
dispatcher *testutil.FakeWorkflowDispatcher,
|
|
gateway *testutil.FakeMessageGateway,
|
|
) *usecase.HandleVoiceMessage {
|
|
textUC := usecase.NewHandleTextMessage(router, dispatcher, &testutil.FakeSessionStore{}, gateway, newTestLogger())
|
|
return usecase.NewHandleVoiceMessage(
|
|
downloader,
|
|
&testutil.FakeAudioConverter{},
|
|
transcriber,
|
|
fileStore,
|
|
textUC,
|
|
gateway,
|
|
newTestLogger(),
|
|
)
|
|
}
|
|
|
|
func TestHandleVoiceMessage_Execute_HappyPath(t *testing.T) {
|
|
downloader := &testutil.FakeFileDownloader{Data: []byte("fake-ogg"), MimeType: "audio/ogg"}
|
|
transcriber := &testutil.FakeSpeechTranscriber{
|
|
Result: port.TranscriptionResult{
|
|
Text: "I need help with my order",
|
|
VTT: "WEBVTT\n\n00:00:00.000 --> 00:00:02.000\nI need help with my order\n\n",
|
|
},
|
|
}
|
|
fileStore := &testutil.FakeVoiceFileStore{
|
|
SaveRawPath: "/tmp/abc.ogg",
|
|
SaveConvertedPath: "/tmp/abc.wav",
|
|
SaveVTTPath: "/tmp/abc.vtt",
|
|
}
|
|
router := &testutil.FakeIntentRouter{RouteResult: entity.Route{IntentName: "order_inquiry"}}
|
|
dispatcher := &testutil.FakeWorkflowDispatcher{Response: entity.WorkflowResponse{ReplyText: "Order status: shipped"}}
|
|
gateway := &testutil.FakeMessageGateway{}
|
|
|
|
uc := newVoiceUC(downloader, transcriber, fileStore, router, dispatcher, gateway)
|
|
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)
|
|
assert.Equal(t, 1, fileStore.CleanupCallCount)
|
|
}
|
|
|
|
func TestHandleVoiceMessage_Execute_DownloadFails(t *testing.T) {
|
|
downloader := &testutil.FakeFileDownloader{Error: errors.New("network error")}
|
|
transcriber := &testutil.FakeSpeechTranscriber{}
|
|
fileStore := &testutil.FakeVoiceFileStore{}
|
|
gateway := &testutil.FakeMessageGateway{}
|
|
|
|
uc := newVoiceUC(downloader, transcriber, fileStore,
|
|
&testutil.FakeIntentRouter{},
|
|
&testutil.FakeWorkflowDispatcher{},
|
|
gateway,
|
|
)
|
|
err := uc.Execute(context.Background(), validVoiceDTO())
|
|
|
|
assert.Error(t, err)
|
|
assert.Equal(t, 0, transcriber.CallCount)
|
|
assert.NotEmpty(t, gateway.LastSentText)
|
|
assert.Equal(t, 0, fileStore.CleanupCallCount)
|
|
}
|
|
|
|
func TestHandleVoiceMessage_Execute_TranscriptionFails(t *testing.T) {
|
|
downloader := &testutil.FakeFileDownloader{Data: []byte("audio"), MimeType: "audio/ogg"}
|
|
transcriber := &testutil.FakeSpeechTranscriber{Error: errors.New("whisper error")}
|
|
fileStore := &testutil.FakeVoiceFileStore{}
|
|
gateway := &testutil.FakeMessageGateway{}
|
|
dispatcher := &testutil.FakeWorkflowDispatcher{}
|
|
|
|
uc := newVoiceUC(downloader, transcriber, fileStore,
|
|
&testutil.FakeIntentRouter{},
|
|
dispatcher,
|
|
gateway,
|
|
)
|
|
err := uc.Execute(context.Background(), validVoiceDTO())
|
|
|
|
assert.Error(t, err)
|
|
assert.Equal(t, 0, dispatcher.CallCount)
|
|
assert.NotEmpty(t, gateway.LastSentText)
|
|
assert.Equal(t, 1, fileStore.CleanupCallCount)
|
|
}
|
|
|
|
func TestHandleVoiceMessage_Execute_NoVTTWhenEmpty(t *testing.T) {
|
|
downloader := &testutil.FakeFileDownloader{Data: []byte("audio"), MimeType: "audio/ogg"}
|
|
transcriber := &testutil.FakeSpeechTranscriber{
|
|
Result: port.TranscriptionResult{Text: "Hello", VTT: ""},
|
|
}
|
|
fileStore := &testutil.FakeVoiceFileStore{}
|
|
gateway := &testutil.FakeMessageGateway{}
|
|
|
|
uc := newVoiceUC(downloader, transcriber, fileStore,
|
|
&testutil.FakeIntentRouter{RouteResult: entity.Route{IntentName: "general_query"}},
|
|
&testutil.FakeWorkflowDispatcher{Response: entity.WorkflowResponse{ReplyText: "OK"}},
|
|
gateway,
|
|
)
|
|
err := uc.Execute(context.Background(), validVoiceDTO())
|
|
|
|
require.NoError(t, err)
|
|
// SaveVTT should NOT have been called — VTTPath stays empty
|
|
assert.Empty(t, fileStore.SaveVTTPath)
|
|
}
|