Files
pipelines/internal/server/chat_close_test.go
T
alan 1b98877d88 Chat list activity: turn running and background tasks on every row, pushed to every connection
chat_sessions rows carry activity (turn_running, background_tasks, seq) from
the hub's own state, and chat_activity is pushed to every connection when a
session's turn starts or ends or its running background-task count changes.
A room whose last viewer left stays observed while background tasks run,
bounded by the unattended limits; a Claude Code process exit ends its tasks.
2026-11-03 19:45:43 +00:00

314 lines
12 KiB
Go

package server
import (
"strings"
"testing"
"github.com/coder/websocket/wsjson"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// TestChatCloseRecordsClosedState is the close-records-state evidence: a
// chat_close on a recorded operator session answers with the closed session, sets
// closed_at in the registry and leaves the session listed (as closed) by
// chat_sessions, so every client sees the same state.
func TestChatCloseRecordsClosedState(t *testing.T) {
store := newOperatorStore(t)
broker := scriptedChat()
broker.sessions = []protocol.ChatSession{{ID: "ses_1", Title: "A", Directory: "/work/a"}}
ctx, _, conn := dialChatOptions(t, Options{Chat: broker, OperatorRegistry: store})
if err := store.RecordOperatorSession(ctx, "ses_1", "/work/a", "A", protocol.ChatSessionTagOperator); err != nil {
t.Fatal(err)
}
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatClose, ID: "cl1", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"}}); err != nil {
t.Fatal(err)
}
var response protocol.Message
if err := readTestFrame(ctx, conn, &response); err != nil {
t.Fatal(err)
}
if response.Type != protocol.ChatClose || response.ID != "cl1" {
t.Fatalf("chat_close response = %+v", response)
}
if response.ChatSession == nil || !response.ChatSession.Closed || response.ChatSession.ClosedAt <= 0 {
t.Fatalf("closed session not reported: %+v", response.ChatSession)
}
if response.ChatClose == nil || response.ChatClose.SessionID != "ses_1" || response.ChatClose.ClosedAt <= 0 {
t.Fatalf("chat_close result = %+v", response.ChatClose)
}
if response.ChatClose.Interrupted {
t.Fatalf("idle session reported an interrupted turn: %+v", response.ChatClose)
}
if _, interrupts, _ := broker.counts(); interrupts != 0 {
t.Fatalf("idle close interrupted a turn: %d", interrupts)
}
rows, err := store.OperatorSessions(ctx)
if err != nil {
t.Fatal(err)
}
if len(rows) != 1 || rows[0].ClosedAt <= 0 {
t.Fatalf("registry did not record the closed state: %+v", rows)
}
// The list still carries the session, marked closed, so it is reachable as
// history rather than disappearing.
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatSessions, ID: "l1"}); err != nil {
t.Fatal(err)
}
if err := readTestFrame(ctx, conn, &response); err != nil {
t.Fatal(err)
}
if response.Type != protocol.ChatSessions || len(response.ChatSessions) != 1 {
t.Fatalf("chat_sessions = %+v", response)
}
if !response.ChatSessions[0].Closed || response.ChatSessions[0].ClosedAt <= 0 {
t.Fatalf("closed session not listed as closed: %+v", response.ChatSessions[0])
}
}
// TestChatCloseStopsRunningTurn is the close-stops-a-running-turn evidence: a
// prompt holds the session (its stream is live), and chat_close from another
// connection interrupts it through the existing broker interrupt and reports that
// a turn was stopped.
func TestChatCloseStopsRunningTurn(t *testing.T) {
store := newOperatorStore(t)
broker := scriptedChat()
broker.block = true
broker.started = make(chan struct{})
broker.done = make(chan struct{})
ctx, _, conns := dialChatConns(t, Options{Chat: broker, OperatorRegistry: store}, 2)
if err := store.RecordOperatorSession(ctx, "ses_1", "/work/a", "A", protocol.ChatSessionTagOperator); err != nil {
t.Fatal(err)
}
// Start a long-running turn on the first connection.
if err := wsjson.Write(ctx, conns[0], protocol.Message{Type: protocol.ChatPrompt, ID: "r1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "long running"}}); err != nil {
t.Fatal(err)
}
var ack protocol.Message
if err := readTestFrame(ctx, conns[0], &ack); err != nil {
t.Fatal(err)
}
if ack.Type != protocol.ChatPrompt {
t.Fatalf("prompt ack = %+v", ack)
}
<-broker.started
// Close from the second connection; it must interrupt the running turn.
if err := wsjson.Write(ctx, conns[1], protocol.Message{Type: protocol.ChatClose, ID: "cl1", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"}}); err != nil {
t.Fatal(err)
}
var response protocol.Message
if err := readTestFrame(ctx, conns[1], &response); err != nil {
t.Fatal(err)
}
if response.Type != protocol.ChatClose || response.ID != "cl1" {
t.Fatalf("chat_close response = %+v", response)
}
if response.ChatClose == nil || !response.ChatClose.Interrupted {
t.Fatalf("close did not record a stopped turn: %+v", response.ChatClose)
}
if _, interrupts, _ := broker.counts(); interrupts != 1 {
t.Fatalf("interrupts = %d, want 1", interrupts)
}
rows, err := store.OperatorSessions(ctx)
if err != nil {
t.Fatal(err)
}
if len(rows) != 1 || rows[0].ClosedAt <= 0 {
t.Fatalf("registry did not record the closed state: %+v", rows)
}
}
// TestChatCloseKeepsSessionAndMessages pins that closing deletes nothing: the
// registry row survives (only its closed_at changes) and the session's history is
// still readable afterwards.
func TestChatCloseKeepsSessionAndMessages(t *testing.T) {
store := newOperatorStore(t)
broker := scriptedChat()
broker.messages = []protocol.ChatMessage{{ID: "m1", SessionID: "ses_1", Role: protocol.ChatRoleUser}}
ctx, _, conn := dialChatOptions(t, Options{Chat: broker, OperatorRegistry: store})
if err := store.RecordOperatorSession(ctx, "ses_1", "/work/a", "A", protocol.ChatSessionTagOperator); err != nil {
t.Fatal(err)
}
before, err := store.OperatorSessions(ctx)
if err != nil {
t.Fatal(err)
}
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatClose, ID: "cl1", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"}}); err != nil {
t.Fatal(err)
}
var response protocol.Message
if err := readTestFrame(ctx, conn, &response); err != nil {
t.Fatal(err)
}
if response.Type != protocol.ChatClose {
t.Fatalf("close failed: %+v", response)
}
after, err := store.OperatorSessions(ctx)
if err != nil {
t.Fatal(err)
}
if len(after) != 1 || after[0].ID != before[0].ID || after[0].Directory != before[0].Directory ||
after[0].Title != before[0].Title || after[0].Tag != before[0].Tag || after[0].CreatedAt != before[0].CreatedAt {
t.Fatalf("close changed the session beyond closed_at:\nbefore %+v\nafter %+v", before[0], after[0])
}
// The transcript is still readable.
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatMessages, ID: "ms1", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"}}); err != nil {
t.Fatal(err)
}
if err := readTestFrame(ctx, conn, &response); err != nil {
t.Fatal(err)
}
if response.Type != protocol.ChatMessages || len(response.ChatMessages) != 1 || response.ChatMessages[0].ID != "m1" {
t.Fatalf("history lost after close: %+v", response)
}
if calls, _, _ := broker.messageQuery(); calls != 1 {
t.Fatalf("message reads = %d, want 1", calls)
}
}
// TestChatReopenClearsClosedState pins that chat_reopen is the inverse of
// chat_close: the closed state is cleared and the session is listed as active
// again.
func TestChatReopenClearsClosedState(t *testing.T) {
store := newOperatorStore(t)
broker := scriptedChat()
ctx, _, conn := dialChatOptions(t, Options{Chat: broker, OperatorRegistry: store})
if err := store.RecordOperatorSession(ctx, "ses_1", "/work/a", "A", protocol.ChatSessionTagOperator); err != nil {
t.Fatal(err)
}
if err := store.CloseOperatorSession(ctx, "ses_1"); err != nil {
t.Fatal(err)
}
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatReopen, ID: "op1", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"}}); err != nil {
t.Fatal(err)
}
var response protocol.Message
if err := readTestFrame(ctx, conn, &response); err != nil {
t.Fatal(err)
}
if response.Type != protocol.ChatReopen || response.ID != "op1" || response.ChatSession == nil {
t.Fatalf("chat_reopen response = %+v", response)
}
if response.ChatSession.Closed || response.ChatSession.ClosedAt != 0 {
t.Fatalf("reopened session still closed: %+v", response.ChatSession)
}
rows, err := store.OperatorSessions(ctx)
if err != nil {
t.Fatal(err)
}
if len(rows) != 1 || rows[0].ClosedAt != 0 {
t.Fatalf("registry still closed after reopen: %+v", rows)
}
}
// TestChatPromptReopensClosedSession pins the second reopen path: simply
// prompting a closed session clears its closed state, so the operator never has
// to make an explicit reopen request.
func TestChatPromptReopensClosedSession(t *testing.T) {
store := newOperatorStore(t)
broker := scriptedChat()
ctx, _, conn := dialChatOptions(t, Options{Chat: broker, OperatorRegistry: store})
if err := store.RecordOperatorSession(ctx, "ses_1", "/work/a", "A", protocol.ChatSessionTagOperator); err != nil {
t.Fatal(err)
}
if err := store.CloseOperatorSession(ctx, "ses_1"); err != nil {
t.Fatal(err)
}
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "r1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "resume"}}); err != nil {
t.Fatal(err)
}
var ack protocol.Message
if err := readTestFrame(ctx, conn, &ack); err != nil {
t.Fatal(err)
}
if ack.Type != protocol.ChatPrompt {
t.Fatalf("prompt ack = %+v", ack)
}
_ = readChatTurn(t, ctx, conn)
rows, err := store.OperatorSessions(ctx)
if err != nil {
t.Fatal(err)
}
if len(rows) != 1 || rows[0].ClosedAt != 0 {
t.Fatalf("prompting did not clear the closed state: %+v", rows)
}
}
// TestChatCloseRefusesNonOperatorSession pins that a pipeline-owned run session
// (which has no operator_sessions row) is refused and nothing is written, so the
// close never touches the sessions the pipelines own.
func TestChatCloseRefusesNonOperatorSession(t *testing.T) {
store := newOperatorStore(t)
broker := scriptedChat()
ctx, _, conn := dialChatOptions(t, Options{Chat: broker, OperatorRegistry: store})
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatClose, ID: "cl1", ChatQuery: &protocol.ChatQuery{SessionID: "ses_run"}}); err != nil {
t.Fatal(err)
}
var response protocol.Message
if err := readTestFrame(ctx, conn, &response); err != nil {
t.Fatal(err)
}
if response.Type != protocol.Error || !strings.Contains(response.Error, "not an operator") {
t.Fatalf("non-operator session not refused: %+v", response)
}
if _, interrupts, _ := broker.counts(); interrupts != 0 {
t.Fatalf("refused close still interrupted: %d", interrupts)
}
rows, err := store.OperatorSessions(ctx)
if err != nil {
t.Fatal(err)
}
if len(rows) != 0 {
t.Fatalf("refused close wrote a registry row: %+v", rows)
}
}
// TestChatCloseRejectsInvalidInput pins that a missing/invalid session id and an
// unconfigured broker/registry are answered with an error before any write.
func TestChatCloseRejectsInvalidInput(t *testing.T) {
store := newOperatorStore(t)
broker := scriptedChat()
ctx, _, conn := dialChatOptions(t, Options{Chat: broker, OperatorRegistry: store})
for _, query := range []*protocol.ChatQuery{nil, {}, {SessionID: "not-a-session"}} {
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatClose, ID: "bad", ChatQuery: query}); err != nil {
t.Fatal(err)
}
var response protocol.Message
if err := readTestFrame(ctx, conn, &response); err != nil {
t.Fatal(err)
}
if response.Type != protocol.Error || response.ID != "bad" {
t.Fatalf("query %+v not rejected: %+v", query, response)
}
}
// No registry configured: refuse rather than close nothing.
noRegCtx, cancelNoReg, noRegConn := dialChatOptions(t, Options{Chat: broker})
defer cancelNoReg()
if err := wsjson.Write(noRegCtx, noRegConn, protocol.Message{Type: protocol.ChatClose, ID: "nr", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"}}); err != nil {
t.Fatal(err)
}
var response protocol.Message
if err := readTestFrame(noRegCtx, noRegConn, &response); err != nil {
t.Fatal(err)
}
if response.Type != protocol.Error || !strings.Contains(response.Error, "registry") {
t.Fatalf("close without registry = %+v", response)
}
// No broker configured: the existing "not configured" error.
noBrokerCtx, cancelNoBroker, noBrokerConn := dialChatOptions(t, Options{OperatorRegistry: store})
defer cancelNoBroker()
if err := wsjson.Write(noBrokerCtx, noBrokerConn, protocol.Message{Type: protocol.ChatClose, ID: "nb", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"}}); err != nil {
t.Fatal(err)
}
if err := readTestFrame(noBrokerCtx, noBrokerConn, &response); err != nil {
t.Fatal(err)
}
if response.Type != protocol.Error || !strings.Contains(response.Error, "not configured") {
t.Fatalf("close without broker = %+v", response)
}
}