package consts

import (
	"context"
	"testing"

	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

	cloudv1 "github.com/tidbcloud/infra-api/api/v1"
)

func TestGetAuditLogSidecarGracefulStopWaitSeconds(t *testing.T) {
	tests := []struct {
		name        string
		cluster     *cloudv1.Cluster
		gracePeriod int
		want        int
	}{
		{
			name:        "Default",
			cluster:     &cloudv1.Cluster{},
			gracePeriod: 0,
			want:        DefaultPodTerminationGracePeriodSeconds - maxPushLogsSeconds,
		},
		{
			name:        "Custom termination grace period",
			cluster:     &cloudv1.Cluster{},
			gracePeriod: 40,
			want:        40 - maxPushLogsSeconds,
		},
		{
			name: "Override annotation valid",
			cluster: &cloudv1.Cluster{
				ObjectMeta: v1.ObjectMeta{
					Annotations: map[string]string{
						AnnoKeyOverrideAuditLogSidecarGracefulStopWaitSeconds: "25",
					},
				},
			},
			gracePeriod: 0,
			want:        25,
		},
		{
			name: "Override annotation invalid",
			cluster: &cloudv1.Cluster{
				ObjectMeta: v1.ObjectMeta{
					Annotations: map[string]string{
						AnnoKeyOverrideAuditLogSidecarGracefulStopWaitSeconds: "invalid",
					},
				},
			},
			gracePeriod: 20,
			want:        20 - maxPushLogsSeconds,
		},
		{
			name: "Override annotation empty",
			cluster: &cloudv1.Cluster{
				ObjectMeta: v1.ObjectMeta{
					Annotations: map[string]string{
						AnnoKeyOverrideAuditLogSidecarGracefulStopWaitSeconds: "",
					},
				},
			},
			gracePeriod: 10,
			want:        10 - maxPushLogsSeconds,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := GetAuditLogSidecarGracefulStopWaitSeconds(context.Background(), tt.cluster, tt.gracePeriod)
			if got != tt.want {
				t.Errorf("%s: got %d, want %d", tt.name, got, tt.want)
			}
		})
	}
}
