package jobs

import (
	"context"
	"errors"
	"testing"

	"gorm.io/gorm"
	"qycw/go-server/internal/bootstrap"
	licenseclient "qycw/go-server/internal/license"
)

func TestWorkerLicenseGateFailsClosedWithoutChecker(t *testing.T) {
	err := (workerLicenseGate{}).check()
	if !errors.Is(err, ErrCommercialLicenseRequired) || !errors.Is(err, licenseclient.ErrLicenseMissing) {
		t.Fatalf("error = %v", err)
	}
}

func TestWorkerLicenseGatePreservesEvaluatorError(t *testing.T) {
	eval := licenseclient.NewEvaluator(nil)
	eval.SetUnavailable()
	err := (workerLicenseGate{eval: eval}).check()
	if !errors.Is(err, ErrCommercialLicenseRequired) || !errors.Is(err, licenseclient.ErrLicenseUnavailable) {
		t.Fatalf("error = %v", err)
	}
}

func TestTaskExecutorRejectsMissingLicenseBeforeExecutingTask(t *testing.T) {
	executor := NewTaskExecutor(&bootstrap.App{DB: &gorm.DB{}}, nil, nil)
	_, err := executor.Execute(context.Background(), "settlement")
	if !errors.Is(err, ErrCommercialLicenseRequired) || !errors.Is(err, licenseclient.ErrLicenseMissing) {
		t.Fatalf("error = %v", err)
	}
}
