test(e2e): add isolated browser journey and cleanup supervision

Add a fake-only end-to-end stack, Firefox journey, and bounded process cleanup for normal, failed, and interrupted runs.

Refs #12

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-11-20 23:39:02 +01:00
parent 8f42c30ec6
commit 4459a81206
28 changed files with 1647 additions and 51 deletions

View File

@ -0,0 +1,39 @@
package server
import (
"fmt"
"github.com/tradarr/backend/internal/auth"
"github.com/tradarr/backend/internal/config"
"github.com/tradarr/backend/internal/models"
"log"
)
type AdminRepository interface {
GetUserByEmail(string) (*models.User, error)
CreateUser(string, string, models.Role) (*models.User, error)
}
func EnsureAdmin(repo AdminRepository, cfg *config.Config) error {
if cfg.AdminEmail == "" {
return nil
}
existing, err := repo.GetUserByEmail(cfg.AdminEmail)
if err != nil {
return err
}
if existing != nil {
return nil
}
if cfg.AdminPassword == "" {
return fmt.Errorf("ADMIN_PASSWORD is required to create the bootstrap admin")
}
hash, err := auth.HashPassword(cfg.AdminPassword)
if err != nil {
return err
}
if _, err = repo.CreateUser(cfg.AdminEmail, hash, models.RoleAdmin); err != nil {
return err
}
log.Printf("admin account created: %s", cfg.AdminEmail)
return nil
}

View File

@ -0,0 +1,51 @@
// Package server composes HTTP and optional scheduled application execution.
// Concrete retrieval/provider adapters belong to the executable's composition root.
package server
import (
"github.com/gin-gonic/gin"
"github.com/tradarr/backend/internal/ai"
"github.com/tradarr/backend/internal/api"
"github.com/tradarr/backend/internal/api/handlers"
"github.com/tradarr/backend/internal/config"
"github.com/tradarr/backend/internal/crypto"
"github.com/tradarr/backend/internal/generation"
"github.com/tradarr/backend/internal/indices"
"github.com/tradarr/backend/internal/models"
"github.com/tradarr/backend/internal/scheduler"
"github.com/tradarr/backend/internal/scraper"
"log"
)
type Dependencies struct {
Feeds map[string]scraper.Scraper
IndexSearcher indices.IndexSearcher
GenerationFactory func(*ai.Pipeline) generation.UseCase
EnableScheduler bool
}
type App struct {
Router *gin.Engine
scheduler *scheduler.Scheduler
}
func New(repo *models.Repository, cfg *config.Config, deps Dependencies) *App {
enc := crypto.New(cfg.EncryptionKey)
pipeline := ai.NewPipeline(repo, enc)
registry := scraper.NewRegistry(repo)
uc := pipeline.NewGenerationUseCase(deps.Feeds)
if deps.GenerationFactory != nil {
uc = deps.GenerationFactory(pipeline)
}
sched := scheduler.New(registry, pipeline, repo)
sched.SetGenerationUseCase(uc)
if deps.EnableScheduler {
if err := sched.Start(); err != nil {
log.Printf("scheduler: %v", err)
}
}
h := handlers.New(repo, cfg, enc, registry, pipeline, sched)
h.SetGenerationUseCase(uc)
h.SetIndexSearcher(deps.IndexSearcher)
return &App{Router: api.SetupRouter(h, cfg.JWTSecret), scheduler: sched}
}
func (a *App) Close() { a.scheduler.Stop() }

View File

@ -0,0 +1,40 @@
package server
import (
"context"
"net/http/httptest"
"testing"
"github.com/tradarr/backend/internal/ai"
"github.com/tradarr/backend/internal/auth"
"github.com/tradarr/backend/internal/config"
"github.com/tradarr/backend/internal/generation"
"github.com/tradarr/backend/internal/indices"
)
type fakeGeneration struct{}
func (fakeGeneration) Generate(context.Context, string) (generation.Result, error) {
return generation.Result{Outcome: generation.GenerationOutcome{Status: "no_results"}}, nil
}
type fakeSearch struct{ calls int }
func (f *fakeSearch) Search(context.Context, string, int) ([]indices.Index, error) {
f.calls++
return []indices.Index{}, nil
}
func TestInjectableCompositionDoesNotStartSchedulerOrNeedDatabase(t *testing.T) {
search := &fakeSearch{}
factoryCalls := 0
app := New(nil, &config.Config{JWTSecret: "composition-test"}, Dependencies{IndexSearcher: search, GenerationFactory: func(*ai.Pipeline) generation.UseCase { factoryCalls++; return fakeGeneration{} }, EnableScheduler: false})
defer app.Close()
token, _ := auth.GenerateToken("owner", "owner@example.test", "user", "composition-test")
request := httptest.NewRequest("GET", "/api/indices/search?q=test", nil)
request.Header.Set("Authorization", "Bearer "+token)
response := httptest.NewRecorder()
app.Router.ServeHTTP(response, request)
if response.Code != 200 || search.calls != 1 || factoryCalls != 1 {
t.Fatalf("status=%d search=%d factory=%d", response.Code, search.calls, factoryCalls)
}
}