0963a071d0
Machine terminals no longer skip output for live viewers. Each connection's subscription is a cursor into the 1 MiB retained tail, drained by the connection's writer as soon as the PTY reader writes (no 50 ms tick; a streaming frame gathers for at most 2 ms). A subscription opened with flow: true gets window (256 KiB) in its reply and acknowledges drawn output with terminal_ack; while one is full the reader stops reading the PTY, so the program waits. A subscription full for 3 s without acking stops holding the terminal and later sees a gap; a gone connection drops its subscriptions. Clients that never ask for flow keep the old pacing (newest 64 KiB per 50 ms). terminal_open/terminal_output replies precede the frames that follow them, which start exactly at the reply's output_end. Chat shell runs share the buffer code: output is pushed as it arrives, oldest first, while the chat's watchers keep short queues; 256 KiB unpushed stops the reader, and watchers behind for 3 s get the old pacing. The app asks for flow, takes offsets from the server (frame end, reply output_end) instead of re-encoded text length, acks per quarter window, and writes replays and bursts into the emulator in slices that yield to the event loop (ChatShellTerminalFeed, shared with chat shells). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
110 lines
3.3 KiB
Go
110 lines
3.3 KiB
Go
//go:build darwin || linux
|
|
|
|
package server
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// A terminal ends with its server: the stop is recorded as ended, and a
|
|
// record a crashed (or tmux-era) server left running is listed as ended
|
|
// after the restart, saying why.
|
|
func TestTerminalEndsWithServer(t *testing.T) {
|
|
terminalHome(t)
|
|
store := newOperatorStore(t)
|
|
first, stop := newTestTerminals(t, store)
|
|
term, err := first.Open(context.Background(), "", "", 0, 0, false)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := first.Input(term.ID, fakeSuPassword+"\r"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
stop()
|
|
first.Wait()
|
|
crashed := protocol.TerminalRecord{ID: "term_crashed", Cwd: "/", CreatedAt: 1, Status: protocol.TerminalRunning, Cols: 80, Rows: 24}
|
|
if err := store.SaveTerminal(context.Background(), crashed, 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
second, _ := newTestTerminals(t, store)
|
|
list, err := second.List(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
reasons := map[string]string{}
|
|
for _, record := range list {
|
|
if record.Status != protocol.TerminalExited || record.EndedAt == 0 {
|
|
t.Fatalf("record = %+v", record)
|
|
}
|
|
reasons[record.ID] = record.Reason
|
|
}
|
|
if len(reasons) != 2 || reasons[term.ID] != terminalStopReason || reasons["term_crashed"] != terminalRestartReason {
|
|
t.Fatalf("reasons = %+v", reasons)
|
|
}
|
|
if _, err := second.Close(context.Background(), "term_crashed"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if list, _ := second.List(context.Background()); len(list) != 1 {
|
|
t.Fatalf("list after closing an ended terminal = %+v", list)
|
|
}
|
|
}
|
|
|
|
// The window title the shell sets is followed across reads and recorded.
|
|
func TestTerminalTitles(t *testing.T) {
|
|
var titles terminalTitles
|
|
if _, ok := titles.feed([]byte("plain \x1b[1mtext")); ok {
|
|
t.Fatal("title from plain text")
|
|
}
|
|
if _, ok := titles.feed([]byte("\x1b]0;~/src")); ok {
|
|
t.Fatal("title before its end")
|
|
}
|
|
if title, ok := titles.feed([]byte("/app\x07more")); !ok || title != "~/src/app" {
|
|
t.Fatalf("title = %q %v", title, ok)
|
|
}
|
|
if title, ok := titles.feed([]byte("\x1b]2;vim\x01 x\x1b\\")); !ok || title != "vim x" {
|
|
t.Fatalf("ST title = %q %v", title, ok)
|
|
}
|
|
if _, ok := titles.feed([]byte("\x1b]8;;https://x\x07")); ok {
|
|
t.Fatal("a hyperlink is not a title")
|
|
}
|
|
if _, ok := titles.feed(terminalLoginMarker); ok {
|
|
t.Fatal("the login marker is not a title")
|
|
}
|
|
if title, _ := titles.feed([]byte("\x1b]0;" + strings.Repeat("é", protocol.TerminalTitleLimit) + "\x07")); len(title) > protocol.TerminalTitleLimit {
|
|
t.Fatalf("title of %d bytes", len(title))
|
|
}
|
|
terminalHome(t)
|
|
m, _ := newTestTerminals(t, newOperatorStore(t))
|
|
term, err := m.Open(context.Background(), "", "", 0, 0, false)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := m.Input(term.ID, fakeSuPassword+"\r"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := m.Input(term.ID, "printf '\\033]0;build-%s\\007' ok\r"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
waitTerminalTitle(t, m, "build-ok")
|
|
}
|
|
|
|
func waitTerminalTitle(t *testing.T, m *terminalManager, want string) {
|
|
t.Helper()
|
|
deadline := time.Now().Add(5 * time.Second)
|
|
for {
|
|
list, _ := m.List(context.Background())
|
|
if len(list) == 1 && list[0].Title == want {
|
|
return
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatalf("list = %+v", list)
|
|
}
|
|
time.Sleep(50 * time.Millisecond)
|
|
}
|
|
}
|