129 lines
3.3 KiB
Go
129 lines
3.3 KiB
Go
package host
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestRecommendDisplayPresets(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
primary *DisplayInfo
|
|
wantRec string
|
|
wantHi bool
|
|
}{
|
|
{
|
|
name: "4K at 150%",
|
|
primary: &DisplayInfo{Width: 3840, Height: 2160, ScalePct: 150, Primary: true},
|
|
wantRec: "3840x2160",
|
|
wantHi: true,
|
|
},
|
|
{
|
|
name: "2K 16:9 at 125%",
|
|
primary: &DisplayInfo{Width: 2560, Height: 1440, ScalePct: 125, Primary: true},
|
|
wantRec: "2560x1440",
|
|
wantHi: false,
|
|
},
|
|
{
|
|
name: "2.5K 16:10 at 150%",
|
|
primary: &DisplayInfo{Width: 2560, Height: 1600, ScalePct: 150, Primary: true},
|
|
wantRec: "2560x1600",
|
|
wantHi: true,
|
|
},
|
|
{
|
|
name: "1080p at 100%",
|
|
primary: &DisplayInfo{Width: 1920, Height: 1080, ScalePct: 100, Primary: true},
|
|
wantRec: "1920x1080",
|
|
wantHi: false,
|
|
},
|
|
{
|
|
name: "nil fallback",
|
|
primary: nil,
|
|
wantRec: "1920x1080",
|
|
wantHi: false,
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
presets, rec := RecommendDisplayPresets(tc.primary)
|
|
if rec != tc.wantRec {
|
|
t.Fatalf("rec = %q, want %q", rec, tc.wantRec)
|
|
}
|
|
if len(presets) < 3 {
|
|
t.Fatalf("expected >= 3 presets, got %d", len(presets))
|
|
}
|
|
var foundRec bool
|
|
for _, p := range presets {
|
|
if p.Recommended {
|
|
foundRec = true
|
|
if p.Resolution != tc.wantRec {
|
|
t.Fatalf("recommended preset %q != %q", p.Resolution, tc.wantRec)
|
|
}
|
|
if p.HiDPI != tc.wantHi {
|
|
t.Fatalf("recommended preset HiDPI = %v, want %v", p.HiDPI, tc.wantHi)
|
|
}
|
|
}
|
|
}
|
|
if !foundRec {
|
|
t.Fatalf("no preset marked recommended")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestInspectDiskUsageQCOW2(t *testing.T) {
|
|
tmp := t.TempDir()
|
|
qcow := filepath.Join(tmp, "disk.qcow2")
|
|
hdr := make([]byte, 1024)
|
|
copy(hdr[0:4], []byte{'Q', 'F', 'I', 0xfb})
|
|
binary.BigEndian.PutUint32(hdr[4:8], 3)
|
|
const wantVirtual uint64 = 128 << 30
|
|
binary.BigEndian.PutUint64(hdr[24:32], wantVirtual)
|
|
if err := os.WriteFile(qcow, hdr, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
usage, err := InspectDiskUsage(qcow)
|
|
if err != nil {
|
|
t.Fatalf("InspectDiskUsage: %v", err)
|
|
}
|
|
if usage.Format != "qcow2" {
|
|
t.Fatalf("Format = %q, want qcow2", usage.Format)
|
|
}
|
|
if usage.VirtualBytes != wantVirtual {
|
|
t.Fatalf("VirtualBytes = %d, want %d", usage.VirtualBytes, wantVirtual)
|
|
}
|
|
if usage.ActualBytes == 0 {
|
|
t.Fatalf("ActualBytes should be non-zero")
|
|
}
|
|
}
|
|
|
|
func TestComputeCPUPercentAndSelfSample(t *testing.T) {
|
|
t0 := time.Now()
|
|
s1 := ProcessSample{PID: 42, CPUDuration: 200 * time.Millisecond, SampledAt: t0}
|
|
s2 := ProcessSample{PID: 42, CPUDuration: 600 * time.Millisecond, SampledAt: t0.Add(time.Second)}
|
|
pct := ComputeCPUPercent(s1, s2, 8)
|
|
if pct < 4.9 || pct > 5.1 {
|
|
t.Fatalf("ComputeCPUPercent = %.2f, want ~5.0", pct)
|
|
}
|
|
|
|
if runtime.GOOS == "windows" {
|
|
self, err := SampleProcessStats(os.Getpid())
|
|
if err != nil {
|
|
t.Fatalf("SampleProcessStats(self): %v", err)
|
|
}
|
|
if self.WorkingSet == 0 {
|
|
t.Fatalf("expected non-zero WorkingSet for current process")
|
|
}
|
|
// Ensure Inspect populates displays cleanly without panicking.
|
|
rep := Inspect(t.TempDir())
|
|
if rep.RecommendedResolution == "" || len(rep.DisplayPresets) == 0 {
|
|
t.Fatalf("Inspect should always populate display presets & recommended resolution")
|
|
}
|
|
}
|
|
}
|