91 lines
2.9 KiB
Go
91 lines
2.9 KiB
Go
// Copyright (c) 2026 Storm Dragon
|
|
// Authors: Storm Dragon, Angus Kola
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestProgressSettingsPersistenceAndInheritance(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "settings.toml")
|
|
store, err := NewStore(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
global := DefaultSettings()
|
|
global.ProgressBar.OutputMode = "both"
|
|
global.ProgressBar.ReportBackground = true
|
|
if err := store.Save(global); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
application := global
|
|
application.ProgressBar.OutputMode = "off"
|
|
if err := store.SaveForApplication("Example Application", application); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
loaded, err := store.Load()
|
|
if err != nil || loaded.ProgressBar != global.ProgressBar {
|
|
t.Fatalf("global progress: %+v, %v", loaded.ProgressBar, err)
|
|
}
|
|
app, found, err := store.LoadForApplication("Example Application")
|
|
if err != nil || !found || app.ProgressBar != application.ProgressBar {
|
|
t.Fatalf("application progress: %+v, %v", app.ProgressBar, err)
|
|
}
|
|
global.ProgressBar.ReportBackground = false
|
|
global.ProgressBar.SpeechPercentageInterval = 5
|
|
if err := store.Save(global); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
app, _, err = store.LoadForApplication("Example Application")
|
|
if err != nil || app.ProgressBar.OutputMode != "off" || app.ProgressBar.ReportBackground || app.ProgressBar.SpeechPercentageInterval != 5 {
|
|
t.Fatalf("inherited progress: %+v, %v", app.ProgressBar, err)
|
|
}
|
|
}
|
|
|
|
func TestObsoleteProgressFieldsDoNotBreakExistingFiles(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "settings.toml")
|
|
contents := "schema-version = 2\nactive-profile = 'default'\n[profiles.default.sound]\nbeep-progress-updates = false\nprogress-beep-interval = 7\nplay-for-value = true\n"
|
|
if err := os.WriteFile(path, []byte(contents), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
store, err := NewStore(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
settings, err := store.Load()
|
|
if err != nil || settings.ProgressBar != DefaultProgressBarSettings() {
|
|
t.Fatalf("existing settings: %+v, %v", settings.ProgressBar, err)
|
|
}
|
|
if err := store.Save(settings); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, key := range []string{"beep-progress-updates", "progress-beep-interval", "play-for-value"} {
|
|
if strings.Contains(string(data), key) {
|
|
t.Fatalf("obsolete field %s was saved", key)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestInvalidProgressSettingsRejected(t *testing.T) {
|
|
for _, change := range []func(*ProgressBarSettings){
|
|
func(s *ProgressBarSettings) { s.OutputMode = "invalid" },
|
|
func(s *ProgressBarSettings) { s.BeepPercentageInterval = 0 },
|
|
func(s *ProgressBarSettings) { s.SpeechPercentageInterval = 101 },
|
|
} {
|
|
settings := DefaultSettings()
|
|
change(&settings.ProgressBar)
|
|
if Validate(settings) == nil {
|
|
t.Fatalf("invalid progress settings accepted: %+v", settings.ProgressBar)
|
|
}
|
|
}
|
|
}
|