package runtimeworker

import (
	"database/sql"
	"strings"
	"testing"

	"github.com/nexuscrm/automation"
	recordmutation "github.com/nexuscrm/backend/internal/runtimeassembly/recordmutation"
	"github.com/nexuscrm/records"
	commandsemantic "github.com/pingcap-inc/nexuscrm-contract/pkg/runtime/commandv1"
)

func TestProductionPlatformCommandRegistryConstructsOnlyTheSelectedCapability(t *testing.T) {
	graph := &recordmutation.RecordsMutationRuntime{
		Flows:       &automation.FlowExecutor{},
		Persistence: &records.PersistenceService{},
	}
	registry, err := newProductionPlatformCommandExecutorRegistry(
		t.Context(), &sql.DB{}, graph, commandsemantic.CapabilityName,
	)
	if err != nil {
		t.Fatal(err)
	}
	if _, ok := registry.resolve(RuntimeFlowCommandKind, RuntimeFlowCommandPayloadFormatVersion); !ok {
		t.Fatal("selected neutral command executor is absent")
	}
}

func TestProductionPlatformCommandRegistryRejectsUnsupportedCapabilityBeforeConstruction(t *testing.T) {
	_, err := newProductionPlatformCommandExecutorRegistry(
		t.Context(), &sql.DB{}, &recordmutation.RecordsMutationRuntime{}, "runtime.unsupported.v1",
	)
	if err == nil || !strings.Contains(err.Error(), "unsupported capability") {
		t.Fatalf("error = %v", err)
	}
}
