package auth

import "testing"

func TestModuleRegistryIsConsistent(t *testing.T) {
	seen := make(map[string]bool, len(ModuleRegistry))
	for _, m := range ModuleRegistry {
		if m.Key == "" {
			t.Errorf("registry has an entry with empty key (label %q)", m.Label)
		}
		if m.Label == "" {
			t.Errorf("module %q has an empty label", m.Key)
		}
		if seen[m.Key] {
			t.Errorf("duplicate module key %q in ModuleRegistry", m.Key)
		}
		seen[m.Key] = true
	}
}

func TestModuleForKey(t *testing.T) {
	cases := map[string]string{
		"dul.create":           "dul",
		"role.permission.read": "role",
		"file_manager.file.read": "file_manager",
		"audit.read":           "audit",
		"filesystem":           "filesystem",
	}
	for in, want := range cases {
		if got := ModuleForKey(in); got != want {
			t.Errorf("ModuleForKey(%q) = %q, want %q", in, got, want)
		}
	}
}

func TestModuleLabelAndRegistration(t *testing.T) {
	if got := ModuleLabel(ModuleDUL); got != "DUL" {
		t.Errorf("ModuleLabel(%q) = %q, want %q", ModuleDUL, got, "DUL")
	}
	if got := ModuleLabel("unknown_module"); got != "unknown_module" {
		t.Errorf("ModuleLabel fallback = %q, want the key itself", got)
	}
	if !IsRegisteredModule(ModuleComplaint) {
		t.Errorf("expected %q to be a registered module", ModuleComplaint)
	}
	if IsRegisteredModule("unknown_module") {
		t.Errorf("did not expect %q to be registered", "unknown_module")
	}
}
