package server import ( "context" "net/http/httptest" "reflect" "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(LargeChatFixtureHandler(t.TempDir())) defer fixture.Close() attached, err := opencode.OpenEndpointAttached(context.Background(), fixture.URL, LargeChatFixturePassword, "", "") if err != nil { t.Fatal(err) } broker := opencode.NewChat(attached.Connection) 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: LargeChatFixtureSessionID, 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 != LargeChatFixtureNewestAnswer || !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) } }