Files
pipelines/internal/server/chat_shell_terminal_ws_test.go
T
alan c3458d8a7f Terminals: one active client per terminal; keystrokes skip the request queue
Several clients drawing one terminal (machine terminal or chat terminal
command) no longer interfere. The server tracks, per terminal, the
connections drawing it, most recently active first: the active client
(the opener, then whoever last typed or focused it) owns the PTY size and
is the only one whose emulator's automatic query answers reach the
process; other clients' answers are acknowledged and dropped, and their
sizes recorded but not applied. When the active connection closes the next
most recent client's size is applied.

Protocol (backwards compatible; all new fields optional):
- terminal_input / chat_shell_input: reply (emulator answer), cols/rows
  (the typist's size, applied before the input).
- terminal_resize / chat_shell_resize: activate (the client focused it).

Latency: PipelinesClient sends terminal_input, terminal_resize,
chat_shell_input and chat_shell_resize at once rather than behind the
previous request's reply. The server reads those onto a per-connection
terminal lane handled in order beside the ordinary request loop, and
terminal_open (up to 5 s waiting for su) now runs off the read loop.

Client: ChatShellTerminalSession routes what the emulator emits while
drawing output to onReply and reports focus through activate();
ChatShellInputPipe keeps answers and typing in separate requests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 23:37:16 +00:00

69 lines
3.1 KiB
Go

//go:build darwin || linux
package server
import (
"strings"
"testing"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// One client starts an interactive command; the other client watching the
// chat types into it, resizes it, and resumes its output by offset.
func TestChatShellTerminalOverWebsocket(t *testing.T) {
ctx, conns, _ := shellChat(t)
start := shellRequest(protocol.ChatShellRun, "r1", "", `printf 'token? '; read -r tok; stty size; echo got:$tok`)
start.ChatShellRunRequest.Cols, start.ChatShellRunRequest.Rows = 80, 24
writeChat(t, ctx, conns[0], start)
reply := readShellFrame(t, ctx, conns[0], func(m protocol.Message) bool { return m.ID == "r1" })
run := reply.ChatShellRun
if reply.Type != protocol.ChatShellRun || run == nil || !run.Terminal || run.Cols != 80 || run.Rows != 24 {
t.Fatalf("start reply = %+v", reply)
}
// The prompt arrives as an output frame before anything is typed.
var seen strings.Builder
var end int64
for !strings.Contains(seen.String(), "token? ") {
frame := readShellFrame(t, ctx, conns[1], func(m protocol.Message) bool {
return m.Type == protocol.ChatShellRunEvent && m.ChatShellRunEvent.RunID == run.ID && m.ChatShellRunEvent.Run == nil
}).ChatShellRunEvent
if frame.Offset != end {
t.Fatalf("frame at %d after %d", frame.Offset, end)
}
seen.WriteString(frame.Output)
end = frame.End
}
resize := shellRequest(protocol.ChatShellResize, "size", run.ID, "")
resize.ChatShellRunRequest.Cols, resize.ChatShellRunRequest.Rows = 120, 33
writeChat(t, ctx, conns[1], resize)
// The starter stays the active client until the watcher types: then the
// watcher's size applies (stty size below).
if got := readShellFrame(t, ctx, conns[1], func(m protocol.Message) bool { return m.ID == "size" }); got.Type != protocol.ChatShellResize || got.ChatShellRun.Cols != 80 {
t.Fatalf("resize reply = %+v", got)
}
input := shellRequest(protocol.ChatShellInput, "in", run.ID, "")
input.ChatShellRunRequest.Data = "abc123\r"
writeChat(t, ctx, conns[1], input)
if got := readShellFrame(t, ctx, conns[1], func(m protocol.Message) bool { return m.ID == "in" }); got.Type != protocol.ChatShellInput {
t.Fatalf("input reply = %+v", got)
}
final, _ := finishedShell(t, ctx, conns[0], run.ID)
if final.Status != protocol.ChatShellRunExited {
t.Fatalf("final = %+v", final)
}
// A reconnecting client asks only for what follows its copy.
resume := shellRequest(protocol.ChatShellRunOutput, "resume", run.ID, "")
resume.ChatShellRunRequest.After = end
writeChat(t, ctx, conns[1], resume)
got := readShellFrame(t, ctx, conns[1], func(m protocol.Message) bool { return m.ID == "resume" }).ChatShellRun
if got == nil || got.OutputStart != end || strings.Contains(got.Output, "token?") || !strings.Contains(got.Output, "33 120\r\ngot:abc123\r\n") || !strings.Contains(got.Text, "got:abc123") {
t.Fatalf("resumed = %+v", got)
}
// Input to a finished run is refused.
writeChat(t, ctx, conns[1], input)
if got := readShellFrame(t, ctx, conns[1], func(m protocol.Message) bool { return m.ID == "in" }); got.Type != protocol.Error {
t.Fatalf("late input reply = %+v", got)
}
}