package packs

import (
	"reflect"
	"strings"
	"testing"

	cp "github.com/pingcap-inc/nexuscrm-contract/pkg/controlplane"
)

func TestExternalComponentReferencesPreserveManifestDependency(t *testing.T) {
	want := []cp.ComponentReference{{Kind: cp.ComponentKindApprovalProcess, BusinessKey: "Opportunity/review", Required: true}}
	got := externalComponentReferences([]ComponentReference{{Kind: ComponentKindApprovalProcess, BusinessKey: "Opportunity/review", Required: true}})
	if !reflect.DeepEqual(got, want) {
		t.Fatalf("external references = %#v, want %#v", got, want)
	}
}

func TestManagedBuildSinkFailsClosedUntilLifecycleWiring(t *testing.T) {
	build := &sourceBackedComponentBuildContext{}
	if err := build.AddManagedComponent(cp.ComponentBuildManagedComponent{}); err == nil || !strings.Contains(err.Error(), "expected projection is required") {
		t.Fatalf("invalid descriptor error = %v", err)
	}
	valid := cp.ComponentBuildManagedComponent{ExpectedProjection: []byte(`{}`)}
	if err := build.AddManagedComponent(valid); err == nil || !strings.Contains(err.Error(), "not wired") {
		t.Fatalf("unwired descriptor error = %v", err)
	}
}
