package infra import "testing" func TestFormatPromExposition(t *testing.T) { t.Run("varias series con y sin labels con timestamp", func(t *testing.T) { samples := []PromSample{ {Name: "node_load1", Value: 0.42}, {Name: "node_cpu_core_percent", Labels: map[string]string{"core": "0"}, Value: 12.5}, {Name: "node_disk_used_bytes", Labels: map[string]string{"mount": "/"}, Value: 1024}, } got := FormatPromExposition(samples, 1700000000000) want := "node_load1 0.42 1700000000000\n" + "node_cpu_core_percent{core=\"0\"} 12.5 1700000000000\n" + "node_disk_used_bytes{mount=\"/\"} 1024 1700000000000\n" if got != want { t.Errorf("got:\n%q\nwant:\n%q", got, want) } }) t.Run("sin timestamp omite el campo", func(t *testing.T) { samples := []PromSample{ {Name: "node_load1", Value: 0.42}, {Name: "node_cpu_percent", Value: 3}, } got := FormatPromExposition(samples, 0) want := "node_load1 0.42\n" + "node_cpu_percent 3\n" if got != want { t.Errorf("got:\n%q\nwant:\n%q", got, want) } }) t.Run("labels ordenadas por clave deterministico", func(t *testing.T) { samples := []PromSample{ {Name: "node_proc_cpu_percent", Labels: map[string]string{"pid": "42", "name": "claude"}, Value: 7.5}, } got := FormatPromExposition(samples, 0) // "name" antes que "pid" alfabeticamente. want := "node_proc_cpu_percent{name=\"claude\",pid=\"42\"} 7.5\n" if got != want { t.Errorf("got:\n%q\nwant:\n%q", got, want) } }) t.Run("escapa backslash comilla y newline en valor de label", func(t *testing.T) { samples := []PromSample{ {Name: "node_proc_cpu_percent", Labels: map[string]string{"name": "a\\b\"c\nd"}, Value: 1}, } got := FormatPromExposition(samples, 0) want := "node_proc_cpu_percent{name=\"a\\\\b\\\"c\\nd\"} 1\n" if got != want { t.Errorf("got:\n%q\nwant:\n%q", got, want) } }) t.Run("sanitiza nombre de metrica invalido", func(t *testing.T) { samples := []PromSample{ {Name: "node.cpu-percent!", Value: 5}, } got := FormatPromExposition(samples, 0) want := "node_cpu_percent_ 5\n" if got != want { t.Errorf("got:\n%q\nwant:\n%q", got, want) } }) t.Run("slice vacio produce string vacio", func(t *testing.T) { got := FormatPromExposition(nil, 1700000000000) if got != "" { t.Errorf("got %q, want empty string", got) } }) }