package app import ( "strings" "testing" "github.com/gdamore/tcell/v2" ) // pasteKeys sends text as a terminal's bracketed paste would: a start mark, // the characters as keys with Enter for each newline, an end mark. func pasteKeys(a *App, text string) { a.handle(tcell.NewEventPaste(true)) for _, r := range text { if r == '\n' { a.handle(tcell.NewEventKey(tcell.KeyEnter, 0, tcell.ModNone)) continue } a.handle(tcell.NewEventKey(tcell.KeyRune, r, tcell.ModNone)) } a.handle(tcell.NewEventPaste(false)) } func TestABracketedPasteReachesAnAgentWindowWhole(t *testing.T) { // Key by key, the two Enters would each have sent a prompt; whole, the // three lines are one paste, kept aside as a token. a, _ := newAgentsApp(t, "[[agent]]\nname=\"Echo\"\ncommand=\"cat\"\n") a.NewAgent("Echo") agent := a.activeAgent() if agent == nil { t.Fatal("no agent window is in front") } pasteKeys(a, "one\ntwo\nthree") if got := agent.view.Input(); got != "[Pasted #1 · 3 lines · 13 chars]" { t.Errorf("the box holds %q", got) } if entries := agent.session.Entries(); len(entries) != 0 { t.Errorf("%d prompt(s) were sent by the newlines inside the paste", len(entries)) } } func TestABracketedPasteIntoAFileIsTypedAsBefore(t *testing.T) { a, _ := newTestApp(t) a.NewFile() pasteKeys(a, "ab") if got := a.activeView().Buffer().Text(); got != "ab" { t.Errorf("the file holds %q after a paste, want %q", got, "ab") } } func TestKeysAreNotLostWhenAPasteIsCancelledByAnotherWindow(t *testing.T) { // The paste starts in an agent window; nothing else in the editor sees // its keys until it ends — so a key layer cannot half-act on them. a, _ := newAgentsApp(t, "[[agent]]\nname=\"Echo\"\ncommand=\"cat\"\n") a.NewAgent("Echo") a.handle(tcell.NewEventPaste(true)) a.handle(tcell.NewEventKey(tcell.KeyF4, 0, tcell.ModNone)) // New file, were it a keystroke a.handle(tcell.NewEventPaste(false)) if len(a.desktop.Windows()) != 1 { t.Errorf("%d windows are open: a key inside a paste acted as a shortcut", len(a.desktop.Windows())) } } func TestEditPasteGoesIntoTheAgentBox(t *testing.T) { a, _ := newAgentsApp(t, "[[agent]]\nname=\"Echo\"\ncommand=\"cat\"\n") a.NewAgent("Echo") a.clipboard.SetText("from a file\nin the editor\n") a.Paste() if got := a.activeAgent().view.Input(); !strings.HasPrefix(got, "[Pasted #1 · 2 lines") { t.Errorf("the box holds %q after Edit ▸ Paste", got) } } func TestCtrlVInAnAgentWindowPastesTheEditorsClipboard(t *testing.T) { a, _ := newAgentsApp(t, "[[agent]]\nname=\"Echo\"\ncommand=\"cat\"\n") a.NewAgent("Echo") a.clipboard.SetText("buildMenus") press(a, tcell.KeyCtrlV, 0, tcell.ModNone) if got := a.activeAgent().view.Input(); got != "buildMenus" { t.Errorf("the box holds %q after Ctrl-V", got) } }