c3458d8a7f
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>
61 lines
2.3 KiB
Go
61 lines
2.3 KiB
Go
package server
|
|
|
|
import "testing"
|
|
|
|
func TestTerminalViewersActiveClient(t *testing.T) {
|
|
var v terminalViewers
|
|
// The first client to resize is active and sizes the terminal.
|
|
if got := v.resize("a", 156, 37, false); got != (terminalSize{156, 37, true}) {
|
|
t.Fatalf("first resize = %+v", got)
|
|
}
|
|
v.attach("b")
|
|
// Another client's size is kept but not applied; its answers are dropped.
|
|
if got := v.resize("b", 121, 11, false); got.ok {
|
|
t.Fatalf("background resize applied: %+v", got)
|
|
}
|
|
if !v.answers("a") || v.answers("b") || v.answers("stranger") {
|
|
t.Fatal("only the active client answers")
|
|
}
|
|
// Typing hands the terminal to b, at b's size; typing again changes nothing.
|
|
if got := v.typed("b"); got != (terminalSize{121, 11, true}) {
|
|
t.Fatalf("typed = %+v", got)
|
|
}
|
|
if got := v.typed("b"); got.ok || v.active() != "b" || v.answers("a") {
|
|
t.Fatalf("typed again = %+v, active %q", got, v.active())
|
|
}
|
|
// Focusing (activate) hands it back at a's new size.
|
|
if got := v.resize("a", 150, 40, true); got != (terminalSize{150, 40, true}) || v.active() != "a" {
|
|
t.Fatalf("activate = %+v", got)
|
|
}
|
|
// A background client leaving changes nothing; the active one leaving
|
|
// hands the terminal to the next most recent, at its last size.
|
|
v.attach("c")
|
|
if got := v.leave("c"); got.ok {
|
|
t.Fatalf("background leave = %+v", got)
|
|
}
|
|
if got := v.leave("a"); got != (terminalSize{121, 11, true}) || v.active() != "b" || !v.answers("b") {
|
|
t.Fatalf("leave = %+v, active %q", got, v.active())
|
|
}
|
|
if got := v.leave("b"); got.ok || v.active() != "" || !v.answers("anyone") {
|
|
t.Fatalf("last leave = %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestTerminalViewersWithoutSizes(t *testing.T) {
|
|
var v terminalViewers
|
|
v.attach("reader")
|
|
// The active client has drawn nothing yet: another's size is applied.
|
|
if got := v.resize("other", 90, 20, false); !got.ok || v.active() != "reader" {
|
|
t.Fatalf("resize = %+v, active %q", got, v.active())
|
|
}
|
|
// Typing from a client of unknown size makes it active without a resize.
|
|
if got := v.typed("typist"); got.ok || v.active() != "typist" {
|
|
t.Fatalf("typed = %+v", got)
|
|
}
|
|
// A connection that is not told apart (tests, internal callers) always
|
|
// applies and answers.
|
|
if got := v.resize("", 10, 5, false); !got.ok || !v.answers("") {
|
|
t.Fatalf("anonymous = %+v", got)
|
|
}
|
|
}
|