state.Save writes session.toml atomically (0600, state dir 0700), drops cursor-memory entries under cache/temp mounts and caps each pane at 200 entries; Load migrates the old ~/.config/vcom/session.toml once. The old UI's session functions are aliases over state, and theme selection uses config.SetKey, so the user's comments and keys survive. config.Save is gone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
121 lines
3.6 KiB
Go
121 lines
3.6 KiB
Go
package state
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
// isolate points HOME and the XDG dirs at a temp dir.
|
|
func isolate(t *testing.T) (home string) {
|
|
home = t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
|
|
t.Setenv("XDG_STATE_HOME", filepath.Join(home, ".local", "state"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
|
|
t.Setenv("SUDO_USER", "")
|
|
return home
|
|
}
|
|
|
|
func TestSessionAtomic(t *testing.T) {
|
|
home := isolate(t)
|
|
s := Session{ActivePane: "right", Left: PaneSession{Path: "/a", EntryName: "x"}}
|
|
if err := Save(s); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dir := filepath.Join(home, ".local", "state", "vcom")
|
|
p := filepath.Join(dir, "session.toml")
|
|
if fi, err := os.Stat(p); err != nil || fi.Mode().Perm() != 0o600 {
|
|
t.Fatalf("session file: %v %v", fi, err)
|
|
}
|
|
if fi, _ := os.Stat(dir); fi.Mode().Perm() != 0o700 {
|
|
t.Fatalf("state dir mode %v", fi.Mode().Perm())
|
|
}
|
|
got, err := Load()
|
|
if err != nil || got.ActivePane != "right" || got.Left.EntryName != "x" {
|
|
t.Fatalf("round trip: %+v %v", got, err)
|
|
}
|
|
|
|
// A failing write leaves the previous session intact and no temp files.
|
|
if os.Geteuid() != 0 {
|
|
before, _ := os.ReadFile(p)
|
|
if err := os.Chmod(dir, 0o500); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = os.Chmod(dir, 0o700) })
|
|
if err := Save(Session{ActivePane: "left"}); err == nil {
|
|
t.Fatal("save into a read-only dir succeeded")
|
|
}
|
|
_ = os.Chmod(dir, 0o700)
|
|
after, _ := os.ReadFile(p)
|
|
if string(before) != string(after) {
|
|
t.Fatal("old session damaged by a failed save")
|
|
}
|
|
if ents, _ := os.ReadDir(dir); len(ents) != 1 {
|
|
t.Fatalf("leftovers in state dir: %v", ents)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSessionMigratesFromOldPath(t *testing.T) {
|
|
home := isolate(t)
|
|
old := filepath.Join(home, ".config", "vcom", "session.toml")
|
|
if err := os.MkdirAll(filepath.Dir(old), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
content := "active_pane = 'right'\n[left]\npath = '/srv'\nentry_name = 'data'\n[left.cursor_memory]\n'/srv' = 'data'\n"
|
|
if err := os.WriteFile(old, []byte(content), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s, err := Load()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if s.ActivePane != "right" || s.Left.Path != "/srv" || s.Left.CursorMemory["/srv"] != "data" {
|
|
t.Fatalf("migrated %+v", s)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(home, ".local", "state", "vcom", "session.toml")); err != nil {
|
|
t.Fatalf("not written to the new location: %v", err)
|
|
}
|
|
if _, err := os.Stat(old); !os.IsNotExist(err) {
|
|
t.Fatal("old session file not removed after migration")
|
|
}
|
|
// Nothing anywhere: empty session, no error.
|
|
isolate(t)
|
|
if s, err := Load(); err != nil || s.ActivePane != "" {
|
|
t.Fatalf("empty: %+v %v", s, err)
|
|
}
|
|
}
|
|
|
|
func TestCursorMemoryCapped(t *testing.T) {
|
|
home := isolate(t)
|
|
mem := map[string]string{}
|
|
for i := range 500 {
|
|
mem[fmt.Sprintf("/data/d%03d", i)] = "f"
|
|
}
|
|
mem["/work/project/src"] = "main.go"
|
|
mem["/work"] = "project"
|
|
cache := filepath.Join(home, ".cache", "vcom", "run-1", "preview")
|
|
mem[cache] = "img.png"
|
|
mem[filepath.Join(os.TempDir(), "vcom-archive-123", "x")] = "y"
|
|
if err := Save(Session{Left: PaneSession{Path: "/work/project", CursorMemory: mem}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s, err := Load()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := s.Left.CursorMemory
|
|
if len(got) != MaxCursorMemory {
|
|
t.Fatalf("%d entries, want %d", len(got), MaxCursorMemory)
|
|
}
|
|
if got["/work/project/src"] != "main.go" || got["/work"] != "project" {
|
|
t.Fatal("entries on the current path must survive the cap")
|
|
}
|
|
for k := range got {
|
|
if k == cache || filepath.Dir(k) == filepath.Join(os.TempDir(), "vcom-archive-123") {
|
|
t.Fatalf("transient path kept: %s", k)
|
|
}
|
|
}
|
|
}
|