package common

import (
	"testing"
	"time"

	"github.com/stretchr/testify/require"
	"google.golang.org/protobuf/types/known/timestamppb"

	clustercontrollerv1 "github.com/tidbcloud/infra-provider/api/tidbcloud/cluster-server/v1"
	"github.com/tidbcloud/infra-provider/regional-server/api/v1alpha1"
)

func TestApplyUpgradeNodesProgress(t *testing.T) {
	now := time.Date(2026, 6, 12, 10, 0, 0, 0, time.UTC)
	uo := &v1alpha1.UpgradeOperation{
		Status: v1alpha1.UpgradeOperationStatus{
			CurrentStep: &v1alpha1.Step{},
		},
	}
	resp := &clustercontrollerv1.UpgradeNodesResponse{
		Message:          "waiting",
		LastProgressTime: timestamppb.New(now.Add(-2 * time.Minute)),
		Phases: []*clustercontrollerv1.StepPhase{
			{
				Name:             "restart-storage",
				BeginTime:        timestamppb.New(now.Add(-10 * time.Minute)),
				ProgressInfo:     "restart-storage=3/5",
				Done:             3,
				Total:            5,
				MaxDone:          3,
				LastProgressTime: timestamppb.New(now.Add(-2 * time.Minute)),
			},
		},
	}

	ApplyUpgradeNodesProgress(uo, resp)
	require.Len(t, uo.Status.CurrentStep.Phases, 1)
	require.Equal(t, "restart-storage", uo.Status.CurrentStep.Phases[0].Name)
	require.Equal(t, "restart-storage=3/5", uo.Status.CurrentStep.Phases[0].ProgressInfo)
	require.NotNil(t, uo.Status.CurrentStep.LastProgressTime)
}
