feat(sources): enforce supported source allowlist
Assign stable keys to the three supported sources, preserve excluded historical rows, and expose history read-only in admin. Add migration, coverage, and responsive browser spec. Refs #13 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@ -0,0 +1,49 @@
|
||||
package generation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/tradarr/backend/internal/models"
|
||||
"github.com/tradarr/backend/internal/scraper"
|
||||
)
|
||||
|
||||
func TestGenerationRoutesOnlyAllowlistedSourceKeysEvenWhenLegacyRowsAreEnabled(t *testing.T) {
|
||||
var sources []models.Source
|
||||
if err := json.Unmarshal([]byte(`[
|
||||
{"id":"y","name":"Yahoo Finance","type":"stocktwits","source_key":"yahoo_finance","enabled":true,"article_limit":20},
|
||||
{"id":"b","name":"Bloomberg","type":"bloomberg","source_key":"bloomberg","enabled":true,"article_limit":20},
|
||||
{"id":"w","name":"Watcher.Guru","type":"watcherguru","source_key":"watcher_guru","enabled":true,"article_limit":20},
|
||||
{"id":"s","name":"StockTwits","type":"stocktwits","source_key":null,"enabled":true,"article_limit":20},
|
||||
{"id":"r","name":"Reuters","type":"reuters","source_key":null,"enabled":true,"article_limit":20}
|
||||
]`), &sources); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
repo := &contractRepository{inputs: map[string]UserInput{"user": {Language: "fr", RankingMode: "title", GlobalLimit: 10, Assets: []models.UserAsset{{Symbol: "SPX"}}, Sources: sources}}, saved: map[string][]GenerationRecord{}}
|
||||
feeds := map[string]*contractScraper{}
|
||||
for _, key := range []string{"yahoo_finance", "bloomberg", "watcher_guru", "stocktwits", "reuters"} {
|
||||
feeds[key] = &contractScraper{name: key, articles: []scraper.Article{{Title: key}}}
|
||||
}
|
||||
scrapers := map[string]scraper.Scraper{}
|
||||
for key, feed := range feeds {
|
||||
scrapers[key] = feed
|
||||
}
|
||||
result, err := NewUseCase(Dependencies{Repository: repo, Scrapers: scrapers, Ranker: &contractRanker{}, Summarizer: &contractSummarizer{}}).Generate(context.Background(), "user")
|
||||
if err != nil || result.Summary == nil {
|
||||
t.Fatalf("generation result=%+v err=%v", result, err)
|
||||
}
|
||||
for _, key := range []string{"yahoo_finance", "bloomberg", "watcher_guru"} {
|
||||
if len(feeds[key].calls) != 1 {
|
||||
t.Errorf("target %s calls=%d, want one", key, len(feeds[key].calls))
|
||||
}
|
||||
}
|
||||
for _, key := range []string{"stocktwits", "reuters"} {
|
||||
if len(feeds[key].calls) != 0 {
|
||||
t.Errorf("unsupported enabled source %s was invoked %d times", key, len(feeds[key].calls))
|
||||
}
|
||||
}
|
||||
if result.Outcome.TotalCandidates != 3 {
|
||||
t.Errorf("target candidate count=%d, want 3", result.Outcome.TotalCandidates)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user