package controlplane

import (
	"os"
	"strings"
	"testing"
)

func TestSourcePackOpsPreflightHelpersStayOutOfRoot(t *testing.T) {
	rootSource := readSourcePackOpsPreflightSourceForStructureTest(t, "source_pack_ops.go")
	for _, forbidden := range []string{
		"func validateInstallPackAttributionPreflight(",
		"func validateInstallPackChildRelationshipNamespacePreflight(",
		"func validateInstallPackOwnerPackSurfaces(",
		"func validateRequestedPackBusinessKeyOwnership(",
		"func validateRequestedConnectedAppOwnership(",
		"func validateLivePackDependencies(",
	} {
		if strings.Contains(rootSource, forbidden) {
			t.Fatalf("source_pack_ops.go still owns install preflight surface %q", forbidden)
		}
	}

	preflightSource := readSourcePackOpsPreflightSourceForStructureTest(t, "source_pack_ops_preflight.go")
	for _, want := range []string{
		"func validateInstallPackAttributionPreflight(",
		"func validateInstallPackChildRelationshipNamespacePreflight(",
		"func validateInstallPackOwnerPackSurfaces(",
		"func validateRequestedPackBusinessKeyOwnership(",
		"func validateRequestedConnectedAppOwnership(",
		"func validateLivePackDependencies(",
	} {
		if !strings.Contains(preflightSource, want) {
			t.Fatalf("source_pack_ops_preflight.go missing install preflight surface %q", want)
		}
	}
}

func readSourcePackOpsPreflightSourceForStructureTest(t *testing.T, path string) string {
	t.Helper()
	raw, err := os.ReadFile(path)
	if err != nil {
		t.Fatalf("read %s: %v", path, err)
	}
	return string(raw)
}
