package monitor import "testing" func TestToLiteInfo(t *testing.T) { info := MonInfo{ Ver: "1.0.0", Cpu: CpuInfo{ CpuBaseInfo: CpuBaseInfo{ Core: 4, Logic: 8, Info: []CPU{{CPU: 0, ModelName: "TestCPU"}}, }, CpuUsedInfo: CpuUsedInfo{UsedPercent: 55.5}, }, Mem: MemInfo{Total: 16 * 1024 * 1024 * 1024, UsedPercent: 60}, Load: LoadInfo{Load1: 0.5}, Net: []NetInfo{ {Name: "eth0", BytesSent: 10, BytesRecv: 20, PacketsSent: 1, PacketsRecv: 2, TotalBytesSent: 100, TotalBytesRecv: 200}, {Name: "eth1", BytesSent: 30, BytesRecv: 40, PacketsSent: 3, PacketsRecv: 4, TotalBytesSent: 300, TotalBytesRecv: 400}, }, Disk: []DiskInfo{ {Device: "C:", MountPoint: "C:\\", UsedPercent: 80, UsedVolume: "80GB", ReadBytes: 5, WriteBytes: 6}, }, } lite := ToLiteInfo(info) if lite.Ver != "1.0.0" { t.Errorf("Ver = %q", lite.Ver) } if lite.Cpu.Core != 4 || lite.Cpu.Logic != 8 { t.Errorf("Cpu Core/Logic = %d/%d, want 4/8", lite.Cpu.Core, lite.Cpu.Logic) } if lite.Cpu.UsedPercent != 55.5 { t.Errorf("Cpu.UsedPercent = %v", lite.Cpu.UsedPercent) } if len(lite.Cpu.Name) != 1 || lite.Cpu.Name[0] != "TestCPU" { t.Errorf("Cpu.Name = %v", lite.Cpu.Name) } if lite.Mem.Total != 16*1024*1024*1024 { t.Errorf("Mem.Total = %d", lite.Mem.Total) } if lite.Load.Load1 != 0.5 { t.Errorf("Load1 = %v", lite.Load.Load1) } // Net 多网卡应累加 if lite.Net.BytesSent != 40 || lite.Net.BytesRecv != 60 { t.Errorf("Net.BytesSent/Recv = %d/%d, want 40/60", lite.Net.BytesSent, lite.Net.BytesRecv) } if lite.Net.TotalBytesSent != 400 || lite.Net.TotalBytesRecv != 600 { t.Errorf("Net.Total = %d/%d, want 400/600", lite.Net.TotalBytesSent, lite.Net.TotalBytesRecv) } if len(lite.Disk) != 1 || lite.Disk[0].Device != "C:" || lite.Disk[0].ReadBytes != 5 { t.Errorf("Disk = %+v", lite.Disk) } } func TestFormatFileSize(t *testing.T) { cases := []struct { in uint64 want string }{ {512, "512.00B"}, {1536, "1.50KB"}, {1024 * 1024, "1.00MB"}, {1024 * 1024 * 1024, "1.00GB"}, } for _, c := range cases { if got := formatFileSize(c.in); got != c.want { t.Errorf("formatFileSize(%d) = %q, want %q", c.in, got, c.want) } } } func TestGetInfoZeroValue(t *testing.T) { // 未调用 Start 时 GetInfo 返回零值快照且不 panic info := GetInfo() if info.Ver != "" { t.Errorf("未启动时 Ver 应为空: %q", info.Ver) } }