vcom/internal/ui/hotfix_test.go
vrubelroman dc3d1b77fe fix(remote): make SSHClient safe to close concurrently; refcount shared clients
- sshConn/sftpCli are immutable after Connect; Close is serialized, only
  sets an atomic closed flag and closes the clients, so concurrent calls
  get errors instead of a data race or nil dereference (N-P0-8)
- Acquire/Release user counting: panes own the client from Connect,
  mirror and remote copy/delete jobs take a reference, and leaving a host
  (goParent at root, re-mirror, exit SSH mode) releases instead of closing
  a client another pane or job still uses (N-P1-12, partial)
- ports TestCloseRace; adds a mirror-then-leave UI regression test

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

549 lines
17 KiB
Go

package ui
// Regression tests for the stage-1 hotfixes (plans/rework/03-tasks.md).
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
"vcom/internal/config"
vfs "vcom/internal/fs"
"vcom/internal/testutil/teadriver"
)
// U-P1-1: status/error messages were assigned but never rendered.
func TestStatusVisible(t *testing.T) {
dir := t.TempDir()
if err := os.Mkdir(filepath.Join(dir, "taken"), 0o755); err != nil {
t.Fatal(err)
}
d := newDriver(t, dir, t.TempDir())
d.Press("f7")
d.Type("taken")
d.Press("enter")
d.Settle(settle)
status := cur(t, d).status
if classifyStatus(status) != statusError {
t.Fatalf("mkdir over existing dir should report an error, status=%q", status)
}
screen := d.Screen()
if !strings.Contains(screen, status) {
t.Fatalf("error %q not on screen:\n%s", status, screen)
}
lines := strings.Split(screen, "\n")
if len(lines) != 40 {
t.Fatalf("view must keep terminal height 40, got %d lines", len(lines))
}
}
func TestStatusLineKeepsHeightWhileFiltering(t *testing.T) {
dir := t.TempDir()
d := newDriver(t, dir, t.TempDir())
d.Press("/")
d.Type("x")
if n := len(strings.Split(d.Screen(), "\n")); n != 40 {
t.Fatalf("filter bar must replace the status line, view has %d lines", n)
}
}
// N-P0-1: F2 in an SSH pane renamed the LOCAL file with the same path.
func TestNP01_RemoteRenameBlocked(t *testing.T) {
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "foo"), "data")
d := newRemoteDriver(t, dir, t.TempDir())
selectName(t, d, "foo")
d.Press("f2")
opened := cur(t, d).modal.kind == modalRename
if !opened && !d.ScreenContains("not supported in SSH panes") {
t.Fatalf("no explanation on screen:\n%s", d.Screen())
}
if opened {
d.Press("ctrl+u")
d.Type("bar")
d.Press("enter")
d.Settle(settle)
}
if !exists(filepath.Join(dir, "foo")) || exists(filepath.Join(dir, "bar")) {
t.Fatal("rename in an SSH pane went through os.Rename on the local filesystem")
}
if opened {
t.Fatal("rename dialog must not open in an SSH pane")
}
}
// N-P0-2: Enter on a remote file ran/edited/opened the LOCAL file with the
// same path.
func TestNP02_RemoteEnterDoesNotTouchLocal(t *testing.T) {
dir := t.TempDir()
marker := filepath.Join(dir, "ran")
script := filepath.Join(dir, "deploy")
writeFile(t, script, "#!/bin/sh\ntouch "+marker+"\n")
if err := os.Chmod(script, 0o755); err != nil {
t.Fatal(err)
}
writeFile(t, filepath.Join(dir, "app.conf"), "x=1\n")
writeFile(t, filepath.Join(dir, "photo.png"), "not really a png")
writeFile(t, filepath.Join(dir, "bundle.zip"), "not really a zip")
t.Setenv("VISUAL", "true") // an editor exists, so only the guard can stop it
started := recordProcesses(t)
d := newRemoteDriver(t, dir, t.TempDir())
for _, name := range []string{"deploy", "app.conf", "photo.png", "bundle.zip"} {
selectName(t, d, name)
d.Press("enter")
d.Settle(settle)
if p := activeOf(t, d).Path; p != dir {
t.Fatalf("Enter on %s changed pane path to %s", name, p)
}
}
if n := len(d.Execs()); n != 0 {
t.Fatalf("Enter on remote files requested %d local process(es) via ExecProcess", n)
}
if len(*started) != 0 {
t.Fatalf("Enter on remote files started local processes: %v", *started)
}
if exists(marker) {
t.Fatal("local executable was run")
}
if !d.ScreenContains("Opening remote files is not supported") {
t.Fatalf("no explanation on screen:\n%s", d.Screen())
}
}
// Control: in a local pane Enter on a non-text file still goes to the
// opener (the guard must not block local files).
func TestNP02_LocalEnterStillOpens(t *testing.T) {
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "photo.png"), "x")
started := recordProcesses(t)
t.Setenv("PATH", fakeOpenerDir(t))
d := newDriver(t, dir, t.TempDir())
selectName(t, d, "photo.png")
d.Press("enter")
d.Settle(settle)
if len(*started) != 1 {
t.Fatalf("local Enter should launch the opener once, got %v (status %q)", *started, cur(t, d).status)
}
}
func fakeOpenerDir(t *testing.T) string {
t.Helper()
bin := t.TempDir()
writeFile(t, filepath.Join(bin, "xdg-open"), "#!/bin/sh\nexit 0\n")
if err := os.Chmod(filepath.Join(bin, "xdg-open"), 0o755); err != nil {
t.Fatal(err)
}
return bin
}
// U-P0-2 / N-P2-10 / N-P1-15: writes into a passive archive pane land in a
// temp dir that is deleted on exit; the host list is not a directory; and
// archive/unpack only understand local paths.
func TestUP02_NoWriteIntoArchivePane(t *testing.T) {
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "inner.txt"), "in archive")
zipPath := filepath.Join(dir, "box.zip")
if err := vfs.CreateArchive(context.Background(), []string{filepath.Join(dir, "inner.txt")}, zipPath, nil); err != nil {
t.Fatal(err)
}
work := t.TempDir()
writeFile(t, filepath.Join(work, "precious.txt"), "user data")
writeFile(t, filepath.Join(work, "other.zip"), "")
if err := vfs.CreateArchive(context.Background(), []string{filepath.Join(work, "precious.txt")}, filepath.Join(work, "other.zip"), nil); err != nil {
t.Fatal(err)
}
d := newDriver(t, dir, work)
selectName(t, d, "box.zip")
d.Press("enter") // left pane enters the archive
d.Settle(settle)
if !activeOf(t, d).InArchive() {
t.Fatal("left pane did not enter the archive")
}
d.Press("tab") // right (local) pane active, left (archive) passive
selectName(t, d, "precious.txt")
for _, key := range []string{"f5", "f6", "f4"} {
d.Press(key)
d.Settle(settle)
if k := cur(t, d).modal.kind; k != modalNone {
t.Fatalf("%s opened dialog %d with an archive pane as target", key, k)
}
if !strings.Contains(cur(t, d).status, "archive") {
t.Fatalf("%s: status %q does not explain the archive target", key, cur(t, d).status)
}
}
selectName(t, d, "other.zip")
d.Press("f11")
d.Settle(settle)
if k := cur(t, d).modal.kind; k != modalNone {
t.Fatalf("unpack into an archive pane opened dialog %d", k)
}
if !exists(filepath.Join(work, "precious.txt")) {
t.Fatal("precious.txt left its directory")
}
}
func TestNP210_NoTransferIntoHostList(t *testing.T) {
work := t.TempDir()
writeFile(t, filepath.Join(work, "a.txt"), "x")
m := newTestModel(t, work, work)
m.right.Path = sshHostListPath
d := teadriver.New(t, m, 160, 40, teadriver.WithLongCmd(300*time.Millisecond))
selectName(t, d, "a.txt")
for _, key := range []string{"f5", "f6", "f4"} {
d.Press(key)
d.Settle(settle)
if k := cur(t, d).modal.kind; k != modalNone {
t.Fatalf("%s opened dialog %d with the host list as target", key, k)
}
}
}
// N-P1-15: archive/unpack with an SSH pane on either side are blocked.
func TestNP115_ArchiveUnpackBlockedWithSSHPane(t *testing.T) {
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "a.txt"), "x")
local := t.TempDir()
writeFile(t, filepath.Join(local, "l.zip"), "")
if err := vfs.CreateArchive(context.Background(), []string{filepath.Join(dir, "a.txt")}, filepath.Join(local, "l.zip"), nil); err != nil {
t.Fatal(err)
}
d := newRemoteDriver(t, dir, local)
selectName(t, d, "a.txt")
d.Press("f4")
d.Settle(settle)
if k := cur(t, d).modal.kind; k != modalNone || cur(t, d).busy {
t.Fatalf("archive in an SSH pane started (modal %d)", k)
}
d.Press("tab")
selectName(t, d, "l.zip")
for _, key := range []string{"f4", "f11"} {
d.Press(key)
d.Settle(settle)
if k := cur(t, d).modal.kind; k != modalNone {
t.Fatalf("%s with an SSH passive pane opened dialog %d", key, k)
}
}
}
func trashFiles(t *testing.T) string {
t.Helper()
return filepath.Join(os.Getenv("HOME"), ".local", "share", "Trash", "files")
}
// U-P0-4: Enter in the delete dialog deleted permanently by default.
func TestUP04_DeleteDefaultsToTrash(t *testing.T) {
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "doc.txt"), "draft")
d := newDriver(t, dir, t.TempDir())
selectName(t, d, "doc.txt")
d.Press("f8")
d.Settle(settle)
if !d.ScreenContains("trash") {
t.Fatalf("dialog should say the file goes to the trash:\n%s", d.Screen())
}
d.Press("enter")
d.Settle(settle)
if exists(filepath.Join(dir, "doc.txt")) {
t.Fatal("doc.txt not removed")
}
b, err := os.ReadFile(filepath.Join(trashFiles(t), "doc.txt"))
if err != nil || string(b) != "draft" {
t.Fatalf("doc.txt not in trash: %v", err)
}
}
func TestUP04_PermanentIsExplicit(t *testing.T) {
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "junk.txt"), "x")
d := newDriver(t, dir, t.TempDir())
selectName(t, d, "junk.txt")
d.Press("f8", "D")
if !d.ScreenContains("Permanently delete") {
t.Fatalf("D should switch to permanent mode:\n%s", d.Screen())
}
d.Press("enter")
d.Settle(settle)
if exists(filepath.Join(dir, "junk.txt")) || exists(filepath.Join(trashFiles(t), "junk.txt")) {
t.Fatal("permanent delete should neither keep nor trash the file")
}
}
// U-P0-4: "n" did not close the dialog although the note promised it.
func TestUP04_NCancels(t *testing.T) {
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "keep.txt"), "x")
d := newDriver(t, dir, t.TempDir())
selectName(t, d, "keep.txt")
d.Press("f8")
if cur(t, d).modal.kind != modalConfirm {
t.Fatal("delete dialog did not open")
}
d.Press("n")
d.Settle(settle)
if k := cur(t, d).modal.kind; k != modalNone {
t.Fatalf("n did not close the dialog (modal %d)", k)
}
if !exists(filepath.Join(dir, "keep.txt")) {
t.Fatal("n deleted the file")
}
}
func TestUP04_NoConfirmOnlyForTrash(t *testing.T) {
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "a.txt"), "x")
d := newDriver(t, dir, t.TempDir(), func(c *config.Config) { c.Behavior.ConfirmDelete = false })
selectName(t, d, "a.txt")
d.Press("f8")
d.Settle(settle)
if exists(filepath.Join(dir, "a.txt")) || !exists(filepath.Join(trashFiles(t), "a.txt")) {
t.Fatal("confirm_delete=false should move straight to the trash")
}
}
// U-P0-3: caret mode panicked under the Ascii color profile (NO_COLOR).
func TestUP03_CaretAsciiNoPanic(t *testing.T) {
profiles := map[string]termenv.Profile{"Ascii": termenv.Ascii, "ANSI": termenv.ANSI, "ANSI256": termenv.ANSI256, "TrueColor": termenv.TrueColor}
for name, profile := range profiles {
t.Run(name, func(t *testing.T) {
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "notes.txt"), "hello world\nsecond line\nthird\n")
m := newTestModel(t, dir, t.TempDir())
d := teadriver.New(t, m, 160, 40, teadriver.WithProfile(profile), teadriver.WithLongCmd(300*time.Millisecond))
d.Settle(settle)
selectName(t, d, "notes.txt")
d.Press("f9") // info pane with text preview
d.Settle(settle)
d.Press("i") // caret mode
d.Settle(settle)
if !cur(t, d).cursorMode {
t.Fatalf("caret mode not entered, status %q", cur(t, d).status)
}
d.Press("j", "l", "v", "l")
if s := d.Screen(); !strings.Contains(s, "second line") {
t.Fatalf("preview text not rendered:\n%s", s)
}
})
}
}
func TestBackgroundPreservingReset(t *testing.T) {
prev := lipgloss.ColorProfile()
defer lipgloss.SetColorProfile(prev)
lipgloss.SetColorProfile(termenv.Ascii)
if _, ok := backgroundPreservingReset("#112233"); ok {
t.Fatal("Ascii profile has no background to preserve")
}
lipgloss.SetColorProfile(termenv.TrueColor)
got, ok := backgroundPreservingReset("#112233")
if !ok || got != "\x1b[39;22;23;24;59;48;2;17;34;51m" {
t.Fatalf("got %q %v", got, ok)
}
}
// U-P1-16: "q" quit the program even with the info pane, a filter or a
// selection open; it must close that layer first.
func TestUP116_QClosesInfo(t *testing.T) {
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "a.txt"), "x")
writeFile(t, filepath.Join(dir, "b.txt"), "y")
d := newDriver(t, dir, t.TempDir())
selectName(t, d, "a.txt")
d.Press("f9")
d.Settle(settle)
if !cur(t, d).infoMode {
t.Fatal("info pane did not open")
}
d.Press("q")
d.Settle(settle)
if d.Quit() {
t.Fatal("q with the info pane open quit the program")
}
if cur(t, d).infoMode {
t.Fatal("q did not close the info pane")
}
d.Press("/")
d.Type("a")
d.Press("enter", "q")
d.Settle(settle)
if d.Quit() || cur(t, d).filterQuery != "" {
t.Fatalf("q should clear the filter (quit=%v filter=%q)", d.Quit(), cur(t, d).filterQuery)
}
d.Press("J") // mark an entry
if len(activeOf(t, d).MarkedEntries()) == 0 {
t.Fatal("J did not mark")
}
d.Press("q")
d.Settle(settle)
if d.Quit() || len(activeOf(t, d).MarkedEntries()) != 0 {
t.Fatal("q should clear the selection first")
}
d.Press("q")
d.Settle(settle)
if !d.Quit() {
t.Fatal("q from the bare browser should quit")
}
}
// U-P1-5: Cyrillic in the filter and rename fields stays Cyrillic, while
// commands still work from the Russian layout.
func TestUP15_CyrillicTextAndCommands(t *testing.T) {
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "отчёт.txt"), "x")
writeFile(t, filepath.Join(dir, "other.txt"), "y")
d := newDriver(t, dir, t.TempDir())
d.Press("/")
d.Type("отч")
if q := cur(t, d).filterQuery; q != "отч" {
t.Fatalf("filter query %q", q)
}
d.Press("esc")
selectName(t, d, "other.txt")
d.Press("к") // "r" on the Russian layout: rename
if cur(t, d).modal.kind != modalRename {
t.Fatal("command key on the Russian layout no longer works")
}
d.Press("ctrl+u")
d.Type("новое.txt")
d.Press("enter")
d.Settle(settle)
if !exists(filepath.Join(dir, "новое.txt")) {
t.Fatal("rename with a Cyrillic name failed")
}
}
func fileSize(t *testing.T, p string) int64 {
t.Helper()
st, err := os.Stat(p)
if err != nil {
t.Fatal(err)
}
return st.Size()
}
// U-P0-1: archiving data.tar as tar into the same directory truncated the
// source (archive name == source name).
func TestUP01_ArchiveDoesNotDestroySource(t *testing.T) {
dir := t.TempDir()
src := filepath.Join(dir, "data.tar")
writeFile(t, src, strings.Repeat("x", 100000))
d := newDriver(t, dir, dir)
selectName(t, d, "data.tar")
d.Press("f4")
d.Settle(settle)
if cur(t, d).modal.kind != modalArchiveType {
t.Fatalf("archive dialog did not open, status %q", cur(t, d).status)
}
d.Press("f") // zip -> tar
if cur(t, d).archiveFormat != "tar" {
t.Fatalf("format %q", cur(t, d).archiveFormat)
}
d.Press("enter")
d.Settle(settle)
if got := fileSize(t, src); got != 100000 {
t.Fatalf("source truncated to %d bytes", got)
}
if !d.ScreenContains("Cannot archive") {
t.Fatalf("refusal not shown:\n%s", d.Screen())
}
}
// U-P0-1: passive pane inside the archived directory made the archive
// include itself.
func TestUP01_ArchiveNotInsideSource(t *testing.T) {
dir := t.TempDir()
proj := filepath.Join(dir, "proj")
writeFile(t, filepath.Join(proj, "a.bin"), strings.Repeat("y", 1<<16))
d := newDriver(t, dir, proj)
selectName(t, d, "proj")
d.Press("f4")
d.Settle(settle)
d.Press("enter")
d.Settle(settle)
entries, _ := os.ReadDir(proj)
if len(entries) != 1 {
t.Fatalf("archive was written inside its own source: %v", entries)
}
if !d.ScreenContains("Cannot archive") {
t.Fatalf("refusal not shown:\n%s", d.Screen())
}
}
// U-P0-1 (adjacent): an existing archive was overwritten without asking.
func TestUP01_ExistingArchiveNeedsConfirm(t *testing.T) {
dir, out := t.TempDir(), t.TempDir()
writeFile(t, filepath.Join(dir, "a.txt"), "new content")
old := filepath.Join(out, "a.zip")
writeFile(t, old, "OLD ARCHIVE")
d := newDriver(t, dir, out)
selectName(t, d, "a.txt")
d.Press("f4")
d.Settle(settle)
d.Press("enter")
d.Settle(settle)
if !d.ScreenContains("Overwrite existing archive") {
t.Fatalf("no overwrite confirmation:\n%s", d.Screen())
}
d.Press("n")
d.Settle(settle)
if b, _ := os.ReadFile(old); string(b) != "OLD ARCHIVE" {
t.Fatal("cancelled overwrite changed the archive")
}
d.Press("f4")
d.Settle(settle)
d.Press("enter")
d.Settle(settle)
d.Press("enter") // confirm overwrite
d.Settle(settle)
if b, _ := os.ReadFile(old); string(b) == "OLD ARCHIVE" || len(b) == 0 {
t.Fatalf("confirmed overwrite did not write the archive (status %q)", cur(t, d).status)
}
leftovers, _ := filepath.Glob(filepath.Join(out, ".vcom-tmp-*"))
if len(leftovers) != 0 {
t.Fatalf("temp files left: %v", leftovers)
}
}
// N-P1-12 / N-P0-8: after mirror (p) both panes share one client; leaving
// the host in one pane closed it under the other.
func TestNP112_MirrorThenLeaveKeepsOtherPaneConnected(t *testing.T) {
m := newTestModel(t, t.TempDir(), t.TempDir())
c := connectTestHost(t)
mountRemote(t, &m, PaneLeft, c, "/")
d := teadriver.New(t, m, 160, 40, teadriver.WithLongCmd(300*time.Millisecond))
d.Settle(settle)
d.Press("p", "p") // mirror twice: the second used to Close the shared client
d.Settle(settle)
if !c.IsConnected() {
t.Fatal("repeated mirror closed the shared client")
}
d.Press("backspace") // left pane at remote root: leave the host
d.Settle(settle)
if !c.IsConnected() {
t.Fatal("leaving the host in one pane closed the client used by the mirrored pane")
}
mm := cur(t, d)
if err := mm.reloadRemotePane(PaneRight, ""); err != nil {
t.Fatalf("mirrored pane broken: %v", err)
}
d.Press("tab", "backspace") // the mirrored pane leaves too
d.Settle(settle)
if c.IsConnected() {
t.Fatal("client should be closed once no pane uses it")
}
}