summaryrefslogtreecommitdiff
path: root/internal/testutil/mocks.go
blob: 811b8409f6981c136ac2d826f7e216ac8a45a60d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package testutil

import (
	"context"
	"fmt"
	"io"
	"strings"
)

// MockHTTPClient mocks HTTP client for testing
type MockHTTPClient struct {
	Responses map[string]*MockResponse
	Errors    map[string]error
	Calls     []string
}

// MockResponse represents a mocked HTTP response
type MockResponse struct {
	StatusCode int
	Body       string
	Headers    map[string]string
}

// Get mocks an HTTP GET request
func (m *MockHTTPClient) Get(url string) (*MockResponse, error) {
	m.Calls = append(m.Calls, fmt.Sprintf("GET %s", url))

	if err, ok := m.Errors[url]; ok {
		return nil, err
	}

	if resp, ok := m.Responses[url]; ok {
		return resp, nil
	}

	return &MockResponse{
		StatusCode: 404,
		Body:       "Not Found",
	}, nil
}

// Post mocks an HTTP POST request
func (m *MockHTTPClient) Post(url string, body interface{}) (*MockResponse, error) {
	m.Calls = append(m.Calls, fmt.Sprintf("POST %s", url))

	if err, ok := m.Errors[url]; ok {
		return nil, err
	}

	if resp, ok := m.Responses[url]; ok {
		return resp, nil
	}

	return &MockResponse{
		StatusCode: 404,
		Body:       "Not Found",
	}, nil
}

// MockOpenAIClient mocks OpenAI API client
type MockOpenAIClient struct {
	TTSResponses   map[string][]byte
	ImageResponses map[string]string
	Errors         map[string]error
	Calls          []string
}

// CreateSpeech mocks OpenAI TTS API
func (m *MockOpenAIClient) CreateSpeech(ctx context.Context, text, voice, model string) (io.ReadCloser, error) {
	call := fmt.Sprintf("TTS: %s (voice=%s, model=%s)", text, voice, model)
	m.Calls = append(m.Calls, call)

	key := fmt.Sprintf("%s-%s-%s", text, voice, model)
	if err, ok := m.Errors[key]; ok {
		return nil, err
	}

	if data, ok := m.TTSResponses[key]; ok {
		return io.NopCloser(strings.NewReader(string(data))), nil
	}

	// Default response
	return io.NopCloser(strings.NewReader("mock audio data")), nil
}

// CreateImage mocks OpenAI DALL-E API
func (m *MockOpenAIClient) CreateImage(ctx context.Context, prompt string) (string, error) {
	call := fmt.Sprintf("Image: %s", prompt)
	m.Calls = append(m.Calls, call)

	if err, ok := m.Errors[prompt]; ok {
		return "", err
	}

	if url, ok := m.ImageResponses[prompt]; ok {
		return url, nil
	}

	// Default response
	return "https://example.com/mock-image.jpg", nil
}

// MockFileSystem mocks file system operations
type MockFileSystem struct {
	Files  map[string][]byte
	Errors map[string]error
	Calls  []string
}

// ReadFile mocks reading a file
func (m *MockFileSystem) ReadFile(path string) ([]byte, error) {
	m.Calls = append(m.Calls, fmt.Sprintf("READ %s", path))

	if err, ok := m.Errors[path]; ok {
		return nil, err
	}

	if data, ok := m.Files[path]; ok {
		return data, nil
	}

	return nil, fmt.Errorf("file not found: %s", path)
}

// WriteFile mocks writing a file
func (m *MockFileSystem) WriteFile(path string, data []byte) error {
	m.Calls = append(m.Calls, fmt.Sprintf("WRITE %s (%d bytes)", path, len(data)))

	if err, ok := m.Errors[path]; ok {
		return err
	}

	m.Files[path] = data
	return nil
}

// Exists mocks checking if a file exists
func (m *MockFileSystem) Exists(path string) bool {
	m.Calls = append(m.Calls, fmt.Sprintf("EXISTS %s", path))
	_, exists := m.Files[path]
	return exists
}

// MockTranslator mocks translation service
type MockTranslator struct {
	Translations map[string]string
	Errors       map[string]error
	Calls        []string
}

// Translate mocks translating text
func (m *MockTranslator) Translate(ctx context.Context, text, fromLang, toLang string) (string, error) {
	call := fmt.Sprintf("Translate: %s (%s->%s)", text, fromLang, toLang)
	m.Calls = append(m.Calls, call)

	if err, ok := m.Errors[text]; ok {
		return "", err
	}

	if translation, ok := m.Translations[text]; ok {
		return translation, nil
	}

	// Default mock translation
	return fmt.Sprintf("mock translation of %s", text), nil
}

// TestDataGenerator generates test data
type TestDataGenerator struct{}

// GenerateBulgarianWord generates a test Bulgarian word
func (g *TestDataGenerator) GenerateBulgarianWord() string {
	words := []string{"ябълка", "котка", "куче", "хляб", "вода", "книга", "стол", "прозорец"}
	return words[0] // Simple implementation, could be randomized
}

// GenerateAudioData generates mock audio data
func (g *TestDataGenerator) GenerateAudioData() []byte {
	// Simple mock MP3 header
	return []byte{0xFF, 0xFB, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00}
}

// GenerateImageData generates mock image data
func (g *TestDataGenerator) GenerateImageData() []byte {
	// Simple mock JPEG header
	return []byte{0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46}
}