feat: refactor

This commit is contained in:
leonardotrapani
2026-02-01 17:24:43 +01:00
parent 13b1de4e04
commit 8df3021a9d
33 changed files with 1290 additions and 1577 deletions
+213
View File
@@ -5,11 +5,16 @@ import (
"io"
"os"
"path/filepath"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/leonardotrapani/hyprvoice/internal/config"
"github.com/leonardotrapani/hyprvoice/internal/injection"
"github.com/leonardotrapani/hyprvoice/internal/llm"
"github.com/leonardotrapani/hyprvoice/internal/recording"
"github.com/leonardotrapani/hyprvoice/internal/transcriber"
)
// TestConfig returns a valid configuration for testing
@@ -180,3 +185,211 @@ func CaptureOutput(t *testing.T, fn func()) string {
out, _ := io.ReadAll(r)
return string(out)
}
// MockRecorder implements recording.Recorder for testing
type MockRecorder struct {
Frames []recording.AudioFrame
StartError error
mu sync.Mutex
recording atomic.Bool
stopCh chan struct{}
}
func NewMockRecorder() *MockRecorder {
return &MockRecorder{
Frames: []recording.AudioFrame{MockAudioFrame(nil)},
}
}
func (m *MockRecorder) Start(ctx context.Context) (<-chan recording.AudioFrame, <-chan error, error) {
if m.StartError != nil {
return nil, nil, m.StartError
}
m.mu.Lock()
m.stopCh = make(chan struct{})
m.mu.Unlock()
m.recording.Store(true)
frameCh := make(chan recording.AudioFrame, len(m.Frames)+1)
errCh := make(chan error, 1)
go func() {
defer close(frameCh)
defer close(errCh)
for _, frame := range m.Frames {
select {
case <-ctx.Done():
return
case <-m.stopCh:
return
case frameCh <- frame:
}
}
// keep channel open until stopped
select {
case <-ctx.Done():
case <-m.stopCh:
}
}()
return frameCh, errCh, nil
}
func (m *MockRecorder) Stop() {
if !m.recording.Load() {
return
}
m.recording.Store(false)
m.mu.Lock()
if m.stopCh != nil {
close(m.stopCh)
m.stopCh = nil
}
m.mu.Unlock()
}
func (m *MockRecorder) IsRecording() bool {
return m.recording.Load()
}
// MockTranscriber implements transcriber.Transcriber for testing
type MockTranscriber struct {
Transcription string
StartError error
StopError error
GetError error
mu sync.Mutex
started bool
}
func NewMockTranscriber(transcription string) *MockTranscriber {
return &MockTranscriber{Transcription: transcription}
}
func (m *MockTranscriber) Start(ctx context.Context, frameCh <-chan recording.AudioFrame) (<-chan error, error) {
if m.StartError != nil {
return nil, m.StartError
}
m.mu.Lock()
m.started = true
m.mu.Unlock()
errCh := make(chan error, 1)
// drain frames in background
go func() {
defer close(errCh)
for range frameCh {
}
}()
return errCh, nil
}
func (m *MockTranscriber) Stop(ctx context.Context) error {
m.mu.Lock()
m.started = false
m.mu.Unlock()
return m.StopError
}
func (m *MockTranscriber) GetFinalTranscription() (string, error) {
if m.GetError != nil {
return "", m.GetError
}
return m.Transcription, nil
}
// MockInjector implements injection.Injector for testing
type MockInjector struct {
InjectedTexts []string
InjectError error
mu sync.Mutex
}
func NewMockInjector() *MockInjector {
return &MockInjector{}
}
func (m *MockInjector) Inject(ctx context.Context, text string) error {
if m.InjectError != nil {
return m.InjectError
}
m.mu.Lock()
m.InjectedTexts = append(m.InjectedTexts, text)
m.mu.Unlock()
return nil
}
func (m *MockInjector) GetInjectedTexts() []string {
m.mu.Lock()
defer m.mu.Unlock()
result := make([]string, len(m.InjectedTexts))
copy(result, m.InjectedTexts)
return result
}
// MockLLMAdapter implements llm.Adapter for testing
type MockLLMAdapter struct {
ProcessedText string
ProcessError error
mu sync.Mutex
ProcessCalled bool
InputText string
}
func NewMockLLMAdapter(processedText string) *MockLLMAdapter {
return &MockLLMAdapter{ProcessedText: processedText}
}
func (m *MockLLMAdapter) Process(ctx context.Context, text string) (string, error) {
m.mu.Lock()
m.ProcessCalled = true
m.InputText = text
m.mu.Unlock()
if m.ProcessError != nil {
return "", m.ProcessError
}
return m.ProcessedText, nil
}
// Factory helpers for pipeline testing
// MockRecorderFactory returns a factory that creates the given mock recorder
func MockRecorderFactory(mock *MockRecorder) func(cfg recording.Config) recording.Recorder {
return func(cfg recording.Config) recording.Recorder {
return mock
}
}
// MockTranscriberFactory returns a factory that creates the given mock transcriber
func MockTranscriberFactory(mock *MockTranscriber) func(cfg transcriber.Config) (transcriber.Transcriber, error) {
return func(cfg transcriber.Config) (transcriber.Transcriber, error) {
return mock, nil
}
}
// MockInjectorFactory returns a factory that creates the given mock injector
func MockInjectorFactory(mock *MockInjector) func(cfg injection.Config) injection.Injector {
return func(cfg injection.Config) injection.Injector {
return mock
}
}
// MockLLMAdapterFactory returns a factory that creates the given mock LLM adapter
func MockLLMAdapterFactory(mock *MockLLMAdapter) func(cfg llm.Config) (llm.Adapter, error) {
return func(cfg llm.Config) (llm.Adapter, error) {
return mock, nil
}
}