package metrics

import "testing"

func TestAggregateConnections(t *testing.T) {
	groups := map[string]bool{
		"PROXY":      true,
		"country_HK": true,
	}
	conns := []map[string]any{
		{
			"upload":   float64(100),
			"download": float64(200),
			"metadata": map[string]any{"host": "www.google.com"},
			"chains":   []any{"PROXY", "node_us_1"},
		},
		{
			"upload":   float64(50),
			"download": float64(150),
			"metadata": map[string]any{"host": "www.google.com"},
			"chains":   []any{"country_HK", "node_us_1"},
		},
		{
			"upload":   float64(10),
			"download": float64(20),
			"metadata": map[string]any{"host": "api.github.com"},
			"chains":   []any{"DIRECT"},
		},
	}
	domains, nodes := AggregateConnections(conns, groups)
	if len(domains) != 2 {
		t.Fatalf("domains: want 2 got %d", len(domains))
	}
	if domains[0].Label != "www.google.com" || domains[0].NodeKey != "node_us_1" || domains[0].Total != 500 {
		t.Fatalf("google row: %+v", domains[0])
	}
	if domains[1].Label != "api.github.com" || domains[1].NodeKey != "DIRECT" {
		t.Fatalf("github row: %+v", domains[1])
	}
	if len(nodes) != 2 {
		t.Fatalf("nodes: want 2 got %d: %+v", len(nodes), nodes)
	}
	if nodes[0].Key != "node_us_1" {
		t.Fatalf("top node: %+v", nodes[0])
	}
}
