sub2api/backend/internal/pkg/windsurf/platform_test.go

95 lines
3.2 KiB
Go

package windsurf
import (
"errors"
"strings"
"testing"
)
func TestBinaryFilename(t *testing.T) {
tests := []struct {
name string
p Platform
want string
wantErr bool
}{
{"linux amd64", Platform{"linux", "amd64"}, "language_server_linux_x64", false},
{"linux arm64", Platform{"linux", "arm64"}, "language_server_linux_arm", false},
{"darwin arm64", Platform{"darwin", "arm64"}, "language_server_macos_arm", false},
{"darwin amd64 (intel mac)", Platform{"darwin", "amd64"}, "language_server_macos_x64", false},
{"windows amd64", Platform{"windows", "amd64"}, "language_server_windows_x64.exe", false},
{"windows arm64", Platform{"windows", "arm64"}, "language_server_windows_arm.exe", false},
{"freebsd", Platform{"freebsd", "amd64"}, "", true},
{"linux 386", Platform{"linux", "386"}, "", true},
{"empty", Platform{}, "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := BinaryFilename(tt.p)
if tt.wantErr {
if err == nil {
t.Fatalf("expected error, got nil (value %q)", got)
}
if !errors.Is(err, ErrUnsupportedPlatform) {
t.Fatalf("expected ErrUnsupportedPlatform in chain, got %v", err)
}
// Error message should point the user toward Docker.
if !strings.Contains(err.Error(), "docker") {
t.Errorf("unsupported-platform error should mention docker, got: %v", err)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != tt.want {
t.Errorf("BinaryFilename(%v) = %q, want %q", tt.p, got, tt.want)
}
})
}
}
func TestDetectPlatformFor(t *testing.T) {
tests := []struct {
name string
goos string
goarch string
rosetta bool
wantOS string
wantArch string
}{
{"linux amd64 unchanged", "linux", "amd64", false, "linux", "amd64"},
{"windows amd64 unchanged", "windows", "amd64", false, "windows", "amd64"},
{"darwin arm64 unchanged", "darwin", "arm64", false, "darwin", "arm64"},
{"darwin amd64 no rosetta stays amd64", "darwin", "amd64", false, "darwin", "amd64"},
{"darwin amd64 under rosetta promotes to arm64", "darwin", "amd64", true, "darwin", "arm64"},
{"linux amd64 rosetta flag ignored (not darwin)", "linux", "amd64", true, "linux", "amd64"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := detectPlatformFor(tt.goos, tt.goarch, tt.rosetta)
if got.OS != tt.wantOS || got.Arch != tt.wantArch {
t.Errorf("detectPlatformFor(%q,%q,%v) = %v, want {OS:%q Arch:%q}",
tt.goos, tt.goarch, tt.rosetta, got, tt.wantOS, tt.wantArch)
}
})
}
}
func TestPlatformString(t *testing.T) {
if got := (Platform{OS: "darwin", Arch: "arm64"}).String(); got != "darwin/arm64" {
t.Errorf("Platform.String() = %q, want %q", got, "darwin/arm64")
}
}
func TestProcTranslatedProbeNonDarwin(t *testing.T) {
// The default probe must always return false on non-darwin platforms
// without calling sysctl. We can only validate this runtime behavior on
// the builder we're on — for non-darwin builders the probe should be
// false even if sysctl were somehow present.
//
// For darwin builders, we accept either result; we only assert that the
// probe does not panic and returns a bool.
_ = defaultProcTranslatedProbe()
}