package main

import "testing"

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

	tests := []struct {
		name                 string
		option               Option
		wantNamespace        string
		wantLeaderElectionID string
	}{
		{
			name:          "defaults remain cluster wide",
			option:        Option{},
			wantNamespace: "",
		},
		{
			name: "watch one namespace",
			option: Option{
				WatchNamespace: "cp9-premium",
			},
			wantNamespace: "cp9-premium",
		},
		{
			name: "custom leader election identity",
			option: Option{
				EnableLeaderElection: true,
				LeaderElectionID:     "cp9-premium-agent-control-server",
			},
			wantLeaderElectionID: "cp9-premium-agent-control-server",
		},
		{
			name: "leader election keeps production identity by default",
			option: Option{
				EnableLeaderElection: true,
			},
			wantLeaderElectionID: defaultLeaderElectionID,
		},
		{
			name: "leader election identity is ignored when disabled",
			option: Option{
				LeaderElectionID: "must-not-be-used",
			},
		},
	}

	for _, tt := range tests {
		tt := tt
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()

			got := tt.option.managerOptions()
			if tt.wantNamespace == "" {
				if len(got.Cache.DefaultNamespaces) != 0 {
					t.Fatalf("expected cluster-wide cache, got namespaces %v", got.Cache.DefaultNamespaces)
				}
			} else if _, ok := got.Cache.DefaultNamespaces[tt.wantNamespace]; !ok {
				t.Fatalf("expected cache namespace %q, got %v", tt.wantNamespace, got.Cache.DefaultNamespaces)
			}
			if got.LeaderElectionID != tt.wantLeaderElectionID {
				t.Fatalf("expected leader election ID %q, got %q", tt.wantLeaderElectionID, got.LeaderElectionID)
			}
		})
	}
}
