//go:build darwin || linux package server import ( "context" "strings" "testing" "time" "git.a-wan.net/alan/pipelines/internal/protocol" ) // startTerminal starts command in a real pseudo-terminal of cols x rows. func startTerminal(t *testing.T, runner *chatShellRunner, command string, cols, rows int) protocol.ChatShellRunRecord { t.Helper() run, err := runner.Start(context.Background(), "ses_1", t.TempDir(), command, cols, rows) if err != nil { t.Fatal(err) } if !run.Terminal { t.Fatalf("run is not in a terminal: %+v", run) } t.Cleanup(func() { _, _ = runner.Stop("ses_1", run.ID) runner.Wait() }) return run } // waitOutput waits until the run's retained output contains want. func waitOutput(t *testing.T, runner *chatShellRunner, runID, want string) { t.Helper() deadline := time.Now().Add(10 * time.Second) for time.Now().Before(deadline) { record, err := runner.Read(context.Background(), "ses_1", runID) if err != nil { t.Fatal(err) } if strings.Contains(record.Output, want) { return } time.Sleep(10 * time.Millisecond) } record, _ := runner.Read(context.Background(), "ses_1", runID) t.Fatalf("output never contained %q: %q", want, record.Output) } func finishedTerminal(t *testing.T, runner *chatShellRunner, frames *shellFrames, runID string) protocol.ChatShellRunRecord { t.Helper() runner.Wait() if final := frames.final(runID); final == nil { t.Fatal("no final lifecycle frame") } stored, err := runner.Read(context.Background(), "ses_1", runID) if err != nil { t.Fatal(err) } return stored } func TestChatShellTerminalReadsTypedLine(t *testing.T) { runner, frames := testShellRunner(t, context.Background()) run := startTerminal(t, runner, `[ -t 0 ] && echo tty:$TERM; printf 'name? '; read -r x; echo got:$x`, 0, 0) waitOutput(t, runner, run.ID, "name? ") if err := runner.Input("ses_2", run.ID, "intruder\r"); err == nil { t.Fatal("another session typed into the run") } if err := runner.Input("ses_1", run.ID, "hello world\r"); err != nil { t.Fatal(err) } stored := finishedTerminal(t, runner, frames, run.ID) if !strings.Contains(stored.Output, "tty:xterm-256color") || !strings.Contains(stored.Output, "got:hello world") || stored.ExitCode == nil || *stored.ExitCode != 0 { t.Fatalf("stored = %+v", stored) } if err := runner.Input("ses_1", run.ID, "late\r"); err == nil { t.Fatal("input reached a finished run") } } func TestChatShellTerminalHiddenInputIsNotRecorded(t *testing.T) { runner, frames := testShellRunner(t, context.Background()) run := startTerminal(t, runner, `stty -echo; printf 'Password: '; read -r pw; stty echo; echo; echo length:${#pw}`, 0, 0) waitOutput(t, runner, run.ID, "Password: ") if err := runner.Input("ses_1", run.ID, "s3cret-token\r"); err != nil { t.Fatal(err) } stored := finishedTerminal(t, runner, frames, run.ID) if !strings.Contains(stored.Text, "length:12") { t.Fatalf("text = %q", stored.Text) } if strings.Contains(stored.Output, "s3cret") || strings.Contains(stored.Text, "s3cret") { t.Fatalf("hidden input reached the record: %q", stored.Output) } frames.mu.Lock() defer frames.mu.Unlock() for _, frame := range frames.frames { if strings.Contains(frame.Output, "s3cret") { t.Fatalf("hidden input reached a frame: %q", frame.Output) } } } func TestChatShellTerminalControlCInterrupts(t *testing.T) { runner, frames := testShellRunner(t, context.Background()) run := startTerminal(t, runner, `echo ready; sleep 30; echo never`, 0, 0) waitOutput(t, runner, run.ID, "ready") started := time.Now() if err := runner.Input("ses_1", run.ID, "\x03"); err != nil { t.Fatal(err) } stored := finishedTerminal(t, runner, frames, run.ID) if time.Since(started) > 5*time.Second || strings.Contains(stored.Output, "never") || stored.Status != protocol.ChatShellRunExited { t.Fatalf("after Ctrl-C = %+v", stored) } if stored.Signal != "interrupt" && (stored.ExitCode == nil || *stored.ExitCode != 130) { t.Fatalf("Ctrl-C ended it with %q / %v", stored.Signal, stored.ExitCode) } } func TestChatShellTerminalResizeReachesTheProcess(t *testing.T) { runner, frames := testShellRunner(t, context.Background()) run := startTerminal(t, runner, `stty size; echo ready; read -r x; stty size`, 90, 25) if run.Cols != 90 || run.Rows != 25 { t.Fatalf("started at %dx%d", run.Cols, run.Rows) } waitOutput(t, runner, run.ID, "ready") if _, err := runner.Resize("ses_1", run.ID, 0, 10); err == nil { t.Fatal("a zero size was accepted") } record, err := runner.Resize("ses_1", run.ID, 132, 41) if err != nil || record.Cols != 132 || record.Rows != 41 { t.Fatalf("resize = %+v, %v", record, err) } // Sizes are clamped to the protocol's bounds. if record, err = runner.Resize("ses_1", run.ID, 5000, 1); err != nil || record.Cols != protocol.ChatShellTerminalMaxCols || record.Rows != protocol.ChatShellTerminalMinRows { t.Fatalf("clamped resize = %+v, %v", record, err) } if _, err = runner.Resize("ses_1", run.ID, 132, 41); err != nil { t.Fatal(err) } if err := runner.Input("ses_1", run.ID, "\r"); err != nil { t.Fatal(err) } stored := finishedTerminal(t, runner, frames, run.ID) if !strings.Contains(stored.Text, "25 90\n") || !strings.Contains(stored.Text, "41 132\n") || stored.Cols != 132 || stored.Rows != 41 { t.Fatalf("sizes seen = %q (%dx%d)", stored.Text, stored.Cols, stored.Rows) } } func TestChatShellTerminalPersistsRawAndPlainText(t *testing.T) { runner, frames := testShellRunner(t, context.Background()) run := startTerminal(t, runner, `printf '\033[31mred\033[0m\n'; printf 'progress 10%%\rprogress 100%%\n'; printf '\033]8;;https://example.com\033\\link\033]8;;\033\\\n'`, 0, 0) stored := finishedTerminal(t, runner, frames, run.ID) if !strings.Contains(stored.Output, "\x1b[31mred\x1b[0m\r\n") || !strings.Contains(stored.Output, "progress 10%\rprogress 100%") { t.Fatalf("raw output = %q", stored.Output) } if stored.Text != "red\nprogress 100%\nlink\n" { t.Fatalf("plain text = %q", stored.Text) } if !stored.Terminal || stored.Cols != protocol.ChatShellTerminalDefaultCols { t.Fatalf("stored terminal facts = %+v", stored) } list, err := runner.List(context.Background(), "ses_1") if err != nil || len(list) != 1 || list[0].Text != "" || list[0].Output != stored.Output { t.Fatalf("list = %+v, %v", list, err) } if prompt := chatShellSharePrompt(stored); strings.Contains(prompt, "\x1b") || !strings.Contains(prompt, "red\nprogress 100%\nlink\n```") { t.Fatalf("shared prompt = %q", prompt) } } func TestChatShellTerminalInputBounds(t *testing.T) { runner, _ := testShellRunner(t, context.Background()) // The command never reads, so queued input piles up. run := startTerminal(t, runner, `stty -icanon -echo min 1; sleep 30`, 0, 0) if err := runner.Input("ses_1", run.ID, strings.Repeat("x", protocol.ChatShellInputLimit+1)); err == nil { t.Fatal("oversized input was accepted") } if err := runner.Input("ses_1", run.ID, ""); err == nil { t.Fatal("empty input was accepted") } if err := runner.Input("ses_1", run.ID, "\xff"); err == nil { t.Fatal("invalid UTF-8 was accepted") } chunk := strings.Repeat("y", protocol.ChatShellInputLimit) refused := false for range protocol.ChatShellInputQueue + 64 { if err := runner.Input("ses_1", run.ID, chunk); err != nil { refused = strings.Contains(err.Error(), "not reading") break } } if !refused { t.Fatal("input to a command that never reads was never refused") } } func TestChatShellRecordAfterResumes(t *testing.T) { record := protocol.ChatShellRunRecord{Output: "héllo world", OutputStart: 100, OutputBytes: 112} if got := chatShellRecordAfter(record, 107); got.Output != "world" || got.OutputStart != 107 { t.Fatalf("resume = %q at %d", got.Output, got.OutputStart) } if got := chatShellRecordAfter(record, 102); got.Output != "llo world" || got.OutputStart != 103 { t.Fatalf("resume inside a character = %q at %d", got.Output, got.OutputStart) } if got := chatShellRecordAfter(record, 50); got.Output != record.Output || got.OutputStart != 100 { t.Fatalf("resume before the tail = %q at %d", got.Output, got.OutputStart) } if got := chatShellRecordAfter(record, 500); got.Output != "" || got.OutputStart != 112 { t.Fatalf("resume after the end = %q at %d", got.Output, got.OutputStart) } }