vcom/internal/ui/helpers_test.go

196 lines
4.7 KiB
Go
Raw Normal View History

package ui
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"
)
func TestMain(m *testing.M) {
log.SetOutput(io.Discard)
os.Exit(m.Run())
}
// isolateEnv points HOME/XDG dirs at a temp dir so session, trash and SSH
// state never touch the real user's files.
func isolateEnv(t *testing.T) string {
t.Helper()
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_DATA_HOME", filepath.Join(home, ".local", "share"))
t.Setenv("SSH_AUTH_SOCK", "")
t.Setenv("VISUAL", "")
t.Setenv("EDITOR", "")
if err := os.MkdirAll(filepath.Join(home, ".ssh"), 0o700); err != nil {
t.Fatal(err)
}
return home
}
// newTestModel builds the old UI model with both panes at the given paths.
func newTestModel(t *testing.T, left, right string, mutate ...func(*config.Config)) Model {
t.Helper()
isolateEnv(t)
cfg := config.Default()
cfg.UI.IconMode = "ascii"
cfg.Startup.LeftPath = left
cfg.Startup.RightPath = right
for _, f := range mutate {
f(&cfg)
}
m, err := NewModel(cfg, "")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
m.cleanupArchiveMounts()
m.cleanupRemoteImageTemp()
})
return m
}
const settle = 5 * time.Second
func newDriver(t *testing.T, left, right string, mutate ...func(*config.Config)) *teadriver.Driver {
t.Helper()
m := newTestModel(t, left, right, mutate...)
d := teadriver.New(t, m, 160, 40, teadriver.WithLongCmd(300*time.Millisecond))
d.Settle(settle)
return d
}
// asModel unwraps the driver's model (Update returns Model or *Model).
func asModel(t *testing.T, tm tea.Model) Model {
t.Helper()
switch v := tm.(type) {
case Model:
return v
case *Model:
return *v
}
t.Fatalf("unexpected model type %T", tm)
return Model{}
}
func cur(t *testing.T, d *teadriver.Driver) Model { return asModel(t, d.Model()) }
func writeFile(t *testing.T, p, content string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(p, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
}
// selectName moves the active pane's cursor to the entry called name using
// j/k presses (as a user would).
func selectName(t *testing.T, d *teadriver.Driver, name string) {
t.Helper()
m := cur(t, d)
p := m.activePane()
idx := -1
for i, e := range p.Entries {
if e.Name == name {
idx = i
}
}
if idx < 0 {
t.Fatalf("entry %q not in active pane %s", name, p.Path)
}
for p.Cursor != idx {
if p.Cursor < idx {
d.Press("j")
} else {
d.Press("k")
}
m = cur(t, d)
p = m.activePane()
}
d.Settle(settle)
}
func selectedEntryName(t *testing.T, d *teadriver.Driver) string {
t.Helper()
m := cur(t, d)
e, ok := m.activePane().Selected()
if !ok {
return ""
}
return e.Name
}
// activeOf returns a copy of the active pane.
func activeOf(t *testing.T, d *teadriver.Driver) BrowserPane {
t.Helper()
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
}