package platform import ( "runtime" "strings" "testing" ) func TestGetOSType(t *testing.T) { got := getOSType() switch runtime.GOOS { case "windows": if got != Windows { t.Errorf("getOSType() = %q, want windows", got) } case "darwin": if got != Darwin { t.Errorf("getOSType() = %q, want darwin", got) } default: if got != Linux { t.Errorf("getOSType() = %q, want linux", got) } } } func TestGetOSTypeDisplay(t *testing.T) { if got := (&Platform{OSType: Darwin}).getOSTypeDisplay(); got != "macOS" { t.Errorf("getOSTypeDisplay(Darwin) = %q, want macOS", got) } if got := (&Platform{OSType: Linux}).getOSTypeDisplay(); got != "linux" { t.Errorf("getOSTypeDisplay(Linux) = %q, want linux", got) } if got := (&Platform{OSType: Windows}).getOSTypeDisplay(); got != "windows" { t.Errorf("getOSTypeDisplay(Windows) = %q, want windows", got) } } func TestPlatformGetCPULevelMethod(t *testing.T) { p := &Platform{CPUInfo: CPUInfo{ArchitectureLevel: "x86-64-v3"}} if got := p.GetCPULevel(); got != "x86-64-v3" { t.Errorf("GetCPULevel() = %q, want x86-64-v3", got) } } func TestGetMacOSVersionName(t *testing.T) { tests := []struct { version string want string }{ {"10.15", "Catalina"}, {"11", "Big Sur"}, {"14", "Sonoma"}, {"15", "Sequoia"}, {"26", "Tahoe"}, {"99", "99"}, // unknown versions pass through unchanged {"", ""}, } for _, tt := range tests { t.Run(tt.version, func(t *testing.T) { if got := getMacOSVersionName(tt.version); got != tt.want { t.Errorf("getMacOSVersionName(%q) = %q, want %q", tt.version, got, tt.want) } }) } } func TestDetectMacOSVersionNonDarwin(t *testing.T) { if runtime.GOOS == "darwin" { t.Skip("covered natively on darwin") } // The runner must not even be consulted off-darwin. runner := &fakeProbeRunner{responses: map[string][]fakeProbeResponse{}} if got := detectMacOSVersionWithRunner(runner); got != "" { t.Errorf("detectMacOSVersionWithRunner() = %q, want empty on non-darwin", got) } if calls := runner.called(); len(calls) != 0 { t.Errorf("runner was called on non-darwin: %+v", calls) } } func TestPlatformString(t *testing.T) { tests := []struct { name string p Platform want string }{ { name: "linux with distro", p: Platform{OSType: Linux, Arch: "amd64", Distro: "arch", DistroFamily: "arch"}, want: "linux amd64 (arch/arch)", }, { name: "linux without distro", p: Platform{OSType: Linux, Arch: "arm64"}, want: "linux arm64", }, { name: "windows", p: Platform{OSType: Windows, Arch: "amd64"}, want: "windows amd64", }, { name: "macOS with codename and cpu model", p: Platform{OSType: Darwin, Version: "15", CPUInfo: CPUInfo{Model: "Apple M4"}}, want: "macOS 15 - Sequoia (Apple M4)", }, { name: "macOS unknown version passes through", p: Platform{OSType: Darwin, Version: "99", CPUInfo: CPUInfo{Model: "Apple M4"}}, want: "macOS 99 (Apple M4)", }, { name: "macOS without cpu model", p: Platform{OSType: Darwin, Version: "14"}, want: "macOS 14 - Sonoma", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := tt.p.String(); got != tt.want { t.Errorf("String() = %q, want %q", got, tt.want) } }) } } // TestDetect runs full platform detection on the host. Probe tools that are // missing fail fast and detection degrades gracefully, so this stays an // integration smoke test rather than asserting specific hardware. func TestDetect(t *testing.T) { p := Detect() if p == nil { t.Fatal("Detect() returned nil") } switch runtime.GOOS { case "linux": if p.OSType != Linux { t.Errorf("OSType = %q, want linux", p.OSType) } if p.Distro == "" { t.Error("Distro must be detected on Linux (os-release is always present)") } if p.DistroFamily == "" { t.Error("DistroFamily must be detected on Linux") } case "windows": if p.OSType != Windows { t.Errorf("OSType = %q, want windows", p.OSType) } case "darwin": if p.OSType != Darwin { t.Errorf("OSType = %q, want darwin", p.OSType) } } if p.Arch != runtime.GOARCH { t.Errorf("Arch = %q, want %q", p.Arch, runtime.GOARCH) } if p.GPUInfo == nil { t.Error("GPUInfo must not be nil") } if p.CPUInfo.ArchitectureLevel == "" { t.Error("CPUInfo.ArchitectureLevel must be populated") } } func TestGetCPULevelPackageFunc(t *testing.T) { if got := GetCPULevel(); got == "" { t.Error("GetCPULevel() returned empty after detection") } } // TestDetectCodecSupportDarwinStub documents the non-darwin stub: it is // compiled on Linux/Windows and must report an error instead of pretending to // detect anything. func TestDetectCodecSupportDarwinStub(t *testing.T) { if runtime.GOOS == "darwin" { t.Skip("real implementation on darwin") } if _, err := detectCodecSupportDarwin(); err == nil { t.Error("detectCodecSupportDarwin() on non-darwin must return an error") } } // TestDetectCodecSupportLinuxHost exercises the real detection switch on this // host; with vainfo/vdpauinfo absent it falls back to the GPU database or // "unknown", but it must always return without error. func TestDetectCodecSupportLinuxHost(t *testing.T) { if runtime.GOOS != "linux" { t.Skip("Linux-only path") } codecs, err := detectCodecSupport(&GPUInfo{Models: []string{}, Brand: ""}) if err != nil { t.Fatalf("detectCodecSupport() error = %v", err) } if len(codecs) == 0 { t.Error("detectCodecSupport() returned no codecs, want at least [unknown]") } for _, codec := range codecs { if strings.TrimSpace(codec) == "" { t.Errorf("detectCodecSupport() returned an empty codec in %v", codecs) } } }