package bootstrapimpl

import (
	"testing"
	"unicode"
)

func TestSystemTablesIncludeApprovalKeyPrefixes(t *testing.T) {
	want := map[string]string{
		"_System_Object":              "!j0",
		"_System_Field":               "!k0",
		"_System_Table":               "!l0",
		"_System_ConnectedApp":        "!m0",
		"_System_ConnectedAppAccess":  "!n0",
		"_System_TriggerSubscription": "!o0",
		"ProcessDefinition":           "04a",
		"ProcessNode":                 "04b",
		"ProcessInstance":             "04g",
		"ProcessInstanceHistory":      "04h",
		"ProcessInstanceWorkitem":     "04i",
		"ProcessInstanceNode":         "0OO",
		"ProcessAction":               "!p0",
	}

	for _, def := range GetSystemTableDefinitions() {
		exp, ok := want[def.TableName]
		if !ok {
			continue
		}
		if def.KeyPrefix != exp {
			t.Fatalf("%s keyPrefix = %q, want %q", def.TableName, def.KeyPrefix, exp)
		}
		delete(want, def.TableName)
	}

	for tableName := range want {
		t.Fatalf("%s missing from system_tables.json", tableName)
	}
}

func TestSystemTablesInternalKeyPrefixesAvoidSalesforceNamespace(t *testing.T) {
	internalTables := map[string]struct{}{
		"_System_Object":              {},
		"_System_Field":               {},
		"_System_Table":               {},
		"_System_File":                {},
		"_System_RecordShare":         {},
		"_System_ConnectedApp":        {},
		"_System_ConnectedAppAccess":  {},
		"_System_TriggerSubscription": {},
		"ProcessAction":               {},
	}
	for _, def := range GetSystemTableDefinitions() {
		if _, ok := internalTables[def.TableName]; !ok {
			continue
		}
		if len(def.KeyPrefix) != 3 || unicode.IsLetter(rune(def.KeyPrefix[0])) || unicode.IsDigit(rune(def.KeyPrefix[0])) {
			t.Fatalf("%s keyPrefix = %q, want internal non-Salesforce namespace prefix", def.TableName, def.KeyPrefix)
		}
	}
}
