feat(sources): add managed RSS feeds (#41)
This commit is contained in:
146
backend/internal/ai/quanwen_generation_ticket41_test.go
Normal file
146
backend/internal/ai/quanwen_generation_ticket41_test.go
Normal file
@ -0,0 +1,146 @@
|
||||
package ai
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/tradarr/backend/internal/generation"
|
||||
"github.com/tradarr/backend/internal/models"
|
||||
"github.com/tradarr/backend/internal/scraper"
|
||||
"github.com/tradarr/backend/internal/scraper/quanwenrss"
|
||||
"github.com/tradarr/backend/internal/sourcefeeds"
|
||||
)
|
||||
|
||||
type rss41Repo struct {
|
||||
input generation.UserInput
|
||||
saved generation.GenerationRecord
|
||||
}
|
||||
|
||||
func (r *rss41Repo) LoadUserInput(context.Context, string) (generation.UserInput, error) {
|
||||
return r.input, nil
|
||||
}
|
||||
func (r *rss41Repo) SaveGeneration(_ context.Context, _ string, record generation.GenerationRecord) error {
|
||||
r.saved = record
|
||||
return nil
|
||||
}
|
||||
|
||||
type rss41Loader struct{}
|
||||
|
||||
func (rss41Loader) SourceFeedURLs(_ context.Context, key string) ([]string, error) {
|
||||
return sourcefeeds.Defaults(key), nil
|
||||
}
|
||||
func TestTicket41RSSMergeThreePassSelectionAndFullSummaryPrompt(t *testing.T) {
|
||||
item := func(title, id, host, body string, full, dated bool) string {
|
||||
date := ""
|
||||
if dated {
|
||||
date = "<pubDate>Fri, 02 Jan 2026 03:04:05 GMT</pubDate>"
|
||||
}
|
||||
field := "description"
|
||||
if full {
|
||||
field = "content:encoded"
|
||||
}
|
||||
return fmt.Sprintf(`<item><title>%s</title><link>https://%s/news/%s</link><guid>%s</guid>%s<%s><![CDATA[<article><p>%s</p></article>]]></%s></item>`, title, host, id, id, date, field, body, field)
|
||||
}
|
||||
literal := item("Watched Company literal", "literal", "www.bloomberg.com", "Synthetic full literal body.", true, true)
|
||||
direct := item("Synthetic direct", "direct", "www.bloomberg.com", "Synthetic full direct body.", true, true)
|
||||
indirect := item("Synthetic indirect", "indirect", "www.bloomberg.com", "Synthetic full indirect body.", true, true)
|
||||
partial := item("Synthetic partial", "partial", "www.bloomberg.com", "Synthetic teaser excluded from Laya.", false, true)
|
||||
investment := item("Synthetic investment", "investment", "www.investing.com", "Synthetic complete investment body.", true, false)
|
||||
urls := sourcefeeds.Defaults(sourcefeeds.Bloomberg)
|
||||
bodies := map[string]string{urls[0]: literal, urls[1]: literal + direct, urls[2]: indirect, urls[3]: partial, sourcefeeds.Defaults(sourcefeeds.Investing)[0]: investment}
|
||||
fetches := 0
|
||||
client := &http.Client{Transport: ticket31Transport(func(r *http.Request) (*http.Response, error) {
|
||||
fetches++
|
||||
if r.URL.Host != sourcefeeds.Host {
|
||||
t.Fatal("article host was fetched")
|
||||
}
|
||||
status := 200
|
||||
content, ok := bodies[r.URL.String()]
|
||||
if !ok {
|
||||
status = 503
|
||||
}
|
||||
body := `<rss xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel>` + content + `</channel></rss>`
|
||||
return &http.Response{StatusCode: status, Header: make(http.Header), Request: r, Body: io.NopCloser(strings.NewReader(body))}, nil
|
||||
})}
|
||||
ranker, _ := NewLayaHTTPRanker(LayaHTTPConfig{BaseURL: "http://fixture.invalid"})
|
||||
passes := []string{}
|
||||
ranker.client = &http.Client{Transport: ticket31Transport(func(r *http.Request) (*http.Response, error) {
|
||||
var payload struct {
|
||||
States []struct{ Title, Content string }
|
||||
Questions map[string]any
|
||||
}
|
||||
if json.NewDecoder(r.Body).Decode(&payload) != nil {
|
||||
t.Fatal("invalid Laya request")
|
||||
}
|
||||
pass := ""
|
||||
for key := range payload.Questions {
|
||||
pass = key
|
||||
}
|
||||
passes = append(passes, pass)
|
||||
results := []any{}
|
||||
for _, state := range payload.States {
|
||||
if strings.Contains(state.Title, "partial") || state.Title == "Watched Company literal" || state.Content == "" {
|
||||
t.Fatal("partial article or literal first-pass selection leaked into Laya")
|
||||
}
|
||||
yes := state.Title == "Synthetic direct" || state.Title == "Synthetic investment"
|
||||
if pass == "indirect_mention" {
|
||||
yes = state.Title == "Synthetic indirect"
|
||||
}
|
||||
probability := 0.1
|
||||
if yes {
|
||||
probability = 0.9
|
||||
}
|
||||
results = append(results, map[string]any{"answers": map[string]any{pass: map[string]any{"type": "noul", "noul": probability, "answer_confidence": 0.99}}, "usage": map[string]any{"truncated": false, "state_tokens_dropped": 0}})
|
||||
}
|
||||
raw, _ := json.Marshal(map[string]any{"results": results, "total_usage": map[string]any{"input_tokens": 1}})
|
||||
return &http.Response{StatusCode: 200, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(string(raw)))}, nil
|
||||
})}
|
||||
bloom, invest := models.SourceBloomberg, models.SourceInvesting
|
||||
repo := &rss41Repo{input: generation.UserInput{Language: "fr", RankingMode: "title_content", SummaryContext: "Synthetic personalized context", SummaryPromptTokenBudget: 250000, GlobalLimit: 4, Assets: []models.UserAsset{{Name: "Watched Company", Symbol: "INTERNAL"}}, Sources: []models.Source{{ID: "b", Type: "bloomberg", SourceKey: &bloom, Enabled: true, ArticleLimit: 4}, {ID: "i", Type: "investing_com", SourceKey: &invest, Enabled: true, ArticleLimit: 1}}}}
|
||||
provider := &incident37Provider{}
|
||||
summarizer := fullGenerationSummarizer{provider: provider, providerName: "openai", model: "gpt-4o-mini"}
|
||||
uc := generation.NewUseCase(generation.Dependencies{Repository: repo, LayaRanker: ranker, Summarizer: summarizer, Scrapers: map[string]scraper.Scraper{bloom: quanwenrss.New(bloom, rss41Loader{}, client), invest: quanwenrss.New(invest, rss41Loader{}, client)}})
|
||||
result, err := uc.Generate(context.Background(), "fixture-user")
|
||||
if err != nil || result.Summary == nil || provider.calls != 1 || fetches != 6 || len(passes) != 2 || passes[0] != "direct_mention" || passes[1] != "indirect_mention" || result.Outcome.Selected != 4 || result.Outcome.ContentIncomplete != 1 || result.Outcome.Status != "partial" {
|
||||
t.Fatal("RSS source/three-pass/synthesis integration contract failed")
|
||||
}
|
||||
if result.Outcome.SourceStats[0].Candidates != 4 || result.Outcome.SourceStats[0].Feeds == nil || result.Outcome.SourceStats[0].Feeds.Failed != 1 {
|
||||
t.Fatal("dedup/source limit/feed diagnostics are wrong")
|
||||
}
|
||||
var prompt struct {
|
||||
Articles []struct {
|
||||
Title, Content, Source string
|
||||
PublishedAt *time.Time `json:"published_at"`
|
||||
}
|
||||
Indices []map[string]string
|
||||
Context struct{ Instructions string }
|
||||
}
|
||||
at := strings.Index(provider.prompt, "\n")
|
||||
if at < 0 || json.Unmarshal([]byte(provider.prompt[at+1:]), &prompt) != nil || len(prompt.Articles) != 4 || prompt.Context.Instructions != repo.input.SummaryContext || len(prompt.Indices) != 1 || prompt.Indices[0]["name"] != "Watched Company" {
|
||||
t.Fatal("summary lost full articles/watchlist/context")
|
||||
}
|
||||
nullable := false
|
||||
for _, article := range prompt.Articles {
|
||||
if article.Content == "" || strings.Contains(article.Title, "partial") {
|
||||
t.Fatal("summary received incomplete content")
|
||||
}
|
||||
if article.Source == invest {
|
||||
nullable = article.PublishedAt == nil
|
||||
}
|
||||
}
|
||||
if !nullable {
|
||||
t.Fatal("missing RSS date was invented")
|
||||
}
|
||||
saved, _ := json.Marshal(repo.saved.Outcome)
|
||||
for _, private := range []string{"http://", "https://", "Synthetic full", "Synthetic complete", "Watched Company", "personalized context"} {
|
||||
if strings.Contains(string(saved), private) {
|
||||
t.Fatal("sensitive article data persisted in outcome")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user