package packs

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

func TestPackSourceValidationHelpersStayOutOfPackSourceGodFile(t *testing.T) {
	packSourceSrc := readPacksSourceForStructure(t, "pack_source.go")
	helperSrc := readPacksSourceForStructure(t, "pack_source_validation_helpers.go")
	strictJSONSrc := readPacksSourceForStructure(t, "../strictjson/strictjson.go")

	for _, symbol := range []string{
		"var semVerRe",
		"var versionConstraintTermRe",
		"func isSupportedVersionConstraint(",
		"func VersionSatisfiesConstraint(",
		"func rejectDuplicateJSONKeys(",
		"func ensureJSONEOF(",
	} {
		if strings.Contains(packSourceSrc, symbol) {
			t.Fatalf("pack_source.go still owns validation helper symbol %q", symbol)
		}
		if !strings.Contains(helperSrc, symbol) {
			t.Fatalf("pack_source_validation_helpers.go is missing validation helper symbol %q", symbol)
		}
	}
	if strings.Contains(helperSrc, "func scanValue(") {
		t.Fatal("pack_source_validation_helpers.go regained the strict JSON token walker")
	}
	for _, symbol := range []string{"func RejectDuplicateKeys(", "func scanValue("} {
		if !strings.Contains(strictJSONSrc, symbol) {
			t.Fatalf("strictjson/strictjson.go is missing validation owner symbol %q", symbol)
		}
	}
}

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