package compatibility

import (
	"strings"
	"testing"

	runtimev1 "github.com/pingcap-inc/nexuscrm-contract/wire-go/gen/runtime/v1"
	"google.golang.org/protobuf/reflect/protoreflect"
)

func TestPrivateListViewWireIsAppBlindAndOwnsExactlyTwoCoarseRPCs(t *testing.T) {
	service := (&runtimev1.ReadPrivateListViewsRequest{}).ProtoReflect().Descriptor().ParentFile().Services().ByName("PrivateListViewService")
	if service == nil || service.Methods().Len() != 2 ||
		service.Methods().Get(0).Name() != "ReadPrivateListViews" ||
		service.Methods().Get(1).Name() != "MutatePrivateListView" {
		t.Fatalf("service methods = %#v", service)
	}

	for _, message := range []protoreflect.MessageDescriptor{
		(&runtimev1.ReadPrivateListViewsRequest{}).ProtoReflect().Descriptor(),
		(&runtimev1.MutatePrivateListViewRequest{}).ProtoReflect().Descriptor(),
		(&runtimev1.PrivateListView{}).ProtoReflect().Descriptor(),
	} {
		for index := 0; index < message.Fields().Len(); index++ {
			name := strings.ToLower(string(message.Fields().Get(index).Name()))
			for _, forbidden := range []string{"org", "principal", "user", "owner", "visibility", "permission", "token", "app_installation", "authority"} {
				if strings.Contains(name, forbidden) {
					t.Fatalf("caller-selected authority field %q entered %s", name, message.FullName())
				}
			}
		}
	}
}
