feat: persist voice samples to disk, generate VTT, cleanup after analysis
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"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"
|
||||
)
|
||||
|
||||
@@ -24,25 +25,44 @@ func validVoiceDTO() dto.IncomingMessageDTO {
|
||||
}
|
||||
}
|
||||
|
||||
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-audio"),
|
||||
MimeType: "audio/ogg",
|
||||
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",
|
||||
},
|
||||
}
|
||||
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"},
|
||||
fileStore := &testutil.FakeVoiceFileStore{
|
||||
SaveRawPath: "/tmp/abc.ogg",
|
||||
SaveConvertedPath: "/tmp/abc.wav",
|
||||
SaveVTTPath: "/tmp/abc.vtt",
|
||||
}
|
||||
dispatcher := &testutil.FakeWorkflowDispatcher{
|
||||
Response: entity.WorkflowResponse{ReplyText: "Order status: shipped"},
|
||||
}
|
||||
sessions := &testutil.FakeSessionStore{}
|
||||
router := &testutil.FakeIntentRouter{RouteResult: entity.Route{IntentName: "order_inquiry"}}
|
||||
dispatcher := &testutil.FakeWorkflowDispatcher{Response: entity.WorkflowResponse{ReplyText: "Order status: shipped"}}
|
||||
gateway := &testutil.FakeMessageGateway{}
|
||||
|
||||
textUC := usecase.NewHandleTextMessage(router, dispatcher, sessions, gateway, newTestLogger())
|
||||
uc := usecase.NewHandleVoiceMessage(downloader, converter, transcriber, textUC, gateway, newTestLogger())
|
||||
|
||||
uc := newVoiceUC(downloader, transcriber, fileStore, router, dispatcher, gateway)
|
||||
err := uc.Execute(context.Background(), validVoiceDTO())
|
||||
|
||||
require.NoError(t, err)
|
||||
@@ -50,50 +70,64 @@ func TestHandleVoiceMessage_Execute_HappyPath(t *testing.T) {
|
||||
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("download error")}
|
||||
downloader := &testutil.FakeFileDownloader{Error: errors.New("network error")}
|
||||
transcriber := &testutil.FakeSpeechTranscriber{}
|
||||
fileStore := &testutil.FakeVoiceFileStore{}
|
||||
gateway := &testutil.FakeMessageGateway{}
|
||||
|
||||
textUC := usecase.NewHandleTextMessage(
|
||||
uc := newVoiceUC(downloader, transcriber, fileStore,
|
||||
&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
|
||||
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",
|
||||
}
|
||||
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{}
|
||||
|
||||
textUC := usecase.NewHandleTextMessage(
|
||||
uc := newVoiceUC(downloader, transcriber, fileStore,
|
||||
&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)
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user