diff --git a/internal/ui/helpers_test.go b/internal/ui/helpers_test.go index 94fc134..e375b65 100644 --- a/internal/ui/helpers_test.go +++ b/internal/ui/helpers_test.go @@ -4,13 +4,17 @@ import ( "io" "log" "os" + "os/exec" "path/filepath" + "strings" "testing" "time" tea "github.com/charmbracelet/bubbletea" "vcom/internal/config" + "vcom/internal/fs/remote" + "vcom/internal/testutil/sshtest" "vcom/internal/testutil/teadriver" ) @@ -136,3 +140,56 @@ func activeOf(t *testing.T, d *teadriver.Driver) BrowserPane { m := cur(t, d) return *m.activePane() } + +// connectTestHost starts an in-process SSH server and connects the app's +// own client to it. The server serves the real filesystem, so remote paths +// and local paths name the same files. +func connectTestHost(t *testing.T) *remote.SSHClient { + t.Helper() + srv := sshtest.Start(t, sshtest.Options{}) + c, err := remote.Connect(remote.SSHHost{ + Name: "testhost", HostName: "127.0.0.1", Port: srv.Port(), User: "u", Password: srv.Password, + }) + if err != nil { + t.Fatalf("connect: %v", err) + } + t.Cleanup(func() { c.Close() }) + return c +} + +// mountRemote puts pane id of m into a remote mount at dir. +func mountRemote(t *testing.T, m *Model, id PaneID, c *remote.SSHClient, dir string) { + t.Helper() + p := m.paneByID(id) + p.PushRemote(RemoteMount{Host: c.Host, RemotePath: dir, Client: c, Connected: true}) + p.Path = dir + if err := m.reloadRemotePane(id, ""); err != nil { + t.Fatalf("reloadRemotePane: %v", err) + } +} + +// newRemoteDriver opens the left pane on dir over SSH and the right pane on +// right (local). +func newRemoteDriver(t *testing.T, dir, right string) *teadriver.Driver { + t.Helper() + m := newTestModel(t, right, right) + c := connectTestHost(t) + mountRemote(t, &m, PaneLeft, c, dir) + d := teadriver.New(t, m, 160, 40, teadriver.WithLongCmd(300*time.Millisecond)) + d.Settle(settle) + return d +} + +// recordProcesses replaces startProcess for the test and returns the list of +// commands that would have been started. +func recordProcesses(t *testing.T) *[]string { + t.Helper() + var started []string + prev := startProcess + startProcess = func(c *exec.Cmd) error { + started = append(started, strings.Join(c.Args, " ")) + return nil + } + t.Cleanup(func() { startProcess = prev }) + return &started +} diff --git a/internal/ui/hotfix_test.go b/internal/ui/hotfix_test.go index a155e7c..5f7bfa0 100644 --- a/internal/ui/hotfix_test.go +++ b/internal/ui/hotfix_test.go @@ -3,10 +3,15 @@ package ui // Regression tests for the stage-1 hotfixes (plans/rework/03-tasks.md). import ( + "context" "os" "path/filepath" "strings" "testing" + "time" + + vfs "vcom/internal/fs" + "vcom/internal/testutil/teadriver" ) // U-P1-1: status/error messages were assigned but never rendered. @@ -43,3 +48,184 @@ func TestStatusLineKeepsHeightWhileFiltering(t *testing.T) { 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 p := activeOf(t, d); !p.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) + } + } +} diff --git a/internal/ui/model.go b/internal/ui/model.go index 1d09bf1..aa72492 100644 --- a/internal/ui/model.go +++ b/internal/ui/model.go @@ -1937,6 +1937,13 @@ func (m *Model) handleOpenSelected() (tea.Model, tea.Cmd) { return m, nil } + if !selected.IsDir { + if err := localFileActionAllowed(m.activePane()); err != nil { + m.status = err.Error() + return m, nil + } + } + if selected.IsDir { if m.activePane().InRemote() { return m.enterRemoteDir(selected) @@ -2290,6 +2297,15 @@ func (m *Model) handleTransfer(kind fileOpKind) (tea.Model, tea.Cmd) { return m, nil } + if m.activePane().Path == sshHostListPath { + m.status = fmt.Sprintf("Cannot %s SSH host entries", operationVerb(kind)) + return m, nil + } + if err := canWriteTo(m.passivePane()); err != nil { + m.status = fmt.Sprintf("Cannot %s: %v", operationVerb(kind), err) + return m, nil + } + sources := m.operationSources() if len(sources) == 0 { log.Printf("[SKIP] Transfer: no sources, kind=%d", kind) @@ -2379,6 +2395,18 @@ func (m *Model) handleTransfer(kind fileOpKind) (tea.Model, tea.Cmd) { } func (m *Model) handleArchive() (tea.Model, tea.Cmd) { + if paneOnRemote(m.activePane()) { + m.status = "Archiving is not supported in SSH panes yet" + return m, nil + } + if err := canWriteTo(m.passivePane()); err != nil { + m.status = fmt.Sprintf("Cannot archive: %v", err) + return m, nil + } + if m.passivePane().InRemote() { + m.status = "Cannot archive into an SSH pane yet" + return m, nil + } sources := m.operationSources() if len(sources) == 0 { log.Printf("[SKIP] Archive: no sources") @@ -2449,6 +2477,18 @@ func (m *Model) handleDelete() (tea.Model, tea.Cmd) { } func (m *Model) handleUnpack() (tea.Model, tea.Cmd) { + if paneOnRemote(m.activePane()) { + m.status = "Unpacking is not supported in SSH panes yet" + return m, nil + } + if err := canWriteTo(m.passivePane()); err != nil { + m.status = fmt.Sprintf("Cannot unpack: %v", err) + return m, nil + } + if m.passivePane().InRemote() { + m.status = "Cannot unpack into an SSH pane yet" + return m, nil + } selected, ok := m.activePane().Selected() if !ok || !isArchiveEntry(selected) { log.Printf("[SKIP] Unpack: no archive selected") @@ -2563,6 +2603,10 @@ func (m *Model) exitViewMode() (tea.Model, tea.Cmd) { } func (m *Model) handleOpenExternal() (tea.Model, tea.Cmd) { + if err := localFileActionAllowed(m.activePane()); err != nil { + m.status = err.Error() + return m, nil + } selected, ok := m.activePane().Selected() if !ok || selected.IsParent || selected.IsDir { m.status = "Select a file to open" @@ -2582,6 +2626,10 @@ func (m *Model) handleOpenExternal() (tea.Model, tea.Cmd) { } func (m *Model) handleEdit() (tea.Model, tea.Cmd) { + if err := localFileActionAllowed(m.activePane()); err != nil { + m.status = err.Error() + return m, nil + } selected, ok := m.activePane().Selected() if !ok || selected.IsParent || selected.IsDir { m.status = "Select a file to edit" @@ -2603,6 +2651,10 @@ func (m *Model) handleEdit() (tea.Model, tea.Cmd) { } func (m *Model) handleExecute(entry vfs.Entry) (tea.Model, tea.Cmd) { + if err := localFileActionAllowed(m.activePane()); err != nil { + m.status = err.Error() + return m, nil + } log.Printf("[ACTION] Execute: path=%s", entry.Path) m.cleanupImageOverlay() cmd := exec.Command(entry.Path) @@ -3036,6 +3088,10 @@ func (m *Model) openMkdirModal() { m.status = "Archive mode is read-only; create directory is disabled" return } + if m.activePane().Path == sshHostListPath { + m.status = "Cannot create a directory in the SSH host list" + return + } input := textinput.New() input.Placeholder = "new-directory" @@ -3057,6 +3113,10 @@ func (m *Model) openRenameModal() { m.status = "Archive mode is read-only; rename is disabled" return } + if paneOnRemote(m.activePane()) { + m.status = "Rename is not supported in SSH panes yet" + return + } selected, ok := m.activePane().Selected() if !ok || selected.IsParent { @@ -5446,12 +5506,16 @@ func externalCommandFromEnv(envVars []string, fallbacks []string, path string) ( return exec.Command(parts[0], args...), filepath.Base(parts[0]), nil } +// startProcess starts a detached external process (xdg-open). Tests +// replace it to observe launches without running anything. +var startProcess = func(c *exec.Cmd) error { return c.Start() } + func startExternalOpenCmd(command *exec.Cmd, path string) tea.Cmd { return func() tea.Msg { command.Stdin = nil command.Stdout = io.Discard command.Stderr = io.Discard - if err := command.Start(); err != nil { + if err := startProcess(command); err != nil { return externalOpenMsg{path: path, err: err} } return externalOpenMsg{path: path} @@ -5588,6 +5652,38 @@ func isEditableEntry(entry vfs.Entry) bool { } } +// sshHostListPath is the pseudo-path of the SSH host list pane. +const sshHostListPath = "ssh://" + +// paneOnRemote reports whether the pane's entries are not local files: a +// remote mount or the SSH host list. +func paneOnRemote(p *BrowserPane) bool { + return p.InRemote() || p.Path == sshHostListPath +} + +// canWriteTo reports why the pane cannot be the destination of a write +// (copy, move, archive, unpack). Archive panes show a temp extraction that +// is deleted on exit, and the host list is not a directory at all. +func canWriteTo(p *BrowserPane) error { + if p.InArchive() { + return fmt.Errorf("target pane is inside an archive (read-only)") + } + if p.Path == sshHostListPath { + return fmt.Errorf("target pane is the SSH host list") + } + return nil +} + +// localFileActionAllowed guards actions that hand a file path to the local +// OS (editor, executable, xdg-open, archive mount). In an SSH pane the path +// is remote, and the same path on this machine is a different file. +func localFileActionAllowed(p *BrowserPane) error { + if paneOnRemote(p) { + return fmt.Errorf("Opening remote files is not supported yet; copy the file to a local pane (F5)") + } + return nil +} + func isArchiveEntry(entry vfs.Entry) bool { return !entry.IsDir && !entry.IsParent && entry.Category() == "archive" }