package configs

import (
	"celer/pkgs/dirs"
	"os"
	"path/filepath"
	"testing"
)

func TestConfigure_Platform(t *testing.T) {
	// Check error.
	var check = func(err error) {
		t.Helper()
		if err != nil {
			t.Fatal(err)
		}
	}

	t.Cleanup(func() {
		check(os.RemoveAll(filepath.Join(dirs.WorkspaceDir, "celer.toml")))
		check(os.RemoveAll(dirs.TmpDir))
		check(os.RemoveAll(dirs.TestCacheDir))
	})

	// Init celer.
	celer := NewCeler()
	check(celer.Init())
	check(celer.SetConfRepo("https://github.com/celer-pkg/test-conf.git", ""))

	t.Run("configure platform success", func(t *testing.T) {
		const newName = "x86_64-linux-ubuntu-22.04"
		check(celer.SetPlatform(newName))
		if celer.platform.Name != newName {
			t.Fatalf("platform should be `%s`", newName)
		}
	})

	t.Run("configure platform error: none exist platform", func(t *testing.T) {
		if err := celer.SetPlatform("xxxx"); err == nil {
			t.Fatal("it should be failed")
		}
	})

	t.Run("configure platform error: empty platform", func(t *testing.T) {
		if err := celer.SetPlatform(""); err != nil {
			if err.Error() != "platform name is empty" {
				t.Fatal("error should be 'platform name is empty'")
			}
		} else {
			t.Fatal("it should be failed")
		}
	})
}
