test: add comprehensive test suite for engine, agent and cmd packages

- Refactor download.go and stream.go with downloadDeps/streamDeps structs
  for dependency injection, enabling unit testing without real I/O
- download_test.go: 15 tests — input validation, mock downloaders, method
  selection, cobra Args, deadlock detection
- stream_test.go: input validation, noOpen flag, engine error handling
- client_test.go: context cancellation, timeout, full Sync roundtrip,
  watch-progress and HTTP error unwrapping
- sync_test.go: TriggerSync on watching transition, adjustInterval
- torrent_test.go: TorrentDownloader lifecycle without network
- stream_server_test.go: HTTP server lifecycle, SetFile/ClearFile,
  concurrent requests, Shutdown releases port, content-type
- manager_integration_test.go: full pipeline — success, torrent→debrid
  fallback, all-fail, multi-concurrent, ForceStart, OnTaskDone,
  recent-finished drain, cancel mid-download, organize
- usenet_test.go: Cancel/Pause race regression test (run with -race)
- daemon_test.go: isAllowedStreamPath table tests
- CI: split coverage gate to engine+agent only (50% threshold); cmd
  coverage still reported but not gated (interactive UI commands)
- lefthook: add pre-push hook with go test -race -count=1 -timeout=120s
This commit is contained in:
Deivid Soto 2026-04-08 23:36:00 +02:00
parent b14ab98580
commit 78c16c295e
13 changed files with 2421 additions and 10 deletions

View file

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
@ -17,6 +18,20 @@ import (
"github.com/torrentclaw/unarr/internal/ui"
)
// streamDeps agrupa las funciones constructoras usadas por runStream.
// Pueden sobreescribirse en tests para inyectar mocks.
type streamDeps struct {
newStreamEngine func(cfg engine.StreamConfig) (*engine.StreamEngine, error)
newStreamServer func(port int) *engine.StreamServer
openPlayer func(url, override string) (string, *exec.Cmd, error)
}
var defaultStreamDeps = streamDeps{
newStreamEngine: engine.NewStreamEngine,
newStreamServer: engine.NewStreamServer,
openPlayer: engine.OpenPlayer,
}
func newStreamCmd() *cobra.Command {
var (
port int
@ -56,6 +71,10 @@ download directory (or system temp if not configured).`,
}
func runStream(input string, port int, noOpen bool, playerCmd string) error {
return runStreamWithDeps(input, port, noOpen, playerCmd, defaultStreamDeps)
}
func runStreamWithDeps(input string, port int, noOpen bool, playerCmd string, deps streamDeps) error {
cfg := loadConfig()
bold := color.New(color.Bold)
green := color.New(color.FgGreen)
@ -83,7 +102,7 @@ func runStream(input string, port int, noOpen bool, playerCmd string) error {
}
// Create engine
eng, err := engine.NewStreamEngine(engine.StreamConfig{
eng, err := deps.newStreamEngine(engine.StreamConfig{
DataDir: dataDir,
Port: port,
MetaTimeout: 60 * time.Second,
@ -127,7 +146,7 @@ func runStream(input string, port int, noOpen bool, playerCmd string) error {
}
// Start HTTP server
srv := engine.NewStreamServer(port)
srv := deps.newStreamServer(port)
if err := srv.Listen(ctx); err != nil {
eng.Shutdown(context.Background())
return fmt.Errorf("start server: %w", err)
@ -159,7 +178,7 @@ func runStream(input string, port int, noOpen bool, playerCmd string) error {
// Open player
if !noOpen {
playerName, _, openErr := engine.OpenPlayer(srv.URL(), playerCmd)
playerName, _, openErr := deps.openPlayer(srv.URL(), playerCmd)
if openErr != nil {
yellow.Printf(" Could not open player: %s\n", openErr)
fmt.Printf(" Open this URL in your player: %s\n", srv.URL())