// Copyright 2021 Harran Ali <harran.m@gmail.com>. All rights reserved.
// Copyright (c) 2024 Zeni Kim <zenik@smarteching.com>
// Use of this source code is governed by MIT-style
// license that can be found in the LICENSE file.

package core

import (
	"fmt"
	"reflect"
	"testing"
)

func TestNewHooks(t *testing.T) {
	mw := NewHooks()
	if fmt.Sprintf("%T", mw) != "*core.Hooks" {
		t.Errorf("failed testing new hook")
	}
}

func TestResloveHooks(t *testing.T) {
	NewHooks()
	mw := ResolveHooks()
	if fmt.Sprintf("%T", mw) != "*core.Hooks" {
		t.Errorf("failed resolve hooks")
	}
}

func TestAttach(t *testing.T) {
	mw := NewHooks()
	tmw := Hook(func(c *Context) {
		c.GetLogger().Info("Testing!")
	})
	mw.Attach(tmw)
	mws := mw.getByIndex(0)
	if reflect.ValueOf(tmw).Pointer() != reflect.ValueOf(mws).Pointer() {
		t.Errorf("Failed testing attach hook")
	}
}

func TestGetHooks(t *testing.T) {
	mw := NewHooks()
	t1 := Hook(func(c *Context) {
		c.GetLogger().Info("testing1!")
	})
	t2 := Hook(func(c *Context) {
		c.GetLogger().Info("testing2!")
	})
	mw.Attach(t1)
	mw.Attach(t2)
	if len(mw.GetHooks()) != 2 {
		t.Errorf("failed testing get hooks")
	}
}

func TestHookGetByIndex(t *testing.T) {
	mw := NewHooks()
	t1 := Hook(func(c *Context) {
		c.GetLogger().Info("testing!")
	})
	mw.Attach(t1)
	if reflect.ValueOf(mw.getByIndex(0)).Pointer() != reflect.ValueOf(t1).Pointer() {
		t.Errorf("failed testing get by index")
	}
}