package routecatalog

import "testing"

func TestLookupMatchesRouteTemplatesAndPrefixes(t *testing.T) {
	descriptor, ok := Lookup("POST", "/admin/payment/orders/alipay/order-1/refund")
	if !ok || !descriptor.Audit || descriptor.Path != "/payment/orders/:provider/:tradeNo/refund" {
		t.Fatalf("descriptor = %#v, ok=%v", descriptor, ok)
	}
	if descriptor, ok = Lookup("GET", "/admin/payment/orders/alipay/order-1"); !ok || descriptor.Audit || descriptor.Public {
		t.Fatalf("private query descriptor = %#v, ok=%v", descriptor, ok)
	}
}

func TestRoutePoliciesShareOneDescriptor(t *testing.T) {
	callback := Describe("POST", "/payment/callback/:provider")
	if !callback.Public || callback.Audit || callback.BodyPolicy != BodyPolicyPaymentCallback {
		t.Fatalf("callback descriptor = %#v", callback)
	}
	if policy := BodyPolicyFor("PUT", "/admin/integration/configs/payment/saobei"); policy != BodyPolicyPaymentConfig {
		t.Fatalf("payment policy = %q", policy)
	}
	if policy := BodyPolicyFor("PUT", "/admin/integration/configs/mq/emqx"); policy != BodyPolicyDefault {
		t.Fatalf("mq policy = %q", policy)
	}
	if descriptor := Describe("POST", "/system/reloadSystem"); !descriptor.Audit || descriptor.Group != "系统服务" {
		t.Fatalf("reload descriptor = %#v", descriptor)
	}
}

func TestEveryRouteHasAPIMetadata(t *testing.T) {
	for _, descriptor := range Descriptors() {
		if descriptor.Group == "" || descriptor.Description == "" {
			t.Errorf("route metadata is incomplete: %#v", descriptor)
		}
	}
}
