package server import ( "context" "testing" "time" "github.com/coder/websocket" "github.com/coder/websocket/wsjson" "git.a-wan.net/alan/pipelines/internal/protocol" ) // listedObservedChat is an observedChat whose endpoint lists ses_1, so // chat_sessions returns its registry row. type listedObservedChat struct{ *observedChat } func (c listedObservedChat) Sessions(context.Context) ([]protocol.ChatSession, error) { return []protocol.ChatSession{{ID: "ses_1"}}, nil } // readActivity reads raw frames until the next chat_activity push. func readActivity(t *testing.T, ctx context.Context, conn *websocket.Conn) protocol.ChatActivity { t.Helper() for { var message protocol.Message if err := wsjson.Read(ctx, conn, &message); err != nil { t.Fatalf("read (waiting for chat_activity): %v", err) } if message.Type == protocol.ChatActivityChanged { if message.ChatActivity == nil { t.Fatal("chat_activity without a body") } return *message.ChatActivity } } } func shellFrame(id, status string) protocol.ChatEventFrame { return protocol.ChatEventFrame{SessionID: "ses_1", Kind: protocol.ChatEventShell, Shell: &protocol.ChatShell{ID: id, Status: status, Command: "sleep 600"}} } func listActivity(t *testing.T, ctx context.Context, conn *websocket.Conn) *protocol.ChatActivity { t.Helper() writeChat(t, ctx, conn, protocol.Message{Type: protocol.ChatSessions, ID: "list"}) reply := readUntil(t, ctx, conn, protocol.ChatSessions) if len(reply.ChatSessions) != 1 || reply.ChatSessions[0].ID != "ses_1" { t.Fatalf("chat_sessions = %+v", reply.ChatSessions) } return reply.ChatSessions[0].Activity } // A connection that never opened the chat still learns, from chat_activity, // when its turn starts and ends and how many background tasks run, and the // list carries the same summary with the same sequence. Output chunks push // nothing. After the last viewer leaves, the session stays observed while a // background task runs, and is released (pushing an idle summary) once the // last one exits. func TestChatActivityReachesEveryConnection(t *testing.T) { fastUnattended(t) t.Setenv("PIPELINES_NTFY_URL", "") store := unattendedStore(t, false) broker := newObservedChat() server := startQueueServer(t, listedObservedChat{broker}, store) viewer := dialChatConn(t, server.ctx, server.srv) list := dialChatConn(t, server.ctx, server.srv) // A session that never changed still carries a sequence (this server's // current one), so it orders after anything kept from an earlier process. initial := listActivity(t, server.ctx, list) if initial == nil || initial.TurnRunning || initial.BackgroundTasks != 0 || initial.Seq == 0 { t.Fatalf("idle row activity = %+v, want an empty summary with a sequence", initial) } sendPrompt(t, server.ctx, viewer, "p1", "build it", "op-1") started := readActivity(t, server.ctx, list) if started.SessionID != "ses_1" || !started.TurnRunning || started.BackgroundTasks != 0 || started.Seq <= initial.Seq { t.Fatalf("turn start push = %+v", started) } broker.push(stepFrameFor(protocol.ChatStepStarted, "turn_1")) broker.push(textFrame("Working on it.")) broker.push(shellFrame("sh_1", protocol.ChatShellRunning)) withTask := readActivity(t, server.ctx, list) if !withTask.TurnRunning || withTask.BackgroundTasks != 1 || withTask.Seq <= started.Seq { t.Fatalf("background task push = %+v (after %+v); a text chunk or the re-announced turn must push nothing", withTask, started) } broker.push(stepFrameFor(protocol.ChatStepEnded, "turn_1")) ended := readActivity(t, server.ctx, list) if ended.TurnRunning || ended.BackgroundTasks != 1 || ended.Seq <= withTask.Seq { t.Fatalf("turn end push = %+v", ended) } if got := listActivity(t, server.ctx, list); got == nil || got.TurnRunning || got.BackgroundTasks != 1 || got.Seq != ended.Seq { t.Fatalf("listed activity = %+v, want %+v", got, ended) } viewer.CloseNow() time.Sleep(100 * time.Millisecond) // let the server notice the disconnect if active, _ := broker.streams(); active != 1 { t.Fatalf("a running background task was not observed after the viewer left: %d streams", active) } broker.push(shellFrame("sh_1", protocol.ChatShellExited)) idle := readActivity(t, server.ctx, list) if idle.TurnRunning || idle.BackgroundTasks != 0 || idle.Seq <= ended.Seq { t.Fatalf("task exit push = %+v", idle) } waitFor(t, "observer released", func() bool { active, _ := broker.streams(); return active == 0 }) if got := listActivity(t, server.ctx, list); got == nil || got.BackgroundTasks != 0 || got.Seq != idle.Seq { t.Fatalf("listed activity after release = %+v, want %+v", got, idle) } } // A background task that never reports its exit does not hold the observer // forever: it is released after chatUnattendedTurnLimit, and the list then // reports no tasks rather than a stale count. func TestChatUnreportedBackgroundTaskIsReleased(t *testing.T) { fastUnattended(t) t.Setenv("PIPELINES_NTFY_URL", "") limit := chatUnattendedTurnLimit chatUnattendedTurnLimit = 300 * time.Millisecond t.Cleanup(func() { chatUnattendedTurnLimit = limit }) store := unattendedStore(t, false) broker := newObservedChat() server := startQueueServer(t, listedObservedChat{broker}, store) viewer := dialChatConn(t, server.ctx, server.srv) list := dialChatConn(t, server.ctx, server.srv) // A reply proves the connection reached its read loop, where it is // attached to the pushes. listActivity(t, server.ctx, list) sendPrompt(t, server.ctx, viewer, "p1", "build it", "op-1") readActivity(t, server.ctx, list) broker.push(stepFrameFor(protocol.ChatStepStarted, "turn_1")) broker.push(shellFrame("sh_1", protocol.ChatShellRunning)) readActivity(t, server.ctx, list) broker.push(stepFrameFor(protocol.ChatStepEnded, "turn_1")) readActivity(t, server.ctx, list) viewer.CloseNow() time.Sleep(100 * time.Millisecond) if active, _ := broker.streams(); active != 1 { t.Fatalf("background task not observed: %d", active) } released := readActivity(t, server.ctx, list) if released.TurnRunning || released.BackgroundTasks != 0 { t.Fatalf("release push = %+v", released) } waitFor(t, "observer released", func() bool { active, _ := broker.streams(); return active == 0 }) if got := listActivity(t, server.ctx, list); got == nil || got.BackgroundTasks != 0 { t.Fatalf("listed activity after expiry = %+v", got) } } // The hub keeps an unattended room for background tasks only: an idle room // with none is released when its last client leaves, a room with one is kept. func TestChatBackgroundTasksKeepUnattendedRoom(t *testing.T) { fastUnattended(t) h := newChatHub() defer func() { h.mu.Lock() for _, room := range h.rooms { room.cancel() } h.mu.Unlock() }() for _, tasks := range []int{0, 1} { id := "ses_idle" if tasks > 0 { id = "ses_task" } h.mu.Lock() room := h.roomLocked(id) if tasks > 0 { h.turnLocked(id).applyShell(&protocol.ChatShell{ID: "sh_1", Status: protocol.ChatShellRunning}) } sub := &chatSub{hub: h, room: room, clientID: "c", ch: make(chan protocol.Message, 1), done: make(chan struct{})} room.subs["c"] = sub h.mu.Unlock() h.detach(id, sub) h.mu.Lock() kept := h.rooms[id] == room h.mu.Unlock() if kept != (tasks > 0) { t.Fatalf("%d background tasks: kept=%v", tasks, kept) } } }