noxarion-x/enforcer/sentinel_test.go
Rene Nochebuena 06c778db51
All checks were successful
Go CI/CD / go-ci (push) Successful in 1m47s
Add initial enforcer module with structured error handling and CI
Introduce 'enforcer' package with `Sentinel` for rich error context and `ApplicationError` for custom error codes.
Include tests, documentation updates, CI workflows, and foundational files like `GO.mod` for project setup.
2025-04-27 00:07:04 -06:00

90 lines
2.0 KiB
Go

package enforcer_test
import (
stderrors "errors"
"testing"
"gitstormr.dev/code-raider/noxarion-x/enforcer"
)
func TestDoomBringer_WithMeta(t *testing.T) {
appErr := enforcer.ApplicationError(400_00_01_01)
if appErr.GetCode() != 400_00_01_01 {
t.Errorf("expected code to be 400_00_01_01")
}
originalError := stderrors.New("tribe is deleted")
doomBringer := enforcer.NewSentinel(appErr, nil, "Stormborn Guild")
_ = doomBringer.WithMeta("key1", "value1").
WithMeta("key2", 42)
err := doomBringer.Error()
if err == "" {
t.Errorf("expected error")
}
doomBringer.OriginalError = originalError
err = doomBringer.Error()
if err == "" {
t.Errorf("expected error")
}
unwrap := doomBringer.Unwrap()
if unwrap == nil {
t.Errorf("expected unwrap")
}
if key1, ok := doomBringer.Metadata["key1"]; !ok {
t.Errorf("expected key1 to be present")
} else {
if key1 != "value1" {
t.Errorf("expected key1 to be value1")
}
}
if key2, ok := doomBringer.Metadata["key2"]; !ok {
t.Errorf("expected key2 to be present")
} else {
if key2 != 42 {
t.Errorf("expected key2 to be 42")
}
}
t.Logf("%#v", doomBringer.Error())
}
func TestNewSentinel(t *testing.T) {
appErr := enforcer.ApplicationError(400_00_01_01)
originalError := stderrors.New("tribe is deleted")
args := []interface{}{"Tribe123"}
sentinel := enforcer.NewSentinel(appErr, originalError, args...)
if appErr.GetHTTPStatus() != sentinel.HTTPStatus {
t.Errorf("expected HTTPStatus to be %d", appErr.GetHTTPStatus())
}
if sentinel.OriginalError.Error() != originalError.Error() {
t.Errorf("expected OriginalError to be %s", originalError.Error())
}
if sentinel.Code != appErr {
t.Errorf("expected Code to be %v", appErr)
}
}
func TestIdentifyInvoker(t *testing.T) {
appErr := enforcer.ApplicationError(400_00_01_01)
originalError := stderrors.New("sorcerer is deleted")
sentinel := enforcer.NewSentinel(
appErr, originalError, "Merlin the Mystic",
)
if sentinel.FunctionRef == "" {
t.Errorf("expected summoner to be present")
}
}