Files
pipelines/internal/server/search_chats_test.go
T
alan bff7115e4e Search: find operator chats by title, directory and message text
A search with kind chat matches the operator-session registry's titles and
directories and the newest messages of recent transcripts through the chat
broker (bounded count, concurrency and one-second budget), answered off the
connection's read loop. The all-kind search is unchanged for older clients.

The app sends the chat search after its run search and shows a Chats group
with title, harness, directory and snippet; a chat hit opens that chat in the
Chat tab. The probe fixture answers chat searches over ?chats=N.
2026-11-03 13:01:53 +00:00

167 lines
6.8 KiB
Go

package server
import (
"context"
"testing"
"time"
"github.com/coder/websocket"
"github.com/coder/websocket/wsjson"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// transcriptChat serves a fixed newest page per session. A session listed in
// slow blocks until its read is canceled, like a harness that never answers.
type transcriptChat struct {
*fakeChat
pages map[string][]protocol.ChatMessage
slow map[string]bool
}
func (c *transcriptChat) Messages(ctx context.Context, sessionID string, limit int, _ string) (protocol.ChatHistoryPage, error) {
if c.slow[sessionID] {
<-ctx.Done()
return protocol.ChatHistoryPage{}, ctx.Err()
}
if limit != protocol.SearchChatMessagesPerChat {
return protocol.ChatHistoryPage{}, context.Canceled
}
return protocol.ChatHistoryPage{Messages: c.pages[sessionID]}, nil
}
func chatText(id, role, text string) protocol.ChatMessage {
return protocol.ChatMessage{ID: id, Role: role, Parts: []protocol.ChatPart{{Type: protocol.ChatPartText, Text: text}}}
}
func chatSearchFixture(t *testing.T, broker *transcriptChat) (context.Context, *websocket.Conn) {
t.Helper()
store := newOperatorStore(t)
ctx, _, conn := dialChatOptions(t, Options{Chat: broker, OperatorRegistry: store})
rows := []struct{ id, directory, title, backend string }{
{"ses_title", "/work/site", "Flaky test hunt", "claude"},
{"ses_dir", "/work/flaky-repo", "Docs pass", "codex"},
{"ses_text", "/work/api", "API refactor", ""},
{"ses_none", "/work/other", "Unrelated", ""},
{"ses_slow", "/work/slow", "Slow harness", ""},
}
for _, row := range rows {
if err := store.RecordOperatorSession(ctx, row.id, row.directory, row.title, protocol.ChatSessionTagOperator); err != nil {
t.Fatal(err)
}
if row.backend != "" {
if err := store.SetOperatorSessionBackend(ctx, row.id, row.backend); err != nil {
t.Fatal(err)
}
}
}
return ctx, conn
}
func chatSearch(t *testing.T, ctx context.Context, conn *websocket.Conn, id, query string) {
t.Helper()
writeChat(t, ctx, conn, protocol.Message{Type: protocol.Search, ID: id, Search: &protocol.SearchRequest{Query: query, Kind: protocol.SearchKindChat}})
}
func readReply(t *testing.T, ctx context.Context, conn *websocket.Conn, id string) protocol.Message {
t.Helper()
for {
var reply protocol.Message
if err := wsjson.Read(ctx, conn, &reply); err != nil {
t.Fatalf("read reply %s: %v", id, err)
}
if reply.ID == id {
return reply
}
}
}
func TestChatSearchMatchesTitleDirectoryAndMessageText(t *testing.T) {
broker := &transcriptChat{fakeChat: scriptedChat(), pages: map[string][]protocol.ChatMessage{
"ses_text": {
chatText("m1", protocol.ChatRoleUser, "Why is the build flaky?"),
chatText("m2", protocol.ChatRoleAssistant, "The FLAKY part is the retry loop in client.go."),
{ID: "m3", Role: protocol.ChatRoleAssistant, Parts: []protocol.ChatPart{{Type: protocol.ChatPartReasoning, Text: "flaky reasoning is not searched"}}},
},
"ses_none": {chatText("n1", protocol.ChatRoleUser, "nothing relevant")},
"ses_title": {chatText("t1", protocol.ChatRoleUser, "flaky again")},
}}
ctx, conn := chatSearchFixture(t, broker)
chatSearch(t, ctx, conn, "c1", "flaky")
reply := readReply(t, ctx, conn, "c1")
if reply.Type != protocol.SearchResults || reply.SearchResults == nil {
t.Fatalf("reply = %+v", reply)
}
got := reply.SearchResults
if got.Kind != protocol.SearchKindChat || got.Truncated || got.ScannedChats != 3 {
t.Fatalf("bounds = kind %q truncated %v scanned %d, want chat, complete, 3 transcripts (title/dir hits skip readback)", got.Kind, got.Truncated, got.ScannedChats)
}
if len(got.Results) != 3 {
t.Fatalf("results = %+v, want title, directory and message hits", got.Results)
}
title, dir, text := got.Results[0], got.Results[1], got.Results[2]
if title.Target != "chat:ses_title" || title.MatchedField != "title" || title.Chat.Backend != "claude" || title.Chat.Snippet != "" {
t.Fatalf("title hit = %+v %+v", title, title.Chat)
}
if dir.Target != "chat:ses_dir" || dir.MatchedField != "directory" || dir.Chat.Directory != "/work/flaky-repo" || dir.Chat.Backend != "codex" {
t.Fatalf("directory hit = %+v %+v", dir, dir.Chat)
}
if text.Target != "chat:ses_text" || text.MatchedField != "message" || text.Chat.MessageID != "m2" || text.Chat.Role != protocol.ChatRoleAssistant || text.Chat.Backend != "opencode" {
t.Fatalf("message hit = %+v %+v (want the newest matching text part)", text, text.Chat)
}
if text.Chat.Snippet != "The FLAKY part is the retry loop in client.go." || text.Chat.Title != "API refactor" || text.Recency == "" {
t.Fatalf("message hit facts = %+v", text.Chat)
}
}
// TestChatSearchBudgetNeverBlocksOtherReplies: a harness that never answers
// only costs its own transcript. The connection keeps answering other requests
// while the chat search waits, and the chat reply still arrives within the
// budget with the registry and finished-transcript hits, marked Truncated.
func TestChatSearchBudgetNeverBlocksOtherReplies(t *testing.T) {
previous := chatSearchBudget
chatSearchBudget = 400 * time.Millisecond
t.Cleanup(func() { chatSearchBudget = previous })
broker := &transcriptChat{fakeChat: scriptedChat(), slow: map[string]bool{"ses_slow": true}, pages: map[string][]protocol.ChatMessage{
"ses_text": {chatText("m1", protocol.ChatRoleUser, "a flaky retry")},
}}
ctx, conn := chatSearchFixture(t, broker)
started := time.Now()
chatSearch(t, ctx, conn, "c1", "flaky")
writeChat(t, ctx, conn, protocol.Message{Type: protocol.Ping, ID: "p1"})
var first protocol.Message
if err := wsjson.Read(ctx, conn, &first); err != nil {
t.Fatal(err)
}
if first.ID != "p1" {
t.Fatalf("first reply = %s %s, want the ping answered while transcripts load", first.ID, first.Type)
}
reply := readReply(t, ctx, conn, "c1")
if elapsed := time.Since(started); elapsed > 3*time.Second {
t.Fatalf("chat search took %v, budget is %v", elapsed, chatSearchBudget)
}
got := reply.SearchResults
if got == nil || !got.Truncated || len(got.Results) != 3 {
t.Fatalf("reply = %+v, want three hits and Truncated", got)
}
if got.Results[2].Target != "chat:ses_text" {
t.Fatalf("message hit missing: %+v", got.Results)
}
}
func TestChatSearchBlankListsRecentAndNeedsRegistry(t *testing.T) {
broker := &transcriptChat{fakeChat: scriptedChat()}
ctx, conn := chatSearchFixture(t, broker)
chatSearch(t, ctx, conn, "b1", " ")
reply := readReply(t, ctx, conn, "b1")
if reply.SearchResults == nil || len(reply.SearchResults.Results) != 5 || reply.SearchResults.ScannedChats != 0 {
t.Fatalf("blank chat search = %+v, want every listed chat and no transcript reads", reply.SearchResults)
}
ctx, _, bare := dialChatOptions(t, Options{Chat: broker})
chatSearch(t, ctx, bare, "n1", "x")
if reply := readReply(t, ctx, bare, "n1"); reply.Type != protocol.Error {
t.Fatalf("no-registry chat search = %+v, want an error", reply)
}
}