package schedule

import (
	"context"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/tidbcloud/metering/pkg/common/constants"
)

func TestClusterMetaTask(t *testing.T) {
	got := ClusterMetaTask()
	assert.Equal(t, testStartTime, got.start)
	assert.Equal(t, constants.ClusterMetaTaskName, got.Name)
}

func TestLastMinuteTimestamp(t *testing.T) {
	type args struct {
		timestamp int64
	}
	tests := []struct {
		name string
		args args
		want int64
	}{
		{name: "left", args: args{timestamp: 1725519601}, want: 1725519600},
		{name: "current", args: args{timestamp: 1725519600}, want: 1725519600},
		{name: "right", args: args{timestamp: 1725519659}, want: 1725519600},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := LastMinuteTimestamp(tt.args.timestamp); got != tt.want {
				t.Errorf("LastMinuteTimestamp() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestNextExecuteTimestamp(t *testing.T) {
	n := LastMinuteTimestamp(time.Now().Unix())
	type args struct {
		ctx        context.Context
		checkpoint int64
		start      int64
	}
	tests := []struct {
		name string
		args args
		want int64
	}{
		{name: "empty", args: args{checkpoint: 0, start: 0}, want: n},
		{name: "start", args: args{checkpoint: 0, start: 1725519600}, want: 1725519600},
		{name: "checkpoint", args: args{checkpoint: 1725519600, start: 1725519601}, want: 1725519660},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := NextExecuteTimestamp(tt.args.ctx, tt.args.checkpoint, tt.args.start); got != tt.want {
				t.Errorf("NextExecuteTimestamp() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestReady(t *testing.T) {
	n := time.Now().Unix()
	type args struct {
		ctx       context.Context
		timestamp int64
	}
	tests := []struct {
		name string
		args args
		want bool
	}{
		{name: "left", args: args{timestamp: n - 1}, want: true},
		{name: "right", args: args{timestamp: n + 1}, want: false},
		{name: "current", args: args{timestamp: n}, want: false},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := Ready(tt.args.ctx, tt.args.timestamp); got != tt.want {
				t.Errorf("Ready() = %v, want %v", got, tt.want)
			}
		})
	}
}
