vcom/internal/ui/qa_hotfix_test.go
vrubelroman ef735fb37c fix(ui): refuse mirror from an archive pane
Mirroring (p) an archive pane gave the passive pane the temporary
extraction directory as a plain writable local directory: changes there
were lost when the archive was closed and the pane was left on a deleted
directory (QA-2). Mirror is refused inside an archive until archive
mounts can be shared.

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

460 lines
15 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package ui
// QA (v0.2.8): independent checks of the stage-1 UI hotfixes, aimed at the
// paths next to the ones the developer fixed. Tests that expose an open bug
// are skipped with "BUG QA-n"; see plans/rework/05-qa-v0.2.8.md.
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/muesli/termenv"
vfs "vcom/internal/fs"
"vcom/internal/testutil/teadriver"
)
// --- U-P1-5: Cyrillic / arbitrary text in every input ---------------------
// Mkdir and rename fields must accept the letters that are also dialog
// keys elsewhere: q (close), n (no), y (confirm), j/k (move), ? (help).
func TestQATextModalsAcceptCommandLetters(t *testing.T) {
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "old.txt"), "x")
d := newDriver(t, dir, t.TempDir())
d.Press("f7")
d.Type("qnyjk?п")
if got := cur(t, d).modal.input.Value(); got != "qnyjk?п" {
t.Fatalf("mkdir input %q, want %q", got, "qnyjk?п")
}
d.Press("enter")
d.Settle(settle)
if !exists(filepath.Join(dir, "qnyjk?п")) {
t.Fatalf("dir not created, status %q", cur(t, d).status)
}
selectName(t, d, "old.txt")
d.Press("f2", "ctrl+u")
d.Type("qй ё?.txt")
d.Press("enter")
d.Settle(settle)
if !exists(filepath.Join(dir, "qй ё?.txt")) {
t.Fatalf("rename failed, status %q", cur(t, d).status)
}
}
// The filter is a text field: typing "jpg" or "kotlin" must filter by that
// text. j/k are bound to Up/Down inside the filter handler and never reach
// the input.
func TestQAFilterAcceptsJK(t *testing.T) {
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "photo.jpg"), "x")
writeFile(t, filepath.Join(dir, "notes.txt"), "x")
d := newDriver(t, dir, t.TempDir())
d.Press("/")
d.Type("jpg")
if q := cur(t, d).filterQuery; q != "jpg" {
t.Fatalf("filter query %q, want jpg", q)
}
}
// Cyrillic filter: the list really narrows (the developer only checked the
// query string).
func TestQAFilterCyrillicNarrows(t *testing.T) {
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "отчёт.txt"), "x")
writeFile(t, filepath.Join(dir, "summary.txt"), "x")
d := newDriver(t, dir, t.TempDir())
d.Press("/")
d.Type("отч")
d.Press("enter")
s := d.Screen()
if !strings.Contains(s, "отчёт.txt") || strings.Contains(s, "summary.txt") {
t.Fatalf("Cyrillic filter did not narrow the list:\n%s", s)
}
}
func openSSHAddForm(t *testing.T, d *teadriver.Driver) {
t.Helper()
d.Press("s") // SSH host list
d.Settle(settle)
if p := activeOf(t, d).Path; p != sshHostListPath {
t.Fatalf("SSH host list not opened, path %q", p)
}
selectName(t, d, "+ Add host")
d.Press("enter")
d.Settle(settle)
if cur(t, d).modal.kind != modalSSHConnect {
t.Fatalf("add-host form not opened (modal %d)", cur(t, d).modal.kind)
}
}
// SSH form: Cyrillic name, and q/n/y/j/k in fields.
func TestQASSHFormCyrillicAndLetters(t *testing.T) {
d := newDriver(t, t.TempDir(), t.TempDir())
openSSHAddForm(t, d)
d.Type("сервер-qnyjk")
d.Press("tab")
d.Type("host.example")
m := cur(t, d)
if got := m.ssh.inputs[0].Value(); got != "сервер-qnyjk" {
t.Fatalf("name field %q", got)
}
if got := m.ssh.inputs[1].Value(); got != "host.example" {
t.Fatalf("host field %q", got)
}
if m.modal.kind != modalSSHConnect {
t.Fatal("form closed while typing")
}
}
// '?' is a valid password character, but in the SSH form it toggles help.
func TestQASSHFormPasswordQuestionMark(t *testing.T) {
d := newDriver(t, t.TempDir(), t.TempDir())
openSSHAddForm(t, d)
for i := 0; i < 4; i++ {
d.Press("tab")
}
d.Type("pa?ss")
if got := cur(t, d).ssh.inputs[4].Value(); got != "pa?ss" {
t.Fatalf("password field %q, want pa?ss", got)
}
}
// Russian layout in yes/no dialogs: "т" (n) cancels, "н" (y) confirms.
func TestQARussianLayoutInConfirm(t *testing.T) {
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "a.txt"), "x")
d := newDriver(t, dir, t.TempDir())
selectName(t, d, "a.txt")
d.Press("f8", "т")
d.Settle(settle)
if cur(t, d).modal.kind != modalNone || !exists(filepath.Join(dir, "a.txt")) {
t.Fatal("т (n on the Russian layout) should cancel")
}
d.Press("f8", "н")
d.Settle(settle)
if exists(filepath.Join(dir, "a.txt")) {
t.Fatal("н (y on the Russian layout) should confirm")
}
}
// --- U-P1-16: q / Esc in every layer --------------------------------------
func TestQAQAndEscCloseEveryLayer(t *testing.T) {
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "a.txt"), "hello\nworld\n")
writeFile(t, filepath.Join(dir, "b.txt"), "x")
type layer struct {
name string
open []string
check func(m Model) bool // true while the layer is open
}
layers := []layer{
{"help", []string{"f1"}, func(m Model) bool { return m.modal.kind == modalHelp }},
{"theme", []string{"t"}, func(m Model) bool { return m.modal.kind == modalThemeSelect }},
{"view", []string{"f3"}, func(m Model) bool { return m.viewMode }},
{"info", []string{"f9"}, func(m Model) bool { return m.infoMode }},
{"caret", []string{"f9", "i"}, func(m Model) bool { return m.cursorMode || m.infoMode }},
{"confirm-delete", []string{"f8"}, func(m Model) bool { return m.modal.kind == modalConfirm }},
{"archive-type", []string{"f4"}, func(m Model) bool { return m.modal.kind == modalArchiveType }},
{"copy-confirm", []string{"f5"}, func(m Model) bool { return m.modal.kind == modalConfirm }},
{"selection", []string{"J"}, func(m Model) bool { return len(m.activePane().MarkedEntries()) > 0 }},
}
for _, closeKey := range []string{"q", "esc"} {
for _, l := range layers {
t.Run(closeKey+"/"+l.name, func(t *testing.T) {
d := newDriver(t, dir, t.TempDir())
selectName(t, d, "a.txt")
d.Press(l.open...)
d.Settle(settle)
if !l.check(cur(t, d)) {
t.Fatalf("layer did not open")
}
for i := 0; i < 3 && l.check(cur(t, d)); i++ {
d.Press(closeKey)
d.Settle(settle)
if d.Quit() {
t.Fatalf("%s quit the program with %s open", closeKey, l.name)
}
}
if l.check(cur(t, d)) {
t.Fatalf("%s did not close %s", closeKey, l.name)
}
if !exists(filepath.Join(dir, "a.txt")) {
t.Fatal("closing a layer deleted a.txt")
}
})
}
}
}
// --- U-P0-1: archive through the UI, all formats ---------------------------
func TestQAArchiveSameDirZipThroughUI(t *testing.T) {
dir := t.TempDir()
src := filepath.Join(dir, "report.zip")
writeFile(t, src, strings.Repeat("r", 40000))
d := newDriver(t, dir, dir)
selectName(t, d, "report.zip")
d.Press("f4")
d.Settle(settle)
if f := cur(t, d).archiveFormat; f != "zip" {
t.Fatalf("default format %q", f)
}
d.Press("enter")
d.Settle(settle)
if fileSize(t, src) != 40000 {
t.Fatal("report.zip truncated")
}
if !d.ScreenContains("Cannot archive") {
t.Fatalf("no refusal on screen:\n%s", d.Screen())
}
}
// Archive a directory into the same pane for every format, then archive it
// again over the existing file with confirmation.
func TestQAArchiveFormatsAndOverwriteThroughUI(t *testing.T) {
formats := []struct {
presses int // presses of "f" from the default zip
ext string
}{{0, ".zip"}, {1, ".tar"}, {2, ".tar.gz"}}
for _, f := range formats {
t.Run(f.ext, func(t *testing.T) {
dir, out := t.TempDir(), t.TempDir()
writeFile(t, filepath.Join(dir, "proj", "a.txt"), "alpha")
d := newDriver(t, dir, out)
selectName(t, d, "proj")
for round := 0; round < 2; round++ {
d.Press("f4")
d.Settle(settle)
for i := 0; i < f.presses; i++ {
d.Press("f")
}
d.Press("enter")
d.Settle(settle)
if round == 1 {
if !d.ScreenContains("Overwrite existing archive") {
t.Fatalf("second archive did not ask to overwrite:\n%s", d.Screen())
}
d.Press("y")
d.Settle(settle)
}
}
arc := filepath.Join(out, "proj"+f.ext)
if st, err := os.Stat(arc); err != nil || st.Size() == 0 {
t.Fatalf("archive %s not written: %v (status %q)", arc, err, cur(t, d).status)
}
left, _ := filepath.Glob(filepath.Join(out, ".vcom-tmp-*"))
if len(left) != 0 {
t.Fatalf("temp files left: %v", left)
}
})
}
}
// --- U-P0-2: archive pane as target, adjacent paths -------------------------
// Mirror (p) from a pane inside an archive gives the passive pane the
// archive's temp extraction directory as a plain local path: it is not
// marked as an archive, so canWriteTo lets copy/move write there, and the
// directory is deleted when the archive is closed.
func TestQAMirrorFromArchiveKeepsReadOnly(t *testing.T) {
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "inner.txt"), "in archive")
if err := vfs.CreateArchive(context.Background(), []string{filepath.Join(dir, "inner.txt")}, filepath.Join(dir, "box.zip"), nil); err != nil {
t.Fatal(err)
}
work := t.TempDir()
writeFile(t, filepath.Join(work, "precious.txt"), "user data")
d := newDriver(t, dir, work)
selectName(t, d, "box.zip")
d.Press("enter")
d.Settle(settle)
if !activeOf(t, d).InArchive() {
t.Fatal("did not enter archive")
}
d.Press("p") // mirror: right pane now shows the archive's temp dir
d.Settle(settle)
m := cur(t, d)
// Developer C: refusing the mirror (passive pane stays on its own
// directory) is the chosen fix; a read-only mirror would pass too.
if m.passivePane().Path == work && !strings.Contains(m.status, "not supported") {
t.Fatalf("mirror neither applied nor refused visibly (status %q)", m.status)
}
if err := canWriteTo(m.passivePane()); err == nil && m.passivePane().Path != work {
// Show the actual loss: move a real file there, close the archive.
m.passivePane()
t.Fatalf("passive pane %q (mirror of an archive) is writable", m.passivePane().Path)
}
}
// Delete/mkdir in a pane that shows an archive's temp dir via mirror.
// (Documented together with QA-2.)
// --- U-P0-3: caret on every profile, with the status line and the filter ---
func TestQACaretAllProfilesNarrowTerminal(t *testing.T) {
for name, profile := range map[string]termenv.Profile{"Ascii": termenv.Ascii, "ANSI": termenv.ANSI, "ANSI256": termenv.ANSI256, "TrueColor": termenv.TrueColor} {
t.Run(name, func(t *testing.T) {
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "t.txt"), "строка один\n\tтаб\n"+strings.Repeat("длинная ", 40)+"\n")
m := newTestModel(t, dir, t.TempDir())
d := teadriver.New(t, m, 72, 18, teadriver.WithProfile(profile), teadriver.WithLongCmd(300*time.Millisecond))
d.Settle(settle)
selectName(t, d, "t.txt")
d.Press("f9")
d.Settle(settle)
d.Press("i")
d.Settle(settle)
d.Press("j", "j", "l", "l", "w", "v", "l", "l", "e", "b", "k")
_ = d.Screen()
d.Press("f3") // view mode on top
d.Settle(settle)
_ = d.Screen()
if n := len(strings.Split(d.Screen(), "\n")); n != 18 {
t.Fatalf("view height %d, want 18", n)
}
})
}
}
// --- U-P1-1: the status line ------------------------------------------------
// Multi-line and very long errors stay on one line and keep the height.
func TestQAStatusLineMultilineAndLong(t *testing.T) {
d := newDriver(t, t.TempDir(), t.TempDir())
m := cur(t, d)
m.status = "Copy failed: first line\nsecond line\r\n" + strings.Repeat("ошибка ", 100)
d2 := teadriver.New(t, m, 80, 20, teadriver.WithoutInit())
s := d2.Screen()
lines := strings.Split(s, "\n")
if len(lines) != 20 {
t.Fatalf("height %d, want 20", len(lines))
}
found := false
for _, l := range lines {
if strings.Contains(l, "first line second line") {
found = true
}
}
if !found {
t.Fatalf("status not joined on one line:\n%s", s)
}
}
// Status line under every profile does not change the line count.
func TestQAStatusLineProfiles(t *testing.T) {
for _, profile := range []termenv.Profile{termenv.Ascii, termenv.ANSI, termenv.TrueColor} {
dir := t.TempDir()
if err := os.Mkdir(filepath.Join(dir, "x"), 0o755); err != nil {
t.Fatal(err)
}
m := newTestModel(t, dir, t.TempDir())
d := teadriver.New(t, m, 100, 25, teadriver.WithProfile(profile), teadriver.WithLongCmd(300*time.Millisecond))
d.Settle(settle)
d.Press("f7")
d.Type("x")
d.Press("enter")
d.Settle(settle)
if !d.ScreenContains("file exists") {
t.Fatalf("profile %v: error not shown:\n%s", profile, d.Screen())
}
if n := len(strings.Split(d.Screen(), "\n")); n != 25 {
t.Fatalf("profile %v: %d lines", profile, n)
}
}
}
// --- U-P0-4: delete defaults to trash ----------------------------------------
// Multi-selection with names containing spaces and Cyrillic, plus a
// directory, all go to the trash.
func TestQADeleteToTrashMultiSelection(t *testing.T) {
dir := t.TempDir()
for _, n := range []string{"a b.txt", "отчёт.txt"} {
writeFile(t, filepath.Join(dir, n), n)
}
writeFile(t, filepath.Join(dir, "папка", "inner"), "x")
d := newDriver(t, dir, t.TempDir())
selectName(t, d, "a b.txt")
m := cur(t, d)
p := m.activePane()
for _, e := range p.Entries {
if !e.IsParent {
p.EnsureMarked(e.Path)
}
}
d = teadriver.New(t, m, 160, 40, teadriver.WithoutInit(), teadriver.WithLongCmd(300*time.Millisecond))
d.Press("f8")
d.Settle(settle)
if !d.ScreenContains("to trash") {
t.Fatalf("dialog does not say trash:\n%s", d.Screen())
}
d.Press("enter")
d.Settle(settle)
left, _ := os.ReadDir(dir)
if len(left) != 0 {
t.Fatalf("left in dir: %v (status %q)", left, cur(t, d).status)
}
trashed, _ := os.ReadDir(trashFiles(t))
if len(trashed) != 3 {
t.Fatalf("trash has %d items, want 3", len(trashed))
}
}
// D toggles to permanent and back: the second D must return to trash.
func TestQADeleteModeToggleBack(t *testing.T) {
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "a.txt"), "x")
d := newDriver(t, dir, t.TempDir())
selectName(t, d, "a.txt")
d.Press("f8", "D", "D")
d.Press("enter")
d.Settle(settle)
if !exists(filepath.Join(trashFiles(t), "a.txt")) {
t.Fatalf("after D,D the file should go to the trash (status %q)", cur(t, d).status)
}
}
// The delete kind must not leak into the next dialog: after a permanent
// delete, the next F8 starts in trash mode again.
func TestQADeleteModeResetsBetweenDialogs(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("f8", "D", "esc")
selectName(t, d, "b.txt")
d.Press("f8", "enter")
d.Settle(settle)
if !exists(filepath.Join(trashFiles(t), "b.txt")) {
t.Fatal("cancelled permanent mode leaked into the next delete")
}
}
// With trash as default, the user sees the failure when the trash is not
// usable (here: a file where the trash directory should be) and the file
// stays.
func TestQADeleteTrashFailureVisible(t *testing.T) {
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "a.txt"), "x")
d := newDriver(t, dir, t.TempDir())
trash := filepath.Join(os.Getenv("HOME"), ".local", "share", "Trash")
writeFile(t, trash, "not a dir")
selectName(t, d, "a.txt")
d.Press("f8", "enter")
d.Settle(settle)
if !exists(filepath.Join(dir, "a.txt")) {
t.Fatal("file lost although the trash is broken")
}
if st := cur(t, d).status; classifyStatus(st) != statusError || !d.ScreenContains("trash") {
t.Fatalf("trash failure not reported: %q\n%s", st, d.Screen())
}
}