vcom/internal/tui/ops_test.go
vrubelroman 066d3494c2 feat(tui): Enter dispatch, external programs, remote edit, mirror, sort, session (6.6)
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>
2026-11-01 14:10:52 +03:00

458 lines
12 KiB
Go

package tui
import (
"context"
"os"
"path/filepath"
"strings"
"sync/atomic"
"testing"
"time"
"vcom/internal/jobs"
"vcom/internal/state"
"vcom/internal/testutil/fstree"
"vcom/internal/tui/modal"
"vcom/internal/tui/panel"
"vcom/internal/vfs"
"vcom/internal/vfs/localfs"
"vcom/internal/vfs/vfstest"
)
// opsH is a harness with a real jobs.Manager whose events are pumped into
// the driver by the test.
type opsH struct {
*harness
mgr *jobs.Manager
ev chan jobs.Event
}
func newOps(t *testing.T, fsys vfs.FS, left, right string, opts ...opt) *opsH {
t.Helper()
ev := make(chan jobs.Event, 8192)
mgr := jobs.New(func(e jobs.Event) { ev <- e }, 2)
t.Cleanup(func() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_ = mgr.Shutdown(ctx)
})
opts = append([]opt{func(d *Deps) {
d.Jobs = mgr
d.StartDirs = [2]string{left, right}
d.SaveSession = func(state.Session) error { return nil } // never the real session file
}}, opts...)
return &opsH{harness: newHarness(t, fsys, left, opts...), mgr: mgr, ev: ev}
}
// pump delivers queued job events.
func (h *opsH) pump() bool {
got := false
for {
select {
case e := <-h.ev:
h.d.Send(JobEvent(e))
got = true
default:
if got {
h.d.Settle(settle)
h.check()
}
return got
}
}
}
// until pumps events until cond holds.
func (h *opsH) until(what string, cond func() bool) {
h.t.Helper()
deadline := time.Now().Add(10 * time.Second)
for !cond() {
if time.Now().After(deadline) {
h.t.Fatalf("timed out waiting for %s\n%s", what, h.d.Screen())
}
if !h.pump() {
time.Sleep(5 * time.Millisecond)
}
}
}
func (h *opsH) idle() {
h.t.Helper()
h.until("jobs to finish", func() bool { return h.mgr.Active() == 0 && len(h.ev) == 0 && len(h.app.ops.jobs) == 0 })
h.pump()
}
func (h *opsH) topDialog(title string) *modal.Dialog {
d, _ := h.app.modals.Top().(*modal.Dialog)
if d == nil || !strings.Contains(d.Title, title) {
return nil
}
return d
}
func (h *opsH) waitDialog(title string) *modal.Dialog {
h.t.Helper()
var d *modal.Dialog
h.until("dialog "+title, func() bool { d = h.topDialog(title); return d != nil })
return d
}
func read(t *testing.T, p string) string {
t.Helper()
b, err := os.ReadFile(p)
if err != nil {
t.Fatal(err)
}
return string(b)
}
func exists(p string) bool { _, err := os.Lstat(p); return err == nil }
func noParts(t *testing.T, dir string) {
t.Helper()
_ = filepath.Walk(dir, func(p string, fi os.FileInfo, err error) error {
if err == nil && strings.HasPrefix(fi.Name(), ".vcom-") {
t.Errorf("temp left: %s", p)
}
return nil
})
}
func TestCopyConflictDialogAndSkip(t *testing.T) {
left := tree(t, fstree.Spec{fstree.File("a.txt", "new"), fstree.File("b.txt", "b")})
right := tree(t, fstree.Spec{fstree.File("a.txt", "old")})
h := newOps(t, localfs.New(), left, right)
h.cursorTo("a.txt")
h.press("insert", "insert", "f5")
d := h.waitDialog("Copy 2 item(s)?")
if !strings.Contains(strings.Join(fieldsOf(d), "|"), "1 item(s) already exist") {
t.Fatalf("plan dialog fields %v", fieldsOf(d))
}
h.press("y") // copy, ask on conflicts
h.waitDialog("Target exists")
h.press("s")
h.idle()
if read(t, filepath.Join(right, "a.txt")) != "old" || read(t, filepath.Join(right, "b.txt")) != "b" {
t.Fatal("skip did not keep the target or b.txt not copied")
}
if !h.d.ScreenContains("1 skipped") {
t.Fatalf("summary missing:\n%s", h.d.Screen())
}
if h.app.modals.Len() != 0 {
t.Fatal("modals left open")
}
}
func fieldsOf(d *modal.Dialog) []string {
var out []string
for _, f := range d.Fields {
out = append(out, f.Label+": "+f.Value)
}
return out
}
// slowFS sleeps on every write (a slow disk or link).
func slowFS(d time.Duration) vfs.FS {
return &vfstest.FaultFS{FS: localfs.New(), Hook: func(op vfstest.Op, _ string) error {
if op == vfstest.OpWrite {
time.Sleep(d)
}
return nil
}}
}
func TestProgressCCAsksConfirm(t *testing.T) {
left := tree(t, fstree.Spec{fstree.File("big", "").WithSize(8 << 20)})
right := t.TempDir()
h := newOps(t, slowFS(5*time.Millisecond), left, right)
h.cursorTo("big")
h.press("f5")
h.waitDialog("Copy 1 item(s)?")
h.press("y")
if _, ok := h.app.modals.Top().(*modal.Progress); !ok {
t.Fatalf("no progress modal: %T", h.app.modals.Top())
}
h.press("c")
h.waitDialog("Cancel Copy big?")
h.press("c") // a second c is not a confirmation (U-P1-16)
if h.topDialog("Cancel") == nil || h.mgr.Active() != 1 {
t.Fatal("c c cancelled without y")
}
h.press("n") // continue
if _, ok := h.app.modals.Top().(*modal.Progress); !ok || h.mgr.Active() != 1 {
t.Fatal("job not running after Continue")
}
h.press("c", "y")
h.idle()
if exists(filepath.Join(right, "big")) {
t.Fatal("cancelled copy left the file")
}
noParts(t, right)
if !h.d.ScreenContains("cancelled") {
t.Fatalf("no cancel note:\n%s", h.d.Screen())
}
}
func TestDeleteDefaultTrash(t *testing.T) {
data := t.TempDir()
t.Setenv("XDG_DATA_HOME", data)
dir := tree(t, fstree.Spec{fstree.File("junk.txt", "x")})
h := newOps(t, localfs.New(), dir, dir)
h.cursorTo("junk.txt")
h.press("f8")
d := h.waitDialog("to the trash?")
if d.Buttons[d.Focused()].Label != "Move to trash" {
t.Fatalf("default button %q", d.Buttons[d.Focused()].Label)
}
h.press("enter")
h.idle()
if exists(filepath.Join(dir, "junk.txt")) || !exists(filepath.Join(data, "Trash", "files", "junk.txt")) {
t.Fatal("not moved to the trash")
}
for i := 0; i < h.app.Active().VisibleLen(); i++ {
if e, _ := h.app.Active().Visible(i); e.Name == "junk.txt" {
t.Fatal("panel not reloaded")
}
}
}
func TestPermanentDeleteDefaultsToCancel(t *testing.T) {
dir := tree(t, fstree.Spec{fstree.File("keep.txt", "x"), fstree.File("sub/f", "y")})
h := newOps(t, localfs.New(), dir, dir)
h.cursorTo("keep.txt")
h.press("X")
h.waitDialog("permanently")
h.press("enter")
h.idle()
if !exists(filepath.Join(dir, "keep.txt")) {
t.Fatal("Enter deleted permanently (default must be Cancel; U-P0-4)")
}
h.cursorTo("sub")
h.press("X")
h.waitDialog("permanently")
h.press("d")
h.idle()
if exists(filepath.Join(dir, "sub")) {
t.Fatal("not deleted")
}
}
// noTrash is a local FS that looks remote (no trash), like SFTP.
type noTrash struct{ vfs.FS }
func (noTrash) Kind() vfs.Kind { return vfs.KindSFTP }
func (noTrash) Volume() vfs.VolumeID { return "ssh:test" }
func (noTrash) Label() string { return "bob@host" }
func (n noTrash) Caps() vfs.Caps {
c := n.FS.Caps()
c.Trash = false
return c
}
func TestRemoteDeleteNoTrashShowsPermanentDialog(t *testing.T) {
dir := tree(t, fstree.Spec{fstree.File("r.txt", "x")})
h := newOps(t, noTrash{localfs.New()}, dir, dir)
h.cursorTo("r.txt")
h.press("f8")
d := h.waitDialog("permanently")
if !strings.Contains(strings.Join(d.Text, " "), "has no trash") || d.Buttons[d.Focused()].Label != "Cancel" {
t.Fatalf("dialog %+v focus %d", d.Text, d.Focused())
}
h.press("enter")
if !exists(filepath.Join(dir, "r.txt")) {
t.Fatal("deleted on Enter")
}
}
func TestTwoJobsParallel(t *testing.T) {
left := tree(t, fstree.Spec{fstree.File("f1", "").WithSize(256 << 10), fstree.File("f2", "").WithSize(256 << 10)})
right := t.TempDir()
g := newGate(t)
var blocked atomic.Int32
fsys := &vfstest.FaultFS{FS: localfs.New(), Hook: func(op vfstest.Op, _ string) error {
if op == vfstest.OpWrite {
blocked.Add(1)
<-g.ch
}
return nil
}}
h := newOps(t, fsys, left, right)
for _, f := range []string{"f1", "f2"} {
h.cursorTo(f)
h.press("f5")
h.waitDialog("Copy 1 item(s)?")
h.press("y", "b") // confirm, then send to the background
}
h.until("two running jobs", func() bool {
n := 0
for _, s := range h.mgr.Snapshot() {
if s.State == jobs.Running {
n++
}
}
return n == 2 && blocked.Load() >= 2
})
g.open()
h.idle()
if !exists(filepath.Join(right, "f1")) || !exists(filepath.Join(right, "f2")) {
t.Fatal("copies missing")
}
}
func TestQuitWithJobsCancelsAndCleans(t *testing.T) {
left := tree(t, fstree.Spec{fstree.File("big", "").WithSize(16 << 20)})
right := t.TempDir()
h := newOps(t, slowFS(5*time.Millisecond), left, right)
h.cursorTo("big")
h.press("f5")
h.waitDialog("Copy 1 item(s)?")
h.press("y", "b")
h.until("copy running", func() bool {
s := h.mgr.Snapshot()
return len(s) == 1 && s[0].Progress.BytesDone > 0
})
h.press("q")
h.waitDialog("Quit and cancel operations?")
h.press("q")
if !h.app.Quitting() {
t.Fatal("not quitting")
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := h.mgr.Shutdown(ctx); err != nil { // main's teardown
t.Fatal(err)
}
noParts(t, right) // N-P2-11
if exists(filepath.Join(right, "big")) {
t.Fatal("partial copy under the target name")
}
}
func TestHostsListNotATarget(t *testing.T) {
dir := tree(t, fstree.Spec{fstree.File("a", "x")})
h := newOps(t, localfs.New(), dir, dir)
h.app.Passive().Source = panel.SourceHosts
h.cursorTo("a")
h.press("f5")
if h.app.modals.Len() != 0 || h.mgr.Active() != 0 {
t.Fatal("copy started into the host list (N-P2-10)")
}
if !h.d.ScreenContains("not a directory") {
t.Fatalf("no explanation:\n%s", h.d.Screen())
}
}
func TestRenameRejectsSlash(t *testing.T) {
dir := tree(t, fstree.Spec{fstree.File("a.txt", "x"), fstree.Dir("sub")})
h := newOps(t, localfs.New(), dir, dir)
h.cursorTo("a.txt")
h.press("f2", "ctrl+u")
h.typ("sub/b.txt")
h.press("enter")
if _, ok := h.app.modals.Top().(*modal.Input); !ok {
t.Fatal("input closed on an invalid name")
}
if !h.d.ScreenContains(`must not contain "/"`) {
t.Fatalf("no error shown:\n%s", h.d.Screen())
}
h.press("esc")
if !exists(filepath.Join(dir, "a.txt")) || exists(filepath.Join(dir, "sub", "b.txt")) {
t.Fatal("file moved")
}
// A valid rename and mkdir land on the new name.
h.press("f2", "ctrl+u")
h.typ("c.txt")
h.press("enter")
h.idle()
if !exists(filepath.Join(dir, "c.txt")) || h.selected() != "c.txt" {
t.Fatalf("rename: selected %q", h.selected())
}
h.press("f7")
h.typ("новая")
h.press("enter")
h.idle()
if fi, err := os.Stat(filepath.Join(dir, "новая")); err != nil || !fi.IsDir() || h.selected() != "новая" {
t.Fatalf("mkdir: %v selected %q", err, h.selected())
}
}
func TestPassiveCursorKeptAfterCopy(t *testing.T) {
left := tree(t, fstree.Spec{fstree.File("a0", "new")})
right := tree(t, fstree.Spec{fstree.File("x1", ""), fstree.File("x2", ""), fstree.File("x3", ""), fstree.File("x4", "")})
h := newOps(t, localfs.New(), left, right)
h.press("tab")
h.cursorTo("x3")
h.press("tab")
h.cursorTo("a0")
h.press("f5")
h.waitDialog("Copy")
h.press("y")
h.idle()
p := h.app.Passive()
if e, _ := p.Selected(); e.Name != "x3" {
t.Fatalf("passive cursor on %q after copy (U-P1-7)", e.Name)
}
found := false
for i := 0; i < p.VisibleLen(); i++ {
e, _ := p.Visible(i)
found = found || e.Name == "a0"
}
if !found {
t.Fatal("passive panel not reloaded")
}
}
func TestDirSizeShowsUnreadable(t *testing.T) {
if os.Geteuid() == 0 {
t.Skip("root reads everything")
}
dir := tree(t, fstree.Spec{fstree.File("d/a", "").WithSize(1000), fstree.File("d/locked/b", "x")})
if err := os.Chmod(filepath.Join(dir, "d", "locked"), 0); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = os.Chmod(filepath.Join(dir, "d", "locked"), 0o755) })
h := newOps(t, localfs.New(), dir, dir)
h.cursorTo("d")
h.press(" ")
if h.app.modals.Len() != 0 {
t.Fatal("dir size must run in the background")
}
h.idle()
if !h.d.ScreenContains("1 unreadable") || !h.d.ScreenContains("≥") {
t.Fatalf("N-P1-14: unreadable not shown:\n%s", h.d.Screen())
}
}
func TestMoveAndJobsList(t *testing.T) {
left := tree(t, fstree.Spec{fstree.File("m.txt", "m")})
right := t.TempDir()
h := newOps(t, localfs.New(), left, right)
h.cursorTo("m.txt")
h.press("f6")
h.waitDialog("Move 1 item(s)?")
h.press("y")
h.idle()
if exists(filepath.Join(left, "m.txt")) || read(t, filepath.Join(right, "m.txt")) != "m" {
t.Fatal("move")
}
h.press("B")
if l, ok := h.app.modals.Top().(*modal.List); !ok || len(l.Items) != 1 || !strings.Contains(l.Items[0].Label, "Move m.txt") {
t.Fatalf("jobs list %T", h.app.modals.Top())
}
h.press("esc")
}
func TestArchivePanelOpsDisabled(t *testing.T) {
dir := tree(t, fstree.Spec{fstree.File("a", "x")})
h := newOps(t, vfs.ReadOnly(localfs.New()), dir, dir)
h.cursorTo("a")
for _, k := range []string{"f5", "f6", "f7", "f8", "X", "f2"} {
h.press(k)
if h.app.modals.Len() != 0 || h.mgr.Active() != 0 {
t.Fatalf("%s acted on a read-only panel", k)
}
}
if !h.d.ScreenContains("read-only") {
t.Fatalf("no explanation:\n%s", h.d.Screen())
}
}