package packs

import (
	"strings"
	"testing"
)

// TestParsePackSourceRefAcceptsLegacyInRepoAlias pins the parser's deprecated
// alias behavior: rows on existing _System_InstalledPack databases that were
// written by the pre-externalization binary carry SourceKind="in_repo". The
// new parser must normalize this to official_repo so the new binary can boot
// against the old database without a substrate-level data migration.
func TestParsePackSourceRefAcceptsLegacyInRepoAlias(t *testing.T) {
	ref, err := ParsePackSourceRef("in_repo:salesforce-crm-standard")
	if err != nil {
		t.Fatalf("ParsePackSourceRef(in_repo:...) error = %v, want nil", err)
	}
	if ref.Kind != PackSourceKindOfficialRepo {
		t.Fatalf("ref.Kind = %q, want %q", ref.Kind, PackSourceKindOfficialRepo)
	}
	if ref.URI != "salesforce-crm-standard" {
		t.Fatalf("ref.URI = %q, want %q", ref.URI, "salesforce-crm-standard")
	}
}

// TestParsePackSourceRefRejectsOtherLegacyKinds confirms the alias is narrow:
// only the specific "in_repo" legacy literal is normalized. Any other
// unknown kind (typos, future labels) must still fail closed.
func TestParsePackSourceRefRejectsOtherLegacyKinds(t *testing.T) {
	_, err := ParsePackSourceRef("internal_repo:salesforce-crm-standard")
	if err == nil {
		t.Fatal("ParsePackSourceRef(internal_repo:...) error = nil, want error")
	}
	if !strings.Contains(err.Error(), "unsupported pack source kind") {
		t.Fatalf("error = %q, want unsupported-kind diagnostic", err)
	}
}
