//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) if got := readShellFrame(t, ctx, conns[1], func(m protocol.Message) bool { return m.ID == "size" }); got.Type != protocol.ChatShellResize || got.ChatShellRun.Cols != 120 { 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) } }