package app import ( "context" "testing" "fast_reader/internal/settings" ) func TestNewApp(t *testing.T) { a := NewApp() if a == nil { t.Fatal("NewApp() returned nil") } if got := a.GetSettings(); got != settings.Default() { t.Errorf("initial settings = %+v, want %+v", got, settings.Default()) } } func TestStartup(t *testing.T) { application := NewApp() ctx := context.Background() // Startup should store the Wails application context. application.Startup(ctx) if application.ctx != ctx { t.Error("Startup() did not store the application context") } } func TestPrepareText(t *testing.T) { a := NewApp() words := a.PrepareText("Hello world") if len(words) != 2 { t.Fatalf("PrepareText() returned %d words, want 2", len(words)) } if words[0].Text != "Hello" || words[1].Text != "world" { t.Errorf("unexpected words: %+v", words) } } func TestPrepareEmptyText(t *testing.T) { application := NewApp() got := application.PrepareText("") if len(got) != 0 { t.Errorf("PrepareText() returned %d words for empty input, want 0", len(got)) } } func TestGetVersion(t *testing.T) { a := NewApp() if got := a.GetVersion(); got != "0.1.0" { t.Errorf("GetVersion() = %q, want %q", got, "0.1.0") } } func TestSaveAndLoadSettings(t *testing.T) { configDir := t.TempDir() t.Setenv("XDG_CONFIG_HOME", configDir) first := NewApp() first.Startup(context.Background()) expected := settings.Settings{ ReadingSpeed: 500, FontSize: 64, Theme: settings.ThemeLight, } if err := first.SaveSettings(expected); err != nil { t.Fatalf("SaveSettings() error = %v", err) } if got := first.GetSettings(); got != expected { t.Errorf("settings = %+v, want %+v", got, expected) } // Simulate restarting the application. second := NewApp() second.Startup(context.Background()) if got := second.GetSettings(); got != expected { t.Errorf("reloaded settings = %+v, want %+v", got, expected) } } func TestSaveSettingsNormalizesValues(t *testing.T) { t.Setenv("XDG_CONFIG_HOME", t.TempDir()) a := NewApp() a.Startup(context.Background()) input := settings.Settings{ ReadingSpeed: 2000, FontSize: 10, Theme: "invalid", } if err := a.SaveSettings(input); err != nil { t.Fatalf("SaveSettings() error = %v", err) } expected := settings.Settings{ ReadingSpeed: settings.MaxReadingSpeed, FontSize: settings.MinFontSize, Theme: settings.ThemeDark, } if got := a.GetSettings(); got != expected { t.Errorf("settings = %+v, want %+v", got, expected) } } func TestResetSettings(t *testing.T) { t.Setenv("XDG_CONFIG_HOME", t.TempDir()) a := NewApp() a.Startup(context.Background()) custom := settings.Settings{ ReadingSpeed: 600, FontSize: 72, Theme: settings.ThemeLight, } if err := a.SaveSettings(custom); err != nil { t.Fatalf("SaveSettings() error = %v", err) } got, err := a.ResetSettings() if err != nil { t.Fatalf("ResetSettings() error = %v", err) } if got != settings.Default() { t.Errorf("ResetSettings() = %+v, want %+v", got, settings.Default()) } if current := a.GetSettings(); current != settings.Default() { t.Errorf("current settings = %+v, want defaults %+v", current, settings.Default()) } }