package imgroup

import "testing"

func TestBroadcastGroupIDGenerateAndParse(t *testing.T) {
	global, err := GlobalBroadcastGroupID(" LaLu ")
	if err != nil {
		t.Fatalf("GlobalBroadcastGroupID failed: %v", err)
	}
	if global != "hy_lalu_bc_g" {
		t.Fatalf("global group mismatch: %s", global)
	}
	parsedGlobal := Parse(global)
	if parsedGlobal.Kind != KindGlobalBroadcast || parsedGlobal.AppCode != "lalu" || parsedGlobal.RegionID != 0 {
		t.Fatalf("global parse mismatch: %+v", parsedGlobal)
	}

	region, err := RegionBroadcastGroupID("lalu", 1001)
	if err != nil {
		t.Fatalf("RegionBroadcastGroupID failed: %v", err)
	}
	if region != "hy_lalu_bc_r_1001" {
		t.Fatalf("region group mismatch: %s", region)
	}
	parsedRegion := Parse(region)
	if parsedRegion.Kind != KindRegionBroadcast || parsedRegion.AppCode != "lalu" || parsedRegion.RegionID != 1001 {
		t.Fatalf("region parse mismatch: %+v", parsedRegion)
	}
}

func TestParseRoomAndRejectInvalidBroadcast(t *testing.T) {
	if parsed := Parse("room-1001"); parsed.Kind != KindRoom || parsed.RoomID != "room-1001" {
		t.Fatalf("room parse mismatch: %+v", parsed)
	}
	if parsed := Parse("hy_lalu_bc_r_0"); parsed.Kind != KindUnknown {
		t.Fatalf("zero region must be rejected: %+v", parsed)
	}
	if _, err := RegionBroadcastGroupID("lalu", 0); err == nil {
		t.Fatal("zero region must not generate a region broadcast group")
	}
	if _, err := GlobalBroadcastGroupID("bad app"); err == nil {
		t.Fatal("app_code with spaces must be rejected")
	}
}
