package updater

import (
	"testing"

	"galaxy/connector"
	gerr "galaxy/error"

	"github.com/stretchr/testify/require"
)

func TestArtifactPathWindowsAddsExe(t *testing.T) {
	t.Parallel()

	got := ArtifactPath("1.2.3", "windows", "amd64", connector.ArtifactKindExecutable)
	require.Equal(t, `ui\client-ui-1.2.3-windows-amd64-executable.exe`, got)
}

func TestLatestCompatibleVersionSelectsPlatformExecutable(t *testing.T) {
	t.Parallel()

	versions := []connector.VersionInfo{
		{OS: "linux", Arch: "amd64", Kind: connector.ArtifactKindExecutable, Version: "1.0.0"},
		{OS: "windows", Arch: "amd64", Kind: connector.ArtifactKindExecutable, Version: "1.2.0"},
		{OS: "windows", Arch: "amd64", Kind: connector.ArtifactKindExecutable, Version: "1.3.0"},
		{OS: "windows", Arch: "arm64", Kind: connector.ArtifactKindExecutable, Version: "9.9.9"},
	}

	got, ok, err := LatestCompatibleVersion(versions, "windows", "amd64", connector.ArtifactKindExecutable)
	require.NoError(t, err)
	require.True(t, ok)
	require.Equal(t, "1.3.0", got.Version)
}

func TestLatestCompatibleVersionRejectsUnsupportedKinds(t *testing.T) {
	t.Parallel()

	versions := []connector.VersionInfo{
		{OS: "windows", Arch: "amd64", Kind: "shared-library", Version: "1.0.0"},
	}

	_, ok, err := LatestCompatibleVersion(versions, "windows", "amd64", connector.ArtifactKindExecutable)
	require.False(t, ok)
	require.Error(t, err)
	require.True(t, gerr.IsService(err))
}

func TestLatestCompatibleVersionRejectsAmbiguousVersions(t *testing.T) {
	t.Parallel()

	versions := []connector.VersionInfo{
		{OS: "windows", Arch: "amd64", Kind: connector.ArtifactKindExecutable, Version: "1.0.0"},
		{OS: "windows", Arch: "amd64", Kind: connector.ArtifactKindExecutable, Version: "1.0.0"},
	}

	_, ok, err := LatestCompatibleVersion(versions, "windows", "amd64", connector.ArtifactKindExecutable)
	require.False(t, ok)
	require.Error(t, err)
	require.True(t, gerr.IsService(err))
}
