package client

import (
	"errors"
	"testing"

	gerr "galaxy/error"
	"github.com/stretchr/testify/require"
)

func TestHandlerErrorDispatchesByClass(t *testing.T) {
	t.Parallel()

	tests := []struct {
		name      string
		err       error
		wantEvent string
	}{
		{name: "connection", err: gerr.WrapConnection(errors.New("dial")), wantEvent: "connection"},
		{name: "storage", err: gerr.WrapStorage(errors.New("write file")), wantEvent: "storage"},
		{name: "service", err: gerr.WrapService(errors.New("bad response")), wantEvent: "service"},
		{name: "unclassified defaults to service", err: errors.New("plain"), wantEvent: "service"},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			var got string
			c := &client{
				onConnectionErrFn: func(error) { got = "connection" },
				onStorageErrFn:    func(error) { got = "storage" },
				onServiceErrFn:    func(error) { got = "service" },
			}

			c.handlerError(tt.err)

			require.Equal(t, tt.wantEvent, got)
		})
	}
}
