package nsdadapter_test

import (
	"testing"

	"git.zetit.ru/zuevav/Bridge-and-Join-s/internal/nsdadapter"
)

func TestRouteToPackageType(t *testing.T) {
	cases := []struct {
		kind nsdadapter.MessageKind
		want nsdadapter.PackageType
	}{
		{nsdadapter.KindTransferRequest, nsdadapter.PackageM2MTransferRequest},
		{nsdadapter.KindTransferDecision, nsdadapter.PackageM2MTransferDecision},
		{nsdadapter.KindTransferResponse, nsdadapter.PackageM2MTransferError},
	}
	for _, c := range cases {
		got, err := nsdadapter.RouteToPackageType(c.kind)
		if err != nil {
			t.Errorf("kind=%s ошибка: %v", c.kind, err)
			continue
		}
		if got != c.want {
			t.Errorf("kind=%s: получено %s, ожидалось %s", c.kind, got, c.want)
		}
	}
}

func TestRouteToPackageTypeUnknown(t *testing.T) {
	if _, err := nsdadapter.RouteToPackageType("unknown"); err == nil {
		t.Error("ожидалась ошибка на неизвестный kind")
	}
}

func TestIncomingPackageKindsContainsExpected(t *testing.T) {
	kinds := nsdadapter.IncomingPackageKinds()
	want := map[nsdadapter.PackageType]bool{
		nsdadapter.PackageM2MTransferDecision: true,
		nsdadapter.PackageSUB16:               true,
	}
	for _, k := range kinds {
		delete(want, k)
	}
	if len(want) > 0 {
		t.Errorf("в IncomingPackageKinds не хватает: %v", want)
	}
}

func TestLookupProfile(t *testing.T) {
	for _, name := range []string{"guest-gost", "test3-rsa", "prod-gost"} {
		p, err := nsdadapter.LookupProfile(name)
		if err != nil {
			t.Errorf("LookupProfile(%s): %v", name, err)
			continue
		}
		if p.Name != name {
			t.Errorf("LookupProfile(%s).Name = %s", name, p.Name)
		}
	}
	if _, err := nsdadapter.LookupProfile("unknown"); err == nil {
		t.Error("ожидалась ошибка на неизвестный профиль")
	}
}
