test(coverage): raise engine+agent coverage above 50%
This commit is contained in:
parent
e89b647dfa
commit
bf18812a3d
10 changed files with 839 additions and 2 deletions
62
internal/agent/disk_test.go
Normal file
62
internal/agent/disk_test.go
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
package agent
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDirSize(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(root, "a.bin"), make([]byte, 100), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Join(root, "sub"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(root, "sub", "b.bin"), make([]byte, 250), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
got, err := DirSize(root)
|
||||
if err != nil {
|
||||
t.Fatalf("DirSize error: %v", err)
|
||||
}
|
||||
if got != 350 {
|
||||
t.Errorf("DirSize = %d, want 350", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirSizeEmpty(t *testing.T) {
|
||||
got, err := DirSize(t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatalf("DirSize empty dir error: %v", err)
|
||||
}
|
||||
if got != 0 {
|
||||
t.Errorf("DirSize empty = %d, want 0", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirSizeMissing(t *testing.T) {
|
||||
// Walk skips unreadable entries — missing path returns 0 with no error.
|
||||
got, err := DirSize("/nonexistent/path/zzz")
|
||||
if err != nil {
|
||||
t.Errorf("DirSize on missing path = err %v, want nil", err)
|
||||
}
|
||||
if got != 0 {
|
||||
t.Errorf("DirSize on missing path = %d, want 0", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiskInfoCurrentDir(t *testing.T) {
|
||||
free, total, err := DiskInfo(".")
|
||||
if err != nil {
|
||||
t.Fatalf("DiskInfo: %v", err)
|
||||
}
|
||||
if total <= 0 {
|
||||
t.Errorf("total bytes should be > 0, got %d", total)
|
||||
}
|
||||
if free > total {
|
||||
t.Errorf("free (%d) should not exceed total (%d)", free, total)
|
||||
}
|
||||
}
|
||||
22
internal/agent/process_unix_test.go
Normal file
22
internal/agent/process_unix_test.go
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
//go:build !windows
|
||||
|
||||
package agent
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsProcessAliveSelf(t *testing.T) {
|
||||
if !IsProcessAlive(os.Getpid()) {
|
||||
t.Errorf("self PID should be alive")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsProcessAliveBogus(t *testing.T) {
|
||||
// PID 0 is reserved (signal 0 to PID 0 broadcasts to the whole pgrp).
|
||||
// Pick a very high PID unlikely to exist.
|
||||
if IsProcessAlive(0x7FFFFFFE) {
|
||||
t.Errorf("very high PID should not be alive")
|
||||
}
|
||||
}
|
||||
|
|
@ -215,3 +215,56 @@ func TestLocalState_EmptySnapshot(t *testing.T) {
|
|||
t.Errorf("expected 0 tasks, got %d", len(snap))
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskStateFromUpdate(t *testing.T) {
|
||||
u := StatusUpdate{
|
||||
TaskID: "task-1",
|
||||
Status: "downloading",
|
||||
Progress: 42,
|
||||
DownloadedBytes: 1024,
|
||||
TotalBytes: 4096,
|
||||
SpeedBps: 100,
|
||||
ETA: 30,
|
||||
ResolvedMethod: "torrent",
|
||||
FileName: "movie.mkv",
|
||||
FilePath: "/tmp/movie.mkv",
|
||||
StreamURL: "http://localhost/stream",
|
||||
ErrorMessage: "",
|
||||
}
|
||||
got := TaskStateFromUpdate(u)
|
||||
if got.TaskID != "task-1" || got.Status != "downloading" || got.Progress != 42 {
|
||||
t.Errorf("basic fields wrong: %+v", got)
|
||||
}
|
||||
if got.DownloadedBytes != 1024 || got.TotalBytes != 4096 || got.SpeedBps != 100 {
|
||||
t.Errorf("byte fields wrong: %+v", got)
|
||||
}
|
||||
if got.ResolvedMethod != "torrent" || got.FileName != "movie.mkv" {
|
||||
t.Errorf("method/name fields wrong: %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestShortID(t *testing.T) {
|
||||
if got := ShortID("abcdef1234567890"); got != "abcdef12" {
|
||||
t.Errorf("ShortID = %q", got)
|
||||
}
|
||||
if got := ShortID("short"); got != "short" {
|
||||
t.Errorf("ShortID short = %q", got)
|
||||
}
|
||||
if got := ShortID(""); got != "" {
|
||||
t.Errorf("ShortID empty = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStateFilePath(t *testing.T) {
|
||||
if got := StateFilePath(); got == "" {
|
||||
t.Errorf("StateFilePath should not be empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTTPError(t *testing.T) {
|
||||
e := &HTTPError{StatusCode: 404, Message: "not found"}
|
||||
got := e.Error()
|
||||
if got == "" || got == "API error 0: " {
|
||||
t.Errorf("HTTPError.Error() unexpected: %q", got)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue