Files
pipelines/internal/server/chat_history_test.go
T

108 lines
3.6 KiB
Go

package server
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"git.a-wan.net/alan/pipelines/internal/opencode"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// Scratch HTTP + real /connect websocket fixture, NOT a live-model/production
// claim. Covers both >1 MiB total (many tools) and one >1 MiB tool string.
func TestChatHistoryLargeFixtureWebsocketTraversalAndReconnect(t *testing.T) {
fixture := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/session/ses_1/message" || r.URL.Query().Get("limit") != "2" {
t.Errorf("unexpected history query %s", r.URL)
}
w.Header().Set("Content-Type", "application/json")
page := 0
switch r.URL.Query().Get("cursor") {
case "":
case "older+/=&":
page = 1
case "last":
page = 2
default:
t.Errorf("cursor not passed opaquely: %s", r.URL)
}
io.WriteString(w, `{"data":[`)
for i := 0; i < 2; i++ {
if i != 0 {
io.WriteString(w, ",")
}
id := 6 - page*2 - i
// Equal timestamps deliberately: order comes from the verified V2
// descending sequence, not a guessed timestamp comparison.
fmt.Fprintf(w, `{"id":"msg_%d","type":"assistant","time":{"created":1},"content":[`, id)
if page == 0 && i == 0 {
io.WriteString(w, `{"type":"tool","id":"huge","state":{"content":[{"type":"text","text":"`)
chunk := strings.Repeat("x", 32<<10)
for n := 0; n < 65; n++ {
io.WriteString(w, chunk)
}
io.WriteString(w, `"}]}},`)
}
if page == 1 && i == 0 {
for n := 0; n < 25; n++ {
fmt.Fprintf(w, `{"type":"tool","id":"tool_%d","state":{"status":"completed","content":[{"type":"text","text":%q}]}},`, n, strings.Repeat("y", 48<<10))
}
}
fmt.Fprintf(w, `{"type":"text","text":"answer %d"}]}`, id)
}
next := []any{"older+/=&", "last", nil}[page]
encoded, _ := json.Marshal(next)
fmt.Fprintf(w, `],"cursor":{"next":%s}}`, encoded)
}))
defer fixture.Close()
broker := opencode.NewChat(&opencode.Connection{Backend: "v2", URL: fixture.URL})
ctx, _, srv, conns := dialChatServer(t, Options{Chat: broker}, 1)
conn := conns[0]
conn.SetReadLimit(8 << 20)
cursor := ""
var ids []string
seen := map[string]bool{}
for page := 0; page < 3; page++ {
writeChat(t, ctx, conn, protocol.Message{Type: protocol.ChatMessages, ID: "history", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1", Limit: 2, Cursor: cursor}})
response := readUntil(t, ctx, conn, protocol.ChatMessages)
if response.ChatHistory == nil || len(response.ChatHistory.Messages) != 2 {
t.Fatalf("missing page: %+v", response)
}
history := response.ChatHistory
if !reflect.DeepEqual(response.ChatMessages, history.Messages) {
t.Fatal("legacy/page views differ")
}
for _, message := range history.Messages {
if seen[message.ID] {
t.Fatalf("duplicate %s", message.ID)
}
seen[message.ID] = true
}
pageIDs := []string{history.Messages[0].ID, history.Messages[1].ID}
ids = append(pageIDs, ids...)
if page == 0 {
newest := history.Messages[1]
if newest.Parts[len(newest.Parts)-1].Text != "answer 6" || !newest.Parts[0].State.Truncated {
t.Fatal("huge tool prevented newest answer")
}
}
cursor = history.NextCursor
if history.HasMore != (page < 2) {
t.Fatal("incorrect continuation")
}
// Reconnect between pages: continuation is server-owned, not a hub tail.
conn.CloseNow()
conn = dialChatConn(t, ctx, srv)
conn.SetReadLimit(8 << 20)
}
if cursor != "" || !reflect.DeepEqual(ids, []string{"msg_1", "msg_2", "msg_3", "msg_4", "msg_5", "msg_6"}) {
t.Fatalf("incomplete traversal: %v cursor=%q", ids, cursor)
}
}