ycdemo / fzf / src/ tui/ light_query_test.go

//go:build !windows

package tui

import (
	"os"
	"strings"
	"testing"
	"time"

	"golang.org/x/term"
)

// Drives queryStartup against a terminal simulated by pipes, with the replies
// already queued so the exchange is deterministic.
func replyingTerminal(t *testing.T, replies string) (*LightRenderer, func() string) {
	t.Helper()
	inR, inW, err := os.Pipe()
	if err != nil {
		t.Fatal(err)
	}
	outR, outW, err := os.Pipe()
	if err != nil {
		t.Fatal(err)
	}
	if _, err := inW.WriteString(replies); err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { inR.Close(); inW.Close(); outR.Close() })

	r := &LightRenderer{ttyin: inR, ttyout: outW, escDelay: defaultEscDelay}
	written := func() string {
		outW.Close()
		var sb strings.Builder
		buf := make([]byte, 4096)
		for {
			n, err := outR.Read(buf)
			sb.Write(buf[:n])
			if err != nil {
				break
			}
		}
		return sb.String()
	}
	return r, written
}

const eraseEcho = "\b \b"

func TestQueryStartup(t *testing.T) {
	for _, tc := range []struct {
		name     string
		replies  string
		row, col int
		paste    string // "", "true" or "false"
		erase    int    // columns the echo took, and so erasures expected
	}{
		{
			name:    "mode already set",
			replies: "\x1b[5;10R\x1b[?2004;1$y\x1b[5;10R",
			row:     4, col: 9, paste: "true",
		},
		{
			name:    "mode reset",
			replies: "\x1b[5;10R\x1b[?2004;2$y\x1b[5;10R",
			row:     4, col: 9, paste: "false",
		},
		{
			name:    "mode not recognized",
			replies: "\x1b[5;10R\x1b[?2004;0$y\x1b[5;10R",
			row:     4, col: 9,
		},
		{
			// Answers the position queries but ignores DECRQM without printing
			name:    "query ignored",
			replies: "\x1b[5;10R\x1b[5;10R",
			row:     4, col: 9,
		},
		{
			// macOS Terminal.app: ends the sequence at '
		
		
		
		
	

 and prints the 'p'
			name:    "query echoed",
			replies: "\x1b[5;10R\x1b[5;11R",
			row:     4, col: 9, erase: 1,
		},
		{
			// A terminal that prints more of what it could not parse
			name:    "longer echo",
			replies: "\x1b[5;10R\x1b[5;13R",
			row:     4, col: 9, erase: 3,
		},
		{
			// The echo wrapped to the next line, where erasing would guess
			name:    "echo wrapped",
			replies: "\x1b[5;80R\x1b[6;1R",
			row:     4, col: 79,
		},
		{
			name:    "no reply at all",
			replies: "",
			row:     -1, col: -1,
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			r, written := replyingTerminal(t, tc.replies)
			row, col, pasteWasSet := r.queryStartup()

			if row != tc.row || col != tc.col {
				t.Errorf("offset (%d,%d), want (%d,%d)", row, col, tc.row, tc.col)
			}
			paste := ""
			if pasteWasSet != nil {
				paste = "false"
				if *pasteWasSet {
					paste = "true"
				}
			}
			if paste != tc.paste {
				t.Errorf("pasteWasSet %q, want %q", paste, tc.paste)
			}

			out := written()
			if got := strings.Count(out, eraseEcho); got != tc.erase {
				t.Errorf("erased %d columns, want %d (wrote %q)", got, tc.erase, out)
			}
			// The paste mode query has to sit between two position queries
			if want := "\x1b[6n\x1b[?2004$p\x1b[6n"; !strings.Contains(out, want) {
				t.Errorf("queries %q, want %q in order", out, want)
			}
		})
	}
}

// The second-chance read has no time limit, so a key typed long after an
// unfinished sequence must not be dropped with it.
func TestLateKeyAfterUnfinishedSequence(t *testing.T) {
	inR, inW, err := os.Pipe()
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { inR.Close(); inW.Close() })
	inW.WriteString("\x1b[12")
	go func() {
		time.Sleep(2 * defaultEscDelay * time.Millisecond)
		inW.WriteString("x")
		time.Sleep(2 * defaultEscDelay * time.Millisecond)
		inW.WriteString("y") // ends the wait if x was dropped
	}()

	r := &LightRenderer{ttyin: inR, escDelay: defaultEscDelay}
	for range 3 {
		if ev := r.GetChar(false); ev.Type == Rune {
			if ev.Char != 'x' {
				t.Errorf("got %q, want 'x'", ev.Char)
			}
			return
		}
	}
	t.Error("the late key was dropped")
}

// A read error during the second-chance read must end fzf cleanly, not panic.
func TestSecondChanceReadError(t *testing.T) {
	inR, inW, err := os.Pipe()
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { inR.Close() })
	inW.WriteString("\x1b[12")
	go func() {
		time.Sleep(2 * defaultEscDelay * time.Millisecond)
		inW.Close()
	}()

	out, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
	if err != nil {
		t.Fatal(err)
	}
	r := &LightRenderer{ttyin: inR, ttyout: out, escDelay: defaultEscDelay, origState: &term.State{}}
	if ev := r.GetChar(false); ev.Type != Fatal {
		t.Errorf("got %s, want Fatal", ev.Type.String())
	}
}
and prints the 'p'\n\t\t\tname: \"query echoed\",\n\t\t\treplies: \"\\x1b[5;10R\\x1b[5;11R\",\n\t\t\trow: 4, col: 9, erase: 1,\n\t\t},\n\t\t{\n\t\t\t// A terminal that prints more of what it could not parse\n\t\t\tname: \"longer echo\",\n\t\t\treplies: \"\\x1b[5;10R\\x1b[5;13R\",\n\t\t\trow: 4, col: 9, erase: 3,\n\t\t},\n\t\t{\n\t\t\t// The echo wrapped to the next line, where erasing would guess\n\t\t\tname: \"echo wrapped\",\n\t\t\treplies: \"\\x1b[5;80R\\x1b[6;1R\",\n\t\t\trow: 4, col: 79,\n\t\t},\n\t\t{\n\t\t\tname: \"no reply at all\",\n\t\t\treplies: \"\",\n\t\t\trow: -1, col: -1,\n\t\t},\n\t} {\n\t\tt.Run(tc.name, func(t *testing.T) {\n\t\t\tr, written := replyingTerminal(t, tc.replies)\n\t\t\trow, col, pasteWasSet := r.queryStartup()\n\n\t\t\tif row != tc.row || col != tc.col {\n\t\t\t\tt.Errorf(\"offset (%d,%d), want (%d,%d)\", row, col, tc.row, tc.col)\n\t\t\t}\n\t\t\tpaste := \"\"\n\t\t\tif pasteWasSet != nil {\n\t\t\t\tpaste = \"false\"\n\t\t\t\tif *pasteWasSet {\n\t\t\t\t\tpaste = \"true\"\n\t\t\t\t}\n\t\t\t}\n\t\t\tif paste != tc.paste {\n\t\t\t\tt.Errorf(\"pasteWasSet %q, want %q\", paste, tc.paste)\n\t\t\t}\n\n\t\t\tout := written()\n\t\t\tif got := strings.Count(out, eraseEcho); got != tc.erase {\n\t\t\t\tt.Errorf(\"erased %d columns, want %d (wrote %q)\", got, tc.erase, out)\n\t\t\t}\n\t\t\t// The paste mode query has to sit between two position queries\n\t\t\tif want := \"\\x1b[6n\\x1b[?2004$p\\x1b[6n\"; !strings.Contains(out, want) {\n\t\t\t\tt.Errorf(\"queries %q, want %q in order\", out, want)\n\t\t\t}\n\t\t})\n\t}\n}\n\n// The second-chance read has no time limit, so a key typed long after an\n// unfinished sequence must not be dropped with it.\nfunc TestLateKeyAfterUnfinishedSequence(t *testing.T) {\n\tinR, inW, err := os.Pipe()\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tt.Cleanup(func() { inR.Close(); inW.Close() })\n\tinW.WriteString(\"\\x1b[12\")\n\tgo func() {\n\t\ttime.Sleep(2 * defaultEscDelay * time.Millisecond)\n\t\tinW.WriteString(\"x\")\n\t\ttime.Sleep(2 * defaultEscDelay * time.Millisecond)\n\t\tinW.WriteString(\"y\") // ends the wait if x was dropped\n\t}()\n\n\tr := &LightRenderer{ttyin: inR, escDelay: defaultEscDelay}\n\tfor range 3 {\n\t\tif ev := r.GetChar(false); ev.Type == Rune {\n\t\t\tif ev.Char != 'x' {\n\t\t\t\tt.Errorf(\"got %q, want 'x'\", ev.Char)\n\t\t\t}\n\t\t\treturn\n\t\t}\n\t}\n\tt.Error(\"the late key was dropped\")\n}\n\n// A read error during the second-chance read must end fzf cleanly, not panic.\nfunc TestSecondChanceReadError(t *testing.T) {\n\tinR, inW, err := os.Pipe()\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tt.Cleanup(func() { inR.Close() })\n\tinW.WriteString(\"\\x1b[12\")\n\tgo func() {\n\t\ttime.Sleep(2 * defaultEscDelay * time.Millisecond)\n\t\tinW.Close()\n\t}()\n\n\tout, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\tr := &LightRenderer{ttyin: inR, ttyout: out, escDelay: defaultEscDelay, origState: &term.State{}}\n\tif ev := r.GetChar(false); ev.Type != Fatal {\n\t\tt.Errorf(\"got %s, want Fatal\", ev.Type.String())\n\t}\n}\n","preview":null,"history":null,"version":"a5ab1f487d16513f256d89cb78030604679fd22c"},"/_session?shell=1":null}