Files
pipelines/internal/server/chat_busy_resume_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

116 lines
3.8 KiB
Go

package server
import (
"context"
"errors"
"testing"
"time"
"github.com/coder/websocket"
"github.com/coder/websocket/wsjson"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// busyChat is a broker whose harness reports a turn in flight on its own
// (Claude Code keeps it in-process) and whose stream can announce a turn the way
// a harness re-announces one on attach.
type busyChat struct {
*fakeChat
running bool
announce bool
}
func (b *busyChat) TurnRunning(string) bool { return b.running }
func (b *busyChat) Stream(ctx context.Context, sessionID string, ready func(), emit func(protocol.ChatEventFrame) error) error {
if ready != nil {
ready()
}
if b.announce {
if err := emit(protocol.ChatEventFrame{SessionID: sessionID, Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepStarted}}); err != nil {
return err
}
}
<-ctx.Done()
return ctx.Err()
}
func subscribeChat(t *testing.T, ctx context.Context, conn *websocket.Conn, id string) {
t.Helper()
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatSubscribe, ID: id, ChatSubscribe: &protocol.ChatQuery{SessionID: "ses_1"}}); err != nil {
t.Fatal(err)
}
var ack protocol.Message
if err := readTestFrame(ctx, conn, &ack); err != nil {
t.Fatal(err)
}
if ack.Type != protocol.ChatSubscribe || ack.ID != id {
t.Fatalf("subscribe ack = %+v", ack)
}
}
// readStepStarted reads frames until an authoritative step-started arrives.
func readStepStarted(t *testing.T, ctx context.Context, conn *websocket.Conn) {
t.Helper()
for {
var message protocol.Message
if err := readTestFrame(ctx, conn, &message); err != nil {
t.Fatalf("no step-started frame: %v", err)
}
event := message.ChatEvent
if event != nil && event.Kind == protocol.ChatEventStep && event.Step != nil && event.Step.Status == protocol.ChatStepStarted && !event.Step.Intermediate {
if event.SessionID != "ses_1" {
t.Fatalf("step frame session = %q", event.SessionID)
}
return
}
}
}
// TestChatSubscribeReportsHarnessBusyTurn pins the reload case: the page's only
// subscription went away (so the room and its record of the prompt are gone)
// while the harness kept working. A fresh subscribe must report the session
// busy immediately instead of waiting for the next streamed delta.
func TestChatSubscribeReportsHarnessBusyTurn(t *testing.T) {
broker := &busyChat{fakeChat: scriptedChat(), running: true}
ctx, _, conn := dialChat(t, broker)
subscribeChat(t, ctx, conn, "sub1")
readStepStarted(t, ctx, conn)
}
// TestChatSubscribeIdleHarnessReplaysNoTurn keeps the idle case honest: no turn
// marker is fabricated for a session whose harness is not working.
func TestChatSubscribeIdleHarnessReplaysNoTurn(t *testing.T) {
broker := &busyChat{fakeChat: scriptedChat()}
ctx, _, conn := dialChat(t, broker)
subscribeChat(t, ctx, conn, "sub1")
readCtx, cancel := context.WithTimeout(ctx, 300*time.Millisecond)
defer cancel()
for {
var message protocol.Message
err := readTestFrame(readCtx, conn, &message)
if err != nil {
if !errors.Is(readCtx.Err(), context.DeadlineExceeded) {
t.Fatalf("read: %v", err)
}
return
}
if message.ChatEvent != nil && message.ChatEvent.Kind == protocol.ChatEventStep {
t.Fatalf("idle session replayed a step: %+v", message.ChatEvent)
}
}
}
// TestChatSubscribeReplaysStreamAnnouncedTurn covers a turn no prompt of this
// room started (a CLI-originated turn, or one re-announced on attach): a second
// device that subscribes later is still told it is running.
func TestChatSubscribeReplaysStreamAnnouncedTurn(t *testing.T) {
broker := &busyChat{fakeChat: scriptedChat(), announce: true}
ctx, _, conns := dialChatConns(t, Options{Chat: broker}, 2)
subscribeChat(t, ctx, conns[0], "sub1")
readStepStarted(t, ctx, conns[0])
subscribeChat(t, ctx, conns[1], "sub2")
readStepStarted(t, ctx, conns[1])
}