package runner

import (
	"errors"
	"testing"
	"time"

	"github.com/easyai/easyai-ai-gateway/apps/api/internal/store"
)

func TestLeaseRenewalFatalityDistinguishesTransientDatabaseErrors(t *testing.T) {
	now := time.Now()
	deadline := now.Add(time.Minute)
	transient := errors.New("lock timeout")
	if taskExecutionLeaseRenewalIsFatal(transient, now, deadline) {
		t.Fatal("transient task lease renewal error must be retried before expiry")
	}
	if concurrencyLeaseRenewalIsFatal(transient, now, deadline) {
		t.Fatal("transient concurrency lease renewal error must be retried before expiry")
	}
	if !taskExecutionLeaseRenewalIsFatal(store.ErrTaskExecutionLeaseLost, now, deadline) {
		t.Fatal("confirmed task lease ownership loss must be fatal")
	}
	if !concurrencyLeaseRenewalIsFatal(store.ErrConcurrencyLeaseLost, now, deadline) {
		t.Fatal("confirmed concurrency lease ownership loss must be fatal")
	}
	if !taskExecutionLeaseRenewalIsFatal(transient, deadline, deadline) {
		t.Fatal("task lease renewal error at expiry must be fatal")
	}
	if !concurrencyLeaseRenewalIsFatal(transient, deadline, deadline) {
		t.Fatal("concurrency lease renewal error at expiry must be fatal")
	}
}
