package recipe

import (
	"testing"

	recipePkg "coopcloud.tech/abra/pkg/recipe"
	"github.com/stretchr/testify/assert"
)

func TestGetLatestVersionReturnsErrorWhenVersionsIsEmpty(t *testing.T) {
	recipe := recipePkg.Recipe{}
	catalogue := recipePkg.RecipeCatalogue{}
	_, err := getLatestVersion(recipe, catalogue)
	assert.Equal(t, err, errEmptyVersionsInCatalogue)
}

func TestGetLatestVersionReturnsLastVersion(t *testing.T) {
	recipe := recipePkg.Recipe{
		Name: "test",
	}
	versions := []map[string]map[string]recipePkg.ServiceMeta{
		make(map[string]map[string]recipePkg.ServiceMeta),
		make(map[string]map[string]recipePkg.ServiceMeta),
	}
	versions[0]["0.0.3"] = make(map[string]recipePkg.ServiceMeta)
	versions[1]["0.0.2"] = make(map[string]recipePkg.ServiceMeta)
	catalogue := make(recipePkg.RecipeCatalogue)
	catalogue["test"] = recipePkg.RecipeMeta{
		Versions: versions,
	}
	version, _ := getLatestVersion(recipe, catalogue)
	assert.Equal(t, version, "0.0.3")
}
