// SPDX-License-Identifier: GPL-3.0-or-later package model import ( "os" "path/filepath" "testing" ) // sandboxHome points HOME and every XDG_* at a temporary directory, as the // other tests here do. func sandboxHome(t *testing.T) string { t.Helper() h := t.TempDir() t.Setenv("HOME", h) for _, v := range []string{"XDG_CONFIG_HOME", "XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME"} { t.Setenv(v, "") } return h } // TestPrefsRoundTrip: what is saved comes back, and the file lands beside // krino.conf. func TestPrefsRoundTrip(t *testing.T) { h := sandboxHome(t) p := DefaultPrefs() p.PreviewHeight = 420 p.Colours = false p.SelectAll = false if err := p.Save(); err != nil { t.Fatal(err) } if got := LoadPrefs(); got != p { t.Errorf("loaded %+v, want %+v", got, p) } want := filepath.Join(h, ".config", "krino", "gui.json") if _, err := os.Stat(want); err != nil { t.Errorf("not written to %s: %v", want, err) } } // TestPrefsWithoutAFile: the first run has no file, and damaged text is not // worth refusing to open a window over. func TestPrefsWithoutAFile(t *testing.T) { sandboxHome(t) if got := LoadPrefs(); got != DefaultPrefs() { t.Errorf("with no file: %+v, want the defaults", got) } if err := os.MkdirAll(filepath.Dir(PrefsFile()), 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(PrefsFile(), []byte("{not json"), 0o644); err != nil { t.Fatal(err) } if got := LoadPrefs(); got != DefaultPrefs() { t.Errorf("with damaged text: %+v, want the defaults", got) } } // TestWithDisplayKeepsTheDividers: the Settings window used to compose a // whole Prefs from its own controls, which wrote the remembered pane // positions back as zeros - so dragging the dividers and then ticking any // checkbox in Settings threw the positions away and the next window opened // at the defaults. func TestWithDisplayKeepsTheDividers(t *testing.T) { p := Prefs{ PreviewWidth: 640, ListWidth: 800, ListHeight: 300, PreviewHeight: 281, Sort: SortName, ShowSize: true, ShowAge: true, ShowRule: true, Colours: true, Preview: true, SelectAll: true, Layout: LayoutSide, } d := p.DisplayOf() d.ShowRule = false d.Sort = SortSize got := p.WithDisplay(d) if got.PreviewWidth != 640 || got.ListWidth != 800 || got.ListHeight != 300 { t.Errorf("the dividers were disturbed: %+v", got) } if got.ShowRule || got.Sort != SortSize { t.Errorf("the change did not take: %+v", got) } if !got.ShowSize || !got.ShowAge || !got.Colours || !got.Preview || !got.SelectAll { t.Errorf("something else changed: %+v", got) } }