package api_test import ( "context" "encoding/json" "github.com/tradarr/backend/internal/api" "github.com/tradarr/backend/internal/api/handlers" "github.com/tradarr/backend/internal/auth" "github.com/tradarr/backend/internal/config" "github.com/tradarr/backend/internal/generation" "github.com/tradarr/backend/internal/models" "net/http/httptest" "testing" "time" ) func TestTicket47ExistingReadContractIncludesScheduledFailuresAndManualRunningPostgres(t *testing.T) { db, _ := ticket7DB(t) for _, q := range []string{ `CREATE TABLE users(id UUID PRIMARY KEY)`, `INSERT INTO users VALUES('00000000-0000-0000-0000-000000000001'),('00000000-0000-0000-0000-000000000002')`, `CREATE TABLE summary_generations(id UUID PRIMARY KEY,user_id UUID,summary_id UUID,outcome JSONB,created_at TIMESTAMPTZ)`, } { if _, err := db.Exec(q); err != nil { t.Fatal(err) } } installJobSchema(t, db) repo := models.NewRepository(db) ctx := context.Background() owner, other := "00000000-0000-0000-0000-000000000001", "00000000-0000-0000-0000-000000000002" now := time.Now().UTC() // Same persistence path as the scheduler: no HTTP job in the context. for i, user := range []string{owner, owner, other} { id := []string{"00000000-0000-0000-0000-000000000011", "00000000-0000-0000-0000-000000000012", "00000000-0000-0000-0000-000000000013"}[i] out := generation.GenerationOutcome{ID: id, Status: "failed", FailureCode: "no_articles_fit_prompt_budget", ArticlesRemovedForPromptBudget: 3, CreatedAt: now.Add(time.Duration(i) * time.Second)} raw, _ := json.Marshal(out) if err := repo.SaveGeneration(ctx, user, id, nil, raw, out.CreatedAt); err != nil { t.Fatal(err) } } job := models.GenerationJob{ID: "00000000-0000-0000-0000-000000000021", UserID: owner, Status: "running", CreatedAt: now.Add(5 * time.Second)} if _, created, err := repo.CreateGenerationJob(ctx, job); err != nil || !created { t.Fatal("manual running setup failed", err) } h := handlers.New(repo, &config.Config{}, nil, nil, nil, nil) router := api.SetupRouter(h, ticket7JWTSecret) token, _ := auth.GenerateToken(owner, "fixture@example.test", "user", ticket7JWTSecret) get := func(path string) *httptest.ResponseRecorder { req := httptest.NewRequest("GET", path, nil) req.Header.Set("Authorization", "Bearer "+token) w := httptest.NewRecorder() router.ServeHTTP(w, req) if w.Code != 200 { t.Fatal(w.Code, w.Body) } return w } var history struct{ Data []models.GenerationHistory } if err := json.Unmarshal(get("/api/summaries/generations?limit=1").Body.Bytes(), &history); err != nil { t.Fatal(err) } if len(history.Data) != 1 || history.Data[0].ID != "00000000-0000-0000-0000-000000000012" { t.Fatal("latest scheduled result missing or ownership/order wrong") } var outcome map[string]json.RawMessage json.Unmarshal(history.Data[0].Outcome, &outcome) if string(outcome["articles_removed_for_prompt_budget"]) != "3" || string(outcome["failure_code"]) != `"no_articles_fit_prompt_budget"` { t.Fatal("safe terminal aggregate missing") } var recent struct{ Data models.GenerationJob } if err := json.Unmarshal(get("/api/summaries/generation-jobs/recent").Body.Bytes(), &recent); err != nil { t.Fatal(err) } if recent.Data.ID != job.ID || recent.Data.Status != "running" { t.Fatal("manual running state missing") } }