package schedulers

import (
	"context"
	"testing"
	"time"

	commM "github.com/pingcap/test-infra/common/model"
	"github.com/pingcap/test-infra/services/tps/pkg/common"
	"github.com/stretchr/testify/assert"
)

func newWaitJob(timeout time.Duration) *common.ArgoJob {
	return &common.ArgoJob{
		PlanExec: &commM.TestPlanExecution{CreatedAt: 0},
		Plan:     &commM.TestPlan{WaitTimeout: int64(timeout.Seconds())},
	}
}

func TestTimeoutPrefilter_timeout(t *testing.T) {
	timeout := 12 * time.Hour
	s := &WaitTimeout{now: func() time.Time {
		return time.Unix(0, 0).Add(timeout + time.Second)
	}}
	job := newWaitJob(timeout)
	err := s.Prefilter(context.TODO(), job)
	assert.Error(t, err)
}

func TestTimeoutPrefilter_no_timeout(t *testing.T) {
	timeout := 12 * time.Hour
	s := &WaitTimeout{now: func() time.Time {
		return time.Unix(0, 0).Add(timeout - time.Second)
	}}
	job := newWaitJob(timeout)
	err := s.Prefilter(context.TODO(), job)
	assert.NoError(t, err)
}
