vcom/internal/ui/config_smoke_test.go
vrubelroman 0f9314c12a feat(logx,main): slog log in the state dir; signal-safe teardown (2C.4)
logx writes slog text records to $XDG_STATE_HOME/vcom/vcom.log (0600, dir
0700), rotates once on start above 5 MiB, takes the level from --debug,
--log-level or VCOM_LOG (default warn) and redacts password/passphrase/
answers attributes. The old UI's log.Printf calls go through an slog
adapter at debug level instead of a new /tmp file per run. main creates
the run dir (sweeping dead ones), quits on SIGHUP/SIGTERM via
program.Send, and after Program.Run always saves the session through the
idempotent Model.Shutdown and removes the run dir.

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

93 lines
2.8 KiB
Go

package ui
import (
"os"
"path/filepath"
"strings"
"testing"
"time"
"vcom/internal/config"
"vcom/internal/testutil/teadriver"
)
// Choosing a theme rewrites only the theme line of the user's config (2C.3):
// comments, unknown keys and order survive.
func TestSmokeThemeSelectKeepsConfigComments(t *testing.T) {
isolateEnv(t)
dir := t.TempDir()
cfgPath := filepath.Join(dir, "vcom.toml")
before := "# my settings\n[ui]\n# the look\ntheme = 'catppuccin-mocha' # favourite\nicon_mode = 'ascii'\n\n[behavior]\nauto_refresh = true # unknown, keep\n"
writeFile(t, cfgPath, before)
cfg, info, err := config.Load(cfgPath)
if err != nil || !info.Found {
t.Fatalf("load: %v %+v", err, info)
}
cfg.Startup.LeftPath, cfg.Startup.RightPath = dir, dir
m, err := NewModel(cfg, info.Path)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { m.cleanupArchiveMounts(); m.cleanupRemoteImageTemp() })
d := teadriver.New(t, m, 160, 40, teadriver.WithLongCmd(300*time.Millisecond))
d.Settle(settle)
d.Press("t", "j", "enter")
d.Settle(settle)
chosen := cur(t, d).cfg.UI.Theme
if chosen == "catppuccin-mocha" {
t.Fatal("theme did not change")
}
b, err := os.ReadFile(cfgPath)
if err != nil {
t.Fatal(err)
}
want := strings.Replace(before, "theme = 'catppuccin-mocha'", "theme = '"+chosen+"'", 1)
if string(b) != want {
t.Fatalf("config rewritten:\n%s\nwant:\n%s", b, want)
}
}
// The session goes to $XDG_STATE_HOME/vcom/session.toml.
func TestSmokeSessionInStateDir(t *testing.T) {
dir := t.TempDir()
d := newDriver(t, dir, dir)
m := cur(t, d)
m.saveSession()
p := filepath.Join(os.Getenv("XDG_STATE_HOME"), "vcom", "session.toml")
if _, err := os.Stat(p); err != nil {
t.Fatalf("session not in state dir: %v", err)
}
if _, err := os.Stat(filepath.Join(os.Getenv("XDG_CONFIG_HOME"), "vcom", "session.toml")); !os.IsNotExist(err) {
t.Fatal("session written to the old config location")
}
}
// main calls Shutdown on the final model after Program.Run (signal, panic):
// it saves the session once and is idempotent after the quit key.
func TestSmokeShutdownSavesSessionOnce(t *testing.T) {
dir := t.TempDir()
d := newDriver(t, dir, dir)
d.Press("q")
d.Settle(settle)
if !d.Quit() {
t.Fatal("q did not quit")
}
p := filepath.Join(os.Getenv("XDG_STATE_HOME"), "vcom", "session.toml")
if err := os.Remove(p); err != nil {
t.Fatalf("quit key did not save the session: %v", err)
}
m := cur(t, d)
m.Shutdown() // already shut down by q: must not save again
if _, err := os.Stat(p); !os.IsNotExist(err) {
t.Fatal("Shutdown ran twice")
}
fresh := newTestModel(t, dir, dir) // re-isolates the environment
fresh.Shutdown()
p = filepath.Join(os.Getenv("XDG_STATE_HOME"), "vcom", "session.toml")
if _, err := os.Stat(p); err != nil {
t.Fatalf("Shutdown on a live model must save the session: %v", err)
}
}