summaryrefslogtreecommitdiff
path: root/internal/gui/audio_paths_test.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-04-02 07:37:19 +0300
committerPaul Buetow <paul@buetow.org>2026-04-02 07:37:19 +0300
commitf623b64d083d52cc9a278e9d229215a6686c6b29 (patch)
treeb82a3371d6a7d1d73d0f22fc6cec0ebcc9f389b1 /internal/gui/audio_paths_test.go
parentc1a2a5a1df54611437d36e1b2f448f78b32e8eb1 (diff)
Fix GUI audio reload and bg-bg session state
Diffstat (limited to 'internal/gui/audio_paths_test.go')
-rw-r--r--internal/gui/audio_paths_test.go134
1 files changed, 101 insertions, 33 deletions
diff --git a/internal/gui/audio_paths_test.go b/internal/gui/audio_paths_test.go
index a61a24e..d412004 100644
--- a/internal/gui/audio_paths_test.go
+++ b/internal/gui/audio_paths_test.go
@@ -5,13 +5,49 @@ import (
"os"
"path/filepath"
"testing"
- "time"
fyneapp "fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/widget"
ttwidget "github.com/dweymouth/fyne-tooltip/widget"
)
+func newGUIAudioTestApp(t *testing.T, tempDir string) *Application {
+ t.Helper()
+
+ fyneApp := fyneapp.New()
+ t.Cleanup(func() {
+ fyneApp.Quit()
+ })
+
+ ctx, cancel := context.WithCancel(context.Background())
+ t.Cleanup(cancel)
+
+ return &Application{
+ config: &Config{
+ OutputDir: tempDir,
+ AudioFormat: "wav",
+ },
+ ctx: ctx,
+ cancel: cancel,
+ queue: NewWordQueue(ctx),
+ wordInput: NewCustomEntry(),
+ audioPlayer: NewAudioPlayer(),
+ imageDisplay: NewImageDisplay(),
+ translationEntry: NewCustomEntry(),
+ cardTypeSelect: widget.NewSelect([]string{"English → Bulgarian", "Bulgarian → Bulgarian"}, nil),
+ imagePromptEntry: NewCustomMultiLineEntry(),
+ statusLabel: widget.NewLabel(""),
+ prevWordBtn: ttwidget.NewButton("", nil),
+ nextWordBtn: ttwidget.NewButton("", nil),
+ keepButton: ttwidget.NewButton("", nil),
+ regenerateImageBtn: ttwidget.NewButton("", nil),
+ regenerateRandomImageBtn: ttwidget.NewButton("", nil),
+ regenerateAudioBtn: ttwidget.NewButton("", nil),
+ regenerateAllBtn: ttwidget.NewButton("", nil),
+ deleteButton: ttwidget.NewButton("", nil),
+ }
+}
+
func TestResolveSingleAudioFileFindsLegacyMp3WhenGuiDefaultIsWav(t *testing.T) {
tempDir := t.TempDir()
wordDir := filepath.Join(tempDir, "word")
@@ -35,13 +71,6 @@ func TestResolveSingleAudioFileFindsLegacyMp3WhenGuiDefaultIsWav(t *testing.T) {
}
func TestGUIDiscoveryAndLoadingRecognizeVoiceSuffixedAudio(t *testing.T) {
- fyneApp := fyneapp.New()
- t.Cleanup(func() {
- fyneApp.Quit()
- })
- ctx, cancel := context.WithCancel(context.Background())
- t.Cleanup(cancel)
-
tempDir := t.TempDir()
wordDir := filepath.Join(tempDir, "word-card")
if err := os.MkdirAll(wordDir, 0755); err != nil {
@@ -58,30 +87,7 @@ func TestGUIDiscoveryAndLoadingRecognizeVoiceSuffixedAudio(t *testing.T) {
t.Fatalf("failed to write voice-suffixed audio file: %v", err)
}
- app := &Application{
- config: &Config{
- OutputDir: tempDir,
- AudioFormat: "wav",
- },
- ctx: ctx,
- cancel: cancel,
- queue: NewWordQueue(context.Background()),
- wordInput: NewCustomEntry(),
- audioPlayer: NewAudioPlayer(),
- imageDisplay: NewImageDisplay(),
- translationEntry: NewCustomEntry(),
- cardTypeSelect: widget.NewSelect([]string{"English → Bulgarian", "Bulgarian → Bulgarian"}, nil),
- imagePromptEntry: NewCustomMultiLineEntry(),
- statusLabel: widget.NewLabel(""),
- prevWordBtn: ttwidget.NewButton("", nil),
- nextWordBtn: ttwidget.NewButton("", nil),
- keepButton: ttwidget.NewButton("", nil),
- regenerateImageBtn: ttwidget.NewButton("", nil),
- regenerateRandomImageBtn: ttwidget.NewButton("", nil),
- regenerateAudioBtn: ttwidget.NewButton("", nil),
- regenerateAllBtn: ttwidget.NewButton("", nil),
- deleteButton: ttwidget.NewButton("", nil),
- }
+ app := newGUIAudioTestApp(t, tempDir)
app.scanExistingWords()
if len(app.existingWords) != 1 || app.existingWords[0] != word {
@@ -89,7 +95,6 @@ func TestGUIDiscoveryAndLoadingRecognizeVoiceSuffixedAudio(t *testing.T) {
}
app.loadExistingFiles(word)
- time.Sleep(50 * time.Millisecond)
if app.currentAudioFile != voiceAudioPath {
t.Fatalf("currentAudioFile = %q, want voice-suffixed path %q", app.currentAudioFile, voiceAudioPath)
@@ -107,6 +112,69 @@ func TestGUIDiscoveryAndLoadingRecognizeVoiceSuffixedAudio(t *testing.T) {
}
}
+func TestLoadExistingFilesPrefersFreshMetadataAudioOverLegacyVoiceSpecificFile(t *testing.T) {
+ tempDir := t.TempDir()
+ wordDir := filepath.Join(tempDir, "word-card")
+ if err := os.MkdirAll(wordDir, 0755); err != nil {
+ t.Fatalf("failed to create word dir: %v", err)
+ }
+
+ word := "ябълка"
+ if err := os.WriteFile(filepath.Join(wordDir, "word.txt"), []byte(word), 0644); err != nil {
+ t.Fatalf("failed to write word file: %v", err)
+ }
+
+ staleLegacyAudio := filepath.Join(wordDir, "audio_legacy-voice.wav")
+ if err := os.WriteFile(staleLegacyAudio, []byte("stale-legacy-audio"), 0644); err != nil {
+ t.Fatalf("failed to write stale legacy audio file: %v", err)
+ }
+
+ freshAudio := filepath.Join(wordDir, "audio.wav")
+ if err := os.WriteFile(freshAudio, []byte("fresh-audio"), 0644); err != nil {
+ t.Fatalf("failed to write fresh audio file: %v", err)
+ }
+
+ metadata := "provider=gemini\nvoice=\nspeed=1.00\nformat=wav\ncardtype=en-bg\naudio_file=audio.wav\n"
+ if err := os.WriteFile(filepath.Join(wordDir, "audio_metadata.txt"), []byte(metadata), 0644); err != nil {
+ t.Fatalf("failed to write audio metadata: %v", err)
+ }
+
+ app := newGUIAudioTestApp(t, tempDir)
+
+ app.loadExistingFiles(word)
+
+ if app.currentAudioFile != freshAudio {
+ t.Fatalf("currentAudioFile = %q, want fresh generated audio %q", app.currentAudioFile, freshAudio)
+ }
+ if app.audioPlayer.audioFile != freshAudio {
+ t.Fatalf("audioPlayer.audioFile = %q, want fresh generated audio %q", app.audioPlayer.audioFile, freshAudio)
+ }
+}
+
+func TestCompletedBgBgJobKeepsBackAudioForSessionNavigation(t *testing.T) {
+ tempDir := t.TempDir()
+ app := newGUIAudioTestApp(t, tempDir)
+
+ job := app.queue.AddWord("ябълка")
+ job.CardType = "bg-bg"
+
+ frontAudio := filepath.Join(tempDir, "card", "audio_front.wav")
+ backAudio := filepath.Join(tempDir, "card", "audio_back.wav")
+ app.queue.CompleteJob(job.ID, "определение", frontAudio, backAudio, "")
+
+ app.loadWordByIndex(0)
+
+ if app.queue.GetCompletedJobs()[0].AudioFileBack != backAudio {
+ t.Fatalf("completed job back audio = %q, want %q", app.queue.GetCompletedJobs()[0].AudioFileBack, backAudio)
+ }
+ if app.currentAudioFileBack != backAudio {
+ t.Fatalf("currentAudioFileBack = %q, want %q", app.currentAudioFileBack, backAudio)
+ }
+ if app.currentCardType != "bg-bg" {
+ t.Fatalf("currentCardType = %q, want %q", app.currentCardType, "bg-bg")
+ }
+}
+
func TestResolveBgBgAudioFilesFindLegacyMp3Files(t *testing.T) {
tempDir := t.TempDir()
wordDir := filepath.Join(tempDir, "word")