Add the nullable FIGI migration, owner-scoped asset identity operations, and the explicit index search and selection flow. Refs #4 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
103 lines
4.4 KiB
Go
103 lines
4.4 KiB
Go
package generation
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"os"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/lib/pq"
|
|
"github.com/tradarr/backend/internal/models"
|
|
"github.com/tradarr/backend/internal/scraper"
|
|
)
|
|
|
|
type ticket4AssetRecorder struct{ assets []models.UserAsset }
|
|
|
|
func (r *ticket4AssetRecorder) Rank(_ context.Context, _ string, assets []models.UserAsset, candidates []Candidate) ([]Candidate, error) {
|
|
r.assets = append([]models.UserAsset(nil), assets...)
|
|
return append([]Candidate(nil), candidates...), nil
|
|
}
|
|
|
|
func TestLegacyNullFIGIAssetStillContributesToGenerationPostgres(t *testing.T) {
|
|
dsn := os.Getenv("TEST_DATABASE_URL")
|
|
if dsn == "" {
|
|
if os.Getenv("TEST_DATABASE_REQUIRED") == "1" {
|
|
t.Fatal("TEST_DATABASE_REQUIRED=1 but TEST_DATABASE_URL is empty")
|
|
}
|
|
t.Skip("TEST_DATABASE_URL absent: isolated PostgreSQL required")
|
|
}
|
|
db, err := sql.Open("postgres", dsn)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
db.SetMaxOpenConns(1)
|
|
schema := fmt.Sprintf("ticket4_generation_asset_%d", time.Now().UnixNano())
|
|
if _, err := db.Exec(`CREATE SCHEMA ` + pq.QuoteIdentifier(schema)); err != nil {
|
|
db.Close()
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := db.Exec(`SET search_path TO ` + pq.QuoteIdentifier(schema)); err != nil {
|
|
_, _ = db.Exec(`DROP SCHEMA ` + pq.QuoteIdentifier(schema) + ` CASCADE`)
|
|
db.Close()
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() {
|
|
_, _ = db.Exec(`DROP SCHEMA ` + pq.QuoteIdentifier(schema) + ` CASCADE`)
|
|
_ = db.Close()
|
|
})
|
|
const userID = "00000000-0000-0000-0000-000000000401"
|
|
for _, statement := range []string{
|
|
`CREATE TABLE users (id UUID PRIMARY KEY, language TEXT NOT NULL DEFAULT 'fr', ranking_mode TEXT NOT NULL DEFAULT 'title')`,
|
|
`CREATE TABLE user_assets (id UUID PRIMARY KEY, user_id UUID NOT NULL, symbol VARCHAR(20) NOT NULL, name TEXT NOT NULL DEFAULT '', figi TEXT NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW())`,
|
|
`CREATE TABLE sources (id UUID PRIMARY KEY, name TEXT NOT NULL, type TEXT NOT NULL, enabled BOOLEAN NOT NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), article_limit INT NOT NULL DEFAULT 20)`,
|
|
`CREATE TABLE settings (key TEXT PRIMARY KEY, value TEXT NOT NULL)`,
|
|
`INSERT INTO users(id,language,ranking_mode) VALUES ('` + userID + `','fr','title')`,
|
|
`INSERT INTO user_assets(id,user_id,symbol,name,figi) VALUES ('00000000-0000-0000-0000-000000000411','` + userID + `','LEGACY','Historic asset',NULL)`,
|
|
`INSERT INTO sources(id,name,type,enabled,article_limit) VALUES ('00000000-0000-0000-0000-000000000421','test source','ticket4',TRUE,20)`,
|
|
`INSERT INTO settings(key,value) VALUES ('summary_max_articles','50')`,
|
|
} {
|
|
if _, err := db.Exec(statement); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
input, err := (RepositoryAdapter{Repo: models.NewRepository(db)}).LoadUserInput(context.Background(), userID)
|
|
if err != nil {
|
|
t.Fatalf("load legacy generation input from PostgreSQL: %v", err)
|
|
}
|
|
if len(input.Assets) != 1 || input.Assets[0].Symbol != "LEGACY" || input.Assets[0].Name != "Historic asset" {
|
|
t.Fatalf("loaded legacy asset=%+v, want symbol/name preserved", input.Assets)
|
|
}
|
|
figiField, hasFIGI := reflect.TypeOf(models.UserAsset{}).FieldByName("FIGI")
|
|
if !hasFIGI || figiField.Type != reflect.TypeOf((*string)(nil)) {
|
|
t.Errorf("UserAsset must expose database NULL FIGI as *string; present=%v", hasFIGI)
|
|
} else if field := reflect.ValueOf(input.Assets[0]).FieldByIndex(figiField.Index); !field.IsNil() {
|
|
t.Errorf("legacy database asset FIGI=%v, want nil", field.Interface())
|
|
}
|
|
|
|
feed := &contractScraper{name: "ticket4", articles: []scraper.Article{{Title: "Legacy asset article"}}}
|
|
ranker := &ticket4AssetRecorder{}
|
|
summarizer := &contractSummarizer{}
|
|
fakeRepository := &contractRepository{inputs: map[string]UserInput{userID: input}, saved: map[string][]GenerationRecord{}}
|
|
uc := NewUseCase(Dependencies{
|
|
Repository: fakeRepository,
|
|
Scrapers: map[string]scraper.Scraper{"ticket4": feed},
|
|
Ranker: ranker,
|
|
Summarizer: summarizer,
|
|
Clock: func() time.Time { return time.Unix(1, 0).UTC() },
|
|
})
|
|
result, err := uc.Generate(context.Background(), userID)
|
|
if err != nil || result.Summary == nil {
|
|
t.Fatalf("generation with legacy NULL FIGI asset: summary=%v err=%v", result.Summary, err)
|
|
}
|
|
if len(feed.calls) != 1 || len(feed.calls[0]) != 1 || feed.calls[0][0] != "LEGACY" {
|
|
t.Errorf("legacy asset contribution to scraper symbols=%v, want [[LEGACY]]", feed.calls)
|
|
}
|
|
if len(ranker.assets) != 1 || ranker.assets[0].Symbol != "LEGACY" {
|
|
t.Errorf("legacy asset did not reach ranker: %+v", ranker.assets)
|
|
}
|
|
}
|