package common

import (
	"testing"
	"reflect"
	"github.com/fiskerinc/cloud-services/pkg/logger"
)

func TestStringToTrexLogLevel(t *testing.T){
	level := UnmarshalLogLevelString("Warning")

	if level != Warn{
		t.Fail()
	}

	level = UnmarshalLogLevelString("ErRoR")

	if level != Error{
		t.Fail()
	}
}

func TestLogLevelToLogger(t *testing.T){
	log := Debug.ToLoggerLevel()
	if !compareLevel(log(), logger.Debug()){
		t.Error("Debug did not give debug logger")
	}

	log = Error.ToLoggerLevel()
	if !compareLevel(log(), logger.Error()){
		t.Error("Error did not give error logger")
	}

	log = Critical.ToLoggerLevel()
	if !compareLevel(log(), logger.Error()){
		t.Error("Critical did not give error logger")
	}
}

func compareLevel(our, theirs *logger.Event)(match bool){
	// If the log level is too low, such as debug, the event comes back as null
	// Surprisingly calling .Msg does not cause an error on it though
	if *our == *theirs{
		return true
	}
	ourVal := reflect.ValueOf(*our).FieldByName("level")
	theirVal := reflect.ValueOf(*theirs).FieldByName("level")
	return ourVal.Int() == theirVal.Int()
} 