vcom/internal/tui/ops_remote_test.go
vrubelroman 280f3880ef feat(tui): file operations through jobs and transfer (6.2)
Copy/move run as jobs: Prepare (scanning) → plan dialog (files, size,
existing targets with a policy choice, hidden marks) → Execute with a
Progress modal (b/Esc background, c asks before cancelling), conflict
dialogs with apply-to-all and Retry/Skip/Skip all/Abort on item errors,
then a summary and reload of the affected panels (each keeps its cursor).
F8 trashes (default button), refused items are offered for permanent
deletion; X deletes permanently (default Cancel), SSH panels without a
trash go straight to that dialog. F7/F2 work through the panel's FS with
name validation (no "/"). Space computes a directory size in the
background ("≥ X (N unreadable)"); B lists jobs and brings one back.
Read-only (archive) panels and the host list are never targets. Jobs hold
references on the panels' mounts while they run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 13:59:22 +03:00

152 lines
4.5 KiB
Go

package tui
import (
"context"
"net"
"os"
"path/filepath"
"strconv"
"sync/atomic"
"testing"
"golang.org/x/crypto/ssh/knownhosts"
"vcom/internal/ssh/conn"
"vcom/internal/testutil/fstree"
"vcom/internal/testutil/sshtest"
"vcom/internal/tui/panel"
"vcom/internal/vfs"
"vcom/internal/vfs/localfs"
"vcom/internal/vfs/sftpfs"
"vcom/internal/vfs/vfstest"
)
// standFS starts an in-process SSH/SFTP server and returns an FS on it (it
// shares this machine's file system: results are checked on disk).
func standFS(t *testing.T) *sftpfs.FS {
t.Helper()
home := sshtest.IsolatedHome(t)
srv := sshtest.Start(t, sshtest.Options{})
kh := filepath.Join(home, ".ssh", "known_hosts")
f, err := os.OpenFile(kh, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600)
if err != nil {
t.Fatal(err)
}
for _, k := range srv.HostKeys {
_, _ = f.WriteString(knownhosts.Line([]string{knownhosts.Normalize(srv.Addr)}, k.PublicKey()) + "\n")
}
f.Close()
pool := conn.NewPool(conn.PoolOptions{DialOptions: conn.DialOptions{Home: home, AgentSocket: "-", KnownHostsFiles: []string{kh}}})
t.Cleanup(func() { pool.Close() })
host, port, _ := net.SplitHostPort(srv.Addr)
p, _ := strconv.Atoi(port)
l, err := pool.Acquire(context.Background(), conn.Endpoint{Alias: "stand", User: "u", Host: host, Port: p, Password: srv.Password})
if err != nil {
t.Fatal(err)
}
fsys := sftpfs.New(l)
t.Cleanup(fsys.Release)
return fsys
}
// mountRemote puts pane at dir on rfs (as connecting does in 6.4).
func (h *opsH) mountRemote(pane int, rfs vfs.FS, dir string) {
h.t.Helper()
p := h.app.Panel(pane)
ret := p.Location()
m := panel.NewMount(rfs, "/", &ret, "", nil, nil)
t := panel.Target{Mounts: append(p.Here().Mounts, m), Path: dir}
h.d.Send(h.app.navigate(pane, t, "", panel.NavPush)())
h.d.Settle(settle)
if p.FS() != rfs {
h.t.Fatalf("pane %d not mounted", pane)
}
}
// counting wraps an FS and counts renames/removes.
func counting(fsys vfs.FS, n *atomic.Int32) vfs.FS {
return &vfstest.FaultFS{FS: fsys, Hook: func(op vfstest.Op, _ string) error {
if op == vfstest.OpRename || op == vfstest.OpRemove {
n.Add(1)
}
return nil
}}
}
// N-P0-1: rename in a remote panel goes through the remote FS, never the
// local file system.
func TestNP01_RemoteRenameUsesRemoteFS(t *testing.T) {
ldir := tree(t, fstree.Spec{fstree.File("foo", "local")})
rdir := tree(t, fstree.Spec{fstree.File("foo", "remote")})
var localOps, remoteOps atomic.Int32
rfs := counting(standFS(t), &remoteOps)
h := newOps(t, counting(localfs.New(), &localOps), ldir, ldir)
h.mountRemote(1, rfs, rdir)
h.press("tab")
h.cursorTo("foo")
h.press("f2", "ctrl+u")
h.typ("bar")
h.press("enter")
h.idle()
if !exists(filepath.Join(rdir, "bar")) || exists(filepath.Join(rdir, "foo")) {
t.Fatal("remote file not renamed")
}
if read(t, filepath.Join(ldir, "foo")) != "local" || localOps.Load() != 0 || remoteOps.Load() != 1 {
t.Fatalf("local ops %d, remote ops %d", localOps.Load(), remoteOps.Load())
}
if h.selected() != "bar" {
t.Fatalf("remote panel cursor on %q", h.selected())
}
}
// N-P1-11: deleting in a remote panel reloads the remote panel (the old UI
// reloaded the local one).
func TestNP111_RemoteDeleteReloadsRemote(t *testing.T) {
ldir := tree(t, fstree.Spec{fstree.File("other", "x")})
rdir := tree(t, fstree.Spec{fstree.File("gone.txt", "x"), fstree.File("stay.txt", "y")})
h := newOps(t, localfs.New(), ldir, ldir)
h.mountRemote(1, standFS(t), rdir)
h.press("tab")
h.cursorTo("gone.txt")
h.press("f8") // no trash over SSH: permanent dialog
h.waitDialog("permanently")
h.press("d")
h.idle()
if exists(filepath.Join(rdir, "gone.txt")) {
t.Fatal("not deleted")
}
p := h.app.Active()
for i := 0; i < p.VisibleLen(); i++ {
if e, _ := p.Visible(i); e.Name == "gone.txt" {
t.Fatal("remote panel still lists the deleted file")
}
}
if h.selected() != "stay.txt" {
t.Fatalf("cursor on %q", h.selected())
}
}
// Copy local → remote and back through the same engine.
func TestCopyToRemotePane(t *testing.T) {
ldir := tree(t, fstree.Spec{fstree.File("up.bin", "").WithSize(200 << 10)})
rdir := t.TempDir()
h := newOps(t, localfs.New(), ldir, ldir)
h.mountRemote(1, standFS(t), rdir)
h.cursorTo("up.bin")
h.press("f5")
h.waitDialog("Copy 1 item(s)?")
h.press("y")
h.idle()
if fi, err := os.Stat(filepath.Join(rdir, "up.bin")); err != nil || fi.Size() != 200<<10 {
t.Fatalf("remote copy: %v", err)
}
found := false
p := h.app.Passive()
for i := 0; i < p.VisibleLen(); i++ {
e, _ := p.Visible(i)
found = found || e.Name == "up.bin"
}
if !found {
t.Fatal("remote panel not reloaded")
}
}