package host import ( "encoding/binary" "fmt" "os" "path/filepath" "runtime" "testing" "time" ) func TestGuestStartsHiDPI(t *testing.T) { cases := []struct { w, h, scale int want bool }{ {1920, 1080, 200, false}, // too small at any scale: the case of a 1x desktop on a 200% screen {2048, 1536, 150, true}, // smallest HiDPI window above 160 ppi {2047, 1536, 200, false}, {2560, 1440, 200, false}, // 1440 high is under 1536 {2560, 1600, 150, true}, {2880, 1800, 145, false}, // 145% is exactly 160 ppi, not above {2880, 1800, 125, false}, {3200, 1800, 100, false}, {3840, 2160, 100, true}, // any density from 3840 x 2160 {3840, 2152, 100, false}, {2880, 1800, 0, false}, // unknown scale counts as 1x } for _, c := range cases { if got := GuestStartsHiDPI(c.w, c.h, c.scale); got != c.want { t.Errorf("GuestStartsHiDPI(%d, %d, %d%%) = %v, want %v", c.w, c.h, c.scale, got, c.want) } } } func TestRecommendDisplayPresets(t *testing.T) { cases := []struct { name string primary *DisplayInfo wantRec string wantHi bool wantSizes []string advice string }{ { name: "3072x1920 at 200%, as a Retina remote display", primary: &DisplayInfo{Width: 3072, Height: 1920, ScalePct: 200, WorkWidth: 3072, WorkHeight: 1824}, wantRec: "2560x1600", wantHi: true, wantSizes: []string{"2560x1600", "1920x1200", "1680x1050", "1280x800"}, }, { name: "4K at 150%", primary: &DisplayInfo{Width: 3840, Height: 2160, ScalePct: 150, WorkWidth: 3840, WorkHeight: 2088}, wantRec: "3200x1800", wantHi: true, wantSizes: []string{"3200x1800", "2880x1620", "2560x1440", "1920x1080"}, }, { // No window fits 3840 x 2160, the one size HiDPI at 100%; full screen is. name: "4K at 100%", primary: &DisplayInfo{Width: 3840, Height: 2160, ScalePct: 100, WorkWidth: 3840, WorkHeight: 2112}, wantRec: "3200x1800", wantHi: false, wantSizes: []string{"3200x1800", "2880x1620", "2560x1440", "1920x1080"}, advice: HiDPIAdviceFullscreen, }, { name: "2.5K 16:10 laptop at 150%", primary: &DisplayInfo{Width: 2560, Height: 1600, ScalePct: 150, WorkWidth: 2560, WorkHeight: 1528}, wantRec: "1920x1200", wantHi: false, wantSizes: []string{"1920x1200", "1680x1050", "1280x800"}, // The whole 2560 x 1600 screen is HiDPI at 150%. advice: HiDPIAdviceFullscreen, }, { name: "1080p at 100%, work area unknown", primary: &DisplayInfo{Width: 1920, Height: 1080, ScalePct: 100}, wantRec: "1600x900", wantHi: false, wantSizes: []string{"1600x900", "1280x720"}, advice: HiDPIAdviceUnavailable, }, { name: "2880x1800 laptop at 100%", primary: &DisplayInfo{Width: 2880, Height: 1800, ScalePct: 100, WorkWidth: 2880, WorkHeight: 1752}, wantRec: "2560x1600", wantHi: false, wantSizes: []string{"2560x1600", "1920x1200", "1680x1050", "1280x800"}, advice: HiDPIAdviceScale, }, { name: "nil fallback", primary: nil, wantRec: "1920x1080", wantHi: false, wantSizes: []string{"1920x1080", "2560x1440", "2880x1800"}, }, } 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) } var sizes []string recs := 0 for _, p := range presets { sizes = append(sizes, p.Resolution) var w, h int _, _ = fmt.Sscanf(p.Resolution, "%dx%d", &w, &h) scale := 0 if tc.primary != nil { scale = tc.primary.ScalePct } if p.HiDPI != (tc.primary != nil && GuestStartsHiDPI(w, h, scale)) { t.Errorf("%s HiDPI = %v disagrees with GuestStartsHiDPI", p.Resolution, p.HiDPI) } if p.Recommended { recs++ if p.Resolution != tc.wantRec || p.HiDPI != tc.wantHi { t.Errorf("recommended %s HiDPI=%v, want %s HiDPI=%v", p.Resolution, p.HiDPI, tc.wantRec, tc.wantHi) } } } if recs != 1 { t.Errorf("%d presets recommended, want 1", recs) } if fmt.Sprint(sizes) != fmt.Sprint(tc.wantSizes) { t.Errorf("sizes = %v, want %v", sizes, tc.wantSizes) } if got := HiDPIAdvice(tc.primary, presets); got != tc.advice { t.Errorf("HiDPIAdvice = %q, want %q", got, tc.advice) } }) } } 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") } if usage.BackingDepth != 0 || usage.BackingBroken { t.Fatalf("standalone BackingDepth=%d Broken=%v, want 0/false", usage.BackingDepth, usage.BackingBroken) } } func writeTestQCOW2(t *testing.T, path, backing string) { t.Helper() hdr := make([]byte, 512) copy(hdr[0:4], []byte{'Q', 'F', 'I', 0xfb}) binary.BigEndian.PutUint32(hdr[4:8], 3) binary.BigEndian.PutUint64(hdr[24:32], 64<<30) if backing != "" { const off = uint64(128) binary.BigEndian.PutUint64(hdr[8:16], off) binary.BigEndian.PutUint32(hdr[16:20], uint32(len(backing))) copy(hdr[off:], []byte(backing)) } if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(path, hdr, 0o644); err != nil { t.Fatal(err) } } func TestInspectQCOW2BackingDepthAndVMDiskChains(t *testing.T) { root := t.TempDir() vms := filepath.Join(root, "data", "reims-ctl", "vms") base := filepath.Join(vms, "base-vm", "disk.qcow2") l1 := filepath.Join(vms, "cow-l1", "disk.qcow2") l2 := filepath.Join(vms, "cow-l2", "disk.qcow2") broken := filepath.Join(vms, "cow-broken", "disk.qcow2") cycleA := filepath.Join(root, "cycle-a.qcow2") cycleB := filepath.Join(root, "cycle-b.qcow2") writeTestQCOW2(t, base, "") writeTestQCOW2(t, l1, base) writeTestQCOW2(t, l2, l1) writeTestQCOW2(t, broken, filepath.Join(root, "missing.qcow2")) writeTestQCOW2(t, cycleA, "cycle-b.qcow2") writeTestQCOW2(t, cycleB, "cycle-a.qcow2") if d, br := InspectQCOW2BackingDepth(base); d != 0 || br { t.Fatalf("base = (%d, %v), want (0, false)", d, br) } if d, br := InspectQCOW2BackingDepth(l1); d != 1 || br { t.Fatalf("l1 = (%d, %v), want (1, false)", d, br) } if d, br := InspectQCOW2BackingDepth(l2); d != 2 || br { t.Fatalf("l2 = (%d, %v), want (2, false)", d, br) } if d, br := InspectQCOW2BackingDepth(broken); d != 1 || !br { t.Fatalf("broken = (%d, %v), want (1, true)", d, br) } if _, br := InspectQCOW2BackingDepth(cycleA); !br { t.Fatalf("expected cycle to be flagged broken") } issues := InspectVMDiskChains(root) if len(issues) != 2 { t.Fatalf("InspectVMDiskChains returned %d issues, want 2: %+v", len(issues), issues) } if issues[0].VMName != "cow-broken" || !issues[0].Broken { t.Fatalf("issues[0] = %+v", issues[0]) } if issues[1].VMName != "cow-l2" || issues[1].Depth != 2 || issues[1].Broken { t.Fatalf("issues[1] = %+v", issues[1]) } } 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") } } }