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:
@ -92,9 +92,9 @@ func (s *contractSummarizer) Summarize(_ context.Context, language string, _ []m
|
||||
|
||||
func TestUseCaseIsolatesTwoUsersAndUsesOnlyEnabledSourcesAndUserInputs(t *testing.T) {
|
||||
alphaID, betaID := "user-alpha", "user-beta"
|
||||
alphaSource := models.Source{ID: "alpha-feed", Type: "alpha", Enabled: true, ArticleLimit: 5}
|
||||
betaSource := models.Source{ID: "beta-feed", Type: "beta", Enabled: true, ArticleLimit: 5}
|
||||
disabled := models.Source{ID: "disabled-feed", Type: "disabled", Enabled: false, ArticleLimit: 5}
|
||||
alphaSource := models.Source{ID: "alpha-feed", Type: "alpha", SourceKey: testSourceKey(models.SourceYahooFinance), Enabled: true, ArticleLimit: 5}
|
||||
betaSource := models.Source{ID: "beta-feed", Type: "beta", SourceKey: testSourceKey(models.SourceBloomberg), Enabled: true, ArticleLimit: 5}
|
||||
disabled := models.Source{ID: "disabled-feed", Type: "disabled", SourceKey: testSourceKey(models.SourceWatcherGuru), Enabled: false, ArticleLimit: 5}
|
||||
alphaScraper := &contractScraper{name: "alpha", articles: []scraper.Article{{Title: "alpha-only", URL: "https://private.test/alpha", Content: "alpha-secret"}}}
|
||||
betaScraper := &contractScraper{name: "beta", articles: []scraper.Article{{Title: "beta-only", URL: "https://private.test/beta", Content: "beta-secret"}}}
|
||||
disabledScraper := &contractScraper{name: "disabled"}
|
||||
@ -107,7 +107,7 @@ func TestUseCaseIsolatesTwoUsersAndUsesOnlyEnabledSourcesAndUserInputs(t *testin
|
||||
},
|
||||
saved: map[string][]GenerationRecord{},
|
||||
}
|
||||
useCase := NewUseCase(Dependencies{Repository: repo, Scrapers: map[string]scraper.Scraper{"alpha": alphaScraper, "beta": betaScraper, "disabled": disabledScraper}, Ranker: ranker, Summarizer: summarizer, Clock: func() time.Time { return time.Date(2026, 11, 20, 12, 0, 0, 0, time.UTC) }})
|
||||
useCase := NewUseCase(Dependencies{Repository: repo, Scrapers: map[string]scraper.Scraper{models.SourceYahooFinance: alphaScraper, models.SourceBloomberg: betaScraper, models.SourceWatcherGuru: disabledScraper}, Ranker: ranker, Summarizer: summarizer, Clock: func() time.Time { return time.Date(2026, 11, 20, 12, 0, 0, 0, time.UTC) }})
|
||||
|
||||
alpha, err := useCase.Generate(context.Background(), alphaID)
|
||||
if err != nil || alpha.Summary == nil {
|
||||
@ -148,8 +148,8 @@ func TestUseCasePreservesLayaOrderThenAppliesPerSourceAndGlobalCaps(t *testing.T
|
||||
feedB := &contractScraper{name: "b", articles: articles[2:]}
|
||||
ranker := &contractRanker{order: []string{"a2", "b1", "a1", "b2", "a3"}}
|
||||
summarizer := &contractSummarizer{}
|
||||
repo := &contractRepository{inputs: map[string]UserInput{"u": {Language: "fr", RankingMode: "title", GlobalLimit: 3, Assets: []models.UserAsset{{Symbol: "IDX"}}, Sources: []models.Source{{ID: "a", Type: "a", Enabled: true, ArticleLimit: 1}, {ID: "b", Type: "b", Enabled: true, ArticleLimit: 2}}}}, saved: map[string][]GenerationRecord{}}
|
||||
uc := NewUseCase(Dependencies{Repository: repo, Scrapers: map[string]scraper.Scraper{"a": feedA, "b": feedB}, Ranker: ranker, Summarizer: summarizer, Clock: func() time.Time { return time.Unix(1, 0) }})
|
||||
repo := &contractRepository{inputs: map[string]UserInput{"u": {Language: "fr", RankingMode: "title", GlobalLimit: 3, Assets: []models.UserAsset{{Symbol: "IDX"}}, Sources: []models.Source{{ID: "a", Type: "a", SourceKey: testSourceKey(models.SourceYahooFinance), Enabled: true, ArticleLimit: 1}, {ID: "b", Type: "b", SourceKey: testSourceKey(models.SourceBloomberg), Enabled: true, ArticleLimit: 2}}}}, saved: map[string][]GenerationRecord{}}
|
||||
uc := NewUseCase(Dependencies{Repository: repo, Scrapers: map[string]scraper.Scraper{models.SourceYahooFinance: feedA, models.SourceBloomberg: feedB}, Ranker: ranker, Summarizer: summarizer, Clock: func() time.Time { return time.Unix(1, 0) }})
|
||||
if _, err := uc.Generate(context.Background(), "u"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -176,10 +176,10 @@ func TestUseCaseNoResultsInvalidLayaAndSummaryFailurePersistNoSummary(t *testing
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
repo := &contractRepository{inputs: map[string]UserInput{"u": {Language: "fr", RankingMode: "title", GlobalLimit: 10, Sources: []models.Source{{ID: "s", Type: "s", Enabled: true, ArticleLimit: 10}}}}, saved: map[string][]GenerationRecord{}}
|
||||
repo := &contractRepository{inputs: map[string]UserInput{"u": {Language: "fr", RankingMode: "title", GlobalLimit: 10, Sources: []models.Source{{ID: "s", Type: "s", SourceKey: testSourceKey(models.SourceYahooFinance), Enabled: true, ArticleLimit: 10}}}}, saved: map[string][]GenerationRecord{}}
|
||||
ranker := &contractRanker{err: tt.rankErr}
|
||||
summarizer := &contractSummarizer{err: tt.summaryErr}
|
||||
uc := NewUseCase(Dependencies{Repository: repo, Scrapers: map[string]scraper.Scraper{"s": &contractScraper{name: "s", articles: tt.articles}}, Ranker: ranker, Summarizer: summarizer, Clock: func() time.Time { return time.Unix(1, 0) }})
|
||||
uc := NewUseCase(Dependencies{Repository: repo, Scrapers: map[string]scraper.Scraper{models.SourceYahooFinance: &contractScraper{name: "s", articles: tt.articles}}, Ranker: ranker, Summarizer: summarizer, Clock: func() time.Time { return time.Unix(1, 0) }})
|
||||
result, err := uc.Generate(context.Background(), "u")
|
||||
if err != nil {
|
||||
t.Fatalf("expected aggregate failure outcome, got error: %v", err)
|
||||
@ -199,8 +199,8 @@ func TestUseCasePartialSourceFailureCanSummarizeRemainingSources(t *testing.T) {
|
||||
bad := &contractScraper{name: "bad", err: errors.New("private error with URL https://private.test/article")}
|
||||
ranker := &contractRanker{order: []string{"good-result"}}
|
||||
summarizer := &contractSummarizer{}
|
||||
repo := &contractRepository{inputs: map[string]UserInput{"u": {Language: "fr", RankingMode: "title_content", GlobalLimit: 10, Sources: []models.Source{{ID: "good", Type: "good", Enabled: true, ArticleLimit: 5}, {ID: "bad", Type: "bad", Enabled: true, ArticleLimit: 5}}}}, saved: map[string][]GenerationRecord{}}
|
||||
uc := NewUseCase(Dependencies{Repository: repo, Scrapers: map[string]scraper.Scraper{"good": good, "bad": bad}, Ranker: ranker, Summarizer: summarizer, Clock: func() time.Time { return time.Unix(1, 0) }})
|
||||
repo := &contractRepository{inputs: map[string]UserInput{"u": {Language: "fr", RankingMode: "title_content", GlobalLimit: 10, Sources: []models.Source{{ID: "good", Type: "good", SourceKey: testSourceKey(models.SourceYahooFinance), Enabled: true, ArticleLimit: 5}, {ID: "bad", Type: "bad", SourceKey: testSourceKey(models.SourceBloomberg), Enabled: true, ArticleLimit: 5}}}}, saved: map[string][]GenerationRecord{}}
|
||||
uc := NewUseCase(Dependencies{Repository: repo, Scrapers: map[string]scraper.Scraper{models.SourceYahooFinance: good, models.SourceBloomberg: bad}, Ranker: ranker, Summarizer: summarizer, Clock: func() time.Time { return time.Unix(1, 0) }})
|
||||
result, err := uc.Generate(context.Background(), "u")
|
||||
if err != nil || result.Summary == nil || result.Outcome.Status != "partial" {
|
||||
t.Fatalf("partial source failure result=%+v err=%v", result, err)
|
||||
@ -221,8 +221,8 @@ func TestUseCaseOutcomeContainsAggregateCountsWithoutArticleIdentifiers(t *testi
|
||||
feed := &contractScraper{name: "source", articles: []scraper.Article{{Title: "candidate-title-secret", URL: "https://private.test/x", Content: "candidate-content-secret"}}}
|
||||
ranker := &contractRanker{order: []string{"candidate-title-secret"}}
|
||||
summarizer := &contractSummarizer{}
|
||||
repo := &contractRepository{inputs: map[string]UserInput{"u": {Language: "fr", RankingMode: "title_content", GlobalLimit: 10, Sources: []models.Source{{ID: "source-id", Type: "source", Enabled: true, ArticleLimit: 5}}}}, saved: map[string][]GenerationRecord{}}
|
||||
uc := NewUseCase(Dependencies{Repository: repo, Scrapers: map[string]scraper.Scraper{"source": feed}, Ranker: ranker, Summarizer: summarizer, Clock: func() time.Time { return time.Unix(1, 0) }})
|
||||
repo := &contractRepository{inputs: map[string]UserInput{"u": {Language: "fr", RankingMode: "title_content", GlobalLimit: 10, Sources: []models.Source{{ID: "source-id", Type: "source", SourceKey: testSourceKey(models.SourceYahooFinance), Enabled: true, ArticleLimit: 5}}}}, saved: map[string][]GenerationRecord{}}
|
||||
uc := NewUseCase(Dependencies{Repository: repo, Scrapers: map[string]scraper.Scraper{models.SourceYahooFinance: feed}, Ranker: ranker, Summarizer: summarizer, Clock: func() time.Time { return time.Unix(1, 0) }})
|
||||
result, err := uc.Generate(context.Background(), "u")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@ -245,8 +245,8 @@ func TestUseCaseCountsTitleOnlyFallbackWhenContentIsUnavailable(t *testing.T) {
|
||||
feed := &contractScraper{name: "source", articles: []scraper.Article{{Title: "title-without-content", Content: ""}}}
|
||||
ranker := &contractRanker{}
|
||||
summarizer := &contractSummarizer{}
|
||||
repo := &contractRepository{inputs: map[string]UserInput{"u": {Language: "en", RankingMode: "title_content", GlobalLimit: 4, Sources: []models.Source{{ID: "source", Type: "source", Enabled: true, ArticleLimit: 4}}}}, saved: map[string][]GenerationRecord{}}
|
||||
uc := NewUseCase(Dependencies{Repository: repo, Scrapers: map[string]scraper.Scraper{"source": feed}, Ranker: ranker, Summarizer: summarizer, Clock: func() time.Time { return time.Unix(1, 0) }})
|
||||
repo := &contractRepository{inputs: map[string]UserInput{"u": {Language: "en", RankingMode: "title_content", GlobalLimit: 4, Sources: []models.Source{{ID: "source", Type: "source", SourceKey: testSourceKey(models.SourceYahooFinance), Enabled: true, ArticleLimit: 4}}}}, saved: map[string][]GenerationRecord{}}
|
||||
uc := NewUseCase(Dependencies{Repository: repo, Scrapers: map[string]scraper.Scraper{models.SourceYahooFinance: feed}, Ranker: ranker, Summarizer: summarizer, Clock: func() time.Time { return time.Unix(1, 0) }})
|
||||
result, err := uc.Generate(context.Background(), "u")
|
||||
if err != nil || result.Summary == nil {
|
||||
t.Fatalf("content fallback should permit generation: result=%+v err=%v", result, err)
|
||||
@ -267,3 +267,5 @@ func candidateTitles(candidates []Candidate) []string {
|
||||
func contains(value, needle string) bool {
|
||||
return len(needle) > 0 && strings.Contains(value, needle)
|
||||
}
|
||||
|
||||
func testSourceKey(key string) *string { return &key }
|
||||
|
||||
Reference in New Issue
Block a user