package utils_test

import (
	"spaceofcheng/services/utils"
	"strings"
	"testing"
)

func TestStrSliceIndexOf(t *testing.T) {
	cases := []map[string]interface{}{
		map[string]interface{}{
			"case":     []string{"a", "b", "c"},
			"expected": int64(1),
		},
		map[string]interface{}{
			"case":     []string{"aa", "bb", "cc"},
			"expected": int64(-1),
		},
	}

	for _, cs := range cases {
		td := cs["case"].([]string)
		ex := cs["expected"].(int64)

		res := utils.StrSliceIndexOf(td, "b")

		if ex != res {
			t.Fatalf("TestStrSliceIndexOf case fail : %v", res)
		}
	}
}

func TestGUIID2IntString(t *testing.T) {
	cases := []map[string]string{
		map[string]string{
			"case":     "3b2da085-8881-4029-9a3d-6d33caf870d0",
			"expected": "553-217-207-305-861",
		},
	}

	for _, cs := range cases {
		res := utils.GUIID2IntString(cs["case"])
		if strings.Join(res, "-") != cs["expected"] {
			t.Fatalf("TestGUIID2IntString fail")
		}
	}

}

func TestEncodeBase64TrimTail(t *testing.T) {
	cases := []map[string]string{
		map[string]string{
			"case":     "1",
			"expected": "MQ",
		},
		map[string]string{
			"case":     "111",
			"expected": "MTEx",
		},
	}

	for _, cs := range cases {
		res := utils.EncodeBase64TrimTail(cs["case"])
		if res != cs["expected"] {
			t.Fatalf("TestEncodeBase64TrimTail fail")
		}
	}
}

func TestDecodeBase64NoTail(t *testing.T) {
	cases := []map[string]string{
		map[string]string{
			"case":     "MQ",
			"expected": "1",
		},
		map[string]string{
			"case":     "MTEx",
			"expected": "111",
		},
	}

	for _, cs := range cases {
		res := utils.DecodeBase64NoTail(cs["case"])
		if res != cs["expected"] {
			t.Fatalf("TestDecodeBase64NoTail fail : %s", cs["case"])
		}
	}
}
