package groups

import (
	"reflect"
	"testing"
)

func TestParseGroup(t *testing.T) {
	data := []byte(`root:x:0:
# comment

wheel:x:10:alice,bob
developers:x:1500:alice
empty:x:1600:
broken:x:notanumber:x
short:x:5
`)
	got := parseGroup(data)
	if len(got) != 4 {
		t.Fatalf("expected 4 valid groups, got %d: %+v", len(got), got)
	}

	wheel := got[1]
	if wheel.Name != "wheel" || wheel.GID != 10 || !wheel.System ||
		!reflect.DeepEqual(wheel.Members, []string{"alice", "bob"}) {
		t.Errorf("wheel parsed wrong: %+v", wheel)
	}

	dev := got[2]
	if dev.GID != 1500 || dev.System {
		t.Errorf("developers should be a non-system group: %+v", dev)
	}

	empty := got[3]
	if len(empty.Members) != 0 {
		t.Errorf("empty group should have no members, got %v", empty.Members)
	}
}

func TestValidateGroupName(t *testing.T) {
	for _, n := range []string{"wheel", "_svc", "dev-team", "g1"} {
		if err := validateGroupName(n); err != nil {
			t.Errorf("validateGroupName(%q) = %v, want nil", n, err)
		}
	}
	for _, n := range []string{"", "-x", "Wheel", "a,b", "foo;rm", "1grp"} {
		if err := validateGroupName(n); err == nil {
			t.Errorf("validateGroupName(%q) = nil, want error", n)
		}
	}
}
