vcom/internal/config/config_test.go
vrubelroman 28fd86b8d5 feat(config): non-fatal loading with warnings and in-place SetKey (2C.2)
Load finds the file through paths (no ./vcom.toml), returns defaults plus an
error for a broken file instead of exiting, warns about unknown and
deprecated keys (strict second pass), resets invalid values with a warning,
and aliases center_width_percent to ui.preview_width_percent. New keys from
design §9.2 get defaults. SetKey edits one line keeping comments, order and
unknown keys, appends missing keys/sections, creates the XDG file, refuses
a file that does not parse and verifies the result before an atomic write.
The old UI shows config errors in the status line and survives an unknown
theme name; the reference vcom.toml is documented and current.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 23:35:23 +03:00

148 lines
4 KiB
Go

package config
import (
"os"
"path/filepath"
"strings"
"testing"
"time"
)
func TestDefaultsValid(t *testing.T) {
c := Default()
if w := c.Validate(); len(w) != 0 {
t.Fatalf("defaults produce warnings: %v", w)
}
// New keys from design §9.2.
if !c.Behavior.ConfirmQuit || c.Transfer.OnConflict != "ask" || !c.Transfer.Preserve ||
c.Transfer.Fsync != "replace" || c.Transfer.MaxParallelJobs != 2 || c.Preview.ImageBackend != "auto" ||
c.Preview.MaxRemoteImageBytes != 20<<20 || c.SSH.KeepaliveInterval.Duration != 15*time.Second ||
c.SSH.ConnectTimeout.Duration != 20*time.Second || c.UI.PreviewWidthPercent != 50 {
t.Fatalf("defaults: %+v", c)
}
}
func TestReferenceConfigParsesClean(t *testing.T) {
data, err := os.ReadFile("../../vcom.toml")
if err != nil {
t.Fatal(err)
}
c, warns, err := Parse(data)
if err != nil {
t.Fatal(err)
}
if len(warns) != 0 {
t.Fatalf("reference vcom.toml produces warnings: %v", warns)
}
if c.UI.Theme != "github-dark" || c.SSH.ConnectTimeout.Duration != 20*time.Second {
t.Fatalf("parsed %+v", c)
}
}
func TestLoadUnknownKeysWarns(t *testing.T) {
data := []byte(`
[ui]
theme = "nord"
show_title_bar = true
app_title = "x"
center_width_percent = 40
mystery = 1
[behavior]
auto_refresh = true
auto_refresh_interval = 5
[nosuchsection]
k = "v"
[ssh]
connect_timeout = "7s"
`)
c, warns, err := Parse(data)
if err != nil {
t.Fatal(err)
}
joined := strings.Join(warns, "\n")
for _, want := range []string{
"unknown key ui.mystery",
"unknown key nosuchsection",
"ui.show_title_bar",
"ui.app_title",
"ui.center_width_percent is renamed to ui.preview_width_percent",
"behavior.auto_refresh is not implemented",
"behavior.auto_refresh_interval",
} {
if !strings.Contains(joined, want) {
t.Errorf("missing warning %q in:\n%s", want, joined)
}
}
if c.UI.Theme != "nord" || c.SSH.ConnectTimeout.Duration != 7*time.Second {
t.Fatalf("known keys not applied: %+v", c)
}
if c.UI.PreviewWidthPercent != 40 {
t.Fatalf("alias center_width_percent not applied: %d", c.UI.PreviewWidthPercent)
}
// The new name wins over the alias.
c, _, _ = Parse([]byte("[ui]\ncenter_width_percent = 40\npreview_width_percent = 60\n"))
if c.UI.PreviewWidthPercent != 60 {
t.Fatalf("preview_width_percent = %d", c.UI.PreviewWidthPercent)
}
}
func TestLoadInvalidValueUsesDefault(t *testing.T) {
c, warns, err := Parse([]byte("[ui]\npane_gap = 9\nicon_mode = 'weird'\n[transfer]\nfsync = 'sometimes'\n"))
if err != nil {
t.Fatal(err)
}
d := Default()
if c.UI.PaneGap != d.UI.PaneGap || c.UI.IconMode != "auto" || c.Transfer.Fsync != "replace" {
t.Fatalf("invalid values not reset: %+v", c)
}
if len(warns) != 3 {
t.Fatalf("warnings: %v", warns)
}
}
func TestLoadBrokenConfigFallsBackToDefaults(t *testing.T) {
dir := t.TempDir()
p := filepath.Join(dir, "vcom.toml")
if err := os.WriteFile(p, []byte("[ui]\ntheme = \"nord\"\nborder = \n"), 0o644); err != nil {
t.Fatal(err)
}
c, info, err := Load(p)
if err == nil {
t.Fatal("broken config must return an error")
}
if !strings.Contains(err.Error(), "line 3") {
t.Errorf("error should point at the line: %v", err)
}
if c != Default() {
t.Fatalf("got %+v, want defaults", c)
}
if !info.Found || info.Path != p {
t.Fatalf("info %+v", info)
}
// A missing explicit file: defaults, no error.
c, info, err = Load(filepath.Join(dir, "none.toml"))
if err != nil || info.Found || c != Default() {
t.Fatalf("missing file: %+v %+v %v", c, info, err)
}
}
func TestLoadIgnoresCwdConfig(t *testing.T) {
cwd := t.TempDir()
if err := os.WriteFile(filepath.Join(cwd, "vcom.toml"), []byte("[ui]\ntheme = 'nord'\n"), 0o644); err != nil {
t.Fatal(err)
}
t.Chdir(cwd)
t.Setenv("HOME", t.TempDir())
t.Setenv("XDG_CONFIG_HOME", "")
t.Setenv("VCOM_CONFIG", "")
c, info, err := Load("")
if err != nil || info.Found || c.UI.Theme == "nord" {
t.Fatalf("cwd config was used: %+v %+v %v", c.UI, info, err)
}
if len(info.Warnings) != 1 || !strings.Contains(info.Warnings[0], "not loaded") {
t.Fatalf("expected a hint about ./vcom.toml, got %v", info.Warnings)
}
}