package formula

import "testing"

// IsSessionFormulaGlobalRoot is the SSoT helper that downstream engines
// (validation rules, flow formulas, approval criteria, …) consume to test
// whether a relation root identifier is a session-bound formula merge-field
// global ($User / $Profile / $UserRole / $Permission). The test pins the
// accepted name forms (bare, $-prefixed, __-prefixed) and the rejected forms
// so consumers cannot drift back into per-engine name lists.
func TestIsSessionFormulaGlobalRoot_AcceptsAllSessionGlobalsInAllForms(t *testing.T) {
	cases := []string{
		"User", "Profile", "UserRole", "Permission",
		"$User", "$Profile", "$UserRole", "$Permission",
		"__User", "__Profile", "__UserRole", "__Permission",
		" User", "User ", "  __Profile  ",
	}
	for _, name := range cases {
		if !IsSessionFormulaGlobalRoot(name) {
			t.Errorf("IsSessionFormulaGlobalRoot(%q) = false; want true", name)
		}
	}
}

func TestIsSessionFormulaGlobalRoot_RejectsNonGlobalNames(t *testing.T) {
	cases := []string{
		"", "Account", "Contract", "Order", "record", "prior", "env", "var",
		"users", "profile", "userrole", "permission", // case-sensitive
		"UserSession", "ProfilePicture", "UserRoleAssignment",
		"$Record", "__Record",
	}
	for _, name := range cases {
		if IsSessionFormulaGlobalRoot(name) {
			t.Errorf("IsSessionFormulaGlobalRoot(%q) = true; want false", name)
		}
	}
}

// The helper's identifier set must stay in lockstep with the formula context
// keys produced by BuildFormulaUserContext. If a future change adds (or
// removes) a session-bound global, this test forces the helper to be updated
// at the same time — keeping the validation-engine read path consistent with
// the construction read path.
func TestIsSessionFormulaGlobalRoot_MatchesBuildFormulaUserContextKeys(t *testing.T) {
	ctx := BuildFormulaUserContext(newUserSession())
	for key := range ctx {
		if !IsSessionFormulaGlobalRoot(key) {
			t.Errorf("BuildFormulaUserContext key %q not recognised as session global", key)
		}
	}
}
