feat(cli): upgrade command, rich status, and version cache
- Replace `upgrade` stub with real command (alias for `self-update`)
- Also register `update` as alias: `unarr update` works too
- Rewrite `status` to show full config, disk usage, daemon state, and
update availability with colored sections
- Add version check cache (1h TTL) so `status` is instant on repeat runs
- Guard against division by zero on empty filesystems
- Guard against negative durations from clock skew
- Guard against stale PID via heartbeat recency check (2 min)
- Add comprehensive test coverage across agent, engine, upgrade, usenet,
arr, library, mediaserver, and UI packages
- Improve Makefile coverage target to exclude cmd/ glue code
- Fix stream handler resource cleanup and ffprobe error handling
2026-03-31 22:05:43 +02:00
|
|
|
package cmd
|
|
|
|
|
|
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
2026-04-08 23:36:00 +02:00
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestIsAllowedStreamPath(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
filePath string
|
|
|
|
|
allowedDirs []string
|
|
|
|
|
want bool
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "path inside download dir",
|
|
|
|
|
filePath: "/downloads/movie.mkv",
|
|
|
|
|
allowedDirs: []string{"/downloads"},
|
|
|
|
|
want: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "path inside subdirectory",
|
|
|
|
|
filePath: "/downloads/sub/movie.mkv",
|
|
|
|
|
allowedDirs: []string{"/downloads"},
|
|
|
|
|
want: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "path traversal attempt",
|
|
|
|
|
filePath: "/downloads/../etc/passwd",
|
|
|
|
|
allowedDirs: []string{"/downloads"},
|
|
|
|
|
want: false,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "path outside all allowed dirs",
|
|
|
|
|
filePath: "/etc/passwd",
|
|
|
|
|
allowedDirs: []string{"/downloads", "/movies"},
|
|
|
|
|
want: false,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "path inside second allowed dir",
|
|
|
|
|
filePath: "/movies/action/movie.mkv",
|
|
|
|
|
allowedDirs: []string{"/downloads", "/movies"},
|
|
|
|
|
want: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "empty allowed dirs",
|
|
|
|
|
filePath: "/downloads/movie.mkv",
|
|
|
|
|
allowedDirs: []string{"", ""},
|
|
|
|
|
want: false,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "path equals allowed dir exactly",
|
|
|
|
|
filePath: "/downloads",
|
|
|
|
|
allowedDirs: []string{"/downloads"},
|
|
|
|
|
want: true,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
got := isAllowedStreamPath(tt.filePath, tt.allowedDirs...)
|
|
|
|
|
if got != tt.want {
|
|
|
|
|
t.Errorf("isAllowedStreamPath(%q, %v) = %v, want %v",
|
|
|
|
|
tt.filePath, tt.allowedDirs, got, tt.want)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
feat(cli): upgrade command, rich status, and version cache
- Replace `upgrade` stub with real command (alias for `self-update`)
- Also register `update` as alias: `unarr update` works too
- Rewrite `status` to show full config, disk usage, daemon state, and
update availability with colored sections
- Add version check cache (1h TTL) so `status` is instant on repeat runs
- Guard against division by zero on empty filesystems
- Guard against negative durations from clock skew
- Guard against stale PID via heartbeat recency check (2 min)
- Add comprehensive test coverage across agent, engine, upgrade, usenet,
arr, library, mediaserver, and UI packages
- Improve Makefile coverage target to exclude cmd/ glue code
- Fix stream handler resource cleanup and ffprobe error handling
2026-03-31 22:05:43 +02:00
|
|
|
|
|
|
|
|
func TestFormatSpeedLog(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
bps int64
|
|
|
|
|
want string
|
|
|
|
|
}{
|
|
|
|
|
{0, "0 B/s"},
|
|
|
|
|
{500, "500 B/s"},
|
|
|
|
|
{1023, "1023 B/s"},
|
|
|
|
|
{1024, "1 KB/s"},
|
|
|
|
|
{10240, "10 KB/s"},
|
|
|
|
|
{1048576, "1.0 MB/s"},
|
|
|
|
|
{5242880, "5.0 MB/s"},
|
|
|
|
|
{1073741824, "1.0 GB/s"},
|
|
|
|
|
{2147483648, "2.0 GB/s"},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.want, func(t *testing.T) {
|
|
|
|
|
got := formatSpeedLog(tt.bps)
|
|
|
|
|
if got != tt.want {
|
|
|
|
|
t.Errorf("formatSpeedLog(%d) = %q, want %q", tt.bps, got, tt.want)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|