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

@ -17,6 +17,26 @@ import (
"github.com/torrentclaw/unarr/internal/parser"
)
// downloadDeps agrupa las funciones constructoras usadas por runDownload.
// Pueden sobreescribirse en tests para inyectar mocks.
type downloadDeps struct {
newTorrentDl func(cfg engine.TorrentConfig) (engine.Downloader, error)
newDebridDl func() engine.Downloader
newAgentClient func(url, key, ua string) *agent.Client
newManager func(cfg engine.ManagerConfig, reporter *engine.ProgressReporter, dls ...engine.Downloader) *engine.Manager
}
var defaultDownloadDeps = downloadDeps{
newTorrentDl: func(cfg engine.TorrentConfig) (engine.Downloader, error) {
return engine.NewTorrentDownloader(cfg)
},
newDebridDl: func() engine.Downloader {
return engine.NewDebridDownloader()
},
newAgentClient: agent.NewClient,
newManager: engine.NewManager,
}
func newDownloadCmd() *cobra.Command {
var method string
@ -48,6 +68,10 @@ daemon instead: 'unarr start'.`,
}
func runDownload(input, method string) error {
return runDownloadWithDeps(input, method, defaultDownloadDeps)
}
func runDownloadWithDeps(input, method string, deps downloadDeps) error {
cfg := loadConfig()
bold := color.New(color.Bold)
green := color.New(color.FgGreen)
@ -84,7 +108,7 @@ func runDownload(input, method string) error {
fmt.Println()
// Create torrent downloader
torrentDl, err := engine.NewTorrentDownloader(engine.TorrentConfig{
torrentDl, err := deps.newTorrentDl(engine.TorrentConfig{
DataDir: outputDir,
MetadataTimeout: 15 * time.Minute,
StallTimeout: 10 * time.Minute,
@ -97,13 +121,13 @@ func runDownload(input, method string) error {
// Create a dummy reporter (no API reporting for one-shot)
reporter := engine.NewProgressReporter(
agent.NewClient(cfg.Auth.APIURL, cfg.Auth.APIKey, "unarr/"+Version),
deps.newAgentClient(cfg.Auth.APIURL, cfg.Auth.APIKey, "unarr/"+Version),
5*time.Second,
)
debridDl := engine.NewDebridDownloader()
debridDl := deps.newDebridDl()
manager := engine.NewManager(engine.ManagerConfig{
manager := deps.newManager(engine.ManagerConfig{
MaxConcurrent: 1,
OutputDir: outputDir,
Organize: engine.OrganizeConfig{