summaryrefslogtreecommitdiff
path: root/internal/anki/apkg_generator_test.go
blob: 68d828d87834c0615170784e86a56f243fc51a67 (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
package anki

import (
	"archive/zip"
	"database/sql"
	"os"
	"path/filepath"
	"testing"
)

func TestNewAPKGGenerator(t *testing.T) {
	gen := NewAPKGGenerator("Test Deck")

	if gen == nil {
		t.Fatal("NewAPKGGenerator returned nil")
	}

	if gen.deckName != "Test Deck" {
		t.Errorf("Expected deck name 'Test Deck', got '%s'", gen.deckName)
	}

	if len(gen.cards) != 0 {
		t.Errorf("Expected empty cards slice, got %d cards", len(gen.cards))
	}

	if len(gen.mediaFiles) != 0 {
		t.Errorf("Expected empty media files, got %d files", len(gen.mediaFiles))
	}
}

func TestAPKGAddCard(t *testing.T) {
	gen := NewAPKGGenerator("Test Deck")

	// Create test files
	tempDir := t.TempDir()
	audioFile := filepath.Join(tempDir, "audio.mp3")
	imageFile := filepath.Join(tempDir, "image.jpg")

	if err := os.WriteFile(audioFile, []byte("audio data"), 0644); err != nil {
		t.Fatalf("Failed to create test audio file: %v", err)
	}
	if err := os.WriteFile(imageFile, []byte("image data"), 0644); err != nil {
		t.Fatalf("Failed to create test image file: %v", err)
	}

	card := Card{
		Bulgarian:   "ябълка",
		AudioFile:   audioFile,
		ImageFile:   imageFile,
		Translation: "apple",
		Notes:       "test note",
	}

	gen.AddCard(card)

	if len(gen.cards) != 1 {
		t.Errorf("Expected 1 card, got %d", len(gen.cards))
	}

	// Media files are populated during copyMediaFiles, not AddCard
	// So we just check that the card was added correctly
	if gen.cards[0].Bulgarian != "ябълка" {
		t.Errorf("Expected Bulgarian 'ябълка', got '%s'", gen.cards[0].Bulgarian)
	}
}

func TestMediaFiles(t *testing.T) {
	gen := NewAPKGGenerator("Test Deck")

	// Add some media files
	gen.mediaFiles["audio.mp3"] = 0
	gen.mediaFiles["image.jpg"] = 1

	if len(gen.mediaFiles) != 2 {
		t.Errorf("Expected 2 media entries, got %d", len(gen.mediaFiles))
	}

	if gen.mediaFiles["audio.mp3"] != 0 {
		t.Errorf("Expected mediaFiles['audio.mp3'] = 0, got %d", gen.mediaFiles["audio.mp3"])
	}

	if gen.mediaFiles["image.jpg"] != 1 {
		t.Errorf("Expected mediaFiles['image.jpg'] = 1, got %d", gen.mediaFiles["image.jpg"])
	}
}

func TestGenerateAPKG(t *testing.T) {
	tempDir := t.TempDir()

	// Create test files
	audioFile := filepath.Join(tempDir, "audio.mp3")
	imageFile := filepath.Join(tempDir, "image.jpg")

	if err := os.WriteFile(audioFile, []byte("test audio data"), 0644); err != nil {
		t.Fatalf("Failed to create APKG test audio file: %v", err)
	}
	if err := os.WriteFile(imageFile, []byte("test image data"), 0644); err != nil {
		t.Fatalf("Failed to create APKG test image file: %v", err)
	}

	gen := NewAPKGGenerator("Test Bulgarian Deck")

	// Add a test card
	gen.AddCard(Card{
		Bulgarian:   "ябълка",
		AudioFile:   audioFile,
		ImageFile:   imageFile,
		Translation: "apple",
		Notes:       "A common fruit",
	})

	// Generate APKG
	outputPath := filepath.Join(tempDir, "test.apkg")
	err := gen.GenerateAPKG(outputPath)
	if err != nil {
		t.Fatalf("GenerateAPKG() error = %v", err)
	}

	// Find the generated file
	files, err := filepath.Glob(filepath.Join(tempDir, "*.apkg"))
	if err != nil {
		t.Fatalf("Error finding apkg file: %v", err)
	}
	if len(files) != 1 {
		t.Fatalf("Expected 1 apkg file, found %d", len(files))
	}
	actualOutputPath := files[0]

	// Verify it's a valid zip file
	reader, err := zip.OpenReader(actualOutputPath)
	if err != nil {
		t.Fatalf("Failed to open APKG as zip: %v", err)
	}
	defer func() {
		if closeErr := reader.Close(); closeErr != nil {
			t.Errorf("Failed to close APKG zip reader: %v", closeErr)
		}
	}()

	// Check for required files
	requiredFiles := map[string]bool{
		"collection.anki2": false,
		"media":            false,
		"0":                false, // audio file
		"1":                false, // image file
	}

	for _, file := range reader.File {
		if _, ok := requiredFiles[file.Name]; ok {
			requiredFiles[file.Name] = true
		}
	}

	for name, found := range requiredFiles {
		if !found {
			t.Errorf("Required file '%s' not found in APKG", name)
		}
	}
}

func TestCreateDatabase(t *testing.T) {
	tempDir := t.TempDir()
	dbPath := filepath.Join(tempDir, "test.anki2")

	gen := NewAPKGGenerator("Test Deck")

	// Add test card
	gen.AddCard(Card{
		Bulgarian:   "котка",
		Translation: "cat",
		Notes:       "An animal",
	})

	err := gen.createDatabase(dbPath)
	if err != nil {
		t.Fatalf("createDatabase() error = %v", err)
	}

	// Verify database exists
	if _, err := os.Stat(dbPath); os.IsNotExist(err) {
		t.Fatal("Database file was not created")
	}

	// Open and verify database structure
	db, err := sql.Open("sqlite3", dbPath)
	if err != nil {
		t.Fatalf("Failed to open database: %v", err)
	}
	defer func() {
		if closeErr := db.Close(); closeErr != nil {
			t.Errorf("Failed to close database: %v", closeErr)
		}
	}()

	// Check core tables exist
	coreTables := []string{"col", "notes", "cards"}
	missingTables := 0
	for _, table := range coreTables {
		var name string
		err := db.QueryRow("SELECT name FROM sqlite_master WHERE type='table' AND name=?", table).Scan(&name)
		if err != nil {
			missingTables++
		}
	}

	// If core tables are missing, the database creation likely failed
	if missingTables == len(coreTables) {
		t.Skip("SQLite database creation not fully implemented or sqlite3 driver not available")
	}

	// Check that a note was created
	var noteCount int
	err = db.QueryRow("SELECT COUNT(*) FROM notes").Scan(&noteCount)
	if err == nil && noteCount != 1 {
		t.Errorf("Expected 1 note, got %d", noteCount)
	}
}

func TestMarshalJSONReturnsErrorForUnsupportedValue(t *testing.T) {
	if _, err := marshalJSON("bad", make(chan int)); err == nil {
		t.Fatal("marshalJSON() error = nil, want error")
	}
}