summaryrefslogtreecommitdiff
path: root/internal/processor/processor_test.go
blob: 40f5cbe8af6ac7735c2dbc59f75a6cb91844570b (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package processor

import (
	"os"
	"path/filepath"
	"testing"

	"codeberg.org/snonux/totalrecall/internal/cli"
)

func TestNewProcessor(t *testing.T) {
	// Set up test environment
	os.Setenv("OPENAI_API_KEY", "test-key")
	defer os.Unsetenv("OPENAI_API_KEY")

	flags := cli.NewFlags()
	p := NewProcessor(flags)

	if p == nil {
		t.Fatal("NewProcessor returned nil")
	}

	if p.flags != flags {
		t.Error("Processor flags not set correctly")
	}

	if p.translator == nil {
		t.Error("Translator not initialized")
	}

	if p.translationCache == nil {
		t.Error("Translation cache not initialized")
	}

	if p.phoneticFetcher == nil {
		t.Error("Phonetic fetcher not initialized")
	}
}

func TestProcessSingleWord_InvalidWord(t *testing.T) {
	flags := cli.NewFlags()
	flags.OutputDir = t.TempDir()
	p := NewProcessor(flags)

	// Test with non-Bulgarian text
	err := p.ProcessSingleWord("hello")
	if err == nil {
		t.Error("Expected error for non-Bulgarian word")
	}

	// Test with empty string
	err = p.ProcessSingleWord("")
	if err == nil {
		t.Error("Expected error for empty word")
	}
}

func TestProcessSingleWord_ValidWord(t *testing.T) {
	// Skip if no API key
	if os.Getenv("OPENAI_API_KEY") == "" {
		t.Skip("Skipping test: OPENAI_API_KEY not set")
	}

	flags := cli.NewFlags()
	flags.OutputDir = t.TempDir()
	flags.SkipAudio = true
	flags.SkipImages = true
	p := NewProcessor(flags)

	err := p.ProcessSingleWord("ябълка")
	if err != nil {
		t.Errorf("ProcessSingleWord failed: %v", err)
	}

	// Check that output directory was created
	if _, err := os.Stat(flags.OutputDir); os.IsNotExist(err) {
		t.Error("Output directory was not created")
	}
}

func TestProcessBatch_InvalidFile(t *testing.T) {
	flags := cli.NewFlags()
	flags.BatchFile = "/nonexistent/file.txt"
	p := NewProcessor(flags)

	err := p.ProcessBatch()
	if err == nil {
		t.Error("Expected error for non-existent batch file")
	}
}

func TestProcessBatch_ValidFile(t *testing.T) {
	// Create test batch file
	tmpDir := t.TempDir()
	batchFile := filepath.Join(tmpDir, "batch.txt")
	content := `ябълка
котка = cat
куче`
	err := os.WriteFile(batchFile, []byte(content), 0644)
	if err != nil {
		t.Fatalf("Failed to create test batch file: %v", err)
	}

	flags := cli.NewFlags()
	flags.OutputDir = tmpDir
	flags.BatchFile = batchFile
	flags.SkipAudio = true
	flags.SkipImages = true
	p := NewProcessor(flags)

	// Skip if no API key
	if os.Getenv("OPENAI_API_KEY") == "" {
		t.Skip("Skipping test: OPENAI_API_KEY not set")
	}

	err = p.ProcessBatch()
	if err != nil {
		t.Errorf("ProcessBatch failed: %v", err)
	}
}

func TestProcessWordWithTranslation_ProvidedTranslation(t *testing.T) {
	flags := cli.NewFlags()
	flags.OutputDir = t.TempDir()
	flags.SkipAudio = true
	flags.SkipImages = true
	p := NewProcessor(flags)

	// Skip if no API key (needed for phonetic fetcher)
	if os.Getenv("OPENAI_API_KEY") == "" {
		t.Skip("Skipping test: OPENAI_API_KEY not set")
	}

	err := p.ProcessWordWithTranslation("ябълка", "apple")
	if err != nil {
		t.Errorf("ProcessWordWithTranslation failed: %v", err)
	}

	// Check that translation was cached
	cached, found := p.translationCache.Get("ябълка")
	if !found || cached != "apple" {
		t.Errorf("Expected cached translation 'apple', got '%s' (found: %v)", cached, found)
	}
}

func TestFindOrCreateWordDirectory(t *testing.T) {
	flags := cli.NewFlags()
	flags.OutputDir = t.TempDir()
	p := NewProcessor(flags)

	// First call should create directory
	dir1 := p.findOrCreateWordDirectory("тест")
	if dir1 == "" {
		t.Error("findOrCreateWordDirectory returned empty string")
	}

	// Check directory exists
	if _, err := os.Stat(dir1); os.IsNotExist(err) {
		t.Error("Directory was not created")
	}

	// Check word.txt was created
	wordFile := filepath.Join(dir1, "word.txt")
	content, err := os.ReadFile(wordFile)
	if err != nil {
		t.Errorf("Failed to read word.txt: %v", err)
	}
	if string(content) != "тест" {
		t.Errorf("Expected word.txt to contain 'тест', got '%s'", string(content))
	}

	// Second call should find existing directory
	dir2 := p.findOrCreateWordDirectory("тест")
	if dir2 != dir1 {
		t.Errorf("Expected to find existing directory %s, got %s", dir1, dir2)
	}
}

func TestFindCardDirectory(t *testing.T) {
	flags := cli.NewFlags()
	flags.OutputDir = t.TempDir()
	p := NewProcessor(flags)

	// Test with non-existent word
	dir := p.findCardDirectory("несъществуваща")
	if dir != "" {
		t.Error("Expected empty string for non-existent word")
	}

	// Create a word directory
	wordDir := p.findOrCreateWordDirectory("тест")

	// Now should find it
	dir = p.findCardDirectory("тест")
	if dir != wordDir {
		t.Errorf("Expected to find directory %s, got %s", wordDir, dir)
	}
}

func TestGenerateAnkiFile(t *testing.T) {
	flags := cli.NewFlags()
	flags.OutputDir = t.TempDir()
	flags.GenerateAnki = true
	flags.AnkiCSV = true // Test CSV format
	p := NewProcessor(flags)

	// Add some test translations
	p.translationCache.Add("ябълка", "apple")
	p.translationCache.Add("котка", "cat")

	// Create dummy word directories and files
	p.findOrCreateWordDirectory("ябълка")
	p.findOrCreateWordDirectory("котка")

	_, err := p.GenerateAnkiFile()
	if err != nil {
		t.Errorf("GenerateAnkiFile failed: %v", err)
	}

	// Check CSV file was created in home directory
	homeDir, _ := os.UserHomeDir()
	csvFile := filepath.Join(homeDir, "anki_import.csv")
	if _, err := os.Stat(csvFile); os.IsNotExist(err) {
		t.Error("CSV file was not created in home directory")
	}
	os.Remove(csvFile) // Clean up
}

func TestGenerateAnkiFile_APKG(t *testing.T) {
	flags := cli.NewFlags()
	flags.OutputDir = t.TempDir()
	flags.GenerateAnki = true
	flags.AnkiCSV = false // Test APKG format
	flags.DeckName = "Test Deck"
	p := NewProcessor(flags)

	// Create test word directories with files
	word1Dir := p.findOrCreateWordDirectory("ябълка")
	word2Dir := p.findOrCreateWordDirectory("котка")

	// Add test translations
	p.translationCache.Add("ябълка", "apple")
	p.translationCache.Add("котка", "cat")

	// Create dummy audio and image files
	os.WriteFile(filepath.Join(word1Dir, "audio.mp3"), []byte("audio1"), 0644)
	os.WriteFile(filepath.Join(word2Dir, "audio.mp3"), []byte("audio2"), 0644)
	os.WriteFile(filepath.Join(word1Dir, "image.jpg"), []byte("image1"), 0644)

	_, err := p.GenerateAnkiFile()
	if err != nil {
		t.Errorf("GenerateAnkiFile (APKG) failed: %v", err)
	}

	// Check APKG file was created in home directory
	homeDir, _ := os.UserHomeDir()
	apkgFile := filepath.Join(homeDir, "Test_Deck.apkg")
	if _, err := os.Stat(apkgFile); os.IsNotExist(err) {
		t.Error("APKG file was not created in home directory")
	}
	os.Remove(apkgFile) // Clean up
}