81 lines
3.3 KiB
Go
81 lines
3.3 KiB
Go
package generation_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/tradarr/backend/internal/ai"
|
|
"github.com/tradarr/backend/internal/generation"
|
|
"github.com/tradarr/backend/internal/models"
|
|
"github.com/tradarr/backend/internal/scraper"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestTicket29CompactGroupsIntegrateBeforeSelectionAndFailureIsGlobal(t *testing.T) {
|
|
for _, failure := range []string{"", "http", "truncated", "question"} {
|
|
t.Run(failure, func(t *testing.T) {
|
|
posts, states := 0, 0
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
posts++
|
|
var body struct {
|
|
States []struct {
|
|
Indices []map[string]string
|
|
Title string
|
|
}
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if posts == 2 && failure == "http" {
|
|
w.WriteHeader(503)
|
|
return
|
|
}
|
|
results := []any{}
|
|
for _, state := range body.States {
|
|
states++
|
|
score := 0.8
|
|
if state.Title == "first" {
|
|
score = 0.9
|
|
}
|
|
if state.Title == "second" && state.Indices[len(state.Indices)-1]["symbol"] == "T29" {
|
|
score = 0.05
|
|
}
|
|
usage := map[string]any{"truncated": false}
|
|
if posts == 2 && failure == "truncated" {
|
|
usage["state_tokens_dropped"] = 1
|
|
}
|
|
if posts == 2 && failure == "question" {
|
|
usage["truncated_questions"] = []string{"direct_mention"}
|
|
}
|
|
results = append(results, map[string]any{"answers": map[string]any{"direct_mention": map[string]any{"type": "noul", "noul": 1 - score, "answer_confidence": 0.99}}, "usage": usage})
|
|
}
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"results": results})
|
|
}))
|
|
defer srv.Close()
|
|
assets := make([]models.UserAsset, 30)
|
|
for i := range assets {
|
|
assets[i] = models.UserAsset{Name: fmt.Sprintf("Company %02d Incorporated", i), Symbol: fmt.Sprintf("T%02d", i)}
|
|
}
|
|
ranker, _ := ai.NewLayaHTTPRanker(ai.LayaHTTPConfig{BaseURL: srv.URL, BatchSize: 4})
|
|
model, key := "selected-model", models.SourceBloomberg
|
|
feed := &ticket17HTTPScraper{articles: []scraper.Article{{Title: "first", URL: "https://fixture.invalid/1"}, {Title: "second", URL: "https://fixture.invalid/2"}}}
|
|
summary := &ticket17HTTPSummary{}
|
|
repo := &ticket17HTTPRepo{input: generation.UserInput{SummaryModelID: &model, Language: "en", RankingMode: "title", GlobalLimit: 1, Assets: assets, Sources: []models.Source{{ID: "source", SourceKey: &key, Enabled: true, ArticleLimit: 2}}}}
|
|
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 || posts != 2 || feed.calls != 1 {
|
|
t.Fatalf("posts=%d feed=%d err=%v", posts, feed.calls, err)
|
|
}
|
|
if failure != "" {
|
|
if summary.calls != 0 || result.Summary != nil || result.Outcome.Status != "failed" || result.Outcome.FailureCode != "ranking_failed" || repo.saved.Summary != nil {
|
|
t.Fatal("partial summary after group failure")
|
|
}
|
|
} else if states != 6 || summary.calls != 1 || len(summary.candidates) != 1 || summary.candidates[0].Article.Title != "second" {
|
|
t.Fatalf("states=%d aggregation/selection mismatch", states)
|
|
}
|
|
})
|
|
}
|
|
}
|