package graph import ( "testing" "time" "kubeviz/internal/model" ) func TestBuildGraphIncludesServiceSelectorEdge(t *testing.T) { ds := &model.Dataset{ Resources: map[string]*model.Resource{ "demo/Service/web": { ID: "demo/Service/web", Kind: "Service", Name: "web", Namespace: "demo", Raw: map[string]any{ "spec": map[string]any{ "ports": []any{ map[string]any{"port": 80, "targetPort": 8080, "protocol": "TCP"}, }, }, }, WorkloadMeta: &model.WorkloadMetadata{ ServiceSelectors: map[string]string{"app": "web"}, }, CreatedAt: time.Now(), }, "demo/Deployment/web": { ID: "demo/Deployment/web", Kind: "Deployment", Name: "web", Namespace: "demo", WorkloadMeta: &model.WorkloadMetadata{ PodTemplateLabels: map[string]string{"app": "web"}, }, CreatedAt: time.Now(), }, }, } resp := BuildGraph(ds, Filters{}, map[string]string{}) if resp.Stats.TotalNodes != 2 { t.Fatalf("expected 2 nodes, got %d", resp.Stats.TotalNodes) } found := false for _, edge := range resp.Edges { if edge.Source == "demo/Service/web" && edge.Target == "demo/Deployment/web" && edge.RelationType == "selects" { if edge.Label == "" { t.Fatalf("expected selects edge label with port/protocol") } found = true } } if !found { t.Fatalf("expected service selector edge not found") } } func TestBuildGraphGroupingByNamespace(t *testing.T) { ds := &model.Dataset{ Resources: map[string]*model.Resource{ "demo/Service/web": {ID: "demo/Service/web", Kind: "Service", Name: "web", Namespace: "demo", CreatedAt: time.Now()}, "demo/Deployment/web": {ID: "demo/Deployment/web", Kind: "Deployment", Name: "web", Namespace: "demo", CreatedAt: time.Now()}, "other/Deployment/other": {ID: "other/Deployment/other", Kind: "Deployment", Name: "other", Namespace: "other", CreatedAt: time.Now()}, }, } resp := BuildGraph(ds, Filters{ GroupBy: "namespace", CollapsedGroups: map[string]bool{ "demo": true, }, }, map[string]string{}) hasDemoGroup := false for _, n := range resp.Nodes { if n.IsGroup && n.GroupBy == "namespace" && n.GroupKey == "demo" { hasDemoGroup = true } } if !hasDemoGroup { t.Fatalf("expected collapsed namespace group node") } }