package packs

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

func TestPackSourceLoadingStaysOutOfValidationRoot(t *testing.T) {
	rootSource := readPackSourceLoadSourceForStructureTest(t, "pack_source.go")
	for _, forbidden := range []string{
		"func ParsePackSourceRef(",
		"func ResolvePackSourceWithOptions(",
		"func resolveStoredPackSourceWithOptions(",
		"func resolveLocalPathSource(",
		"func parsePackManifest(",
		"func loadManifestPayloadFiles(",
		"func gitShowFileAtRevision(",
	} {
		if strings.Contains(rootSource, forbidden) {
			t.Fatalf("pack_source.go still owns source loading surface %q", forbidden)
		}
	}

	loadSource := readPackSourceLoadSourceForStructureTest(t, "pack_source_load.go")
	for _, want := range []string{
		"func ParsePackSourceRef(",
		"func ResolvePackSourceWithOptions(",
		"func resolveStoredPackSourceWithOptions(",
		"func resolveLocalPathSource(",
		"func parsePackManifest(",
		"func loadManifestPayloadFiles(",
		"func gitShowFileAtRevision(",
	} {
		if !strings.Contains(loadSource, want) {
			t.Fatalf("pack_source_load.go missing source loading surface %q", want)
		}
	}
}

func readPackSourceLoadSourceForStructureTest(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)
}
