package boundary_test

import (
	"path/filepath"
	"strings"
	"testing"
)

func TestComponentKindRegistrationCarrierReachesEverySourceConsumer(t *testing.T) {
	root := repoRoot(t)
	checks := map[string][]string{
		"backend/internal/setupcmd/setup.go": {
			"registrationSet := productHooks.ComponentKindRegistrationSet()",
			"runnerOptions := controlplane.RunnerOptions{ComponentKindRegistrationSet: registrationSet}",
			"runValidatePackWithComponentKindRegistrationSet",
			"controlplane.ApplyAllWithOptionsAndRunnerOptions",
			"controlplane.ApplyAllIfNeededWithOptionsAndRunnerOptions",
			"controlplane.ApplySourceConvergenceWithRunnerOptions",
			"controlplane.NewRunnerWithOptions",
			"controlplane.ReapplyPackFromSourceWithSchemaReadinessAndRunnerOptions",
			"controlplane.RunRepairWithLockAndRunnerOptions",
			"controlplane.RebuildDerivedWithLockAndRunnerOptions",
			"controlplane.VerifyDeepWithRunnerOptions",
			"controlplane.VerifyInstalledPackByNameWithRunnerOptions",
		},
		"backend/internal/setupcmd/metadata_program_check.go": {
			"controlplane.NewRunnerWithOptions",
			"packs.RegisterInstalledRuntimeMetadataWithComponentKindRegistrationSet",
			"ComponentKindRegistrationSet: registrationSet",
		},
		"backend/pkg/serverruntime/connector_push_runtime.go": {
			"componentKindRegistrationSet packs.ComponentKindRegistrationSet",
			"ComponentKindRegistrationSet: &r.componentKindRegistrationSet",
		},
	}
	for rel, required := range checks {
		source := readFile(t, filepath.Join(root, filepath.FromSlash(rel)))
		for _, fragment := range required {
			if !containsIgnoringWhitespace(source, fragment) {
				t.Fatalf("%s lost registration carrier %q", rel, fragment)
			}
		}
	}

	setupSource := readFile(t, filepath.Join(root, "backend", "internal", "setupcmd", "setup.go"))
	for _, legacy := range []string{
		"controlplane.ApplyAllWithOptions(ctx", "controlplane.ApplyAllIfNeededWithOptions(ctx",
		"controlplane.ApplySourceConvergence(ctx", "controlplane.NewRunner(db)",
		"controlplane.ReapplyPackFromSourceWithSchemaReadiness(ctx", "controlplane.RunRepairWithLock(ctx",
		"controlplane.RebuildDerivedWithLock(ctx", "controlplane.VerifyDeep(ctx",
		"controlplane.VerifyInstalledPackByName(ctx", "controlplane.VerifyInstalledPackByNameWithRunner(ctx",
		"packs.ValidateInstallablePackSource(*source)",
	} {
		if strings.Contains(setupSource, legacy) {
			t.Fatalf("backend/internal/setupcmd/setup.go still uses registration-empty source consumer %q", legacy)
		}
	}
}
