32c9c6b1df
A kind=chat search with scope chat:<id> returns one hit per text-part occurrence (message id, part, UTF-16 offset/length), newest message first, paged by cursor (a reply's next_cursor or a chat_messages next_cursor), with optional case-sensitive matching. Scoped and global chat searches no longer cancel each other on one connection. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
148 lines
4.9 KiB
Go
148 lines
4.9 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strconv"
|
|
"testing"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// scopedHistoryChat serves one session's history oldest first in pages of the
|
|
// requested size; a cursor is the index one past the page's oldest message.
|
|
type scopedHistoryChat struct {
|
|
*fakeChat
|
|
messages []protocol.ChatMessage
|
|
}
|
|
|
|
func (c *scopedHistoryChat) Messages(_ context.Context, _ string, limit int, cursor string) (protocol.ChatHistoryPage, error) {
|
|
end := len(c.messages)
|
|
if cursor != "" {
|
|
n, err := strconv.Atoi(cursor)
|
|
if err != nil {
|
|
return protocol.ChatHistoryPage{}, err
|
|
}
|
|
end = n
|
|
}
|
|
start := max(end-limit, 0)
|
|
page := protocol.ChatHistoryPage{Messages: c.messages[start:end]}
|
|
if start > 0 {
|
|
page.HasMore = true
|
|
page.NextCursor = strconv.Itoa(start)
|
|
}
|
|
return page, nil
|
|
}
|
|
|
|
func pagedHistory(n int) []protocol.ChatMessage {
|
|
messages := make([]protocol.ChatMessage, n)
|
|
for i := range messages {
|
|
text := fmt.Sprintf("message %d", i)
|
|
if i%10 == 0 {
|
|
text = fmt.Sprintf("Needle %d and needle again", i)
|
|
}
|
|
messages[i] = chatText(fmt.Sprintf("m%d", i), protocol.ChatRoleAssistant, text)
|
|
}
|
|
return messages
|
|
}
|
|
|
|
func TestScopedChatSearchReturnsEveryOccurrenceNewestFirst(t *testing.T) {
|
|
broker := &scopedHistoryChat{fakeChat: scriptedChat(), messages: pagedHistory(450)}
|
|
response, err := searchChatScoped(context.Background(), protocol.SearchRequest{
|
|
Query: "needle", Kind: protocol.SearchKindChat, Scope: protocol.SearchChatScope("ses_a"), Limit: 200,
|
|
}, broker)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(response.Results) != 90 || response.NextCursor != "" || response.Truncated {
|
|
t.Fatalf("results=%d cursor=%q truncated=%v", len(response.Results), response.NextCursor, response.Truncated)
|
|
}
|
|
first := response.Results[0].Chat
|
|
if first.MessageID != "m440" || first.Offset != 0 || first.Length != 6 {
|
|
t.Fatalf("first hit = %+v", first)
|
|
}
|
|
second := response.Results[1].Chat
|
|
if second.MessageID != "m440" || second.Offset != len("Needle 440 and ") {
|
|
t.Fatalf("second hit = %+v", second)
|
|
}
|
|
if last := response.Results[89].Chat; last.MessageID != "m0" {
|
|
t.Fatalf("last hit = %+v", last)
|
|
}
|
|
}
|
|
|
|
func TestScopedChatSearchPagesThroughCursor(t *testing.T) {
|
|
broker := &scopedHistoryChat{fakeChat: scriptedChat(), messages: pagedHistory(450)}
|
|
request := protocol.SearchRequest{Query: "needle", Kind: protocol.SearchKindChat, Scope: "chat:ses_a", Limit: 7}
|
|
var ids []string
|
|
for pages := 0; ; pages++ {
|
|
if pages > 50 {
|
|
t.Fatal("cursor never ended")
|
|
}
|
|
response, err := searchChatScoped(context.Background(), request, broker)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, hit := range response.Results {
|
|
ids = append(ids, hit.Chat.MessageID)
|
|
}
|
|
if response.NextCursor == "" {
|
|
break
|
|
}
|
|
request.Cursor = response.NextCursor
|
|
}
|
|
if len(ids) != 90 {
|
|
t.Fatalf("hits across pages = %d", len(ids))
|
|
}
|
|
seen := map[string]int{}
|
|
for _, id := range ids {
|
|
seen[id]++
|
|
}
|
|
if len(seen) != 45 || seen["m0"] != 2 || seen["m440"] != 2 {
|
|
t.Fatalf("each needle message twice, got %v", seen)
|
|
}
|
|
}
|
|
|
|
func TestScopedChatSearchStartsAtHistoryCursorAndMatchesCase(t *testing.T) {
|
|
broker := &scopedHistoryChat{fakeChat: scriptedChat(), messages: pagedHistory(450)}
|
|
response, err := searchChatScoped(context.Background(), protocol.SearchRequest{
|
|
Query: "Needle", Kind: protocol.SearchKindChat, Scope: "chat:ses_a", Cursor: "250", CaseSensitive: true,
|
|
}, broker)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(response.Results) != 25 || response.Results[0].Chat.MessageID != "m240" {
|
|
t.Fatalf("results=%d first=%+v", len(response.Results), response.Results[0].Chat)
|
|
}
|
|
}
|
|
|
|
func TestScopedChatSearchCountsUTF16Offsets(t *testing.T) {
|
|
matches := runeMatches([]rune("😀 Ünïcode ünïcode"), []rune("ÜNÏ"), false)
|
|
if len(matches) != 2 || matches[0].offset != 3 || matches[1].offset != 11 || matches[0].length != 3 {
|
|
t.Fatalf("matches = %+v", matches)
|
|
}
|
|
if got := runeMatches([]rune("aaaa"), []rune("aa"), true); len(got) != 2 {
|
|
t.Fatalf("overlapping matches counted: %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestScopedChatSearchRejectsAnInvalidScope(t *testing.T) {
|
|
broker := &scopedHistoryChat{fakeChat: scriptedChat()}
|
|
if _, err := searchChatScoped(context.Background(), protocol.SearchRequest{Query: "x", Scope: "chat:../etc"}, broker); err == nil {
|
|
t.Fatal("invalid scope accepted")
|
|
}
|
|
}
|
|
|
|
func TestScopedChatSearchOverTheSocket(t *testing.T) {
|
|
broker := &transcriptChat{fakeChat: scriptedChat(), pages: map[string][]protocol.ChatMessage{}}
|
|
ctx, conn := chatSearchFixture(t, broker)
|
|
writeChat(t, ctx, conn, protocol.Message{Type: protocol.Search, ID: "scoped", Search: &protocol.SearchRequest{
|
|
Query: "flaky", Kind: protocol.SearchKindChat, Scope: "chat:ses_text",
|
|
}})
|
|
reply := readReply(t, ctx, conn, "scoped")
|
|
// transcriptChat refuses any page size but the global search's, so the
|
|
// scoped search's own read comes back as an error reply.
|
|
if reply.Type != protocol.Error {
|
|
t.Fatalf("reply = %+v", reply)
|
|
}
|
|
}
|