Enter on a file (archives still go to 6.5): an executable opens a Run / Edit / View menu and never runs by itself (U-P1-3, N-P0-2; Run pauses for Enter afterwards); text goes to $VISUAL/$EDITOR (parsed with quotes); other files go to xdg-open, reaped in the background (no zombies: U-P1-15). A file on SSH (or in an archive) is downloaded to the run dir; edits are uploaded back via WriteAtomic only if the copy changed, and if the server file changed meanwhile the user chooses overwrite / save a copy beside / keep the local copy (U-P1-12). p mirrors with a reference per mount (U-P1-14, N-P1-12); S sorts (saved to the config); y copies paths. The session goes through internal/state: restored at start (remote and archive panels as their local place), saved every 30 s when changed, on SIGHUP (tui.Signal) and in main's teardown (App.Shutdown). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
383 lines
12 KiB
Go
383 lines
12 KiB
Go
package tui
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"vcom/internal/state"
|
|
"vcom/internal/testutil/fstree"
|
|
"vcom/internal/tui/modal"
|
|
"vcom/internal/tui/panel"
|
|
"vcom/internal/vfs"
|
|
"vcom/internal/vfs/localfs"
|
|
)
|
|
|
|
// execRec runs foreground programs synchronously and records them.
|
|
type execRec struct {
|
|
mu sync.Mutex
|
|
cmds [][]string
|
|
}
|
|
|
|
func (r *execRec) opt(d *Deps) {
|
|
d.Exec = func(c *exec.Cmd, done tea.ExecCallback) tea.Cmd {
|
|
r.mu.Lock()
|
|
r.cmds = append(r.cmds, c.Args)
|
|
r.mu.Unlock()
|
|
return func() tea.Msg { return done(c.Run()) }
|
|
}
|
|
}
|
|
|
|
func (r *execRec) list() [][]string {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
return append([][]string(nil), r.cmds...)
|
|
}
|
|
|
|
func script(t *testing.T, body string) string {
|
|
p := filepath.Join(t.TempDir(), "prog")
|
|
if err := os.WriteFile(p, []byte("#!/bin/sh\n"+body+"\n"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return p
|
|
}
|
|
|
|
func noSave(d *Deps) { d.SaveSession = func(state.Session) error { return nil } }
|
|
|
|
func TestEnterExecutableOpensRunMenu(t *testing.T) {
|
|
dir := tree(t, fstree.Spec{fstree.File("deploy.sh", "#!/bin/sh\necho ran > \"$(dirname \"$0\")/ran.txt\"\n").WithMode(0o755)})
|
|
rec := &execRec{}
|
|
h := newOps(t, localfs.New(), dir, dir, rec.opt, noSave)
|
|
h.cursorTo("deploy.sh")
|
|
h.press("enter")
|
|
l, ok := h.app.modals.Top().(*modal.List)
|
|
if !ok || !strings.Contains(l.Title, "executable") {
|
|
t.Fatalf("no run menu: %T", h.app.modals.Top())
|
|
}
|
|
if len(rec.list()) != 0 || exists(filepath.Join(dir, "ran.txt")) {
|
|
t.Fatal("Enter ran the executable (U-P1-3, N-P0-2)")
|
|
}
|
|
h.press("esc")
|
|
if exists(filepath.Join(dir, "ran.txt")) {
|
|
t.Fatal("ran after Esc")
|
|
}
|
|
h.press("enter", "enter") // Run
|
|
h.idle()
|
|
cmds := rec.list()
|
|
if len(cmds) != 1 || cmds[0][0] != "sh" || cmds[0][len(cmds[0])-1] != filepath.Join(dir, "deploy.sh") {
|
|
t.Fatalf("run command %v", cmds)
|
|
}
|
|
}
|
|
|
|
func TestNoZombieAfterOpen(t *testing.T) {
|
|
dir := tree(t, fstree.Spec{fstree.File("pic.bin", "\x00\x01\x02binary")})
|
|
pidFile := filepath.Join(t.TempDir(), "pid")
|
|
opener := script(t, "echo $$ > "+pidFile)
|
|
h := newOps(t, localfs.New(), dir, dir, noSave, func(d *Deps) { d.Opener = []string{opener} })
|
|
h.cursorTo("pic.bin")
|
|
h.press("enter")
|
|
h.idle()
|
|
if !h.d.ScreenContains("Opened pic.bin") {
|
|
t.Fatalf("not opened:\n%s", h.d.Screen())
|
|
}
|
|
var pid string
|
|
for i := 0; i < 100 && pid == ""; i++ {
|
|
b, _ := os.ReadFile(pidFile)
|
|
pid = strings.TrimSpace(string(b))
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
if pid == "" {
|
|
t.Fatal("opener did not run")
|
|
}
|
|
deadline := time.Now().Add(2 * time.Second)
|
|
for {
|
|
stat, err := os.ReadFile("/proc/" + pid + "/stat")
|
|
if err != nil {
|
|
return // reaped
|
|
}
|
|
if fields := strings.Fields(string(stat)); len(fields) > 2 && fields[2] != "Z" {
|
|
// still running: wait
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatalf("opener %s left as %q (zombie; U-P1-15)", pid, strings.Fields(string(stat))[2])
|
|
}
|
|
time.Sleep(20 * time.Millisecond)
|
|
}
|
|
}
|
|
|
|
func TestEditLocalRunsEditor(t *testing.T) {
|
|
dir := tree(t, fstree.Spec{fstree.File("notes.txt", "hello\n")})
|
|
rec := &execRec{}
|
|
editor := script(t, `for a; do f=$a; done; echo edited >> "$f"`) // edits its last argument
|
|
h := newOps(t, localfs.New(), dir, dir, rec.opt, noSave, func(d *Deps) {
|
|
d.Getenv = func(k string) string {
|
|
if k == "EDITOR" {
|
|
return `"` + editor + `" --wait`
|
|
}
|
|
return ""
|
|
}
|
|
})
|
|
h.cursorTo("notes.txt")
|
|
h.press("enter") // text → editor
|
|
h.idle()
|
|
cmds := rec.list()
|
|
if len(cmds) != 1 || cmds[0][0] != editor || cmds[0][1] != "--wait" {
|
|
t.Fatalf("editor command %v", cmds)
|
|
}
|
|
if read(t, filepath.Join(dir, "notes.txt")) != "hello\nedited\n" {
|
|
t.Fatal("editor did not get the file")
|
|
}
|
|
h.press("f4")
|
|
h.idle()
|
|
if n := len(rec.list()); n != 2 {
|
|
t.Fatalf("F4: %d commands", n)
|
|
}
|
|
}
|
|
|
|
// remoteLike wraps the local FS as a remote one (no exec, no trash).
|
|
type remoteLike struct{ vfs.FS }
|
|
|
|
func (remoteLike) Kind() vfs.Kind { return vfs.KindSFTP }
|
|
func (remoteLike) Volume() vfs.VolumeID { return "ssh:fake" }
|
|
func (remoteLike) Label() string { return "bob@fake" }
|
|
func (r remoteLike) Caps() vfs.Caps {
|
|
c := r.FS.Caps()
|
|
c.Exec, c.Trash = false, false
|
|
return c
|
|
}
|
|
|
|
func editorEnv(editor string) opt {
|
|
return func(d *Deps) {
|
|
d.Getenv = func(k string) string {
|
|
if k == "EDITOR" {
|
|
return editor
|
|
}
|
|
return ""
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRemoteEditUploadsBack(t *testing.T) {
|
|
ldir := t.TempDir()
|
|
rdir := tree(t, fstree.Spec{fstree.File("conf.ini", "a=1\n").WithMode(0o640)})
|
|
run := t.TempDir()
|
|
rec := &execRec{}
|
|
h := newOps(t, localfs.New(), ldir, ldir, rec.opt, noSave, editorEnv(script(t, `echo b=2 >> "$1"`)),
|
|
func(d *Deps) { d.RunDir = run })
|
|
h.mountRemote(1, remoteLike{localfs.New()}, rdir)
|
|
h.press("tab")
|
|
h.cursorTo("conf.ini")
|
|
h.press("enter")
|
|
h.idle()
|
|
if got := read(t, filepath.Join(rdir, "conf.ini")); got != "a=1\nb=2\n" {
|
|
t.Fatalf("remote content %q (N-P0-2: edit must upload back)", got)
|
|
}
|
|
if fi, _ := os.Stat(filepath.Join(rdir, "conf.ini")); fi.Mode().Perm() != 0o640 {
|
|
t.Fatalf("mode %v", fi.Mode())
|
|
}
|
|
if cmds := rec.list(); len(cmds) != 1 || !strings.HasPrefix(cmds[0][1], run) {
|
|
t.Fatalf("editor not on a run-dir copy: %v", cmds)
|
|
}
|
|
if ents, _ := os.ReadDir(run); len(ents) != 0 {
|
|
t.Fatalf("temp copy left: %v", ents)
|
|
}
|
|
noParts(t, rdir)
|
|
if !h.d.ScreenContains("Uploaded conf.ini") {
|
|
t.Fatalf("no note:\n%s", h.d.Screen())
|
|
}
|
|
}
|
|
|
|
func TestRemoteEditConflictAsks(t *testing.T) {
|
|
ldir := t.TempDir()
|
|
rdir := tree(t, fstree.Spec{fstree.File("conf.ini", "a=1\n")})
|
|
remote := filepath.Join(rdir, "conf.ini")
|
|
// The editor edits the copy while someone changes the server file.
|
|
editor := script(t, `echo mine >> "$1"; sleep 0.01; echo theirs >> "`+remote+`"; touch -d '2001-01-01' "`+remote+`"`)
|
|
h := newOps(t, localfs.New(), ldir, ldir, (&execRec{}).opt, noSave, editorEnv(editor), func(d *Deps) { d.RunDir = t.TempDir() })
|
|
h.mountRemote(1, remoteLike{localfs.New()}, rdir)
|
|
h.press("tab")
|
|
h.cursorTo("conf.ini")
|
|
h.press("f4")
|
|
d := h.waitDialog("changed on the server")
|
|
if d.Buttons[d.Focused()].Label != "Save a copy beside" {
|
|
t.Fatalf("default %q", d.Buttons[d.Focused()].Label)
|
|
}
|
|
h.press("enter")
|
|
h.idle()
|
|
if got := read(t, remote); got != "a=1\ntheirs\n" {
|
|
t.Fatalf("server version overwritten: %q (U-P1-12)", got)
|
|
}
|
|
if got := read(t, filepath.Join(rdir, "conf (1).ini")); got != "a=1\nmine\n" {
|
|
t.Fatalf("copy beside %q", got)
|
|
}
|
|
}
|
|
|
|
func TestSortMenuAndYank(t *testing.T) {
|
|
dir := tree(t, fstree.Spec{fstree.File("a", "1"), fstree.File("b", "333"), fstree.File("c", "22")})
|
|
var saved []string
|
|
var clip atomic.Value
|
|
h := newOps(t, localfs.New(), dir, dir, noSave, func(d *Deps) {
|
|
d.SetConfigKey = func(_ string, section, key string, v any) (string, error) {
|
|
saved = append(saved, section+"."+key+"="+strconv.Quote(strings.TrimSpace(strings.ReplaceAll(toString(v), "\n", ""))))
|
|
return "x", nil
|
|
}
|
|
d.Clipboard = func(s string) error { clip.Store(s); return nil }
|
|
})
|
|
h.press("S", "j", "enter") // Size
|
|
h.idle()
|
|
var names []string
|
|
p := h.app.Active()
|
|
for i := 0; i < p.VisibleLen(); i++ {
|
|
if e, _ := p.Visible(i); !e.Parent {
|
|
names = append(names, e.Name)
|
|
}
|
|
}
|
|
if strings.Join(names, "") != "acb" {
|
|
t.Fatalf("sorted by size: %v", names)
|
|
}
|
|
if len(saved) != 1 || !strings.HasPrefix(saved[0], "browser.sort.by=") {
|
|
t.Fatalf("saved %v", saved)
|
|
}
|
|
h.cursorTo("b")
|
|
h.press("y")
|
|
h.idle()
|
|
if clip.Load() != filepath.Join(dir, "b") {
|
|
t.Fatalf("clipboard %v", clip.Load())
|
|
}
|
|
}
|
|
|
|
func toString(v any) string {
|
|
switch x := v.(type) {
|
|
case string:
|
|
return x
|
|
case bool:
|
|
return strconv.FormatBool(x)
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// U-P1-14, N-P1-12: after mirror, leaving the archive (or the host) in the
|
|
// active panel leaves the mirrored panel working.
|
|
func TestUP114_MirrorArchiveAndRemote(t *testing.T) {
|
|
dir := archiveDir(t)
|
|
h := newOps(t, localfs.New(), dir, dir, noSave)
|
|
h.enterArchive("box.zip")
|
|
h.press("p")
|
|
if !inArchive(h.app.Passive()) {
|
|
t.Fatal("mirror did not enter the archive in the other panel")
|
|
}
|
|
h.press("backspace") // leave the archive in the active panel
|
|
if inArchive(h.app.Active()) || !inArchive(h.app.Passive()) {
|
|
t.Fatal("panels wrong after leaving")
|
|
}
|
|
h.press("tab")
|
|
h.cursorTo("b.txt")
|
|
h.press("f3")
|
|
if f, err := h.app.Active().FS().Open(t.Context(), "/b.txt"); err != nil {
|
|
t.Fatalf("mirrored archive dead: %v", err)
|
|
} else {
|
|
f.Close()
|
|
}
|
|
|
|
// Remote: every mount holds its own reference.
|
|
ldir := t.TempDir()
|
|
rdir := tree(t, fstree.Spec{fstree.File("r.txt", "r")})
|
|
h2 := newOps(t, localfs.New(), ldir, ldir, noSave)
|
|
var refs atomic.Int32
|
|
rfs := standFS(t)
|
|
p := h2.app.Panel(0)
|
|
ret := p.Location()
|
|
refs.Store(1)
|
|
m := panel.NewMount(rfs, "/", &ret, "", func() { refs.Add(-1) }, func() func() {
|
|
refs.Add(1)
|
|
return func() { refs.Add(-1) }
|
|
})
|
|
h2.d.Send(h2.app.navigate(0, panel.Target{Mounts: append(p.Here().Mounts, m), Path: rdir}, "", panel.NavPush)())
|
|
h2.d.Settle(settle)
|
|
h2.press("p")
|
|
if refs.Load() != 2 {
|
|
t.Fatalf("refs after mirror = %d", refs.Load())
|
|
}
|
|
h2.press("backspace") // active leaves the host (its history may keep its reference)
|
|
h2.idle()
|
|
if refs.Load() < 1 {
|
|
t.Fatalf("refs after leaving = %d: the mirrored panel lost its reference", refs.Load())
|
|
}
|
|
if _, err := h2.app.Passive().FS().ReadDir(t.Context(), rdir); err != nil {
|
|
t.Fatalf("mirrored remote panel dead: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestSessionRestoresLocalForRemote(t *testing.T) {
|
|
ldir := tree(t, fstree.Spec{fstree.File("pick", ""), fstree.File("z", "")})
|
|
rdir := tree(t, fstree.Spec{fstree.File("r.txt", "")})
|
|
var got state.Session
|
|
h := newOps(t, localfs.New(), ldir, ldir, func(d *Deps) { d.SaveSession = func(s state.Session) error { got = s; return nil } })
|
|
h.press("tab")
|
|
h.cursorTo("pick")
|
|
h.mountRemote(1, remoteLike{localfs.New()}, rdir)
|
|
if err := h.app.Shutdown(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Right.Path != ldir || got.Right.EntryName != "" && got.Right.EntryName != "pick" || got.ActivePane != "right" {
|
|
t.Fatalf("session %+v: a remote panel must be saved as its local place", got)
|
|
}
|
|
// Restore: start dirs from the session, active pane and cursor.
|
|
got.Right.EntryName = "pick"
|
|
h2 := newOps(t, localfs.New(), got.Left.Path, got.Right.Path, noSave, func(d *Deps) { d.Session = &got })
|
|
if h2.app.active != 1 || h2.app.Panel(1).Path() != ldir {
|
|
t.Fatalf("restored active %d path %s", h2.app.active, h2.app.Panel(1).Path())
|
|
}
|
|
if e, _ := h2.app.Panel(1).Selected(); e.Name != "pick" {
|
|
t.Fatalf("restored cursor on %q", e.Name)
|
|
}
|
|
}
|
|
|
|
func TestSignalSavesSession(t *testing.T) {
|
|
dir := tree(t, fstree.Spec{fstree.File("f", "")})
|
|
saved := make(chan state.Session, 1)
|
|
h := newOps(t, localfs.New(), dir, dir, func(d *Deps) { d.SaveSession = func(s state.Session) error { saved <- s; return nil } })
|
|
h.d.Send(Signal())
|
|
h.d.Settle(settle)
|
|
select {
|
|
case s := <-saved:
|
|
if s.Left.Path != dir {
|
|
t.Fatalf("saved %+v", s)
|
|
}
|
|
default:
|
|
t.Fatal("session not saved on signal")
|
|
}
|
|
if !h.d.Quit() {
|
|
t.Fatal("did not quit on signal")
|
|
}
|
|
if err := h.app.Shutdown(); err != nil || len(saved) != 0 {
|
|
t.Fatal("teardown saved twice")
|
|
}
|
|
}
|
|
|
|
func TestShellSplit(t *testing.T) {
|
|
cases := map[string][]string{
|
|
`code --wait`: {"code", "--wait"},
|
|
`"/opt/my editor/bin" -n`: {"/opt/my editor/bin", "-n"},
|
|
`vim -c 'set ft=go'`: {"vim", "-c", "set ft=go"},
|
|
`emacs\ client -t`: {"emacs client", "-t"},
|
|
`nano "a \"q\" b"`: {"nano", `a "q" b`},
|
|
}
|
|
for in, want := range cases {
|
|
got, err := shellSplit(in)
|
|
if err != nil || strings.Join(got, "|") != strings.Join(want, "|") {
|
|
t.Errorf("shellSplit(%q) = %q, %v", in, got, err)
|
|
}
|
|
}
|
|
if _, err := shellSplit(`vim "x`); err == nil {
|
|
t.Error("unterminated quote accepted")
|
|
}
|
|
}
|