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 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) } } } 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) } }