fix(laya): group followed assets to avoid truncation

This commit is contained in:
2026-11-23 18:38:42 +01:00
parent 23f725e5d9
commit d3883d47ea
6 changed files with 379 additions and 83 deletions

View File

@ -0,0 +1,86 @@
package generation_test
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/tradarr/backend/internal/ai"
"github.com/tradarr/backend/internal/generation"
"github.com/tradarr/backend/internal/models"
"github.com/tradarr/backend/internal/scraper"
)
func TestTicket28GenerationAggregatesAllAssetsBeforeSelectionAndFailsClosed(t *testing.T) {
for _, failure := range []string{"", "truncated_long_article", "http"} {
t.Run(failure, func(t *testing.T) {
calls := 0
content := strings.Repeat("PRIVATE_LONG_CONTENT ", 2000)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++
var body struct {
States []struct {
Indices []map[string]string
Title, Content string
}
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
t.Error(err)
}
if len(body.States) != 2 {
t.Error("expected both articles")
}
for _, state := range body.States {
if len(state.Indices) != 1 || state.Content != content {
t.Error("asset grouping or exact content lost")
}
}
if calls == 2 && failure == "http" {
w.WriteHeader(503)
return
}
results := []any{}
for i := range body.States {
score := []float64{0.2, 0.8}[i]
if calls == 2 {
score = []float64{0.9, 0.05}[i]
}
usage := map[string]any{"truncated": false}
if calls == 2 && failure == "truncated_long_article" {
usage["truncated"] = true
usage["state_tokens_dropped"] = 800
usage["truncated_questions"] = []string{"relevance"}
}
results = append(results, map[string]any{"answers": map[string]any{"relevance": map[string]any{"type": "score", "score": score, "answer_confidence": 0.99}}, "usage": usage})
}
_ = json.NewEncoder(w).Encode(map[string]any{"results": results})
}))
defer srv.Close()
ranker, _ := ai.NewLayaHTTPRanker(ai.LayaHTTPConfig{BaseURL: srv.URL})
model, key := "model", models.SourceYahooFinance
feed := &ticket17HTTPScraper{articles: []scraper.Article{{Title: "first", URL: "https://fixture.invalid/first", Content: content}, {Title: "second", URL: "https://fixture.invalid/second", Content: content}}}
summary := &ticket17HTTPSummary{}
repo := &ticket17HTTPRepo{input: generation.UserInput{SummaryModelID: &model, Language: "fr", RankingMode: "title_content", GlobalLimit: 1, Assets: []models.UserAsset{{Name: "A", Symbol: "A"}, {Name: "B", Symbol: "B"}}, Sources: []models.Source{{ID: "source", SourceKey: &key, Enabled: true, ArticleLimit: 1}}}}
uc := generation.NewUseCase(generation.Dependencies{Repository: repo, SummaryResolver: &ticket17HTTPResolver{provider: summary}, LayaRanker: ranker, Scrapers: map[string]scraper.Scraper{key: feed}})
result, err := uc.Generate(context.Background(), "owner")
if err != nil || calls != 2 || feed.calls != 1 {
t.Fatalf("calls=%d feed=%d err=%v", calls, feed.calls, err)
}
if failure != "" {
if result.Summary != nil || summary.calls != 0 || result.Outcome.Status != "failed" || result.Outcome.FailureCode != "ranking_failed" || repo.saved.Summary != nil {
t.Fatal("partial summary after group failure")
}
} else if summary.calls != 1 || len(summary.candidates) != 1 || summary.candidates[0].Article.Title != "second" || result.Outcome.Status != "succeeded" {
t.Fatal("selection happened before all groups", fmt.Sprint(result.Outcome))
}
saved, _ := json.Marshal(repo.saved.Outcome)
if strings.Contains(string(saved), "PRIVATE_LONG_CONTENT") || strings.Contains(string(saved), "fixture.invalid") {
t.Fatal("candidate persisted in outcome")
}
})
}
}